FramerateManager.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 = boost::chrono::high_resolution_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 frome in lastFrameTimes array
  22. ui32 lastFrameIndex;
  23. public:
  24. FramerateManager(int targetFramerate);
  25. /// must be called every frame
  26. /// updates framerate calculations and executes sleep to maintain target frame rate
  27. void framerateDelay();
  28. /// returns duration of last frame in seconds
  29. ui32 getElapsedMilliseconds() const;
  30. /// returns current estimation of frame rate
  31. ui32 getFramerate() const;
  32. };