FrameRateCounter.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // FrameRateCounter.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Content;
  14. using Microsoft.Xna.Framework.Graphics;
  15. #endregion
  16. namespace CollisionSample
  17. {
  18. /// <summary>
  19. /// General Timing and Frame Rate Display Component.
  20. /// Add this to the GameComponentCollection to display the frame rate
  21. /// </summary>
  22. public class FrameRateCounter : DrawableGameComponent
  23. {
  24. ContentManager content;
  25. SpriteBatch spriteBatch;
  26. SpriteFont spriteFont;
  27. int frameRate = 0;
  28. int frameCounter = 0;
  29. TimeSpan elapsedTime = TimeSpan.Zero;
  30. /// <summary>
  31. /// Constructor which initializes the Content Manager which is used later for loading the font for display.
  32. /// </summary>
  33. /// <param name="game"></param>
  34. public FrameRateCounter(Game game)
  35. : base(game)
  36. {
  37. content = new ContentManager(game.Services);
  38. }
  39. /// <summary>
  40. /// Graphics device objects are created here including the font.
  41. /// </summary>
  42. protected override void LoadContent()
  43. {
  44. spriteBatch = new SpriteBatch(GraphicsDevice);
  45. spriteFont = content.Load<SpriteFont>("content\\Font");
  46. }
  47. /// <summary>
  48. ///
  49. /// </summary>
  50. protected override void UnloadContent()
  51. {
  52. content.Unload();
  53. }
  54. /// <summary>
  55. /// The Update function is where the timing is measured and frame rate is computed.
  56. /// </summary>
  57. /// <param name="gameTime"></param>
  58. public override void Update(GameTime gameTime)
  59. {
  60. elapsedTime += gameTime.ElapsedGameTime;
  61. if (elapsedTime > TimeSpan.FromSeconds(1))
  62. {
  63. elapsedTime -= TimeSpan.FromSeconds(1);
  64. frameRate = frameCounter;
  65. frameCounter = 0;
  66. }
  67. }
  68. #region Draw
  69. /// <summary>
  70. /// Frame rate display occurs during the Draw method and uses the Font and Sprite batch to render text.
  71. /// </summary>
  72. /// <param name="gameTime"></param>
  73. public override void Draw(GameTime gameTime)
  74. {
  75. frameCounter++;
  76. string fps = string.Format("fps: {0}", frameRate);
  77. spriteBatch.Begin();
  78. spriteBatch.DrawString(spriteFont, fps, new Vector2(32, 32), Color.White);
  79. spriteBatch.End();
  80. }
  81. #endregion
  82. }
  83. }