aboutsummaryrefslogtreecommitdiff
path: root/gameloop/src
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-09-18 16:52:54 +0200
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-09-18 16:52:54 +0200
commit8bf919f750807060f3ac2c640b8a02300af1733c (patch)
tree7bae57855fc3b98779bdfecbea141e7ec1eed68c /gameloop/src
parent61e382cedd71127033f91551298607e2e78c3809 (diff)
gameloop poc working with variable and constant update
Diffstat (limited to 'gameloop/src')
-rw-r--r--gameloop/src/loopManager.cpp38
-rw-r--r--gameloop/src/timer.cpp66
2 files changed, 60 insertions, 44 deletions
diff --git a/gameloop/src/loopManager.cpp b/gameloop/src/loopManager.cpp
index c439a3e..f7ce886 100644
--- a/gameloop/src/loopManager.cpp
+++ b/gameloop/src/loopManager.cpp
@@ -19,24 +19,28 @@ void LoopManager::processInput(){
void LoopManager::fixedUpdate(){
fprintf(stderr,"fixed update\n");
}
-void LoopManager::loop(){
- fprintf(stderr,"loop. \n");
- LoopTimer& timer = LoopTimer::getInstance();
- while(gameRunning){
- timer.update();
- lag += timer.getDeltaTime();
- processInput();
- while (lag >= timer.getFixedDeltaTime())
- {
- fixedUpdate();
- lag -= timer.getFixedDeltaTime();
- }
- //update();
- //timer.enforceFrameRate();
- render();
- }
- window.destroyWindow();
+void LoopManager::loop() {
+ LoopTimer& timer = LoopTimer::getInstance();
+ timer.start();
+
+ while (gameRunning) {
+ timer.update();
+
+ while (timer.getLag() >= timer.getFixedDeltaTime()) {
+ processInput();
+ fixedUpdate();
+ timer.advanceFixedUpdate();
+ }
+
+ update();
+ render();
+
+ timer.enforceFrameRate();
+ }
+
+ window.destroyWindow();
}
+
void LoopManager::setup(){
gameRunning = window.initWindow();
LoopTimer::getInstance().start();
diff --git a/gameloop/src/timer.cpp b/gameloop/src/timer.cpp
index 6428797..1e3045f 100644
--- a/gameloop/src/timer.cpp
+++ b/gameloop/src/timer.cpp
@@ -1,56 +1,68 @@
#include "timer.h"
-// Singleton instance for global access
+// Constructor (private)
+LoopTimer::LoopTimer(){}
+
+// Get the singleton instance of the timer
LoopTimer& LoopTimer::getInstance() {
static LoopTimer instance;
return instance;
}
-// Private constructor
-LoopTimer::LoopTimer() : lastTime(0), deltaTime(0), frequency(0), FPS(0), frameTargetTime(0) {}
-
+// Start the timer (initialize frame time)
void LoopTimer::start() {
-
+ lastFrameTime = SDL_GetTicks64();
+ elapsedTime = 0;
+ elapsedFixedTime = 0;
+ deltaTime = 0;
}
+// Update the timer, calculate deltaTime
void LoopTimer::update() {
- Uint64 currentTime = SDL_GetTicks64();
- deltaTime = (currentTime - lastFrameTime) / 1000.0;
- time += deltaTime;
- lastFrameTime = currentTime;
+ uint64_t currentFrameTime = SDL_GetTicks64();
+ deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; // Convert to seconds
+
+ if (deltaTime > maximumDeltaTime) {
+ deltaTime = maximumDeltaTime;
+ }
+
+ elapsedTime += deltaTime;
+ lastFrameTime = currentFrameTime;
}
-float LoopTimer::getDeltaTime() const {
+double LoopTimer::getDeltaTime() const {
return deltaTime;
}
-float LoopTimer::getDeltaTimeMs() const {
- return deltaTime * 1000;
-}
int LoopTimer::getCurrentTime() const {
- return SDL_GetTicks64();
+ return SDL_GetTicks();
}
-float LoopTimer::getFixedDeltaTime() const {
+void LoopTimer::advanceFixedUpdate() {
+ elapsedFixedTime += fixedDeltaTime;
+}
+
+double 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;
+ this->FPS = FPS;
+ frameTargetTime = 1.0 / FPS;
}
+
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);
- }
+ 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;
}