aboutsummaryrefslogtreecommitdiff
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
parent61e382cedd71127033f91551298607e2e78c3809 (diff)
gameloop poc working with variable and constant update
-rw-r--r--gameloop/include/loopManager.h3
-rw-r--r--gameloop/include/timer.h29
-rw-r--r--gameloop/src/loopManager.cpp38
-rw-r--r--gameloop/src/timer.cpp66
4 files changed, 75 insertions, 61 deletions
diff --git a/gameloop/include/loopManager.h b/gameloop/include/loopManager.h
index 5ed918d..06bcd5f 100644
--- a/gameloop/include/loopManager.h
+++ b/gameloop/include/loopManager.h
@@ -18,9 +18,8 @@ class LoopManager{
bool gameRunning = false;
WindowManager window;
int timeScale = 1;
- float lag = 0.0;
+ float accumulator = 0.0;
double currentTime;
double t = 0.0;
double dt = 0.01;
- state previous;
};
diff --git a/gameloop/include/timer.h b/gameloop/include/timer.h
index 2769e4d..a245e5c 100644
--- a/gameloop/include/timer.h
+++ b/gameloop/include/timer.h
@@ -7,25 +7,24 @@ public:
static LoopTimer& getInstance();
void start();
void update();
- float getDeltaTime() const;
- float getDeltaTimeMs() const;
- float getFixedDeltaTime() const;
+ double getDeltaTime() const;
int getCurrentTime() const;
+ void advanceFixedUpdate();
+ double getFixedDeltaTime() const;
void setFPS(int FPS);
int getFPS() const;
void enforceFrameRate();
- float getLag() const;
+ double getLag() const;
private:
LoopTimer();
- int FPS = 30;
- float frameTargetTime = FPS / 1000;
- int lastTime;
- float fixedDeltaTime = 2;
- float maxDeltaTime = 1;
- float fixedTime = 0;
- int lastFrameTime;
- float deltaTime;
- float time = 0;
- int frequency;
- float lag;
+ int FPS = 50;
+ double gameScale = 1;
+ double maximumDeltaTime = 0.25;
+ double deltaTime;
+ double frameTargetTime = FPS / 1000;
+ double fixedDeltaTime = 0.01;
+ double elapsedTime;
+ double elapsedFixedTime;
+ double time;
+ uint64_t lastFrameTime;
};
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;
}