-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFPSLimiter.hpp
46 lines (40 loc) · 1.26 KB
/
FPSLimiter.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @file FPSLimiter.hpp
* @brief A class that limits the frame rate of the game to a specified value.
* @author Josh Kennedy
*
* Kablam!
* Copyright (C) 2023 Josh Kennedy.
*/
#pragma once
#include <SDL2/SDL.h>
#include <chrono>
#include <thread>
// borrowed from https://discourse.libsdl.org/t/fps-leak-while-capping-frame-rate/27601/10
// tweaked so that the frame time is calculated in nanoseconds, keeping the mantissa of the frame time
// -- and using SDL_GetTicks64() to get the current time.
class FPSLimiter
{
public:
explicit FPSLimiter(int64_t frameTime = 60)
: m_frameTime{ (double)(1000.0 / frameTime) }
, m_startTime{ SDL_GetTicks64() }
, m_sleepTime{ 0.0 }
, m_frameTimeDebt{ 0.0 }
{
}
inline void run()
{
m_sleepTime = (m_frameTime - m_frameTimeDebt) - (SDL_GetTicks64() - m_startTime);
if (m_sleepTime > 0) {
std::this_thread::sleep_for(std::chrono::nanoseconds(static_cast<unsigned long>(m_sleepTime * 1'000'000)));
}
m_frameTimeDebt = (SDL_GetTicks64() - m_startTime) - (m_frameTime - m_frameTimeDebt);
m_startTime = SDL_GetTicks64();
}
private:
double m_frameTime;
uint64_t m_startTime;
double m_sleepTime;
double m_frameTimeDebt;
};