DebugSystem.cs 4.9 KB

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