2
0

FpsCounter.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // FpsCounter.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. #endregion
  14. namespace RobotGameData.Helper
  15. {
  16. /// <summary>
  17. /// Process frame per second
  18. /// </summary>
  19. public class FpsCounter
  20. {
  21. #region Fields
  22. protected int fps = 0;
  23. protected int frameCount = 0;
  24. protected float totalElapsedTime = 0.0f;
  25. #endregion
  26. #region Properties
  27. public int Fps
  28. {
  29. get { return fps; }
  30. }
  31. #endregion
  32. /// <summary>
  33. /// Initialize members
  34. /// </summary>
  35. public void Initialize()
  36. {
  37. fps = 0;
  38. frameCount = 0;
  39. totalElapsedTime = 0.0f;
  40. }
  41. /// <summary>
  42. /// Updates the frame count
  43. /// </summary>
  44. public void Update(GameTime gameTime)
  45. {
  46. totalElapsedTime += (float)gameTime.ElapsedRealTime.TotalSeconds;
  47. frameCount++;
  48. // Calculate frame count during one second
  49. if (totalElapsedTime >= 1.0f)
  50. {
  51. fps = frameCount;
  52. frameCount = 0;
  53. totalElapsedTime = 0.0f;
  54. }
  55. }
  56. }
  57. }