From e49893e8de74534494792955c50ea0eabaf3ba38 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 11 Dec 2024 15:01:46 +0100 Subject: WIP fix LoopTimerManager --- src/crepe/manager/LoopTimerManager.cpp | 47 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 24 deletions(-) (limited to 'src/crepe/manager/LoopTimerManager.cpp') diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index a306eb7..92a2150 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -6,7 +6,9 @@ #include "LoopTimerManager.h" using namespace crepe; +using namespace std::chrono; using namespace std::chrono_literals; + LoopTimerManager::LoopTimerManager(Mediator & mediator) : Manager(mediator) { this->mediator.loop_timer = *this; dbg_trace(); @@ -15,15 +17,14 @@ LoopTimerManager::LoopTimerManager(Mediator & mediator) : Manager(mediator) { void LoopTimerManager::start() { this->last_frame_time = std::chrono::steady_clock::now(); - this->elapsed_time = 0s; + this->elapsed_time = elapsed_time_t{0}; // by starting the elapsed_fixed_time at (0 - fixed_delta_time) in milliseconds it calls a fixed update at the start of the loop. - this->elapsed_fixed_time - = -std::chrono::duration_cast(this->fixed_delta_time); - this->delta_time = 0s; + // this->elapsed_fixed_time = -this->fixed_delta_time; + this->delta_time = duration_t{0}; } void LoopTimerManager::update() { - TimePoint_t current_frame_time + time_point_t current_frame_time = std::chrono::steady_clock::now(); // Convert to duration in seconds for delta time this->delta_time = current_frame_time - last_frame_time; @@ -32,20 +33,20 @@ void LoopTimerManager::update() { this->delta_time = this->maximum_delta_time; } if (this->delta_time > 0s) { - this->actual_fps = 1.0 / this->delta_time.count(); + this->actual_fps = 1.0 / duration_cast(this->delta_time).count(); } else { this->actual_fps = 0; } - this->elapsed_time += std::chrono::duration_cast(this->delta_time); + this->elapsed_time += duration_cast(this->delta_time); this->last_frame_time = current_frame_time; } -Duration_t LoopTimerManager::get_delta_time() const {return this->delta_time * this->time_scale;} +duration_t LoopTimerManager::get_delta_time() const {return this->delta_time * this->time_scale;} -ElapsedTime_t LoopTimerManager::get_elapsed_time() const { return this->elapsed_time; } +elapsed_time_t LoopTimerManager::get_elapsed_time() const { return this->elapsed_time; } void LoopTimerManager::advance_fixed_elapsed_time() { - this->elapsed_fixed_time += std::chrono::duration_cast(this->fixed_delta_time); + this->elapsed_fixed_time += std::chrono::duration_cast(this->fixed_delta_time); } void LoopTimerManager::set_target_framerate(unsigned fps) { @@ -53,7 +54,7 @@ void LoopTimerManager::set_target_framerate(unsigned fps) { //check if fps is lower or equals 0 if (fps <= 0) return; // target time per frame in seconds - this->frame_target_time = Duration_t(1s) / this->target_fps; + this->frame_target_time = duration_t(1s) / this->target_fps; } unsigned LoopTimerManager::get_fps() const { return this->actual_fps; } @@ -63,32 +64,30 @@ void LoopTimerManager::set_time_scale(double value) { this->time_scale = value; float LoopTimerManager::get_time_scale() const { return this->time_scale; } void LoopTimerManager::enforce_frame_rate() { - TimePoint_t current_frame_time - = std::chrono::steady_clock::now(); - Duration_t frame_duration = current_frame_time - this->last_frame_time; + time_point_t current_frame_time = std::chrono::steady_clock::now(); + duration_t frame_duration = current_frame_time - this->last_frame_time; + // Check if frame duration is less than the target frame time if (frame_duration < this->frame_target_time) { - std::chrono::microseconds delay_time - = std::chrono::duration_cast(this->frame_target_time - - frame_duration); - - if (delay_time.count() > 0) { + duration_t delay_time = this->frame_target_time - frame_duration; + if (delay_time > 0s) { std::this_thread::sleep_for(delay_time); } } } -Duration_t LoopTimerManager::get_lag() const { - return (this->elapsed_time - this->elapsed_fixed_time); +duration_t LoopTimerManager::get_lag() const { + return this->elapsed_time - this->elapsed_fixed_time; } -Duration_t LoopTimerManager::get_scaled_fixed_delta_time() const { +duration_t LoopTimerManager::get_scaled_fixed_delta_time() const { return this->fixed_delta_time * this->time_scale; } + void LoopTimerManager::set_fixed_delta_time(float seconds) { - this->fixed_delta_time = Duration_t(seconds); + this->fixed_delta_time = duration_t(seconds); } -Duration_t LoopTimerManager::get_fixed_delta_time() const { +duration_t LoopTimerManager::get_fixed_delta_time() const { return this->fixed_delta_time; } -- cgit v1.2.3 From 68c9e7511ea52c6ee70d052bbdf2923cd68bfa8a Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 11 Dec 2024 15:01:58 +0100 Subject: `make format` --- mwe/events/include/event.h | 2 +- src/crepe/api/LoopManager.cpp | 8 ++++---- src/crepe/api/LoopManager.h | 2 +- src/crepe/manager/LoopTimerManager.cpp | 14 +++++++------- src/crepe/system/AISystem.cpp | 2 +- src/test/LoopTimerTest.cpp | 5 ++--- 6 files changed, 16 insertions(+), 17 deletions(-) (limited to 'src/crepe/manager/LoopTimerManager.cpp') diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index e1b220b..ee1bf52 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -148,7 +148,7 @@ private: }; class ShutDownEvent : public Event { public: - ShutDownEvent() : Event("ShutDownEvent"){}; + ShutDownEvent() : Event("ShutDownEvent") {}; REGISTER_EVENT_TYPE(ShutDownEvent) diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index aa4a21a..b5e5ff7 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -1,4 +1,6 @@ #include "../facade/SDLContext.h" +#include "../manager/EventManager.h" +#include "../manager/LoopTimerManager.h" #include "../system/AISystem.h" #include "../system/AnimatorSystem.h" #include "../system/AudioSystem.h" @@ -8,8 +10,6 @@ #include "../system/PhysicsSystem.h" #include "../system/RenderSystem.h" #include "../system/ScriptSystem.h" -#include "../manager/EventManager.h" -#include "../manager/LoopTimerManager.h" #include "../util/Log.h" #include "LoopManager.h" @@ -54,7 +54,7 @@ void LoopManager::loop() { this->frame_update(); this->loop_timer.enforce_frame_rate(); } - }catch(const exception & e){ + } catch (const exception & e) { Log::logf(Log::Level::ERROR, "Exception caught in main loop: {}", e.what()); this->event_manager.trigger_event(ShutDownEvent{}); } @@ -72,7 +72,7 @@ void LoopManager::fixed_update() { } // will be called every frame -void LoopManager::frame_update() { +void LoopManager::frame_update() { this->scene_manager.load_next_scene(); this->get_system().update(); //render diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 0110695..487f07a 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -69,6 +69,7 @@ private: //! Indicates whether the game is running. bool game_running = false; + private: //! Global context Mediator mediator; @@ -90,7 +91,6 @@ private: SDLContext & sdl_context = SDLContext::get_instance(); private: - /** * \brief Callback function for ShutDownEvent * diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 92a2150..ccca950 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -24,8 +24,7 @@ void LoopTimerManager::start() { } void LoopTimerManager::update() { - time_point_t current_frame_time - = std::chrono::steady_clock::now(); + time_point_t current_frame_time = std::chrono::steady_clock::now(); // Convert to duration in seconds for delta time this->delta_time = current_frame_time - last_frame_time; @@ -41,12 +40,15 @@ void LoopTimerManager::update() { this->last_frame_time = current_frame_time; } -duration_t LoopTimerManager::get_delta_time() const {return this->delta_time * this->time_scale;} +duration_t LoopTimerManager::get_delta_time() const { + return this->delta_time * this->time_scale; +} elapsed_time_t LoopTimerManager::get_elapsed_time() const { return this->elapsed_time; } void LoopTimerManager::advance_fixed_elapsed_time() { - this->elapsed_fixed_time += std::chrono::duration_cast(this->fixed_delta_time); + this->elapsed_fixed_time + += std::chrono::duration_cast(this->fixed_delta_time); } void LoopTimerManager::set_target_framerate(unsigned fps) { @@ -88,6 +90,4 @@ void LoopTimerManager::set_fixed_delta_time(float seconds) { this->fixed_delta_time = duration_t(seconds); } -duration_t LoopTimerManager::get_fixed_delta_time() const { - return this->fixed_delta_time; -} +duration_t LoopTimerManager::get_fixed_delta_time() const { return this->fixed_delta_time; } diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp index ffb1bcd..ed22203 100644 --- a/src/crepe/system/AISystem.cpp +++ b/src/crepe/system/AISystem.cpp @@ -1,8 +1,8 @@ #include #include -#include "manager/LoopTimerManager.h" #include "manager/ComponentManager.h" +#include "manager/LoopTimerManager.h" #include "manager/Mediator.h" #include "AISystem.h" diff --git a/src/test/LoopTimerTest.cpp b/src/test/LoopTimerTest.cpp index f99f109..5e1eccf 100644 --- a/src/test/LoopTimerTest.cpp +++ b/src/test/LoopTimerTest.cpp @@ -71,9 +71,8 @@ TEST_F(LoopTimerTest, getCurrentTime) { auto end_time = steady_clock::now(); // Get the elapsed time in seconds as a double - auto elapsed_time = std::chrono::duration_cast(end_time - start_time).count(); - + auto elapsed_time + = std::chrono::duration_cast(end_time - start_time).count(); ASSERT_NEAR(loop_timer.get_elapsed_time().count(), elapsed_time, 5); } - -- cgit v1.2.3