DebugSystem.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #region Using Statements
  36. using Microsoft.Xna.Framework;
  37. namespace HoneycombRush.GameDebugTools
  38. {
  39. /// <summary>
  40. /// DebugSystem is a helper class that streamlines the creation of the various GameDebug
  41. /// pieces. While games are free to add only the pieces they care about, DebugSystem allows
  42. /// games to quickly create and add all the components by calling the Initialize method.
  43. /// </summary>
  44. public class DebugSystem
  45. {
  46. private static DebugSystem singletonInstance;
  47. /// <summary>
  48. /// Gets the singleton instance of the debug system. You must call Initialize
  49. /// to create the instance.
  50. /// </summary>
  51. public static DebugSystem Instance
  52. {
  53. get { return singletonInstance; }
  54. }
  55. /// <summary>
  56. /// Gets the DebugManager for the system.
  57. /// </summary>
  58. public DebugManager DebugManager { get; private set; }
  59. /// <summary>
  60. /// Gets the DebugCommandUI for the system.
  61. /// </summary>
  62. public DebugCommandUI DebugCommandUI { get; private set; }
  63. /// <summary>
  64. /// Gets the FpsCounter for the system.
  65. /// </summary>
  66. public FpsCounter FpsCounter { get; private set; }
  67. /// <summary>
  68. /// Gets the TimeRuler for the system.
  69. /// </summary>
  70. public TimeRuler TimeRuler { get; private set; }
  71. #if !WINDOWS_PHONE
  72. /// <summary>
  73. /// Gets the RemoteDebugCommand for the system.
  74. /// </summary>
  75. public RemoteDebugCommand RemoteDebugCommand { get; private set; }
  76. #endif
  77. /// <summary>
  78. /// Initializes the DebugSystem and adds all components to the game's Components collection.
  79. /// </summary>
  80. /// <param name="game">The game using the DebugSystem.</param>
  81. #endregion
  82. /// <param name="debugFont">The font to use by the DebugSystem.</param>
  83. /// <returns>The DebugSystem for the game to use.</returns>
  84. public static DebugSystem Initialize(Game game, string debugFont)
  85. {
  86. // if the singleton exists, return that; we don't want two systems being created for a game
  87. if (singletonInstance != null)
  88. return singletonInstance;
  89. // Create the system
  90. singletonInstance = new DebugSystem();
  91. // Create all of the system components
  92. singletonInstance.DebugManager = new DebugManager(game, debugFont);
  93. game.Components.Add(singletonInstance.DebugManager);
  94. singletonInstance.DebugCommandUI = new DebugCommandUI(game);
  95. game.Components.Add(singletonInstance.DebugCommandUI);
  96. singletonInstance.FpsCounter = new FpsCounter(game);
  97. game.Components.Add(singletonInstance.FpsCounter);
  98. singletonInstance.TimeRuler = new TimeRuler(game);
  99. game.Components.Add(singletonInstance.TimeRuler);
  100. #if !WINDOWS_PHONE
  101. singletonInstance.RemoteDebugCommand = new RemoteDebugCommand(game);
  102. game.Components.Add(singletonInstance.RemoteDebugCommand);
  103. #endif
  104. return singletonInstance;
  105. }
  106. // Private constructor; games should use Initialize
  107. private DebugSystem() { }
  108. }
  109. }