DebugSystem.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //-----------------------------------------------------------------------------
  2. // DebugSystem.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. /*
  8. * To get started with the GameDebugTools, go to your main game class, override the Initialize method and add the
  9. * following line of code:
  10. *
  11. * GameDebugTools.DebugSystem.Initialize(this, "MyFont");
  12. *
  13. * where "MyFont" is the name a SpriteFont in your content project. This method will initialize all of the debug
  14. * tools and add the necessary components to your game. To begin instrumenting your game, add the following line of
  15. * code to the top of your Update method:
  16. *
  17. * GameDebugTools.DebugSystem.Instance.TimeRuler.StartFrame()
  18. *
  19. * Once you have that in place, you can add markers throughout your game by surrounding your code with BeginMark and
  20. * EndMark calls of the TimeRuler. For example:
  21. *
  22. * GameDebugTools.DebugSystem.Instance.TimeRuler.BeginMark("SomeCode", Color.Blue);
  23. * // Your code goes here
  24. * GameDebugTools.DebugSystem.Instance.TimeRuler.EndMark("SomeCode");
  25. *
  26. * Then you can display these results by setting the Visible property of the TimeRuler to true. This will give you a
  27. * visual display you can use to profile your game for optimizations.
  28. *
  29. * The GameDebugTools also come with an FpsCounter and a DebugCommandUI, which allows you to type commands at runtime
  30. * and toggle the various displays as well as registering your own commands that enable you to alter your game without
  31. * having to restart.
  32. */
  33. using Microsoft.Xna.Framework;
  34. namespace PerformanceMeasuring.GameDebugTools
  35. {
  36. /// <summary>
  37. /// DebugSystem is a helper class that streamlines the creation of the various GameDebug
  38. /// pieces. While games are free to add only the pieces they care about, DebugSystem allows
  39. /// games to quickly create and add all the components by calling the Initialize method.
  40. /// </summary>
  41. public class DebugSystem
  42. {
  43. private static DebugSystem singletonInstance;
  44. /// <summary>
  45. /// Gets the singleton instance of the debug system. You must call Initialize
  46. /// to create the instance.
  47. /// </summary>
  48. public static DebugSystem Instance
  49. {
  50. get { return singletonInstance; }
  51. }
  52. /// <summary>
  53. /// Gets the DebugManager for the system.
  54. /// </summary>
  55. public DebugManager DebugManager { get; private set; }
  56. /// <summary>
  57. /// Gets the DebugCommandUI for the system.
  58. /// </summary>
  59. public DebugCommandUI DebugCommandUI { get; private set; }
  60. /// <summary>
  61. /// Gets the FpsCounter for the system.
  62. /// </summary>
  63. public FpsCounter FpsCounter { get; private set; }
  64. /// <summary>
  65. /// Gets the TimeRuler for the system.
  66. /// </summary>
  67. public TimeRuler TimeRuler { get; private set; }
  68. // RemoteDebugCommand is not supported in MonoGame 3.8.*
  69. // It required XNA networking features that are no longer available
  70. /// <summary>
  71. /// Initializes the DebugSystem and adds all components to the game's Components collection.
  72. /// </summary>
  73. /// <param name="game">The game using the DebugSystem.</param>
  74. /// <param name="debugFont">The font to use by the DebugSystem.</param>
  75. /// <returns>The DebugSystem for the game to use.</returns>
  76. public static DebugSystem Initialize(Game game, string debugFont)
  77. {
  78. // if the singleton exists, return that; we don't want two systems being created for a game
  79. if (singletonInstance != null)
  80. return singletonInstance;
  81. // Create the system
  82. singletonInstance = new DebugSystem();
  83. // Create all of the system components
  84. singletonInstance.DebugManager = new DebugManager(game, debugFont);
  85. game.Components.Add(singletonInstance.DebugManager);
  86. singletonInstance.DebugCommandUI = new DebugCommandUI(game);
  87. game.Components.Add(singletonInstance.DebugCommandUI);
  88. singletonInstance.FpsCounter = new FpsCounter(game);
  89. game.Components.Add(singletonInstance.FpsCounter);
  90. singletonInstance.TimeRuler = new TimeRuler(game);
  91. game.Components.Add(singletonInstance.TimeRuler);
  92. // RemoteDebugCommand is not supported in MonoGame 3.8.*
  93. return singletonInstance;
  94. }
  95. // Private constructor; games should use Initialize
  96. private DebugSystem() { }
  97. }
  98. }