aboutsummaryrefslogtreecommitdiff
path: root/gameloop/src/timer.cpp
diff options
context:
space:
mode:
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);
+ }
+}