diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-09-27 14:00:57 +0200 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-09-27 14:00:57 +0200 |
commit | a78f7bbfcdabad9550afe22f615b973b92cb074f (patch) | |
tree | 318e888219965634e3b81be9b74a0522af2136e0 /mwe/gameloop/src/timer.cpp | |
parent | 86a7bd1aee2ee96079e00e2696a624fd6b642fa0 (diff) | |
parent | f4560e02f703f1c6f857c8e5af63fa9fc4ca6438 (diff) |
fixed merge conflict
Diffstat (limited to 'mwe/gameloop/src/timer.cpp')
-rw-r--r-- | mwe/gameloop/src/timer.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/mwe/gameloop/src/timer.cpp b/mwe/gameloop/src/timer.cpp new file mode 100644 index 0000000..03fb9eb --- /dev/null +++ b/mwe/gameloop/src/timer.cpp @@ -0,0 +1,68 @@ +#include "timer.h" +LoopTimer::LoopTimer(){} + +LoopTimer& LoopTimer::getInstance() { + static LoopTimer instance; + return instance; +} + +void LoopTimer::start() { + lastFrameTime = SDL_GetTicks64(); + elapsedTime = 0; + elapsedFixedTime = 0; + deltaTime = 0; +} + +void LoopTimer::update() { + uint64_t currentFrameTime = SDL_GetTicks64(); + deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; + + 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; +} |