aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-04 10:26:32 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-04 10:26:32 +0100
commitc7c4cc0e3b1a3152256bc8ebf6494c19519538db (patch)
tree9c7a2201d94c8a51bb93de7cea45a9f14e2da5ea /src/crepe
parentf137451d0edb13543919cf2d1a3af379cb3a1485 (diff)
looptimer no singleton
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/api/LoopManager.cpp31
-rw-r--r--src/crepe/api/LoopManager.h4
-rw-r--r--src/crepe/api/LoopTimer.cpp37
-rw-r--r--src/crepe/api/LoopTimer.h15
4 files changed, 42 insertions, 45 deletions
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp
index 7edf4d1..4a6d2cd 100644
--- a/src/crepe/api/LoopManager.cpp
+++ b/src/crepe/api/LoopManager.cpp
@@ -6,20 +6,25 @@
#include "../system/PhysicsSystem.h"
#include "../system/RenderSystem.h"
#include "../system/ScriptSystem.h"
-
+#include "../api/EventManager.h"
#include "LoopManager.h"
#include "LoopTimer.h"
-
+#include <iostream>
using namespace crepe;
using namespace std;
LoopManager::LoopManager() {
+ this->loop_timer = make_unique<LoopTimer>();
this->load_system<AnimatorSystem>();
this->load_system<CollisionSystem>();
this->load_system<ParticleSystem>();
this->load_system<PhysicsSystem>();
this->load_system<RenderSystem>();
this->load_system<ScriptSystem>();
+ EventManager::get_instance().subscribe<ShutDownEvent>([this](const ShutDownEvent& event) {
+ return this->on_shutdown(event);
+ });
+
}
void LoopManager::process_input() {
@@ -35,29 +40,27 @@ void LoopManager::set_running(bool running) { this->game_running = running; }
void LoopManager::fixed_update() {}
void LoopManager::loop() {
- LoopTimer & timer = LoopTimer::get_instance();
- timer.start();
+ this->loop_timer->start();
while (game_running) {
- timer.update();
+ this->loop_timer->update();
- while (timer.get_lag() >= timer.get_fixed_delta_time()) {
+ while (this->loop_timer->get_lag() >= this->loop_timer->get_fixed_delta_time()) {
this->process_input();
this->fixed_update();
- timer.advance_fixed_update();
+ this->loop_timer->advance_fixed_update();
}
this->update();
this->render();
-
- timer.enforce_frame_rate();
+ this->loop_timer->enforce_frame_rate();
}
}
void LoopManager::setup() {
this->game_running = true;
- LoopTimer::get_instance().start();
- LoopTimer::get_instance().set_fps(200);
+ this->loop_timer->start();
+ this->loop_timer->set_fps(60);
}
void LoopManager::render() {
@@ -65,5 +68,9 @@ void LoopManager::render() {
this->get_system<RenderSystem>().update();
}
}
+bool LoopManager::on_shutdown(const ShutDownEvent & e){
+ this->game_running = false;
+ return false;
+}
-void LoopManager::update() { LoopTimer & timer = LoopTimer::get_instance(); }
+void LoopManager::update() {}
diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h
index 13e6dac..ff1ff55 100644
--- a/src/crepe/api/LoopManager.h
+++ b/src/crepe/api/LoopManager.h
@@ -7,7 +7,7 @@
#include "api/SceneManager.h"
namespace crepe {
-
+class LoopTimer;
/**
* \brief Main game loop manager
*
@@ -91,6 +91,8 @@ private:
SceneManager scene_manager{component_manager};
private:
+ std::unique_ptr<LoopTimer> loop_timer;
+ bool on_shutdown(const ShutDownEvent & e);
/**
* \brief Collection of System instances
*
diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp
index 15a0e3a..d0a19d7 100644
--- a/src/crepe/api/LoopTimer.cpp
+++ b/src/crepe/api/LoopTimer.cpp
@@ -9,10 +9,6 @@ using namespace crepe;
LoopTimer::LoopTimer() { dbg_trace(); }
-LoopTimer & LoopTimer::get_instance() {
- static LoopTimer instance;
- return instance;
-}
void LoopTimer::start() {
this->last_frame_time = std::chrono::steady_clock::now();
@@ -56,22 +52,25 @@ void LoopTimer::set_game_scale(double value) { this->game_scale = value; }
double LoopTimer::get_game_scale() const { return this->game_scale; }
void LoopTimer::enforce_frame_rate() {
- std::chrono::steady_clock::time_point current_frame_time
- = std::chrono::steady_clock::now();
- std::chrono::milliseconds frame_duration
- = std::chrono::duration_cast<std::chrono::milliseconds>(current_frame_time
- - this->last_frame_time);
-
- if (frame_duration < this->frame_target_time) {
- std::chrono::milliseconds delay_time
- = std::chrono::duration_cast<std::chrono::milliseconds>(this->frame_target_time
- - frame_duration);
- if (delay_time.count() > 0) {
- SDLContext::get_instance().delay(delay_time.count());
- }
- }
+ auto current_frame_time = std::chrono::steady_clock::now();
+ auto frame_duration = current_frame_time - this->last_frame_time;
- this->last_frame_time = current_frame_time;
+ if (frame_duration < this->frame_target_time) {
+ auto remaining_time = this->frame_target_time - frame_duration;
+
+ // Sleep for most of the remaining time using SDLContext
+ if (remaining_time > std::chrono::microseconds(2000)) { // 2ms threshold
+ SDLContext::get_instance().delay(
+ std::chrono::duration_cast<std::chrono::milliseconds>(remaining_time).count());
+ }
+
+ // Busy-wait for the last tiny remaining duration
+ while (std::chrono::steady_clock::now() - current_frame_time < remaining_time) {
+ // Busy wait
+ }
+ }
+
+ this->last_frame_time = std::chrono::steady_clock::now(); // Update frame time
}
double LoopTimer::get_lag() const {
diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h
index 9393439..b20203d 100644
--- a/src/crepe/api/LoopTimer.h
+++ b/src/crepe/api/LoopTimer.h
@@ -6,13 +6,7 @@ namespace crepe {
class LoopTimer {
public:
- /**
- * \brief Get the singleton instance of LoopTimer.
- *
- * \return A reference to the LoopTimer instance.
- */
- static LoopTimer & get_instance();
-
+ LoopTimer();
/**
* \brief Get the current delta time for the current frame.
*
@@ -97,12 +91,7 @@ private:
*/
double get_lag() const;
- /**
- * \brief Construct a new LoopTimer object.
- *
- * Private constructor for singleton pattern to restrict instantiation outside the class.
- */
- LoopTimer();
+
/**
* \brief Update the timer to the current frame.