diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-09-18 16:52:54 +0200 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-09-18 16:52:54 +0200 |
commit | 8bf919f750807060f3ac2c640b8a02300af1733c (patch) | |
tree | 7bae57855fc3b98779bdfecbea141e7ec1eed68c /gameloop/src/timer.cpp | |
parent | 61e382cedd71127033f91551298607e2e78c3809 (diff) |
gameloop poc working with variable and constant update
Diffstat (limited to 'gameloop/src/timer.cpp')
-rw-r--r-- | gameloop/src/timer.cpp | 66 |
1 files changed, 39 insertions, 27 deletions
diff --git a/gameloop/src/timer.cpp b/gameloop/src/timer.cpp index 6428797..1e3045f 100644 --- a/gameloop/src/timer.cpp +++ b/gameloop/src/timer.cpp @@ -1,56 +1,68 @@ #include "timer.h" -// Singleton instance for global access +// Constructor (private) +LoopTimer::LoopTimer(){} + +// Get the singleton instance of the timer LoopTimer& LoopTimer::getInstance() { static LoopTimer instance; return instance; } -// Private constructor -LoopTimer::LoopTimer() : lastTime(0), deltaTime(0), frequency(0), FPS(0), frameTargetTime(0) {} - +// 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 currentTime = SDL_GetTicks64(); - deltaTime = (currentTime - lastFrameTime) / 1000.0; - time += deltaTime; - lastFrameTime = currentTime; + uint64_t currentFrameTime = SDL_GetTicks64(); + deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; // Convert to seconds + + if (deltaTime > maximumDeltaTime) { + deltaTime = maximumDeltaTime; + } + + elapsedTime += deltaTime; + lastFrameTime = currentFrameTime; } -float LoopTimer::getDeltaTime() const { +double LoopTimer::getDeltaTime() const { return deltaTime; } -float LoopTimer::getDeltaTimeMs() const { - return deltaTime * 1000; -} int LoopTimer::getCurrentTime() const { - return SDL_GetTicks64(); + return SDL_GetTicks(); } -float LoopTimer::getFixedDeltaTime() const { +void LoopTimer::advanceFixedUpdate() { + elapsedFixedTime += fixedDeltaTime; +} + +double 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; + this->FPS = FPS; + frameTargetTime = 1.0 / FPS; } + 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); - } + 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; } |