2
0

FramerateManager.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include "StdInc.h"
  11. #include "FramerateManager.h"
  12. #include "../../lib/CConfigHandler.h"
  13. #include <SDL_video.h>
  14. FramerateManager::FramerateManager(int targetFrameRate)
  15. : lastFrameIndex(0)
  16. , lastFrameTimes({})
  17. , lastTimePoint(Clock::now())
  18. , vsyncEnabled(settings["video"]["vsync"].Bool())
  19. {
  20. if(vsyncEnabled)
  21. {
  22. static int display_in_use = 0;
  23. SDL_DisplayMode mode;
  24. SDL_GetCurrentDisplayMode(display_in_use, &mode);
  25. int displayRefreshRate = mode.refresh_rate;
  26. logGlobal->info("Display refresh rate is %d", displayRefreshRate);
  27. targetFrameTime = Duration(boost::chrono::seconds(1)) / displayRefreshRate;
  28. } else
  29. {
  30. targetFrameTime = Duration(boost::chrono::seconds(1)) / targetFrameRate;
  31. }
  32. boost::range::fill(lastFrameTimes, targetFrameTime);
  33. }
  34. void FramerateManager::framerateDelay()
  35. {
  36. Duration timeSpentBusy = Clock::now() - lastTimePoint;
  37. // FPS is higher than it should be, then wait some time
  38. if(timeSpentBusy < targetFrameTime)
  39. boost::this_thread::sleep_for(targetFrameTime - timeSpentBusy);
  40. // compute actual timeElapsed taking into account actual sleep interval
  41. // limit it to 100 ms to avoid breaking animation in case of huge lag (e.g. triggered breakpoint)
  42. TimePoint currentTicks = Clock::now();
  43. Duration timeElapsed = currentTicks - lastTimePoint;
  44. if(timeElapsed > boost::chrono::milliseconds(100))
  45. timeElapsed = boost::chrono::milliseconds(100);
  46. lastTimePoint = currentTicks;
  47. lastFrameIndex = (lastFrameIndex + 1) % lastFrameTimes.size();
  48. lastFrameTimes[lastFrameIndex] = timeElapsed;
  49. }
  50. ui32 FramerateManager::getElapsedMilliseconds() const
  51. {
  52. return lastFrameTimes[lastFrameIndex] / boost::chrono::milliseconds(1);
  53. }
  54. ui32 FramerateManager::getFramerate() const
  55. {
  56. Duration accumulatedTime = std::accumulate(lastFrameTimes.begin(), lastFrameTimes.end(), Duration());
  57. auto actualFrameTime = accumulatedTime / lastFrameTimes.size();
  58. if(actualFrameTime == actualFrameTime.zero())
  59. return 0;
  60. return std::round(boost::chrono::duration<double>(1) / actualFrameTime);
  61. };