blob: 64287970e4fa9859fc51944d1a6c58e5e7934470 (
plain)
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
47
48
49
50
51
52
53
54
55
56
|
#include "timer.h"
// Singleton instance for global access
LoopTimer& LoopTimer::getInstance() {
static LoopTimer instance;
return instance;
}
// Private constructor
LoopTimer::LoopTimer() : lastTime(0), deltaTime(0), frequency(0), FPS(0), frameTargetTime(0) {}
void LoopTimer::start() {
}
void LoopTimer::update() {
Uint64 currentTime = SDL_GetTicks64();
deltaTime = (currentTime - lastFrameTime) / 1000.0;
time += deltaTime;
lastFrameTime = currentTime;
}
float LoopTimer::getDeltaTime() const {
return deltaTime;
}
float LoopTimer::getDeltaTimeMs() const {
return deltaTime * 1000;
}
int LoopTimer::getCurrentTime() const {
return SDL_GetTicks64();
}
float LoopTimer::getFixedDeltaTime() const {
return fixedDeltaTime;
}
void LoopTimer::setFPS(int FPS) {
if (FPS > 0) {
this->FPS = FPS;
frameTargetTime = 1000 / FPS;
}
}
float LoopTimer::getLag() const{
return lag;
}
int LoopTimer::getFPS() const {
return FPS;
}
// Delay to match target frame time
void LoopTimer::enforceFrameRate() {
int waitTime = frameTargetTime - (SDL_GetTicks64() - lastFrameTime);
if(waitTime > 0 && waitTime <= frameTargetTime){
SDL_Delay(waitTime);
}
}
|