FramerateManager.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * FramerateManager.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. /// Framerate manager controls current game frame rate by constantly trying to reach targeted frame rate
  12. class FramerateManager
  13. {
  14. using Clock = std::chrono::steady_clock;
  15. using TimePoint = Clock::time_point;
  16. using Duration = Clock::duration;
  17. /// cyclic buffer of durations of last frames
  18. std::array<Duration, 60> lastFrameTimes;
  19. Duration targetFrameTime;
  20. TimePoint lastTimePoint;
  21. /// index of last measured from in lastFrameTimes array
  22. ui32 lastFrameIndex;
  23. bool vsyncEnabled;
  24. public:
  25. FramerateManager(int targetFramerate);
  26. /// must be called every frame
  27. /// updates framerate calculations and executes sleep to maintain target frame rate
  28. void framerateDelay();
  29. /// returns duration of last frame in seconds
  30. ui32 getElapsedMilliseconds() const;
  31. /// returns current estimation of frame rate
  32. ui32 getFramerate() const;
  33. };