blob: 03a2dbbadf6f33dbc64346ccc90c3f1a0d71e744 (
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#include "timer.h"
// Constructor (private)
LoopTimer::LoopTimer(){}
// Get the singleton instance of the timer
LoopTimer& LoopTimer::getInstance() {
static LoopTimer instance;
return instance;
}
// Start the timer (initialize frame time)
void LoopTimer::start() {
lastFrameTime = SDL_GetTicks64();
elapsedTime = 0;
elapsedFixedTime = 0;
deltaTime = 0;
}
// Update the timer, calculate deltaTime
void LoopTimer::update() {
uint64_t currentFrameTime = SDL_GetTicks64();
deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; // Convert to seconds
if (deltaTime > maximumDeltaTime) {
deltaTime = maximumDeltaTime;
}
deltaTime *= gameScale;
elapsedTime += deltaTime;
lastFrameTime = currentFrameTime;
}
double LoopTimer::getDeltaTime() const {
return deltaTime;
}
int LoopTimer::getCurrentTime() const {
return SDL_GetTicks();
}
void LoopTimer::advanceFixedUpdate() {
elapsedFixedTime += fixedDeltaTime;
}
double LoopTimer::getFixedDeltaTime() const {
return fixedDeltaTime;
}
void LoopTimer::setFPS(int FPS) {
this->FPS = FPS;
frameTargetTime = 1.0 / FPS;
}
int LoopTimer::getFPS() const {
return FPS;
}
void LoopTimer::setGameScale(double value){
gameScale = value;
};
double LoopTimer::getGameScale(){
return gameScale;
}
void LoopTimer::enforceFrameRate() {
uint64_t currentFrameTime = SDL_GetTicks64();
double frameDuration = (currentFrameTime - lastFrameTime) / 1000.0;
if (frameDuration < frameTargetTime) {
uint32_t delayTime = (uint32_t)((frameTargetTime - frameDuration) * 1000.0);
SDL_Delay(delayTime);
}
}
double LoopTimer::getLag() const {
return elapsedTime - elapsedFixedTime;
}
|