aboutsummaryrefslogtreecommitdiff
path: root/gameloop/src/timer.cpp
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-09-18 15:11:04 +0200
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-09-18 15:11:04 +0200
commit61e382cedd71127033f91551298607e2e78c3809 (patch)
treedb946bf0dac5cbb504ac7875fb323243d2a6db74 /gameloop/src/timer.cpp
parent7eafe293868d5d3875d0bec147e22a266e4ec86c (diff)
gameloop save
Diffstat (limited to 'gameloop/src/timer.cpp')
-rw-r--r--gameloop/src/timer.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/gameloop/src/timer.cpp b/gameloop/src/timer.cpp
new file mode 100644
index 0000000..6428797
--- /dev/null
+++ b/gameloop/src/timer.cpp
@@ -0,0 +1,56 @@
+#include "timer.h"
+
+// Singleton instance for global access
+LoopTimer& LoopTimer::getInstance() {
+ static LoopTimer instance;
+ return instance;
+}
+
+// Private constructor
+LoopTimer::LoopTimer() : lastTime(0), deltaTime(0), frequency(0), FPS(0), frameTargetTime(0) {}
+
+void LoopTimer::start() {
+
+}
+
+void LoopTimer::update() {
+ Uint64 currentTime = SDL_GetTicks64();
+ deltaTime = (currentTime - lastFrameTime) / 1000.0;
+ time += deltaTime;
+ lastFrameTime = currentTime;
+}
+
+float LoopTimer::getDeltaTime() const {
+ return deltaTime;
+}
+float LoopTimer::getDeltaTimeMs() const {
+ return deltaTime * 1000;
+}
+int LoopTimer::getCurrentTime() const {
+ return SDL_GetTicks64();
+}
+
+float 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;
+}
+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);
+ }
+}