2
0

FPSCounter.cs 1005 B

123456789101112131415161718192021222324252627282930
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace OpenVIII
  4. {
  5. /// <summary>
  6. /// https://stackoverflow.com/questions/20676185/xna-monogame-getting-the-frames-per-second
  7. /// </summary>
  8. internal static class FPSCounter
  9. {
  10. public static double CurrentFramesPerSecond { get; private set; }
  11. public static double AverageFramesPerSecond { get; private set; }
  12. private const int MAX_SAMPLES = 100;
  13. private static Queue<double> SAMPLES = new Queue<double>();
  14. public static void Update()
  15. {
  16. if (SAMPLES != null)
  17. {
  18. if (Memory.ElapsedGameTime.TotalSeconds > 0)
  19. CurrentFramesPerSecond = 1.0d / Memory.ElapsedGameTime.TotalSeconds;
  20. SAMPLES.Enqueue(CurrentFramesPerSecond);
  21. while (SAMPLES.Count > MAX_SAMPLES)
  22. SAMPLES.Dequeue();
  23. AverageFramesPerSecond = SAMPLES.Average(x => x);
  24. }
  25. }
  26. }
  27. }