diff options
Diffstat (limited to 'mwe/gameloop/src/timer.cpp')
-rw-r--r-- | mwe/gameloop/src/timer.cpp | 86 |
1 files changed, 48 insertions, 38 deletions
diff --git a/mwe/gameloop/src/timer.cpp b/mwe/gameloop/src/timer.cpp index 61e144d..03fb9eb 100644 --- a/mwe/gameloop/src/timer.cpp +++ b/mwe/gameloop/src/timer.cpp @@ -1,58 +1,68 @@ #include "timer.h" +LoopTimer::LoopTimer(){} -// Constructor (private) -LoopTimer::LoopTimer() {} - -// Get the singleton instance of the timer -LoopTimer & LoopTimer::getInstance() { - static LoopTimer instance; - return instance; +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; + 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; - } + uint64_t currentFrameTime = SDL_GetTicks64(); + deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; - elapsedTime += deltaTime; - lastFrameTime = currentFrameTime; + if (deltaTime > maximumDeltaTime) { + deltaTime = maximumDeltaTime; + } + deltaTime *= gameScale; + elapsedTime += deltaTime; + lastFrameTime = currentFrameTime; } -double LoopTimer::getDeltaTime() const { return deltaTime; } -int LoopTimer::getCurrentTime() const { return SDL_GetTicks(); } +double LoopTimer::getDeltaTime() const { + return deltaTime; +} +int LoopTimer::getCurrentTime() const { + return SDL_GetTicks(); +} -void LoopTimer::advanceFixedUpdate() { elapsedFixedTime += fixedDeltaTime; } +void LoopTimer::advanceFixedUpdate() { + elapsedFixedTime += fixedDeltaTime; +} -double LoopTimer::getFixedDeltaTime() const { return fixedDeltaTime; } +double LoopTimer::getFixedDeltaTime() const { + return fixedDeltaTime; +} void LoopTimer::setFPS(int FPS) { - this->FPS = FPS; - frameTargetTime = 1.0 / FPS; + this->FPS = FPS; + frameTargetTime = 1.0 / FPS; } -int LoopTimer::getFPS() const { return 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; + 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); - } + if (frameDuration < frameTargetTime) { + uint32_t delayTime = (uint32_t)((frameTargetTime - frameDuration) * 1000.0); + SDL_Delay(delayTime); + } +} +double LoopTimer::getLag() const { + return elapsedTime - elapsedFixedTime; } -double LoopTimer::getLag() const { return elapsedTime - elapsedFixedTime; } |