aboutsummaryrefslogtreecommitdiff
path: root/mwe/gameloop/src/timer.cpp
diff options
context:
space:
mode:
authorjaroWMR <jarorutjes07@gmail.com>2024-10-09 19:58:30 +0200
committerjaroWMR <jarorutjes07@gmail.com>2024-10-09 19:58:30 +0200
commit2884c56b066fcafb4f0042799f8a16ea3b238d2a (patch)
tree9c30844807e8ab5f48a17773d19e5b565cb1a59d /mwe/gameloop/src/timer.cpp
parent5f84969c851530ebc430be2cf8e99c945ff7a4a7 (diff)
parent08d0b07c19edfe8c81dfe4e21c0d4c1ef128b628 (diff)
merge with master
Diffstat (limited to 'mwe/gameloop/src/timer.cpp')
-rw-r--r--mwe/gameloop/src/timer.cpp79
1 files changed, 32 insertions, 47 deletions
diff --git a/mwe/gameloop/src/timer.cpp b/mwe/gameloop/src/timer.cpp
index 03fb9eb..97baef6 100644
--- a/mwe/gameloop/src/timer.cpp
+++ b/mwe/gameloop/src/timer.cpp
@@ -1,68 +1,53 @@
#include "timer.h"
-LoopTimer::LoopTimer(){}
+LoopTimer::LoopTimer() {}
-LoopTimer& LoopTimer::getInstance() {
- static LoopTimer instance;
- return instance;
+LoopTimer & LoopTimer::getInstance() {
+ static LoopTimer instance;
+ return instance;
}
void LoopTimer::start() {
- lastFrameTime = SDL_GetTicks64();
- elapsedTime = 0;
- elapsedFixedTime = 0;
- deltaTime = 0;
+ lastFrameTime = SDL_GetTicks64();
+ elapsedTime = 0;
+ elapsedFixedTime = 0;
+ deltaTime = 0;
}
void LoopTimer::update() {
- uint64_t currentFrameTime = SDL_GetTicks64();
- deltaTime = (currentFrameTime - lastFrameTime) / 1000.0;
+ uint64_t currentFrameTime = SDL_GetTicks64();
+ deltaTime = (currentFrameTime - lastFrameTime) / 1000.0;
- if (deltaTime > maximumDeltaTime) {
- deltaTime = maximumDeltaTime;
- }
+ if (deltaTime > maximumDeltaTime) {
+ deltaTime = maximumDeltaTime;
+ }
deltaTime *= gameScale;
- elapsedTime += deltaTime;
- lastFrameTime = currentFrameTime;
+ 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;
-}
-void LoopTimer::setGameScale(double value){
- gameScale = value;
-};
-double LoopTimer::getGameScale(){
- return gameScale;
-}
+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);
- }
-}
-double LoopTimer::getLag() const {
- return elapsedTime - elapsedFixedTime;
+ if (frameDuration < frameTargetTime) {
+ uint32_t delayTime
+ = (uint32_t) ((frameTargetTime - frameDuration) * 1000.0);
+ SDL_Delay(delayTime);
+ }
}
+double LoopTimer::getLag() const { return elapsedTime - elapsedFixedTime; }