From 020cdfddcd06e162515deee4941ce91f3a945ee6 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sun, 24 Nov 2024 16:38:58 +0100 Subject: fps counter functionality --- src/crepe/api/LoopManager.cpp | 2 +- src/crepe/api/LoopTimer.cpp | 13 ++++++------- src/crepe/api/LoopTimer.h | 11 +++++++---- 3 files changed, 14 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 7edf4d1..df09f7e 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -57,7 +57,7 @@ void LoopManager::loop() { void LoopManager::setup() { this->game_running = true; LoopTimer::get_instance().start(); - LoopTimer::get_instance().set_fps(200); + LoopTimer::get_instance().set_target_fps(200); } void LoopManager::render() { diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index 15a0e3a..f94e24b 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -30,7 +30,8 @@ void LoopTimer::update() { if (this->delta_time > this->maximum_delta_time) { this->delta_time = this->maximum_delta_time; } - + this->actual_fps = 1.0 / this->delta_time.count(); + this->delta_time *= this->game_scale; this->elapsed_time += this->delta_time; this->last_frame_time = current_frame_time; @@ -44,13 +45,13 @@ void LoopTimer::advance_fixed_update() { this->elapsed_fixed_time += this->fixed double LoopTimer::get_fixed_delta_time() const { return this->fixed_delta_time.count(); } -void LoopTimer::set_fps(int fps) { - this->fps = fps; +void LoopTimer::set_target_fps(int fps) { + this->target_fps = fps; // target time per frame in seconds - this->frame_target_time = std::chrono::duration(1.0) / fps; + this->frame_target_time = std::chrono::duration(1.0) / target_fps; } -int LoopTimer::get_fps() const { return this->fps; } +int LoopTimer::get_fps() const { return this->actual_fps; } void LoopTimer::set_game_scale(double value) { this->game_scale = value; } @@ -70,8 +71,6 @@ void LoopTimer::enforce_frame_rate() { SDLContext::get_instance().delay(delay_time.count()); } } - - this->last_frame_time = current_frame_time; } double LoopTimer::get_lag() const { diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index 9393439..eea60bb 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -35,7 +35,7 @@ public: * * \param fps The desired frames rendered per second. */ - void set_fps(int fps); + void set_target_fps(int fps); /** * \brief Get the current frames per second (FPS). @@ -121,8 +121,10 @@ private: void advance_fixed_update(); private: - //! Current frames per second - int fps = 50; + //! Target frames per second + int target_fps = 50; + //! Actual frames per second + int actual_fps = 0; //! Current game scale double game_scale = 1; //! Maximum delta time in seconds to avoid large jumps @@ -130,7 +132,7 @@ private: //! Delta time for the current frame in seconds std::chrono::duration delta_time{0.0}; //! Target time per frame in seconds - std::chrono::duration frame_target_time = std::chrono::duration(1.0) / fps; + std::chrono::duration frame_target_time = std::chrono::duration(1.0) / target_fps; //! Fixed delta time for fixed updates in seconds std::chrono::duration fixed_delta_time = std::chrono::duration(1.0) / 50.0; //! Total elapsed game time in seconds @@ -139,6 +141,7 @@ private: std::chrono::duration elapsed_fixed_time{0.0}; //! Time of the last frame std::chrono::steady_clock::time_point last_frame_time; + }; } // namespace crepe -- cgit v1.2.3 From 9f4f2fa8eac190ccb407c3f911ac5978cb4c3e3a Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sun, 24 Nov 2024 19:36:32 +0100 Subject: start of tests --- src/crepe/api/LoopTimer.h | 1 - src/test/gameLoopTest.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/test/gameLoopTest.cpp (limited to 'src') diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index eea60bb..0a48e20 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -68,7 +68,6 @@ private: * Initializes the timer to begin tracking frame times. */ void start(); - /** * \brief Enforce the frame rate limit. * diff --git a/src/test/gameLoopTest.cpp b/src/test/gameLoopTest.cpp new file mode 100644 index 0000000..af80b27 --- /dev/null +++ b/src/test/gameLoopTest.cpp @@ -0,0 +1,34 @@ +#define private public +#define protected public +#include "api/LoopManager.h" +#include "api/LoopTimer.h" +#include +#include + +using namespace std; +using namespace std::chrono_literals; +using namespace crepe; + +class GameLoopTest : public ::testing::Test { +public: +LoopManager loop_manager; +LoopTimer loop_timer = LoopTimer::get_instance(); +protected: + void SetUp() override { + loop_timer.start(); + loop_manager.start(); + } + + void TearDown() override { + + } +}; +TEST_F(GameLoopTest, TestDeltaTime) { + auto start_time = std::chrono::steady_clock::now(); + + loop_timer.update(); + double delta_time = loop_timer.get_delta_time(); + + auto elapsed_time = std::chrono::steady_clock::now() - start_time; + EXPECT_LE(delta_time, std::chrono::duration(elapsed_time).count()); // delta_time should match or be slightly less +} -- cgit v1.2.3 From 6e03da99701b1916ee2f6a3dd7d10f7d283f2dd9 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sun, 24 Nov 2024 19:38:13 +0100 Subject: loopTimer tests --- src/test/gameLoopTest.cpp | 34 ---------------------------------- src/test/loopTimerTest.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 34 deletions(-) delete mode 100644 src/test/gameLoopTest.cpp create mode 100644 src/test/loopTimerTest.cpp (limited to 'src') diff --git a/src/test/gameLoopTest.cpp b/src/test/gameLoopTest.cpp deleted file mode 100644 index af80b27..0000000 --- a/src/test/gameLoopTest.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#define private public -#define protected public -#include "api/LoopManager.h" -#include "api/LoopTimer.h" -#include -#include - -using namespace std; -using namespace std::chrono_literals; -using namespace crepe; - -class GameLoopTest : public ::testing::Test { -public: -LoopManager loop_manager; -LoopTimer loop_timer = LoopTimer::get_instance(); -protected: - void SetUp() override { - loop_timer.start(); - loop_manager.start(); - } - - void TearDown() override { - - } -}; -TEST_F(GameLoopTest, TestDeltaTime) { - auto start_time = std::chrono::steady_clock::now(); - - loop_timer.update(); - double delta_time = loop_timer.get_delta_time(); - - auto elapsed_time = std::chrono::steady_clock::now() - start_time; - EXPECT_LE(delta_time, std::chrono::duration(elapsed_time).count()); // delta_time should match or be slightly less -} diff --git a/src/test/loopTimerTest.cpp b/src/test/loopTimerTest.cpp new file mode 100644 index 0000000..a3b1646 --- /dev/null +++ b/src/test/loopTimerTest.cpp @@ -0,0 +1,32 @@ +#define private public +#define protected public +#include "api/LoopManager.h" +#include "api/LoopTimer.h" +#include +#include + +using namespace std; +using namespace std::chrono_literals; +using namespace crepe; + +class LoopTimerTest : public ::testing::Test { +public: +LoopTimer loop_timer = LoopTimer::get_instance(); +protected: + void SetUp() override { + loop_timer.start(); + } + + void TearDown() override { + + } +}; +TEST_F(LoopTimerTest, TestDeltaTime) { + auto start_time = std::chrono::steady_clock::now(); + + loop_timer.update(); + double delta_time = loop_timer.get_delta_time(); + + auto elapsed_time = std::chrono::steady_clock::now() - start_time; + EXPECT_LE(delta_time, std::chrono::duration(elapsed_time).count()); +} -- cgit v1.2.3 From 4d6d5a6ff5366877cbc7b8e24110b56a332a7353 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 27 Nov 2024 10:15:23 +0100 Subject: removed gameloop example --- src/example/CMakeLists.txt | 1 - src/example/gameloop.cpp | 7 ------- 2 files changed, 8 deletions(-) delete mode 100644 src/example/gameloop.cpp (limited to 'src') diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 560e2bc..85ec466 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -19,5 +19,4 @@ endfunction() add_example(asset_manager) add_example(savemgr) add_example(rendering_particle) -add_example(gameloop) diff --git a/src/example/gameloop.cpp b/src/example/gameloop.cpp deleted file mode 100644 index a676f20..0000000 --- a/src/example/gameloop.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "crepe/api/LoopManager.h" -using namespace crepe; -int main() { - LoopManager gameloop; - gameloop.start(); - return 1; -} -- cgit v1.2.3 From c7c4cc0e3b1a3152256bc8ebf6494c19519538db Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 4 Dec 2024 10:26:32 +0100 Subject: looptimer no singleton --- src/crepe/api/LoopManager.cpp | 31 +++++++++++++++++++------------ src/crepe/api/LoopManager.h | 4 +++- src/crepe/api/LoopTimer.cpp | 37 ++++++++++++++++++------------------- src/crepe/api/LoopTimer.h | 15 ++------------- 4 files changed, 42 insertions(+), 45 deletions(-) (limited to 'src') 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 using namespace crepe; using namespace std; LoopManager::LoopManager() { + this->loop_timer = make_unique(); this->load_system(); this->load_system(); this->load_system(); this->load_system(); this->load_system(); this->load_system(); + EventManager::get_instance().subscribe([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().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 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(current_frame_time - - this->last_frame_time); - - if (frame_duration < this->frame_target_time) { - std::chrono::milliseconds delay_time - = std::chrono::duration_cast(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(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. -- cgit v1.2.3 From 1e72559664cb7cc68c1c404f1709d679d35a66e2 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 4 Dec 2024 10:54:52 +0100 Subject: test --- src/crepe/api/LoopManager.cpp | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 14e68c2..4a6d2cd 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -59,13 +59,8 @@ void LoopManager::loop() { void LoopManager::setup() { this->game_running = true; -<<<<<<< HEAD - LoopTimer::get_instance().start(); - LoopTimer::get_instance().set_target_fps(200); -======= this->loop_timer->start(); this->loop_timer->set_fps(60); ->>>>>>> wouter/gameloop-improvements } void LoopManager::render() { -- cgit v1.2.3 From 5a43793e247fbffec590d334b89cc34d19049f45 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 4 Dec 2024 16:38:15 +0100 Subject: gameloop tests --- src/crepe/api/LoopManager.cpp | 10 +++++++--- src/crepe/api/LoopManager.h | 5 ++++- src/crepe/api/LoopTimer.cpp | 17 ++++++++-------- src/example/CMakeLists.txt | 2 +- src/test/loopTimerTest.cpp | 45 ++++++++++++++++++++++--------------------- 5 files changed, 44 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 4a6d2cd..e584ba7 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -1,3 +1,5 @@ +#include + #include "../facade/SDLContext.h" #include "../system/AnimatorSystem.h" @@ -60,12 +62,12 @@ void LoopManager::loop() { void LoopManager::setup() { this->game_running = true; this->loop_timer->start(); - this->loop_timer->set_fps(60); + this->loop_timer->set_target_fps(60); } void LoopManager::render() { if (this->game_running) { - this->get_system().update(); + //this->get_system().update(); } } bool LoopManager::on_shutdown(const ShutDownEvent & e){ @@ -73,4 +75,6 @@ bool LoopManager::on_shutdown(const ShutDownEvent & e){ return false; } -void LoopManager::update() {} +void LoopManager::update() { + std::cout << this->loop_timer->get_fps() << std::endl; +} diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index ff1ff55..3bf54b9 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -5,9 +5,10 @@ #include "../ComponentManager.h" #include "../system/System.h" #include "api/SceneManager.h" +#include "api/Event.h" +#include "api/LoopTimer.h" namespace crepe { -class LoopTimer; /** * \brief Main game loop manager * @@ -91,7 +92,9 @@ private: SceneManager scene_manager{component_manager}; private: + //! loop timer instance std::unique_ptr loop_timer; + //! callback function for shutdown event 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 fe5544d..15011ca 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -1,4 +1,5 @@ #include +#include #include "../facade/SDLContext.h" #include "../util/Log.h" @@ -56,14 +57,14 @@ void LoopTimer::enforce_frame_rate() { auto current_frame_time = std::chrono::steady_clock::now(); auto frame_duration = current_frame_time - this->last_frame_time; - if (frame_duration < this->frame_target_time) { - std::chrono::milliseconds delay_time - = std::chrono::duration_cast(this->frame_target_time - - frame_duration); - if (delay_time.count() > 0) { - SDLContext::get_instance().delay(delay_time.count()); - } - } + // Check if frame duration is less than the target frame time + if (frame_duration < this->frame_target_time) { + auto delay_time = std::chrono::duration_cast(this->frame_target_time - frame_duration); + + if (delay_time.count() > 0) { + std::this_thread::sleep_for(delay_time); + } + } } double LoopTimer::get_lag() const { diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 85ec466..54100cf 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -19,4 +19,4 @@ endfunction() add_example(asset_manager) add_example(savemgr) add_example(rendering_particle) - +add_example(gameloop) diff --git a/src/test/loopTimerTest.cpp b/src/test/loopTimerTest.cpp index a3b1646..d2f7d9b 100644 --- a/src/test/loopTimerTest.cpp +++ b/src/test/loopTimerTest.cpp @@ -1,32 +1,33 @@ -#define private public -#define protected public -#include "api/LoopManager.h" -#include "api/LoopTimer.h" -#include #include +#include +#include +#include "api/LoopTimer.h" -using namespace std; -using namespace std::chrono_literals; +using namespace std::chrono; using namespace crepe; class LoopTimerTest : public ::testing::Test { -public: -LoopTimer loop_timer = LoopTimer::get_instance(); protected: - void SetUp() override { - loop_timer.start(); - } + LoopTimer loop_timer; - void TearDown() override { - - } + void SetUp() override { + loop_timer.start(); // Reset loop timer before each test. + } }; -TEST_F(LoopTimerTest, TestDeltaTime) { - auto start_time = std::chrono::steady_clock::now(); - - loop_timer.update(); - double delta_time = loop_timer.get_delta_time(); +TEST_F(LoopTimerTest, EnforcesTargetFrameRate) { + // Set the target FPS to 60 (which gives a target time per frame of ~16.67 ms) + loop_timer.set_target_fps(60); + + // Simulate a short update (frame duration less than the target frame time) + auto start_time = steady_clock::now(); + loop_timer.enforce_frame_rate(); // Enforce the frame rate + + // Check that the loop timer's current time is greater than or equal to the target frame time + auto elapsed_time = steady_clock::now() - start_time; + auto elapsed_ms = duration_cast(elapsed_time).count(); - auto elapsed_time = std::chrono::steady_clock::now() - start_time; - EXPECT_LE(delta_time, std::chrono::duration(elapsed_time).count()); + // Assert that the elapsed time is close to the target frame time + // For 60 FPS, the target frame time is around 16.67ms + ASSERT_GE(elapsed_ms, 16); // Make sure it's at least 16 ms (could be slightly more) + ASSERT_LE(elapsed_ms, 18); // Ensure it's not too much longer } -- cgit v1.2.3 From d9e46281c1e24a5f23d779d314e5df87fa3317a3 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 4 Dec 2024 21:33:38 +0100 Subject: tests --- src/crepe/api/LoopManager.cpp | 13 +++++------ src/crepe/api/LoopManager.h | 16 ++++++-------- src/example/CMakeLists.txt | 1 - src/test/CMakeLists.txt | 1 + src/test/loopManagerTest.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++ src/test/loopTimerTest.cpp | 36 ++++++++++++++++++++++++++----- 6 files changed, 94 insertions(+), 23 deletions(-) create mode 100644 src/test/loopManagerTest.cpp (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index e584ba7..9bedbcc 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -1,17 +1,17 @@ #include #include "../facade/SDLContext.h" - #include "../system/AnimatorSystem.h" #include "../system/CollisionSystem.h" #include "../system/ParticleSystem.h" #include "../system/PhysicsSystem.h" #include "../system/RenderSystem.h" #include "../system/ScriptSystem.h" + #include "../api/EventManager.h" #include "LoopManager.h" #include "LoopTimer.h" -#include + using namespace crepe; using namespace std; @@ -37,7 +37,6 @@ void LoopManager::start() { this->setup(); this->loop(); } -void LoopManager::set_running(bool running) { this->game_running = running; } void LoopManager::fixed_update() {} @@ -46,7 +45,7 @@ void LoopManager::loop() { while (game_running) { this->loop_timer->update(); - + while (this->loop_timer->get_lag() >= this->loop_timer->get_fixed_delta_time()) { this->process_input(); this->fixed_update(); @@ -67,7 +66,7 @@ void LoopManager::setup() { void LoopManager::render() { if (this->game_running) { - //this->get_system().update(); + this->get_system().update(); } } bool LoopManager::on_shutdown(const ShutDownEvent & e){ @@ -75,6 +74,4 @@ bool LoopManager::on_shutdown(const ShutDownEvent & e){ return false; } -void LoopManager::update() { - std::cout << this->loop_timer->get_fps() << std::endl; -} +void LoopManager::update() {} diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 3bf54b9..eb2b525 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -16,6 +16,12 @@ namespace crepe { */ class LoopManager { public: + /** + * \brief Start the gameloop + * + * This is the start of the engine where the setup is called and then the loop keeps running until the game stops running. + * Developers need to call this function to run the game. + */ void start(); LoopManager(); @@ -68,14 +74,6 @@ private: * This function updates physics and game logic based on LoopTimer's fixed_delta_time. */ void fixed_update(); - - /** - * \brief Set game running variable - * - * \param running running (false = game shutdown, true = game running) - */ - void set_running(bool running); - /** * \brief Function for executing render-related systems. * @@ -100,7 +98,7 @@ private: * \brief Collection of System instances * * This map holds System instances indexed by the system's class typeid. It is filled in the - * constructor of \c LoopManager using LoopManager::load_system. + * constructor of LoopManager using LoopManager::load_system. */ std::unordered_map> systems; /** diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 54100cf..6f92d45 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -19,4 +19,3 @@ endfunction() add_example(asset_manager) add_example(savemgr) add_example(rendering_particle) -add_example(gameloop) diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index d310f6a..b126add 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -12,4 +12,5 @@ target_sources(test_main PUBLIC ValueBrokerTest.cpp DBTest.cpp Vector2Test.cpp + loopTimerTest.cpp ) diff --git a/src/test/loopManagerTest.cpp b/src/test/loopManagerTest.cpp new file mode 100644 index 0000000..6e66ce7 --- /dev/null +++ b/src/test/loopManagerTest.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#define private public +#define protected public +#include "api/LoopTimer.h" +#include "api/LoopManager.h" + +using namespace std::chrono; +using namespace crepe; + +class LoopTimerTest : public ::testing::Test { +protected: + LoopManager loop_manager; + + void SetUp() override { + // Setting up loop manager and start the loop + loop_manager.setup(); + loop_manager.loop_timer->set_target_fps(60); + } +}; + +// Test to check if exactly 5 fixed updates are done every second (50Hz) +TEST_F(LoopTimerTest, FixedUpdateCalledAt50Hz) { + // Set target fixed delta time to 20ms (50Hz fixed updates) + loop_manager.loop_timer->set_fixed_delta_time(milliseconds(20)); + + int fixed_update_count = 0; + + // We want to simulate the game loop for about 1 second + auto start_time = steady_clock::now(); + + // Simulate the game loop for 1 second + while (duration_cast(steady_clock::now() - start_time).count() < 1) { + loop_manager.loop_timer->update(); + + // Simulate processing fixed updates while there's lag to advance + while (loop_manager.loop_timer->get_lag() >= loop_manager.loop_timer->get_fixed_delta_time()) { + loop_manager.fixed_update(); // Process fixed update + fixed_update_count++; // Count the number of fixed updates + loop_manager.loop_timer->advance_fixed_update(); + } + + // We do not need to call render or update for this test + loop_manager.loop_timer->enforce_frame_rate(); // Enforce the frame rate (this would normally go to the display) + } + + // We expect 5 fixed updates to occur in 1 second at 50Hz + ASSERT_EQ(fixed_update_count, 5); +} diff --git a/src/test/loopTimerTest.cpp b/src/test/loopTimerTest.cpp index d2f7d9b..9bbbff3 100644 --- a/src/test/loopTimerTest.cpp +++ b/src/test/loopTimerTest.cpp @@ -1,6 +1,8 @@ #include #include #include +#define private public +#define protected public #include "api/LoopTimer.h" using namespace std::chrono; @@ -11,23 +13,47 @@ protected: LoopTimer loop_timer; void SetUp() override { - loop_timer.start(); // Reset loop timer before each test. + loop_timer.start(); } }; TEST_F(LoopTimerTest, EnforcesTargetFrameRate) { // Set the target FPS to 60 (which gives a target time per frame of ~16.67 ms) loop_timer.set_target_fps(60); - // Simulate a short update (frame duration less than the target frame time) auto start_time = steady_clock::now(); - loop_timer.enforce_frame_rate(); // Enforce the frame rate + loop_timer.enforce_frame_rate(); - // Check that the loop timer's current time is greater than or equal to the target frame time auto elapsed_time = steady_clock::now() - start_time; auto elapsed_ms = duration_cast(elapsed_time).count(); - // Assert that the elapsed time is close to the target frame time // For 60 FPS, the target frame time is around 16.67ms ASSERT_GE(elapsed_ms, 16); // Make sure it's at least 16 ms (could be slightly more) ASSERT_LE(elapsed_ms, 18); // Ensure it's not too much longer } +TEST_F(LoopTimerTest, SetTargetFps) { + // Set the target FPS to 120 + loop_timer.set_target_fps(120); + + // Calculate the expected frame time (~8.33ms per frame) + auto expected_frame_time = std::chrono::duration(1.0 / 120.0); + + ASSERT_NEAR(loop_timer.frame_target_time.count(), expected_frame_time.count(), 0.001); +} +TEST_F(LoopTimerTest, DeltaTimeCalculation) { + // Set the target FPS to 60 (16.67 ms per frame) + loop_timer.set_target_fps(60); + + auto start_time = steady_clock::now(); + loop_timer.update(); + auto end_time = steady_clock::now(); + + // Check the delta time + double delta_time = loop_timer.get_delta_time(); + + auto elapsed_time = duration_cast(end_time - start_time).count(); + + // Assert that delta_time is close to the elapsed time + ASSERT_GE(delta_time, elapsed_time / 1000.0); + ASSERT_LE(delta_time, (elapsed_time + 2) / 1000.0); +} + -- cgit v1.2.3 From 7a9da9c1be04f401a701931f59ee85b1d37f0de0 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 5 Dec 2024 11:01:59 +0100 Subject: changed time back to 1 second --- src/crepe/api/LoopManager.h | 12 +++++++++--- src/crepe/api/LoopTimer.cpp | 2 +- src/test/LoopManagerTest.cpp | 1 - 3 files changed, 10 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 17bddd1..c50f5aa 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -5,8 +5,9 @@ #include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" #include "../manager/SceneManager.h" +#include "../manager/SceneManager.h" #include "../system/System.h" -#include "manager/SceneManager.h" + #include "api/Event.h" #include "api/LoopTimer.h" @@ -19,6 +20,7 @@ namespace crepe { */ class LoopManager { public: + LoopManager(); /** * \brief Start the gameloop * @@ -26,7 +28,7 @@ public: * Developers need to call this function to run the game. */ void start(); - LoopManager(); + /** * \brief Add a new concrete scene to the scene manager @@ -101,7 +103,11 @@ private: std::unique_ptr loop_timer; private: - //! callback function for shutdown event + /** + * \brief Callback function for ShutDownEvent + * + * This function sets the game_running variable to false, stopping the gameloop and therefor quitting the game. + */ 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 07f0f75..eedb5ee 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -47,7 +47,7 @@ double LoopTimer::get_fixed_delta_time() const { return this->fixed_delta_time.c void LoopTimer::set_target_fps(int fps) { this->target_fps = fps; // target time per frame in seconds - this->frame_target_time = std::chrono::duration(1.0) / target_fps; + this->frame_target_time = std::chrono::duration(1.0) / this->target_fps; } int LoopTimer::get_fps() const { return this->actual_fps; } diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp index af6cb1c..7937649 100644 --- a/src/test/LoopManagerTest.cpp +++ b/src/test/LoopManagerTest.cpp @@ -39,6 +39,5 @@ TEST_F(LoopManagerTest, FixedUpdate) { loop_manager.loop_timer->enforce_frame_rate(); } - // gameloop is 99 because it first takes 20 millisecond to build the lag to execute the fixed loop ASSERT_EQ(fixed_update_count, 50); } -- cgit v1.2.3 From d78ba1aafe83b4d5cb64ea696089e7517691cd6f Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 5 Dec 2024 11:06:40 +0100 Subject: make format --- src/crepe/api/LoopManager.cpp | 12 +++--- src/crepe/api/LoopManager.h | 5 +-- src/crepe/api/LoopTimer.cpp | 27 +++++++------- src/crepe/api/LoopTimer.h | 6 +-- src/test/LoopManagerTest.cpp | 46 ++++++++++++----------- src/test/LoopTimerTest.cpp | 87 ++++++++++++++++++++----------------------- 6 files changed, 87 insertions(+), 96 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 040cb93..5879d79 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -1,12 +1,12 @@ #include "../facade/SDLContext.h" +#include "../manager/EventManager.h" #include "../system/AnimatorSystem.h" #include "../system/CollisionSystem.h" #include "../system/ParticleSystem.h" #include "../system/PhysicsSystem.h" #include "../system/RenderSystem.h" #include "../system/ScriptSystem.h" -#include "../manager/EventManager.h" #include "LoopManager.h" @@ -20,9 +20,8 @@ LoopManager::LoopManager() { this->load_system(); this->load_system(); this->load_system(); - EventManager::get_instance().subscribe([this](const ShutDownEvent& event) { - return this->on_shutdown(event); - }); + EventManager::get_instance().subscribe( + [this](const ShutDownEvent & event) { return this->on_shutdown(event); }); this->loop_timer = make_unique(); this->mediator.loop_timer = *loop_timer; } @@ -41,7 +40,7 @@ void LoopManager::loop() { while (game_running) { this->loop_timer->update(); - + while (this->loop_timer->get_lag() >= this->loop_timer->get_fixed_delta_time()) { this->process_input(); this->fixed_update(); @@ -55,7 +54,6 @@ void LoopManager::loop() { } void LoopManager::setup() { - this->game_running = true; this->loop_timer->start(); @@ -67,7 +65,7 @@ void LoopManager::render() { this->get_system().update(); } -bool LoopManager::on_shutdown(const ShutDownEvent & e){ +bool LoopManager::on_shutdown(const ShutDownEvent & e) { this->game_running = false; return false; } diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index c50f5aa..6a212eb 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -5,10 +5,8 @@ #include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" #include "../manager/SceneManager.h" -#include "../manager/SceneManager.h" #include "../system/System.h" - #include "api/Event.h" #include "api/LoopTimer.h" @@ -28,7 +26,6 @@ public: * Developers need to call this function to run the game. */ void start(); - /** * \brief Add a new concrete scene to the scene manager @@ -101,8 +98,8 @@ private: SDLContext & sdl_context = SDLContext::get_instance(); //! loop timer instance std::unique_ptr loop_timer; + private: - /** * \brief Callback function for ShutDownEvent * diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index eedb5ee..8fb7ce8 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -10,13 +10,13 @@ using namespace crepe; LoopTimer::LoopTimer() { dbg_trace(); } - void LoopTimer::start() { this->last_frame_time = std::chrono::steady_clock::now(); - + this->elapsed_time = std::chrono::milliseconds(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(fixed_delta_time); + this->elapsed_fixed_time + = -std::chrono::duration_cast(fixed_delta_time); this->delta_time = std::chrono::milliseconds(0); } @@ -30,7 +30,7 @@ void LoopTimer::update() { this->delta_time = this->maximum_delta_time; } this->actual_fps = 1.0 / this->delta_time.count(); - + this->delta_time *= this->game_scale; this->elapsed_time += this->delta_time; this->last_frame_time = current_frame_time; @@ -56,17 +56,18 @@ 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() { - auto current_frame_time = std::chrono::steady_clock::now(); - auto frame_duration = current_frame_time - this->last_frame_time; + auto current_frame_time = std::chrono::steady_clock::now(); + auto 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) { - auto delay_time = std::chrono::duration_cast(this->frame_target_time - frame_duration); + // Check if frame duration is less than the target frame time + if (frame_duration < this->frame_target_time) { + auto delay_time = std::chrono::duration_cast( + this->frame_target_time - frame_duration); - if (delay_time.count() > 0) { - std::this_thread::sleep_for(delay_time); - } - } + if (delay_time.count() > 0) { + std::this_thread::sleep_for(delay_time); + } + } } double LoopTimer::get_lag() const { diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index e348628..c4294d7 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -90,8 +90,6 @@ private: */ double get_lag() const; - - /** * \brief Update the timer to the current frame. * @@ -120,7 +118,8 @@ private: //! Delta time for the current frame in seconds std::chrono::duration delta_time{0.0}; //! Target time per frame in seconds - std::chrono::duration frame_target_time = std::chrono::duration(1.0) / target_fps; + std::chrono::duration frame_target_time + = std::chrono::duration(1.0) / target_fps; //! Fixed delta time for fixed updates in seconds std::chrono::duration fixed_delta_time = std::chrono::duration(1.0) / 50.0; //! Total elapsed game time in seconds @@ -129,7 +128,6 @@ private: std::chrono::duration elapsed_fixed_time{0.0}; //! Time of the last frame std::chrono::steady_clock::time_point last_frame_time; - }; } // namespace crepe diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp index 7937649..5897906 100644 --- a/src/test/LoopManagerTest.cpp +++ b/src/test/LoopManagerTest.cpp @@ -1,43 +1,45 @@ -#include #include +#include #include #define private public #define protected public -#include "api/LoopTimer.h" #include "api/LoopManager.h" +#include "api/LoopTimer.h" using namespace std::chrono; using namespace crepe; class LoopManagerTest : public ::testing::Test { protected: - LoopManager loop_manager; + LoopManager loop_manager; - void SetUp() override { - // Setting up loop manager and start the loop - loop_manager.loop_timer->set_target_fps(60); - } + void SetUp() override { + // Setting up loop manager and start the loop + loop_manager.loop_timer->set_target_fps(60); + } }; //Test to check if exactly 5 fixed updates are done every second (50Hz) TEST_F(LoopManagerTest, FixedUpdate) { - loop_manager.loop_timer->fixed_delta_time = std::chrono::milliseconds(20); + loop_manager.loop_timer->fixed_delta_time = std::chrono::milliseconds(20); loop_manager.loop_timer->set_target_fps(50); - int fixed_update_count = 0; + int fixed_update_count = 0; loop_manager.loop_timer->start(); - // We want to simulate the game loop for about 1 second - auto start_time = steady_clock::now(); + // We want to simulate the game loop for about 1 second + auto start_time = steady_clock::now(); - // Simulate the game loop for 1 second - while (duration_cast(steady_clock::now() - start_time) < std::chrono::milliseconds(1000)) { + // Simulate the game loop for 1 second + while (duration_cast(steady_clock::now() - start_time) + < std::chrono::milliseconds(1000)) { loop_manager.loop_timer->update(); - // Simulate processing fixed updates while there's lag to advance - while (loop_manager.loop_timer->get_lag() >= loop_manager.loop_timer->get_fixed_delta_time()) { - fixed_update_count++; - loop_manager.loop_timer->advance_fixed_update(); - } - - loop_manager.loop_timer->enforce_frame_rate(); - } - ASSERT_EQ(fixed_update_count, 50); + // Simulate processing fixed updates while there's lag to advance + while (loop_manager.loop_timer->get_lag() + >= loop_manager.loop_timer->get_fixed_delta_time()) { + fixed_update_count++; + loop_manager.loop_timer->advance_fixed_update(); + } + + loop_manager.loop_timer->enforce_frame_rate(); + } + ASSERT_EQ(fixed_update_count, 50); } diff --git a/src/test/LoopTimerTest.cpp b/src/test/LoopTimerTest.cpp index 6e3f118..9068c72 100644 --- a/src/test/LoopTimerTest.cpp +++ b/src/test/LoopTimerTest.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include #define private public #define protected public @@ -10,72 +10,67 @@ using namespace crepe; class LoopTimerTest : public ::testing::Test { protected: - LoopTimer loop_timer; + LoopTimer loop_timer; - void SetUp() override { - loop_timer.start(); - } + void SetUp() override { loop_timer.start(); } }; TEST_F(LoopTimerTest, EnforcesTargetFrameRate) { - // Set the target FPS to 60 (which gives a target time per frame of ~16.67 ms) - loop_timer.set_target_fps(60); + // Set the target FPS to 60 (which gives a target time per frame of ~16.67 ms) + loop_timer.set_target_fps(60); - auto start_time = steady_clock::now(); - loop_timer.enforce_frame_rate(); + auto start_time = steady_clock::now(); + loop_timer.enforce_frame_rate(); - auto elapsed_time = steady_clock::now() - start_time; - auto elapsed_ms = duration_cast(elapsed_time).count(); + auto elapsed_time = steady_clock::now() - start_time; + auto elapsed_ms = duration_cast(elapsed_time).count(); - // For 60 FPS, the target frame time is around 16.67ms - ASSERT_GE(elapsed_ms, 16); // Make sure it's at least 16 ms (could be slightly more) - ASSERT_LE(elapsed_ms, 18); // Ensure it's not too much longer + // For 60 FPS, the target frame time is around 16.67ms + ASSERT_GE(elapsed_ms, 16); // Make sure it's at least 16 ms (could be slightly more) + ASSERT_LE(elapsed_ms, 18); // Ensure it's not too much longer } TEST_F(LoopTimerTest, SetTargetFps) { - // Set the target FPS to 120 - loop_timer.set_target_fps(120); - - // Calculate the expected frame time (~8.33ms per frame) - auto expected_frame_time = std::chrono::duration(1.0 / 120.0); + // Set the target FPS to 120 + loop_timer.set_target_fps(120); - ASSERT_NEAR(loop_timer.frame_target_time.count(), expected_frame_time.count(), 0.001); + // Calculate the expected frame time (~8.33ms per frame) + auto expected_frame_time = std::chrono::duration(1.0 / 120.0); + + ASSERT_NEAR(loop_timer.frame_target_time.count(), expected_frame_time.count(), 0.001); } TEST_F(LoopTimerTest, DeltaTimeCalculation) { - // Set the target FPS to 60 (16.67 ms per frame) - loop_timer.set_target_fps(60); + // Set the target FPS to 60 (16.67 ms per frame) + loop_timer.set_target_fps(60); + + auto start_time = steady_clock::now(); + loop_timer.update(); + auto end_time = steady_clock::now(); - auto start_time = steady_clock::now(); - loop_timer.update(); - auto end_time = steady_clock::now(); + // Check the delta time + double delta_time = loop_timer.get_delta_time(); - // Check the delta time - double delta_time = loop_timer.get_delta_time(); + auto elapsed_time = duration_cast(end_time - start_time).count(); - auto elapsed_time = duration_cast(end_time - start_time).count(); - - // Assert that delta_time is close to the elapsed time - ASSERT_GE(delta_time, elapsed_time / 1000.0); - ASSERT_LE(delta_time, (elapsed_time + 2) / 1000.0); + // Assert that delta_time is close to the elapsed time + ASSERT_GE(delta_time, elapsed_time / 1000.0); + ASSERT_LE(delta_time, (elapsed_time + 2) / 1000.0); } TEST_F(LoopTimerTest, getCurrentTime) { - // Set the target FPS to 60 (16.67 ms per frame) - loop_timer.set_target_fps(60); + // Set the target FPS to 60 (16.67 ms per frame) + loop_timer.set_target_fps(60); - auto start_time = steady_clock::now(); + auto start_time = steady_clock::now(); - // Sleep for 500 milliseconds - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + // Sleep for 500 milliseconds + std::this_thread::sleep_for(std::chrono::milliseconds(100)); - loop_timer.update(); + loop_timer.update(); - auto end_time = steady_clock::now(); + auto end_time = steady_clock::now(); - // Get the elapsed time in seconds as a double - auto elapsed_time = duration_cast>(end_time - start_time).count(); + // Get the elapsed time in seconds as a double + auto elapsed_time + = duration_cast>(end_time - start_time).count(); - ASSERT_NEAR(loop_timer.get_current_time(), elapsed_time, 0.001); - - + ASSERT_NEAR(loop_timer.get_current_time(), elapsed_time, 0.001); } - - -- cgit v1.2.3 From 090a2e8c26ade017a09049782465d7f7cea623ec Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 5 Dec 2024 12:37:14 +0100 Subject: save --- src/test/LoopTimerTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/test/LoopTimerTest.cpp b/src/test/LoopTimerTest.cpp index 9068c72..7652093 100644 --- a/src/test/LoopTimerTest.cpp +++ b/src/test/LoopTimerTest.cpp @@ -61,7 +61,7 @@ TEST_F(LoopTimerTest, getCurrentTime) { auto start_time = steady_clock::now(); - // Sleep for 500 milliseconds + // Sleep std::this_thread::sleep_for(std::chrono::milliseconds(100)); loop_timer.update(); -- cgit v1.2.3 From 823935627c7743c36e1b832670f931d6a67abe2a Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 5 Dec 2024 16:24:45 +0100 Subject: save --- src/crepe/api/LoopManager.h | 4 +- src/crepe/system/InputSystem.h | 84 ++++++++++++++++++++++++++++++++++++++++++ src/test/LoopManagerTest.cpp | 56 ++++++++++++++-------------- 3 files changed, 115 insertions(+), 29 deletions(-) create mode 100644 src/crepe/system/InputSystem.h (limited to 'src') diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 6a212eb..7097ee1 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -61,7 +61,7 @@ private: * * Updates the game state based on the elapsed time since the last frame. */ - void update(); + virtual void update(); /** * \brief Late update which is called after update(). @@ -75,7 +75,7 @@ private: * * This function updates physics and game logic based on LoopTimer's fixed_delta_time. */ - void fixed_update(); + virtual void fixed_update(); /** * \brief Function for executing render-related systems. * diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h new file mode 100644 index 0000000..0c42bd6 --- /dev/null +++ b/src/crepe/system/InputSystem.h @@ -0,0 +1,84 @@ +#pragma once + +#include "../facade/SDLContext.h" +#include "../types.h" +#include "../util/OptionalRef.h" + +#include "System.h" + +namespace crepe { + +class Camera; +class Button; +class Transform; + +/** + * \brief Handles the processing of input events created by SDLContext + * + * This system processes events such as mouse clicks, mouse movement, and keyboard + * actions. It is responsible for detecting interactions with UI buttons and + * passing the corresponding events to the registered listeners. + */ +class InputSystem : public System { +public: + using System::System; + + /** + * \brief Updates the system, processing all input events. + * This method processes all events and triggers corresponding actions. + */ + void update() override; + +private: + //! Stores the last position of the mouse when the button was pressed. + ivec2 last_mouse_down_position; + + //! Stores the last mouse button pressed. + MouseButton last_mouse_button = MouseButton::NONE; + + //! The maximum allowable distance between mouse down and mouse up to register as a click. + const int click_tolerance = 5; + + /** + * \brief Handles the mouse click event. + * \param mouse_button The mouse button involved in the click. + * \param world_mouse_x The X coordinate of the mouse in world space. + * \param world_mouse_y The Y coordinate of the mouse in world space. + * + * This method processes the mouse click event and triggers the corresponding button action. + */ + void handle_click(const MouseButton & mouse_button, const int world_mouse_x, + const int world_mouse_y); + + /** + * \brief Handles the mouse movement event. + * \param event_data The event data containing information about the mouse movement. + * \param world_mouse_x The X coordinate of the mouse in world space. + * \param world_mouse_y The Y coordinate of the mouse in world space. + * + * This method processes the mouse movement event and updates the button hover state. + */ + void handle_move(const SDLContext::EventData & event_data, const int world_mouse_x, + const int world_mouse_y); + + /** + * \brief Checks if the mouse position is inside the bounds of the button. + * \param world_mouse_x The X coordinate of the mouse in world space. + * \param world_mouse_y The Y coordinate of the mouse in world space. + * \param button The button to check. + * \param transform The transform component of the button. + * \return True if the mouse is inside the button, false otherwise. + */ + bool is_mouse_inside_button(const int world_mouse_x, const int world_mouse_y, + const Button & button, const Transform & transform); + + /** + * \brief Handles the button press event, calling the on_click callback if necessary. + * \param button The button being pressed. + * + * This method triggers the on_click action for the button when it is pressed. + */ + void handle_button_press(Button & button); +}; + +} // namespace crepe diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp index 5897906..f2ca8db 100644 --- a/src/test/LoopManagerTest.cpp +++ b/src/test/LoopManagerTest.cpp @@ -1,6 +1,8 @@ #include #include +#include #include + #define private public #define protected public #include "api/LoopManager.h" @@ -11,35 +13,35 @@ using namespace crepe; class LoopManagerTest : public ::testing::Test { protected: - LoopManager loop_manager; + class TestGameLoop : public crepe::LoopManager { + public: + MOCK_METHOD(void, fixed_update, (), (override)); + MOCK_METHOD(void, update, (), (override)); + }; + + TestGameLoop test_loop; - void SetUp() override { - // Setting up loop manager and start the loop - loop_manager.loop_timer->set_target_fps(60); - } + void SetUp() override { + test_loop.loop_timer->set_target_fps(60); // Example target FPS + } }; -//Test to check if exactly 5 fixed updates are done every second (50Hz) +// Test to check if exactly 50 fixed updates occur in 1 second (50Hz) TEST_F(LoopManagerTest, FixedUpdate) { - loop_manager.loop_timer->fixed_delta_time = std::chrono::milliseconds(20); - loop_manager.loop_timer->set_target_fps(50); - int fixed_update_count = 0; - loop_manager.loop_timer->start(); - // We want to simulate the game loop for about 1 second - auto start_time = steady_clock::now(); - - // Simulate the game loop for 1 second - while (duration_cast(steady_clock::now() - start_time) - < std::chrono::milliseconds(1000)) { - loop_manager.loop_timer->update(); - // Simulate processing fixed updates while there's lag to advance - while (loop_manager.loop_timer->get_lag() - >= loop_manager.loop_timer->get_fixed_delta_time()) { - fixed_update_count++; - loop_manager.loop_timer->advance_fixed_update(); - } - - loop_manager.loop_timer->enforce_frame_rate(); - } - ASSERT_EQ(fixed_update_count, 50); + // Arrange + using ::testing::AtLeast; + using ::testing::Exactly; + + test_loop.loop_timer->fixed_delta_time = std::chrono::milliseconds(20); + test_loop.start(); + // Expect the `fixed_update` method to be called exactly 50 times + EXPECT_CALL(test_loop, fixed_update()).Times(Exactly(50)); + + auto start_time = steady_clock::now(); + + // Act: Simulate the game loop for 1 second + while (duration_cast(steady_clock::now() - start_time) < std::chrono::milliseconds(1000)) { + + } + test_loop.game_running = false; } -- cgit v1.2.3 From a0070890fcdb422db85660fc44bcc709832870b8 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 5 Dec 2024 19:10:12 +0100 Subject: gameloop test working --- src/crepe/api/LoopManager.cpp | 5 +++-- src/crepe/api/LoopManager.h | 2 +- src/test/LoopManagerTest.cpp | 44 ++++++++++++++++++++++++++----------------- 3 files changed, 31 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 5879d79..454afe8 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -26,7 +26,9 @@ LoopManager::LoopManager() { this->mediator.loop_timer = *loop_timer; } -void LoopManager::process_input() { this->sdl_context.handle_events(this->game_running); } +void LoopManager::process_input() { + + this->sdl_context.handle_events(this->game_running); } void LoopManager::start() { this->setup(); @@ -57,7 +59,6 @@ void LoopManager::setup() { this->game_running = true; this->loop_timer->start(); - this->loop_timer->set_target_fps(200); } void LoopManager::render() { diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 7097ee1..2161dff 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -81,7 +81,7 @@ private: * * Renders the current state of the game to the screen. */ - void render(); + virtual void render(); bool game_running = false; diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp index f2ca8db..503eb1f 100644 --- a/src/test/LoopManagerTest.cpp +++ b/src/test/LoopManagerTest.cpp @@ -2,12 +2,13 @@ #include #include #include - +#include #define private public #define protected public #include "api/LoopManager.h" #include "api/LoopTimer.h" - +#include "manager/EventManager.h" +#include "api/Event.h" using namespace std::chrono; using namespace crepe; @@ -15,33 +16,42 @@ class LoopManagerTest : public ::testing::Test { protected: class TestGameLoop : public crepe::LoopManager { public: - MOCK_METHOD(void, fixed_update, (), (override)); + MOCK_METHOD(void, fixed_update, (), (override)); MOCK_METHOD(void, update, (), (override)); + MOCK_METHOD(void, render, (), (override)); }; TestGameLoop test_loop; - + // LoopManager test_loop; void SetUp() override { - test_loop.loop_timer->set_target_fps(60); // Example target FPS + test_loop.loop_timer->set_target_fps(10); + } }; -// Test to check if exactly 50 fixed updates occur in 1 second (50Hz) TEST_F(LoopManagerTest, FixedUpdate) { // Arrange - using ::testing::AtLeast; - using ::testing::Exactly; + test_loop.loop_timer->set_target_fps(60); - test_loop.loop_timer->fixed_delta_time = std::chrono::milliseconds(20); - test_loop.start(); - // Expect the `fixed_update` method to be called exactly 50 times - EXPECT_CALL(test_loop, fixed_update()).Times(Exactly(50)); + // Set expectations for the mock calls + EXPECT_CALL(test_loop, render).Times(::testing::Exactly(60)); + EXPECT_CALL(test_loop, update).Times(::testing::Exactly(60)); + EXPECT_CALL(test_loop, fixed_update).Times(::testing::AtLeast(50)); - auto start_time = steady_clock::now(); + // Start the loop in a separate thread + std::thread loop_thread([&]() { test_loop.start(); }); - // Act: Simulate the game loop for 1 second - while (duration_cast(steady_clock::now() - start_time) < std::chrono::milliseconds(1000)) { + // Let the loop run for exactly 1 second + std::this_thread::sleep_for(std::chrono::seconds(1)); - } - test_loop.game_running = false; + // Stop the game loop + test_loop.game_running = false; + + // Wait for the loop thread to finish + loop_thread.join(); + + // Test finished } + + + -- cgit v1.2.3 From a73ff31b67faa7e6a922cfb5598f56f80bc01d62 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 7 Dec 2024 14:19:16 +0100 Subject: added loopTimer and eventManager to mediator and removed the singletons --- src/crepe/api/CMakeLists.txt | 6 -- src/crepe/api/IKeyListener.cpp | 19 ----- src/crepe/api/IKeyListener.h | 50 ------------- src/crepe/api/IMouseListener.cpp | 29 ------- src/crepe/api/IMouseListener.h | 73 ------------------ src/crepe/api/LoopManager.cpp | 18 ++--- src/crepe/api/LoopManager.h | 9 ++- src/crepe/api/LoopTimer.cpp | 75 ------------------- src/crepe/api/LoopTimer.h | 133 --------------------------------- src/crepe/manager/CMakeLists.txt | 2 + src/crepe/manager/EventManager.cpp | 6 +- src/crepe/manager/EventManager.h | 24 ++---- src/crepe/manager/LoopTimerManager.cpp | 77 +++++++++++++++++++ src/crepe/manager/LoopTimerManager.h | 133 +++++++++++++++++++++++++++++++++ src/crepe/manager/Mediator.h | 8 +- src/test/EventTest.cpp | 88 +++++++++------------- src/test/LoopManagerTest.cpp | 29 ++++--- src/test/LoopTimerTest.cpp | 17 ++--- src/test/ScriptTest.h | 4 +- 19 files changed, 303 insertions(+), 497 deletions(-) delete mode 100644 src/crepe/api/IKeyListener.cpp delete mode 100644 src/crepe/api/IKeyListener.h delete mode 100644 src/crepe/api/IMouseListener.cpp delete mode 100644 src/crepe/api/IMouseListener.h delete mode 100644 src/crepe/api/LoopTimer.cpp delete mode 100644 src/crepe/api/LoopTimer.h create mode 100644 src/crepe/manager/LoopTimerManager.cpp create mode 100644 src/crepe/manager/LoopTimerManager.h (limited to 'src') diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 7da9dca..60d9dc5 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -13,10 +13,7 @@ target_sources(crepe PUBLIC Metadata.cpp Camera.cpp Animator.cpp - IKeyListener.cpp - IMouseListener.cpp LoopManager.cpp - LoopTimer.cpp Asset.cpp EventHandler.cpp Script.cpp @@ -45,9 +42,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES EventHandler.h EventHandler.hpp Event.h - IKeyListener.h - IMouseListener.h LoopManager.h - LoopTimer.h Asset.h ) diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp deleted file mode 100644 index 8642655..0000000 --- a/src/crepe/api/IKeyListener.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "IKeyListener.h" - -using namespace crepe; - -// Constructor with specified channel -IKeyListener::IKeyListener(event_channel_t channel) - : event_manager(EventManager::get_instance()) { - this->press_id = event_manager.subscribe( - [this](const KeyPressEvent & event) { return this->on_key_pressed(event); }, channel); - this->release_id = event_manager.subscribe( - [this](const KeyReleaseEvent & event) { return this->on_key_released(event); }, - channel); -} - -// Destructor, unsubscribe events -IKeyListener::~IKeyListener() { - event_manager.unsubscribe(this->press_id); - event_manager.unsubscribe(this->release_id); -} diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h deleted file mode 100644 index 6ded107..0000000 --- a/src/crepe/api/IKeyListener.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once - -#include "../manager/EventManager.h" - -#include "Event.h" -#include "EventHandler.h" - -namespace crepe { - -/** - * \class IKeyListener - * \brief Interface for keyboard event handling in the application. - */ -class IKeyListener { -public: - /** - * \brief Constructs an IKeyListener with a specified channel. - * \param channel The channel ID for event handling. - */ - IKeyListener(event_channel_t channel = EventManager::CHANNEL_ALL); - virtual ~IKeyListener(); - IKeyListener(const IKeyListener &) = delete; - IKeyListener & operator=(const IKeyListener &) = delete; - IKeyListener & operator=(IKeyListener &&) = delete; - IKeyListener(IKeyListener &&) = delete; - - /** - * \brief Pure virtual function to handle key press events. - * \param event The key press event to handle. - * \return True if the event was handled, false otherwise. - */ - virtual bool on_key_pressed(const KeyPressEvent & event) = 0; - - /** - * \brief Pure virtual function to handle key release events. - * \param event The key release event to handle. - * \return True if the event was handled, false otherwise. - */ - virtual bool on_key_released(const KeyReleaseEvent & event) = 0; - -private: - //! Key press event id - subscription_t press_id = -1; - //! Key release event id - subscription_t release_id = -1; - //! EventManager reference - EventManager & event_manager; -}; - -} // namespace crepe diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp deleted file mode 100644 index 989aeb3..0000000 --- a/src/crepe/api/IMouseListener.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "IMouseListener.h" - -using namespace crepe; - -IMouseListener::IMouseListener(event_channel_t channel) - : event_manager(EventManager::get_instance()) { - this->click_id = event_manager.subscribe( - [this](const MouseClickEvent & event) { return this->on_mouse_clicked(event); }, - channel); - - this->press_id = event_manager.subscribe( - [this](const MousePressEvent & event) { return this->on_mouse_pressed(event); }, - channel); - - this->release_id = event_manager.subscribe( - [this](const MouseReleaseEvent & event) { return this->on_mouse_released(event); }, - channel); - - this->move_id = event_manager.subscribe( - [this](const MouseMoveEvent & event) { return this->on_mouse_moved(event); }, channel); -} - -IMouseListener::~IMouseListener() { - // Unsubscribe event handlers - event_manager.unsubscribe(this->click_id); - event_manager.unsubscribe(this->press_id); - event_manager.unsubscribe(this->release_id); - event_manager.unsubscribe(this->move_id); -} diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h deleted file mode 100644 index 9e4fdf7..0000000 --- a/src/crepe/api/IMouseListener.h +++ /dev/null @@ -1,73 +0,0 @@ -#pragma once - -#include "../manager/EventManager.h" - -#include "Event.h" -#include "EventHandler.h" - -namespace crepe { - -/** - * \class IMouseListener - * \brief Interface for mouse event handling in the application. - */ -class IMouseListener { -public: - /** - * \brief Constructs an IMouseListener with a specified channel. - * \param channel The channel ID for event handling. - */ - IMouseListener(event_channel_t channel = EventManager::CHANNEL_ALL); - virtual ~IMouseListener(); - IMouseListener & operator=(const IMouseListener &) = delete; - IMouseListener(const IMouseListener &) = delete; - IMouseListener & operator=(const IMouseListener &&) = delete; - IMouseListener(IMouseListener &&) = delete; - - /** - * \brief Move assignment operator (deleted). - */ - IMouseListener & operator=(IMouseListener &&) = delete; - - /** - * \brief Handles a mouse click event. - * \param event The mouse click event to handle. - * \return True if the event was handled, false otherwise. - */ - virtual bool on_mouse_clicked(const MouseClickEvent & event) = 0; - - /** - * \brief Handles a mouse press event. - * \param event The mouse press event to handle. - * \return True if the event was handled, false otherwise. - */ - virtual bool on_mouse_pressed(const MousePressEvent & event) = 0; - - /** - * \brief Handles a mouse release event. - * \param event The mouse release event to handle. - * \return True if the event was handled, false otherwise. - */ - virtual bool on_mouse_released(const MouseReleaseEvent & event) = 0; - - /** - * \brief Handles a mouse move event. - * \param event The mouse move event to handle. - * \return True if the event was handled, false otherwise. - */ - virtual bool on_mouse_moved(const MouseMoveEvent & event) = 0; - -private: - //! Mouse click event id - subscription_t click_id = -1; - //! Mouse press event id - subscription_t press_id = -1; - //! Mouse release event id - subscription_t release_id = -1; - //! Mouse move event id - subscription_t move_id = -1; - //! EventManager reference - EventManager & event_manager; -}; - -} //namespace crepe diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 454afe8..69cbfaf 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -1,3 +1,4 @@ +#include #include "../facade/SDLContext.h" #include "../manager/EventManager.h" @@ -20,10 +21,8 @@ LoopManager::LoopManager() { this->load_system(); this->load_system(); this->load_system(); - EventManager::get_instance().subscribe( + this->event_manager.subscribe( [this](const ShutDownEvent & event) { return this->on_shutdown(event); }); - this->loop_timer = make_unique(); - this->mediator.loop_timer = *loop_timer; } void LoopManager::process_input() { @@ -38,27 +37,28 @@ void LoopManager::start() { void LoopManager::fixed_update() {} void LoopManager::loop() { - this->loop_timer->start(); + while (game_running) { - this->loop_timer->update(); + this->loop_timer.update(); - while (this->loop_timer->get_lag() >= this->loop_timer->get_fixed_delta_time()) { + while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_delta_time()) { this->process_input(); + event_manager.dispatch_events(); this->fixed_update(); - this->loop_timer->advance_fixed_update(); + this->loop_timer.advance_fixed_update(); } this->update(); this->render(); - this->loop_timer->enforce_frame_rate(); + this->loop_timer.enforce_frame_rate(); } } void LoopManager::setup() { this->game_running = true; - this->loop_timer->start(); + this->loop_timer.start(); } void LoopManager::render() { diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 2161dff..00f5409 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -5,10 +5,11 @@ #include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" #include "../manager/SceneManager.h" +#include "../manager/EventManager.h" +#include "../manager/LoopTimerManager.h" #include "../system/System.h" #include "api/Event.h" -#include "api/LoopTimer.h" namespace crepe { /** @@ -96,8 +97,10 @@ private: //! SDL context \todo no more singletons! SDLContext & sdl_context = SDLContext::get_instance(); - //! loop timer instance - std::unique_ptr loop_timer; + //! LoopTimer instance + LoopTimerManager loop_timer{mediator}; + //! EventManager instance + EventManager event_manager{mediator}; private: /** diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp deleted file mode 100644 index 8fb7ce8..0000000 --- a/src/crepe/api/LoopTimer.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include - -#include "../facade/SDLContext.h" -#include "../util/Log.h" - -#include "LoopTimer.h" - -using namespace crepe; - -LoopTimer::LoopTimer() { dbg_trace(); } - -void LoopTimer::start() { - this->last_frame_time = std::chrono::steady_clock::now(); - - this->elapsed_time = std::chrono::milliseconds(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(fixed_delta_time); - this->delta_time = std::chrono::milliseconds(0); -} - -void LoopTimer::update() { - auto current_frame_time = std::chrono::steady_clock::now(); - // Convert to duration in seconds for delta time - this->delta_time = std::chrono::duration_cast>( - current_frame_time - last_frame_time); - - if (this->delta_time > this->maximum_delta_time) { - this->delta_time = this->maximum_delta_time; - } - this->actual_fps = 1.0 / this->delta_time.count(); - - this->delta_time *= this->game_scale; - this->elapsed_time += this->delta_time; - this->last_frame_time = current_frame_time; -} - -double LoopTimer::get_delta_time() const { return this->delta_time.count(); } - -double LoopTimer::get_current_time() const { return this->elapsed_time.count(); } - -void LoopTimer::advance_fixed_update() { this->elapsed_fixed_time += this->fixed_delta_time; } - -double LoopTimer::get_fixed_delta_time() const { return this->fixed_delta_time.count(); } - -void LoopTimer::set_target_fps(int fps) { - this->target_fps = fps; - // target time per frame in seconds - this->frame_target_time = std::chrono::duration(1.0) / this->target_fps; -} - -int LoopTimer::get_fps() const { return this->actual_fps; } - -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() { - auto current_frame_time = std::chrono::steady_clock::now(); - auto 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) { - auto delay_time = std::chrono::duration_cast( - this->frame_target_time - frame_duration); - - if (delay_time.count() > 0) { - std::this_thread::sleep_for(delay_time); - } - } -} - -double LoopTimer::get_lag() const { - return (this->elapsed_time - this->elapsed_fixed_time).count(); -} diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h deleted file mode 100644 index c4294d7..0000000 --- a/src/crepe/api/LoopTimer.h +++ /dev/null @@ -1,133 +0,0 @@ -#pragma once - -#include - -namespace crepe { - -class LoopTimer { -public: - LoopTimer(); - /** - * \brief Get the current delta time for the current frame. - * - * \return Delta time in seconds since the last frame. - */ - double get_delta_time() const; - - /** - * \brief Get the current game time. - * - * \note The current game time may vary from real-world elapsed time. It is the cumulative - * sum of each frame's delta time. - * - * \return Elapsed game time in seconds. - */ - double get_current_time() const; - - /** - * \brief Set the target frames per second (FPS). - * - * \param fps The desired frames rendered per second. - */ - void set_target_fps(int fps); - - /** - * \brief Get the current frames per second (FPS). - * - * \return Current FPS. - */ - int get_fps() const; - - /** - * \brief Get the current game scale. - * - * \return The current game scale, where 0 = paused, 1 = normal speed, and values > 1 speed - * up the game. - */ - double get_game_scale() const; - - /** - * \brief Set the game scale. - * - * \param game_scale The desired game scale (0 = pause, 1 = normal speed, > 1 = speed up). - */ - void set_game_scale(double game_scale); - -private: - friend class LoopManager; - - /** - * \brief Start the loop timer. - * - * Initializes the timer to begin tracking frame times. - */ - void start(); - /** - * \brief Enforce the frame rate limit. - * - * Ensures that the game loop does not exceed the target FPS by delaying frame updates as - * necessary. - */ - void enforce_frame_rate(); - - /** - * \brief Get the fixed delta time for consistent updates. - * - * Fixed delta time is used for operations that require uniform time steps, such as physics - * calculations. - * - * \return Fixed delta time in seconds. - */ - double get_fixed_delta_time() const; - - /** - * \brief Get the accumulated lag in the game loop. - * - * Lag represents the difference between the target frame time and the actual frame time, - * useful for managing fixed update intervals. - * - * \return Accumulated lag in seconds. - */ - double get_lag() const; - - /** - * \brief Update the timer to the current frame. - * - * Calculates and updates the delta time for the current frame and adds it to the cumulative - * game time. - */ - void update(); - - /** - * \brief Advance the game loop by a fixed update interval. - * - * This method progresses the game state by a consistent, fixed time step, allowing for - * stable updates independent of frame rate fluctuations. - */ - void advance_fixed_update(); - -private: - //! Target frames per second - int target_fps = 50; - //! Actual frames per second - int actual_fps = 0; - //! Current game scale - double game_scale = 1; - //! Maximum delta time in seconds to avoid large jumps - std::chrono::duration maximum_delta_time{0.25}; - //! Delta time for the current frame in seconds - std::chrono::duration delta_time{0.0}; - //! Target time per frame in seconds - std::chrono::duration frame_target_time - = std::chrono::duration(1.0) / target_fps; - //! Fixed delta time for fixed updates in seconds - std::chrono::duration fixed_delta_time = std::chrono::duration(1.0) / 50.0; - //! Total elapsed game time in seconds - std::chrono::duration elapsed_time{0.0}; - //! Total elapsed time for fixed updates in seconds - std::chrono::duration elapsed_fixed_time{0.0}; - //! Time of the last frame - std::chrono::steady_clock::time_point last_frame_time; -}; - -} // namespace crepe diff --git a/src/crepe/manager/CMakeLists.txt b/src/crepe/manager/CMakeLists.txt index 517b8a2..29d6df0 100644 --- a/src/crepe/manager/CMakeLists.txt +++ b/src/crepe/manager/CMakeLists.txt @@ -4,6 +4,7 @@ target_sources(crepe PUBLIC Manager.cpp SaveManager.cpp SceneManager.cpp + LoopTimerManager.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -16,5 +17,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SaveManager.h SceneManager.h SceneManager.hpp + LoopTimerManager.h ) diff --git a/src/crepe/manager/EventManager.cpp b/src/crepe/manager/EventManager.cpp index 20f0dd3..9b0fa95 100644 --- a/src/crepe/manager/EventManager.cpp +++ b/src/crepe/manager/EventManager.cpp @@ -3,11 +3,9 @@ using namespace crepe; using namespace std; -EventManager & EventManager::get_instance() { - static EventManager instance; - return instance; +EventManager::EventManager(Mediator & mediator) : Manager(mediator){ + this->mediator.event_manager = *this; } - void EventManager::dispatch_events() { for (auto & event : this->events_queue) { this->handle_event(event.type, event.channel, *event.event.get()); diff --git a/src/crepe/manager/EventManager.h b/src/crepe/manager/EventManager.h index d634f54..5f8b107 100644 --- a/src/crepe/manager/EventManager.h +++ b/src/crepe/manager/EventManager.h @@ -8,6 +8,8 @@ #include "../api/Event.h" #include "../api/EventHandler.h" +#include "Manager.h" + namespace crepe { //! Event listener unique ID @@ -22,27 +24,16 @@ typedef size_t subscription_t; typedef size_t event_channel_t; /** - * \class EventManager * \brief Manages event subscriptions, triggers, and queues, enabling decoupled event handling. * * The `EventManager` acts as a centralized event system. It allows for registering callbacks * for specific event types, triggering events synchronously, queueing events for later * processing, and managing subscriptions via unique identifiers. */ -class EventManager { +class EventManager : public Manager { public: static constexpr const event_channel_t CHANNEL_ALL = -1; - - /** - * \brief Get the singleton instance of the EventManager. - * - * This method returns the unique instance of the EventManager, creating it if it - * doesn't already exist. Ensures only one instance is active in the program. - * - * \return Reference to the singleton instance of the EventManager. - */ - static EventManager & get_instance(); - + EventManager(Mediator & mediator); /** * \brief Subscribe to a specific event type. * @@ -107,12 +98,7 @@ public: void clear(); private: - /** - * \brief Default constructor for the EventManager. - * - * Constructor is private to enforce the singleton pattern. - */ - EventManager() = default; + /** * \struct QueueEntry diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp new file mode 100644 index 0000000..8156c6d --- /dev/null +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -0,0 +1,77 @@ +#include +#include + +#include "../facade/SDLContext.h" +#include "../util/Log.h" + +#include "LoopTimerManager.h" + +using namespace crepe; + +LoopTimerManager::LoopTimerManager(Mediator & mediator) : Manager(mediator) { + this->mediator.loop_timer = *this; + dbg_trace(); + } + +void LoopTimerManager::start() { + this->last_frame_time = std::chrono::steady_clock::now(); + + this->elapsed_time = std::chrono::milliseconds(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(fixed_delta_time); + this->delta_time = std::chrono::milliseconds(0); +} + +void LoopTimerManager::update() { + auto current_frame_time = std::chrono::steady_clock::now(); + // Convert to duration in seconds for delta time + this->delta_time = std::chrono::duration_cast>( + current_frame_time - last_frame_time); + + if (this->delta_time > this->maximum_delta_time) { + this->delta_time = this->maximum_delta_time; + } + this->actual_fps = 1.0 / this->delta_time.count(); + + this->elapsed_time += this->delta_time; + this->last_frame_time = current_frame_time; +} + +double LoopTimerManager::get_delta_time() const { return this->delta_time.count() * this->game_scale; } + +double LoopTimerManager::get_current_time() const { return this->elapsed_time.count(); } + +void LoopTimerManager::advance_fixed_update() { this->elapsed_fixed_time += this->fixed_delta_time; } + +double LoopTimerManager::get_fixed_delta_time() const { return this->fixed_delta_time.count(); } + +void LoopTimerManager::set_target_fps(int fps) { + this->target_fps = fps; + // target time per frame in seconds + this->frame_target_time = std::chrono::duration(1.0) / this->target_fps; +} + +int LoopTimerManager::get_fps() const { return this->actual_fps; } + +void LoopTimerManager::set_time_scale(double value) { this->game_scale = value; } + +double LoopTimerManager::get_time_scale() const { return this->game_scale; } +void LoopTimerManager::enforce_frame_rate() { + auto current_frame_time = std::chrono::steady_clock::now(); + auto 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) { + auto delay_time = std::chrono::duration_cast( + this->frame_target_time - frame_duration); + + if (delay_time.count() > 0) { + std::this_thread::sleep_for(delay_time); + } + } +} + +double LoopTimerManager::get_lag() const { + return (this->elapsed_time - this->elapsed_fixed_time).count(); +} diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h new file mode 100644 index 0000000..dba0f66 --- /dev/null +++ b/src/crepe/manager/LoopTimerManager.h @@ -0,0 +1,133 @@ +#pragma once + +#include +#include "../manager/Manager.h" +namespace crepe { + +class LoopTimerManager : public Manager { +public: + LoopTimerManager(Mediator & mediator); + /** + * \brief Get the current delta time for the current frame. + * + * \return Delta time in seconds since the last frame. + */ + double get_delta_time() const; + + /** + * \brief Get the current game time. + * + * \note The current game time may vary from real-world elapsed time. It is the cumulative + * sum of each frame's delta time. + * + * \return Elapsed game time in seconds. + */ + double get_current_time() const; + + /** + * \brief Set the target frames per second (FPS). + * + * \param fps The desired frames rendered per second. + */ + void set_target_fps(int fps); + + /** + * \brief Get the current frames per second (FPS). + * + * \return Current FPS. + */ + int get_fps() const; + + /** + * \brief Get the current time scale. + * + * \return The current time scale, where 0 = paused, 1 = normal speed, and values > 1 speed + * up the game. + */ + double get_time_scale() const; + + /** + * \brief Set the time scale. + * + * \param game_scale The desired time scale (0 = pause, 1 = normal speed, > 1 = speed up). + */ + void set_time_scale(double game_scale); + +private: + friend class LoopManager; + + /** + * \brief Start the loop timer. + * + * Initializes the timer to begin tracking frame times. + */ + void start(); + /** + * \brief Enforce the frame rate limit. + * + * Ensures that the game loop does not exceed the target FPS by delaying frame updates as + * necessary. + */ + void enforce_frame_rate(); + + /** + * \brief Get the fixed delta time for consistent updates. + * + * Fixed delta time is used for operations that require uniform time steps, such as physics + * calculations. + * + * \return Fixed delta time in seconds. + */ + double get_fixed_delta_time() const; + + /** + * \brief Get the accumulated lag in the game loop. + * + * Lag represents the difference between the target frame time and the actual frame time, + * useful for managing fixed update intervals. + * + * \return Accumulated lag in seconds. + */ + double get_lag() const; + + /** + * \brief Update the timer to the current frame. + * + * Calculates and updates the delta time for the current frame and adds it to the cumulative + * game time. + */ + void update(); + + /** + * \brief Advance the game loop by a fixed update interval. + * + * This method progresses the game state by a consistent, fixed time step, allowing for + * stable updates independent of frame rate fluctuations. + */ + void advance_fixed_update(); + +private: + //! Target frames per second + int target_fps = 50; + //! Actual frames per second + int actual_fps = 0; + //! Current game scale + double game_scale = 1; + //! Maximum delta time in seconds to avoid large jumps + std::chrono::duration maximum_delta_time{0.25}; + //! Delta time for the current frame in seconds + std::chrono::duration delta_time{0.0}; + //! Target time per frame in seconds + std::chrono::duration frame_target_time + = std::chrono::duration(1.0) / target_fps; + //! Fixed delta time for fixed updates in seconds + std::chrono::duration fixed_delta_time = std::chrono::duration(1.0) / 50.0; + //! Total elapsed game time in seconds + std::chrono::duration elapsed_time{0.0}; + //! Total elapsed time for fixed updates in seconds + std::chrono::duration elapsed_fixed_time{0.0}; + //! Time of the last frame + std::chrono::steady_clock::time_point last_frame_time; +}; + +} // namespace crepe diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index cd96614..c72af8e 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -3,14 +3,14 @@ #include "../util/OptionalRef.h" // TODO: remove these singletons: -#include "EventManager.h" #include "SaveManager.h" namespace crepe { class ComponentManager; class SceneManager; -class LoopTimer; +class LoopTimerManager; +class EventManager; /** * Struct to pass references to classes that would otherwise need to be singletons down to * other classes within the engine hierarchy. Made to prevent constant changes to subclasses to @@ -27,8 +27,8 @@ struct Mediator { OptionalRef component_manager; OptionalRef scene_manager; OptionalRef save_manager = SaveManager::get_instance(); - OptionalRef event_manager = EventManager::get_instance(); - OptionalRef loop_timer; + OptionalRef event_manager; + OptionalRef loop_timer; }; } // namespace crepe diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp index dccd554..8479998 100644 --- a/src/test/EventTest.cpp +++ b/src/test/EventTest.cpp @@ -1,56 +1,41 @@ #include #include - #include -#include -#include #include - +#include using namespace std; using namespace std::chrono_literals; using namespace crepe; class EventManagerTest : public ::testing::Test { protected: + Mediator mediator; + EventManager event_mgr{mediator}; void SetUp() override { // Clear any existing subscriptions or events before each test - EventManager::get_instance().clear(); + event_mgr.clear(); } void TearDown() override { // Ensure cleanup after each test - EventManager::get_instance().clear(); + event_mgr.clear(); } }; -class MockKeyListener : public IKeyListener { -public: - MOCK_METHOD(bool, on_key_pressed, (const KeyPressEvent & event), (override)); - MOCK_METHOD(bool, on_key_released, (const KeyReleaseEvent & event), (override)); -}; - -class MockMouseListener : public IMouseListener { -public: - MOCK_METHOD(bool, on_mouse_clicked, (const MouseClickEvent & event), (override)); - MOCK_METHOD(bool, on_mouse_pressed, (const MousePressEvent & event), (override)); - MOCK_METHOD(bool, on_mouse_released, (const MouseReleaseEvent & event), (override)); - MOCK_METHOD(bool, on_mouse_moved, (const MouseMoveEvent & event), (override)); -}; - TEST_F(EventManagerTest, EventSubscription) { EventHandler key_handler = [](const KeyPressEvent & e) { return true; }; // Subscribe to KeyPressEvent - EventManager::get_instance().subscribe(key_handler, 1); + event_mgr.subscribe(key_handler, 1); // Verify subscription (not directly verifiable; test by triggering event) - EventManager::get_instance().trigger_event( + event_mgr.trigger_event( KeyPressEvent{ .repeat = true, .key = Keycode::A, }, 1); - EventManager::get_instance().trigger_event( + event_mgr.trigger_event( KeyPressEvent{ .repeat = true, .key = Keycode::A, @@ -68,12 +53,12 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_all_channels) { EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; }; - EventManager::get_instance().subscribe(mouse_handler, + event_mgr.subscribe(mouse_handler, EventManager::CHANNEL_ALL); MouseClickEvent click_event{ .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; - EventManager::get_instance().trigger_event(click_event, + event_mgr.trigger_event(click_event, EventManager::CHANNEL_ALL); EXPECT_TRUE(triggered); @@ -88,19 +73,18 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_one_channel) { EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; }; - EventManager::get_instance().subscribe(mouse_handler, test_channel); + event_mgr.subscribe(mouse_handler, test_channel); MouseClickEvent click_event{ .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; - EventManager::get_instance().trigger_event(click_event, + event_mgr.trigger_event(click_event, EventManager::CHANNEL_ALL); EXPECT_FALSE(triggered); - EventManager::get_instance().trigger_event(click_event, test_channel); + event_mgr.trigger_event(click_event, test_channel); } TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { - EventManager & event_manager = EventManager::get_instance(); // Flags to track handler calls bool triggered_true = false; @@ -126,11 +110,11 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { // Test event MouseClickEvent click_event{ .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; - event_manager.subscribe(mouse_handler_true, EventManager::CHANNEL_ALL); - event_manager.subscribe(mouse_handler_false, EventManager::CHANNEL_ALL); + event_mgr.subscribe(mouse_handler_true, EventManager::CHANNEL_ALL); + event_mgr.subscribe(mouse_handler_false, EventManager::CHANNEL_ALL); // Trigger event - event_manager.trigger_event(click_event, EventManager::CHANNEL_ALL); + event_mgr.trigger_event(click_event, EventManager::CHANNEL_ALL); // Check that only the true handler was triggered EXPECT_TRUE(triggered_true); @@ -139,12 +123,12 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { // Reset and clear triggered_true = false; triggered_false = false; - event_manager.clear(); - event_manager.subscribe(mouse_handler_false, EventManager::CHANNEL_ALL); - event_manager.subscribe(mouse_handler_true, EventManager::CHANNEL_ALL); + event_mgr.clear(); + event_mgr.subscribe(mouse_handler_false, EventManager::CHANNEL_ALL); + event_mgr.subscribe(mouse_handler_true, EventManager::CHANNEL_ALL); // Trigger event again - event_manager.trigger_event(click_event, EventManager::CHANNEL_ALL); + event_mgr.trigger_event(click_event, EventManager::CHANNEL_ALL); // Check that both handlers were triggered EXPECT_TRUE(triggered_true); @@ -152,7 +136,6 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { } TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) { - EventManager & event_manager = EventManager::get_instance(); bool triggered1 = false; bool triggered2 = false; int test_channel = 1; @@ -170,21 +153,20 @@ TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) { EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; // Allows propagation }; - event_manager.subscribe(mouse_handler1); - event_manager.subscribe(mouse_handler2, test_channel); + event_mgr.subscribe(mouse_handler1); + event_mgr.subscribe(mouse_handler2, test_channel); - event_manager.queue_event( + event_mgr.queue_event( MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}); - event_manager.queue_event( + event_mgr.queue_event( MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}, test_channel); - event_manager.dispatch_events(); + event_mgr.dispatch_events(); EXPECT_TRUE(triggered1); EXPECT_TRUE(triggered2); } TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { - EventManager & event_manager = EventManager::get_instance(); // Flags to track if handlers are triggered bool triggered1 = false; @@ -207,15 +189,15 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { return false; // Allows propagation }; // Subscribe handlers - subscription_t handler1_id = event_manager.subscribe(mouse_handler1); - subscription_t handler2_id = event_manager.subscribe(mouse_handler2); + subscription_t handler1_id = event_mgr.subscribe(mouse_handler1); + subscription_t handler2_id = event_mgr.subscribe(mouse_handler2); // Queue events - event_manager.queue_event( + event_mgr.queue_event( MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}); // Dispatch events - both handlers should be triggered - event_manager.dispatch_events(); + event_mgr.dispatch_events(); EXPECT_TRUE(triggered1); // Handler 1 should be triggered EXPECT_TRUE(triggered2); // Handler 2 should be triggered @@ -224,14 +206,14 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { triggered2 = false; // Unsubscribe handler1 - event_manager.unsubscribe(handler1_id); + event_mgr.unsubscribe(handler1_id); // Queue the same event again - event_manager.queue_event( + event_mgr.queue_event( MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}); // Dispatch events - only handler 2 should be triggered, handler 1 should NOT - event_manager.dispatch_events(); + event_mgr.dispatch_events(); EXPECT_FALSE(triggered1); // Handler 1 should NOT be triggered EXPECT_TRUE(triggered2); // Handler 2 should be triggered @@ -239,14 +221,14 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { triggered2 = false; // Unsubscribe handler2 - event_manager.unsubscribe(handler2_id); + event_mgr.unsubscribe(handler2_id); // Queue the event again - event_manager.queue_event( + event_mgr.queue_event( MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}); // Dispatch events - no handler should be triggered - event_manager.dispatch_events(); + event_mgr.dispatch_events(); EXPECT_FALSE(triggered1); // Handler 1 should NOT be triggered EXPECT_FALSE(triggered2); // Handler 2 should NOT be triggered } diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp index 503eb1f..57f7a2e 100644 --- a/src/test/LoopManagerTest.cpp +++ b/src/test/LoopManagerTest.cpp @@ -2,13 +2,11 @@ #include #include #include -#include #define private public #define protected public -#include "api/LoopManager.h" -#include "api/LoopTimer.h" -#include "manager/EventManager.h" -#include "api/Event.h" +#include +#include +#include using namespace std::chrono; using namespace crepe; @@ -24,19 +22,18 @@ protected: TestGameLoop test_loop; // LoopManager test_loop; void SetUp() override { - test_loop.loop_timer->set_target_fps(10); } }; TEST_F(LoopManagerTest, FixedUpdate) { // Arrange - test_loop.loop_timer->set_target_fps(60); + test_loop.loop_timer.set_target_fps(60); // Set expectations for the mock calls EXPECT_CALL(test_loop, render).Times(::testing::Exactly(60)); EXPECT_CALL(test_loop, update).Times(::testing::Exactly(60)); - EXPECT_CALL(test_loop, fixed_update).Times(::testing::AtLeast(50)); + EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(50)); // Start the loop in a separate thread std::thread loop_thread([&]() { test_loop.start(); }); @@ -46,12 +43,26 @@ TEST_F(LoopManagerTest, FixedUpdate) { // Stop the game loop test_loop.game_running = false; - // Wait for the loop thread to finish loop_thread.join(); // Test finished } +TEST_F(LoopManagerTest, ShutDown) { + // Arrange + test_loop.loop_timer.set_target_fps(60); + EXPECT_CALL(test_loop, render).Times(::testing::AtLeast(1)); + EXPECT_CALL(test_loop, update).Times(::testing::AtLeast(1)); + EXPECT_CALL(test_loop, fixed_update).Times(::testing::AtLeast(1)); + // Start the loop in a separate thread + std::thread loop_thread([&]() { test_loop.start(); }); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + test_loop.event_manager.trigger_event(ShutDownEvent{}); + // Wait for the loop thread to finish + loop_thread.join(); + + // Test finished +} diff --git a/src/test/LoopTimerTest.cpp b/src/test/LoopTimerTest.cpp index 7652093..09b4e00 100644 --- a/src/test/LoopTimerTest.cpp +++ b/src/test/LoopTimerTest.cpp @@ -1,16 +1,17 @@ #include -#include #include +#include #define private public #define protected public -#include "api/LoopTimer.h" - +#include +#include using namespace std::chrono; using namespace crepe; class LoopTimerTest : public ::testing::Test { protected: - LoopTimer loop_timer; + Mediator mediator; + LoopTimerManager loop_timer{mediator}; void SetUp() override { loop_timer.start(); } }; @@ -25,8 +26,7 @@ TEST_F(LoopTimerTest, EnforcesTargetFrameRate) { auto elapsed_ms = duration_cast(elapsed_time).count(); // For 60 FPS, the target frame time is around 16.67ms - ASSERT_GE(elapsed_ms, 16); // Make sure it's at least 16 ms (could be slightly more) - ASSERT_LE(elapsed_ms, 18); // Ensure it's not too much longer + ASSERT_NEAR(elapsed_ms,16.7,1); } TEST_F(LoopTimerTest, SetTargetFps) { // Set the target FPS to 120 @@ -48,11 +48,10 @@ TEST_F(LoopTimerTest, DeltaTimeCalculation) { // Check the delta time double delta_time = loop_timer.get_delta_time(); - auto elapsed_time = duration_cast(end_time - start_time).count(); + auto elapsed_time = duration_cast(end_time - start_time).count(); // Assert that delta_time is close to the elapsed time - ASSERT_GE(delta_time, elapsed_time / 1000.0); - ASSERT_LE(delta_time, (elapsed_time + 2) / 1000.0); + ASSERT_NEAR(delta_time, elapsed_time, 1); } TEST_F(LoopTimerTest, getCurrentTime) { diff --git a/src/test/ScriptTest.h b/src/test/ScriptTest.h index 1bbfdd3..ee68c23 100644 --- a/src/test/ScriptTest.h +++ b/src/test/ScriptTest.h @@ -7,7 +7,7 @@ #include #include #include - +#include class ScriptTest : public testing::Test { protected: crepe::Mediator mediator; @@ -15,7 +15,7 @@ protected: public: crepe::ComponentManager component_manager{mediator}; crepe::ScriptSystem system{mediator}; - + crepe::EventManager event_mgr{mediator}; class MyScript : public crepe::Script { // NOTE: explicitly stating `public:` is not required on actual scripts -- cgit v1.2.3 From a06ea7d18008d0859818f7e8fd3e595e7dd05772 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 7 Dec 2024 14:44:46 +0100 Subject: changed looptimerManager doxygen --- src/crepe/manager/LoopTimerManager.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index dba0f66..fee6310 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -1,9 +1,19 @@ #pragma once #include -#include "../manager/Manager.h" + +#include "Manager.h" + namespace crepe { +/** + * \brief Manages timing and frame rate for the game loop. + * + * The LoopTimerManager class is responsible for calculating and managing timing functions + * such as delta time, frames per second (FPS), fixed time steps, and time scaling. It ensures + * consistent frame updates and supports game loop operations, such as handling fixed updates + * for physics and other time-sensitive operations. + */ class LoopTimerManager : public Manager { public: LoopTimerManager(Mediator & mediator); @@ -49,6 +59,8 @@ public: /** * \brief Set the time scale. * + * time_scale is a value that changes the delta time that can be retrieved using get_delta_time function. + * * \param game_scale The desired time scale (0 = pause, 1 = normal speed, > 1 = speed up). */ void set_time_scale(double game_scale); -- cgit v1.2.3 From f05458cdbf68e8efe1ed812f57e957921921941d Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 7 Dec 2024 14:51:12 +0100 Subject: more doxygen changes --- src/crepe/api/LoopManager.cpp | 1 - src/crepe/api/LoopManager.h | 9 +++++---- src/crepe/manager/LoopTimerManager.cpp | 6 +++--- src/crepe/manager/LoopTimerManager.h | 10 +++++----- 4 files changed, 13 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 69cbfaf..42a1e77 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -1,4 +1,3 @@ -#include #include "../facade/SDLContext.h" #include "../manager/EventManager.h" diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 00f5409..d07ef66 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -94,13 +94,14 @@ private: ComponentManager component_manager{mediator}; //! Scene manager instance SceneManager scene_manager{mediator}; - - //! SDL context \todo no more singletons! - SDLContext & sdl_context = SDLContext::get_instance(); - //! LoopTimer instance + //! LoopTimerManager instance LoopTimerManager loop_timer{mediator}; //! EventManager instance EventManager event_manager{mediator}; + + //! SDL context \todo no more singletons! + SDLContext & sdl_context = SDLContext::get_instance(); + private: /** diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 8156c6d..2379fdd 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -38,7 +38,7 @@ void LoopTimerManager::update() { this->last_frame_time = current_frame_time; } -double LoopTimerManager::get_delta_time() const { return this->delta_time.count() * this->game_scale; } +double LoopTimerManager::get_delta_time() const { return this->delta_time.count() * this->time_scale; } double LoopTimerManager::get_current_time() const { return this->elapsed_time.count(); } @@ -54,9 +54,9 @@ void LoopTimerManager::set_target_fps(int fps) { int LoopTimerManager::get_fps() const { return this->actual_fps; } -void LoopTimerManager::set_time_scale(double value) { this->game_scale = value; } +void LoopTimerManager::set_time_scale(double value) { this->time_scale = value; } -double LoopTimerManager::get_time_scale() const { return this->game_scale; } +double LoopTimerManager::get_time_scale() const { return this->time_scale; } void LoopTimerManager::enforce_frame_rate() { auto current_frame_time = std::chrono::steady_clock::now(); auto frame_duration = current_frame_time - this->last_frame_time; diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index fee6310..cd05bf2 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -51,7 +51,7 @@ public: /** * \brief Get the current time scale. * - * \return The current time scale, where 0 = paused, 1 = normal speed, and values > 1 speed + * \return The current time scale, where (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up). * up the game. */ double get_time_scale() const; @@ -61,9 +61,9 @@ public: * * time_scale is a value that changes the delta time that can be retrieved using get_delta_time function. * - * \param game_scale The desired time scale (0 = pause, 1 = normal speed, > 1 = speed up). + * \param time_scale The desired time scale (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up). */ - void set_time_scale(double game_scale); + void set_time_scale(double time_scale); private: friend class LoopManager; @@ -123,8 +123,8 @@ private: int target_fps = 50; //! Actual frames per second int actual_fps = 0; - //! Current game scale - double game_scale = 1; + //! time scale for speeding up or slowing down the game (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up) + double time_scale = 1; //! Maximum delta time in seconds to avoid large jumps std::chrono::duration maximum_delta_time{0.25}; //! Delta time for the current frame in seconds -- cgit v1.2.3 From 24c9a9ab277897a7191d4e99213c2ab9f5d4ecd8 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 7 Dec 2024 15:32:47 +0100 Subject: make format --- src/crepe/api/LoopManager.cpp | 5 +-- src/crepe/api/LoopManager.h | 5 +-- src/crepe/manager/EventManager.cpp | 2 +- src/crepe/manager/EventManager.h | 1 - src/crepe/manager/LoopTimerManager.cpp | 18 ++++++--- src/test/EventTest.cpp | 13 +++---- src/test/LoopManagerTest.cpp | 70 ++++++++++++++++------------------ src/test/LoopTimerTest.cpp | 4 +- src/test/ScriptTest.h | 2 +- 9 files changed, 57 insertions(+), 63 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index c25e31e..f41c357 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -27,9 +27,7 @@ LoopManager::LoopManager() { [this](const ShutDownEvent & event) { return this->on_shutdown(event); }); } -void LoopManager::process_input() { - this->get_system().update(); -} +void LoopManager::process_input() { this->get_system().update(); } void LoopManager::start() { this->setup(); @@ -45,7 +43,6 @@ void LoopManager::fixed_update() { } void LoopManager::loop() { - while (game_running) { this->loop_timer.update(); diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 9986aa5..6b2e857 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -4,11 +4,11 @@ #include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" -#include "../manager/SceneManager.h" #include "../manager/EventManager.h" #include "../manager/LoopTimerManager.h" -#include "../system/System.h" #include "../manager/Mediator.h" +#include "../manager/SceneManager.h" +#include "../system/System.h" #include "api/Event.h" @@ -103,7 +103,6 @@ private: //! SDL context \todo no more singletons! SDLContext & sdl_context = SDLContext::get_instance(); - private: /** * \brief Callback function for ShutDownEvent diff --git a/src/crepe/manager/EventManager.cpp b/src/crepe/manager/EventManager.cpp index 9b0fa95..6aa49ee 100644 --- a/src/crepe/manager/EventManager.cpp +++ b/src/crepe/manager/EventManager.cpp @@ -3,7 +3,7 @@ using namespace crepe; using namespace std; -EventManager::EventManager(Mediator & mediator) : Manager(mediator){ +EventManager::EventManager(Mediator & mediator) : Manager(mediator) { this->mediator.event_manager = *this; } void EventManager::dispatch_events() { diff --git a/src/crepe/manager/EventManager.h b/src/crepe/manager/EventManager.h index 30b929c..ba55edf 100644 --- a/src/crepe/manager/EventManager.h +++ b/src/crepe/manager/EventManager.h @@ -98,7 +98,6 @@ public: void clear(); private: - /** * \struct QueueEntry * \brief Represents an entry in the event queue. diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 2379fdd..9bf30ae 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -8,10 +8,10 @@ using namespace crepe; -LoopTimerManager::LoopTimerManager(Mediator & mediator) : Manager(mediator) { +LoopTimerManager::LoopTimerManager(Mediator & mediator) : Manager(mediator) { this->mediator.loop_timer = *this; - dbg_trace(); - } + dbg_trace(); +} void LoopTimerManager::start() { this->last_frame_time = std::chrono::steady_clock::now(); @@ -38,13 +38,19 @@ void LoopTimerManager::update() { this->last_frame_time = current_frame_time; } -double LoopTimerManager::get_delta_time() const { return this->delta_time.count() * this->time_scale; } +double LoopTimerManager::get_delta_time() const { + return this->delta_time.count() * this->time_scale; +} double LoopTimerManager::get_current_time() const { return this->elapsed_time.count(); } -void LoopTimerManager::advance_fixed_update() { this->elapsed_fixed_time += this->fixed_delta_time; } +void LoopTimerManager::advance_fixed_update() { + this->elapsed_fixed_time += this->fixed_delta_time; +} -double LoopTimerManager::get_fixed_delta_time() const { return this->fixed_delta_time.count(); } +double LoopTimerManager::get_fixed_delta_time() const { + return this->fixed_delta_time.count(); +} void LoopTimerManager::set_target_fps(int fps) { this->target_fps = fps; diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp index 8479998..82272b5 100644 --- a/src/test/EventTest.cpp +++ b/src/test/EventTest.cpp @@ -1,8 +1,8 @@ -#include -#include #include #include #include +#include +#include using namespace std; using namespace std::chrono_literals; using namespace crepe; @@ -53,13 +53,11 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_all_channels) { EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; }; - event_mgr.subscribe(mouse_handler, - EventManager::CHANNEL_ALL); + event_mgr.subscribe(mouse_handler, EventManager::CHANNEL_ALL); MouseClickEvent click_event{ .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; - event_mgr.trigger_event(click_event, - EventManager::CHANNEL_ALL); + event_mgr.trigger_event(click_event, EventManager::CHANNEL_ALL); EXPECT_TRUE(triggered); } @@ -77,8 +75,7 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_one_channel) { MouseClickEvent click_event{ .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; - event_mgr.trigger_event(click_event, - EventManager::CHANNEL_ALL); + event_mgr.trigger_event(click_event, EventManager::CHANNEL_ALL); EXPECT_FALSE(triggered); event_mgr.trigger_event(click_event, test_channel); diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp index 13a0ada..f73605e 100644 --- a/src/test/LoopManagerTest.cpp +++ b/src/test/LoopManagerTest.cpp @@ -1,67 +1,63 @@ #include -#include #include +#include #include #define private public #define protected public #include -#include #include +#include using namespace std::chrono; using namespace crepe; class LoopManagerTest : public ::testing::Test { protected: - class TestGameLoop : public crepe::LoopManager { - public: - MOCK_METHOD(void, fixed_update, (), (override)); - MOCK_METHOD(void, update, (), (override)); + class TestGameLoop : public crepe::LoopManager { + public: + MOCK_METHOD(void, fixed_update, (), (override)); + MOCK_METHOD(void, update, (), (override)); MOCK_METHOD(void, render, (), (override)); - }; + }; - TestGameLoop test_loop; - void SetUp() override { - - } + TestGameLoop test_loop; + void SetUp() override {} }; TEST_F(LoopManagerTest, FixedUpdate) { - // Arrange - test_loop.loop_timer.set_target_fps(60); + // Arrange + test_loop.loop_timer.set_target_fps(60); - // Set expectations for the mock calls - EXPECT_CALL(test_loop, render).Times(::testing::Exactly(60)); + // Set expectations for the mock calls + EXPECT_CALL(test_loop, render).Times(::testing::Exactly(60)); EXPECT_CALL(test_loop, update).Times(::testing::Exactly(60)); - EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(50)); + EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(50)); - // Start the loop in a separate thread - std::thread loop_thread([&]() { test_loop.start(); }); + // Start the loop in a separate thread + std::thread loop_thread([&]() { test_loop.start(); }); - // Let the loop run for exactly 1 second - std::this_thread::sleep_for(std::chrono::seconds(1)); + // Let the loop run for exactly 1 second + std::this_thread::sleep_for(std::chrono::seconds(1)); - // Stop the game loop - test_loop.game_running = false; - // Wait for the loop thread to finish - loop_thread.join(); + // Stop the game loop + test_loop.game_running = false; + // Wait for the loop thread to finish + loop_thread.join(); - // Test finished + // Test finished } TEST_F(LoopManagerTest, ShutDown) { - // Arrange - test_loop.loop_timer.set_target_fps(60); + // Arrange + test_loop.loop_timer.set_target_fps(60); - EXPECT_CALL(test_loop, render).Times(::testing::AtLeast(1)); + EXPECT_CALL(test_loop, render).Times(::testing::AtLeast(1)); EXPECT_CALL(test_loop, update).Times(::testing::AtLeast(1)); - EXPECT_CALL(test_loop, fixed_update).Times(::testing::AtLeast(1)); - // Start the loop in a separate thread - std::thread loop_thread([&]() { test_loop.start(); }); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + EXPECT_CALL(test_loop, fixed_update).Times(::testing::AtLeast(1)); + // Start the loop in a separate thread + std::thread loop_thread([&]() { test_loop.start(); }); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); test_loop.event_manager.trigger_event(ShutDownEvent{}); - // Wait for the loop thread to finish - loop_thread.join(); + // Wait for the loop thread to finish + loop_thread.join(); - // Test finished + // Test finished } - - diff --git a/src/test/LoopTimerTest.cpp b/src/test/LoopTimerTest.cpp index 09b4e00..c6655d9 100644 --- a/src/test/LoopTimerTest.cpp +++ b/src/test/LoopTimerTest.cpp @@ -1,6 +1,6 @@ #include -#include #include +#include #define private public #define protected public #include @@ -26,7 +26,7 @@ TEST_F(LoopTimerTest, EnforcesTargetFrameRate) { auto elapsed_ms = duration_cast(elapsed_time).count(); // For 60 FPS, the target frame time is around 16.67ms - ASSERT_NEAR(elapsed_ms,16.7,1); + ASSERT_NEAR(elapsed_ms, 16.7, 1); } TEST_F(LoopTimerTest, SetTargetFps) { // Set the target FPS to 120 diff --git a/src/test/ScriptTest.h b/src/test/ScriptTest.h index ee68c23..e0205ff 100644 --- a/src/test/ScriptTest.h +++ b/src/test/ScriptTest.h @@ -6,8 +6,8 @@ #include #include #include -#include #include +#include class ScriptTest : public testing::Test { protected: crepe::Mediator mediator; -- cgit v1.2.3 From 278e81b4ab039802ced33810aef640e61f1ab47a Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 7 Dec 2024 15:35:42 +0100 Subject: removed duplicate dispatch --- src/crepe/api/LoopManager.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index f41c357..4d97e16 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -27,7 +27,10 @@ LoopManager::LoopManager() { [this](const ShutDownEvent & event) { return this->on_shutdown(event); }); } -void LoopManager::process_input() { this->get_system().update(); } +void LoopManager::process_input() { + this->get_system().update(); + this->event_manager.dispatch_events(); + } void LoopManager::start() { this->setup(); @@ -35,8 +38,6 @@ void LoopManager::start() { } void LoopManager::fixed_update() { - EventManager & ev = this->mediator.event_manager; - ev.dispatch_events(); this->get_system().update(); this->get_system().update(); this->get_system().update(); @@ -49,7 +50,6 @@ void LoopManager::loop() { while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_delta_time()) { this->process_input(); - event_manager.dispatch_events(); this->fixed_update(); this->loop_timer.advance_fixed_update(); } -- cgit v1.2.3 From f0fba1667882764d4fee426e0c14f6ec3e11cdbc Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 7 Dec 2024 15:39:23 +0100 Subject: added line break --- src/crepe/manager/LoopTimerManager.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 9bf30ae..e30b90d 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -63,6 +63,7 @@ int LoopTimerManager::get_fps() const { return this->actual_fps; } void LoopTimerManager::set_time_scale(double value) { this->time_scale = value; } double LoopTimerManager::get_time_scale() const { return this->time_scale; } + void LoopTimerManager::enforce_frame_rate() { auto current_frame_time = std::chrono::steady_clock::now(); auto frame_duration = current_frame_time - this->last_frame_time; -- cgit v1.2.3 From 3467c96eff775fccb24a6ecd7c15a7f42660a7ff Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 7 Dec 2024 15:40:32 +0100 Subject: removed comments --- src/crepe/manager/Mediator.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src') diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index 987f4d4..eb8a7a5 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -4,9 +4,8 @@ // TODO: remove these singletons: #include "../facade/SDLContext.h" -//#include "EventManager.h" + #include "SaveManager.h" -//#include "LoopTimerManager.h" namespace crepe { -- cgit v1.2.3 From cc1b36f4f0a20695bcfc5c16c853cb32312fe70c Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sun, 8 Dec 2024 17:32:11 +0100 Subject: added scaled fixed delta time --- src/crepe/api/LoopManager.cpp | 2 +- src/crepe/manager/LoopTimerManager.cpp | 7 ++++++ src/crepe/manager/LoopTimerManager.h | 42 ++++++++++++++++++++++++---------- src/test/LoopManagerTest.cpp | 22 ++++++++++++++++++ 4 files changed, 60 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 4d97e16..d17aee1 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -48,7 +48,7 @@ void LoopManager::loop() { while (game_running) { this->loop_timer.update(); - while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_delta_time()) { + while (this->loop_timer.get_lag() >= this->loop_timer.get_scaled_fixed_delta_time()) { this->process_input(); this->fixed_update(); this->loop_timer.advance_fixed_update(); diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index e30b90d..5d4545f 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -82,3 +82,10 @@ void LoopTimerManager::enforce_frame_rate() { double LoopTimerManager::get_lag() const { return (this->elapsed_time - this->elapsed_fixed_time).count(); } +double LoopTimerManager::get_scaled_fixed_delta_time() const{ + return this->fixed_delta_time.count() * this->time_scale; +} +void LoopTimerManager::set_fixed_delta_time(int seconds) { + this->fixed_delta_time = std::chrono::duration(seconds); +} + diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index cd05bf2..8fb4461 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -65,6 +65,35 @@ public: */ void set_time_scale(double time_scale); + /** + * \brief Get the scaled fixed delta time om seconds. + * + * The fixed delta time is used for operations that require uniform time steps, + * such as physics calculations, and is scaled by the current time scale. + * + * \return The fixed delta time, scaled by time scale, in seconds. + */ + double get_scaled_fixed_delta_time() const; + + /** + * \brief Get the fixed delta time in seconds without scaling by the time scale. + * + * This value is used in the LoopManager to determine how many times + * the fixed_update should be called within a given interval. + * + * \return The unscaled fixed delta time in seconds. + */ + double get_fixed_delta_time() const; + + /** + * \brief Set the fixed_delta_time in seconds. + * + * \param ms fixed_delta_time in seconds. + * + * The fixed_delta_time value is used to determine how many times per second the fixed_update and process_input functions are called. + */ + void set_fixed_delta_time(int seconds); + private: friend class LoopManager; @@ -81,17 +110,6 @@ private: * necessary. */ void enforce_frame_rate(); - - /** - * \brief Get the fixed delta time for consistent updates. - * - * Fixed delta time is used for operations that require uniform time steps, such as physics - * calculations. - * - * \return Fixed delta time in seconds. - */ - double get_fixed_delta_time() const; - /** * \brief Get the accumulated lag in the game loop. * @@ -123,7 +141,7 @@ private: int target_fps = 50; //! Actual frames per second int actual_fps = 0; - //! time scale for speeding up or slowing down the game (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up) + //! Time scale for speeding up or slowing down the game (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up) double time_scale = 1; //! Maximum delta time in seconds to avoid large jumps std::chrono::duration maximum_delta_time{0.25}; diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp index f73605e..55ccbb3 100644 --- a/src/test/LoopManagerTest.cpp +++ b/src/test/LoopManagerTest.cpp @@ -45,6 +45,28 @@ TEST_F(LoopManagerTest, FixedUpdate) { // Test finished } +TEST_F(LoopManagerTest, ScaledFixedUpdate) { + // Arrange + test_loop.loop_timer.set_target_fps(60); + + // Set expectations for the mock calls + EXPECT_CALL(test_loop, render).Times(::testing::Exactly(60)); + EXPECT_CALL(test_loop, update).Times(::testing::Exactly(60)); + EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(50)); + + // Start the loop in a separate thread + std::thread loop_thread([&]() { test_loop.start(); }); + + // Let the loop run for exactly 1 second + std::this_thread::sleep_for(std::chrono::seconds(1)); + + // Stop the game loop + test_loop.game_running = false; + // Wait for the loop thread to finish + loop_thread.join(); + + // Test finished +} TEST_F(LoopManagerTest, ShutDown) { // Arrange test_loop.loop_timer.set_target_fps(60); -- cgit v1.2.3 From 2c698f3b6d61fe7a494c9e88620e427b5059cee2 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sun, 8 Dec 2024 20:05:51 +0100 Subject: make format --- src/crepe/api/LoopManager.cpp | 6 +++--- src/crepe/manager/LoopTimerManager.cpp | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index d17aee1..922e66b 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -27,10 +27,10 @@ LoopManager::LoopManager() { [this](const ShutDownEvent & event) { return this->on_shutdown(event); }); } -void LoopManager::process_input() { - this->get_system().update(); +void LoopManager::process_input() { + this->get_system().update(); this->event_manager.dispatch_events(); - } +} void LoopManager::start() { this->setup(); diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 5d4545f..9d9897d 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -82,10 +82,9 @@ void LoopTimerManager::enforce_frame_rate() { double LoopTimerManager::get_lag() const { return (this->elapsed_time - this->elapsed_fixed_time).count(); } -double LoopTimerManager::get_scaled_fixed_delta_time() const{ +double LoopTimerManager::get_scaled_fixed_delta_time() const { return this->fixed_delta_time.count() * this->time_scale; } void LoopTimerManager::set_fixed_delta_time(int seconds) { - this->fixed_delta_time = std::chrono::duration(seconds); + this->fixed_delta_time = std::chrono::duration(seconds); } - -- cgit v1.2.3 From 77be74d880675c548417c7ff5af17e1785c62e05 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 9 Dec 2024 09:41:33 +0100 Subject: changed some function names --- src/crepe/api/LoopManager.cpp | 7 ++++--- src/crepe/api/LoopManager.h | 9 +-------- src/crepe/manager/LoopTimerManager.cpp | 10 +++++----- src/crepe/manager/LoopTimerManager.h | 2 +- src/test/LoopManagerTest.cpp | 8 ++++---- 5 files changed, 15 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 922e66b..f588d7f 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -48,13 +48,13 @@ void LoopManager::loop() { while (game_running) { this->loop_timer.update(); - while (this->loop_timer.get_lag() >= this->loop_timer.get_scaled_fixed_delta_time()) { + while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_loop_interval()) { this->process_input(); this->fixed_update(); this->loop_timer.advance_fixed_update(); } - this->update(); + this->frame_update(); this->render(); this->loop_timer.enforce_frame_rate(); } @@ -72,9 +72,10 @@ void LoopManager::render() { this->get_system().update(); this->get_system().update(); } + bool LoopManager::on_shutdown(const ShutDownEvent & e) { this->game_running = false; return false; } -void LoopManager::update() {} +void LoopManager::frame_update() {} diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 6b2e857..f94cea1 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -63,14 +63,7 @@ private: * * Updates the game state based on the elapsed time since the last frame. */ - virtual void update(); - - /** - * \brief Late update which is called after update(). - * - * This function can be used for final adjustments before rendering. - */ - void late_update(); + virtual void frame_update(); /** * \brief Fixed update executed at a fixed rate. diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 9d9897d..9c77785 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -48,10 +48,6 @@ void LoopTimerManager::advance_fixed_update() { this->elapsed_fixed_time += this->fixed_delta_time; } -double LoopTimerManager::get_fixed_delta_time() const { - return this->fixed_delta_time.count(); -} - void LoopTimerManager::set_target_fps(int fps) { this->target_fps = fps; // target time per frame in seconds @@ -82,9 +78,13 @@ void LoopTimerManager::enforce_frame_rate() { double LoopTimerManager::get_lag() const { return (this->elapsed_time - this->elapsed_fixed_time).count(); } -double LoopTimerManager::get_scaled_fixed_delta_time() const { +double LoopTimerManager::get_fixed_delta_time() const { return this->fixed_delta_time.count() * this->time_scale; } void LoopTimerManager::set_fixed_delta_time(int seconds) { this->fixed_delta_time = std::chrono::duration(seconds); } + +double LoopTimerManager::get_fixed_loop_interval() const { + return this->fixed_delta_time.count(); +} diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index 8fb4461..84178eb 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -73,7 +73,7 @@ public: * * \return The fixed delta time, scaled by time scale, in seconds. */ - double get_scaled_fixed_delta_time() const; + double get_fixed_loop_interval() const; /** * \brief Get the fixed delta time in seconds without scaling by the time scale. diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp index 55ccbb3..c44ebda 100644 --- a/src/test/LoopManagerTest.cpp +++ b/src/test/LoopManagerTest.cpp @@ -15,7 +15,7 @@ protected: class TestGameLoop : public crepe::LoopManager { public: MOCK_METHOD(void, fixed_update, (), (override)); - MOCK_METHOD(void, update, (), (override)); + MOCK_METHOD(void, frame_update, (), (override)); MOCK_METHOD(void, render, (), (override)); }; @@ -29,7 +29,7 @@ TEST_F(LoopManagerTest, FixedUpdate) { // Set expectations for the mock calls EXPECT_CALL(test_loop, render).Times(::testing::Exactly(60)); - EXPECT_CALL(test_loop, update).Times(::testing::Exactly(60)); + EXPECT_CALL(test_loop, frame_update).Times(::testing::Exactly(60)); EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(50)); // Start the loop in a separate thread @@ -51,7 +51,7 @@ TEST_F(LoopManagerTest, ScaledFixedUpdate) { // Set expectations for the mock calls EXPECT_CALL(test_loop, render).Times(::testing::Exactly(60)); - EXPECT_CALL(test_loop, update).Times(::testing::Exactly(60)); + EXPECT_CALL(test_loop, frame_update).Times(::testing::Exactly(60)); EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(50)); // Start the loop in a separate thread @@ -72,7 +72,7 @@ TEST_F(LoopManagerTest, ShutDown) { test_loop.loop_timer.set_target_fps(60); EXPECT_CALL(test_loop, render).Times(::testing::AtLeast(1)); - EXPECT_CALL(test_loop, update).Times(::testing::AtLeast(1)); + EXPECT_CALL(test_loop, frame_update).Times(::testing::AtLeast(1)); EXPECT_CALL(test_loop, fixed_update).Times(::testing::AtLeast(1)); // Start the loop in a separate thread std::thread loop_thread([&]() { test_loop.start(); }); -- cgit v1.2.3 From 9a8bc1fb1f7da2164cef6fa8cc1f4b48bd9c1769 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 9 Dec 2024 17:11:02 +0100 Subject: merge master --- src/crepe/api/LoopManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 4f08bab..464dab9 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -64,7 +64,7 @@ void LoopManager::setup() { this->game_running = true; this->loop_timer.start(); this->scene_manager.load_next_scene(); - timer.start(); + this->loop_timer.start(); } void LoopManager::render() { -- cgit v1.2.3 From e7d90aa34ebb5898785d8432e01f25c5b03d021b Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 9 Dec 2024 17:13:37 +0100 Subject: removed white line and standard target fps = 60 --- src/crepe/api/LoopManager.cpp | 1 - src/crepe/manager/LoopTimerManager.h | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 464dab9..de249ec 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -1,5 +1,4 @@ #include "../facade/SDLContext.h" - #include "../manager/EventManager.h" #include "../system/AnimatorSystem.h" #include "../system/CollisionSystem.h" diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index 84178eb..8fc69ed 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -138,7 +138,7 @@ private: private: //! Target frames per second - int target_fps = 50; + int target_fps = 60; //! Actual frames per second int actual_fps = 0; //! Time scale for speeding up or slowing down the game (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up) -- cgit v1.2.3 From 61e13d06b1062cb0c41db3ca59bb09c0aff1dd95 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 9 Dec 2024 17:17:54 +0100 Subject: fixed a small mistake in sceneManager and added the load_next_scene to the loop --- src/crepe/api/LoopManager.cpp | 4 +++- src/crepe/manager/SceneManager.cpp | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index de249ec..15fa52d 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -78,4 +78,6 @@ bool LoopManager::on_shutdown(const ShutDownEvent & e) { return false; } -void LoopManager::frame_update() {} +void LoopManager::frame_update() { + this->scene_manager.load_next_scene(); +} diff --git a/src/crepe/manager/SceneManager.cpp b/src/crepe/manager/SceneManager.cpp index 50a9fbb..d4ca90b 100644 --- a/src/crepe/manager/SceneManager.cpp +++ b/src/crepe/manager/SceneManager.cpp @@ -32,4 +32,7 @@ void SceneManager::load_next_scene() { // Load the new scene scene->load_scene(); + + //clear the next scene + next_scene.clear(); } -- cgit v1.2.3 From fa2d728d847e5aa29859c8cf72d8ee8df363373e Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 9 Dec 2024 17:25:50 +0100 Subject: added doxygens --- src/crepe/manager/EventManager.h | 3 +++ src/crepe/manager/LoopTimerManager.cpp | 2 +- src/crepe/manager/LoopTimerManager.h | 12 ++++++++---- 3 files changed, 12 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/crepe/manager/EventManager.h b/src/crepe/manager/EventManager.h index ba55edf..639e37f 100644 --- a/src/crepe/manager/EventManager.h +++ b/src/crepe/manager/EventManager.h @@ -33,6 +33,9 @@ typedef size_t event_channel_t; class EventManager : public Manager { public: static constexpr const event_channel_t CHANNEL_ALL = -1; + /** + * \param mediator A reference to a Mediator object used for transfering managers. + */ EventManager(Mediator & mediator); /** * \brief Subscribe to a specific event type. diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 9c77785..597b214 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -81,7 +81,7 @@ double LoopTimerManager::get_lag() const { double LoopTimerManager::get_fixed_delta_time() const { return this->fixed_delta_time.count() * this->time_scale; } -void LoopTimerManager::set_fixed_delta_time(int seconds) { +void LoopTimerManager::set_fixed_delta_time(double seconds) { this->fixed_delta_time = std::chrono::duration(seconds); } diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index 8fc69ed..77674b4 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -16,7 +16,10 @@ namespace crepe { */ class LoopTimerManager : public Manager { public: - LoopTimerManager(Mediator & mediator); + /** + * \param mediator A reference to a Mediator object used for transfering managers. + */ + LoopTimerManager(Mediator &mediator); /** * \brief Get the current delta time for the current frame. * @@ -66,7 +69,7 @@ public: void set_time_scale(double time_scale); /** - * \brief Get the scaled fixed delta time om seconds. + * \brief Get the scaled fixed delta time in seconds. * * The fixed delta time is used for operations that require uniform time steps, * such as physics calculations, and is scaled by the current time scale. @@ -88,13 +91,14 @@ public: /** * \brief Set the fixed_delta_time in seconds. * - * \param ms fixed_delta_time in seconds. + * \param seconds fixed_delta_time in seconds. * * The fixed_delta_time value is used to determine how many times per second the fixed_update and process_input functions are called. */ - void set_fixed_delta_time(int seconds); + void set_fixed_delta_time(double seconds); private: + //! Friend relation to use start,enforce_frame_rate,get_lag,update,advance_fixed_update. friend class LoopManager; /** -- cgit v1.2.3 From 2c1f7df48bfd1ea87912826c4faa39a18e936146 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 9 Dec 2024 17:29:09 +0100 Subject: switched doxygen --- src/crepe/manager/LoopTimerManager.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index 77674b4..5a9eda8 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -69,22 +69,22 @@ public: void set_time_scale(double time_scale); /** - * \brief Get the scaled fixed delta time in seconds. + * \brief Get the fixed delta time in seconds without scaling by the time scale. * - * The fixed delta time is used for operations that require uniform time steps, - * such as physics calculations, and is scaled by the current time scale. + * This value is used in the LoopManager to determine how many times + * the fixed_update should be called within a given interval. * - * \return The fixed delta time, scaled by time scale, in seconds. + * \return The unscaled fixed delta time in seconds. */ double get_fixed_loop_interval() const; /** - * \brief Get the fixed delta time in seconds without scaling by the time scale. + * \brief Get the scaled fixed delta time in seconds. * - * This value is used in the LoopManager to determine how many times - * the fixed_update should be called within a given interval. + * The fixed delta time is used for operations that require uniform time steps, + * such as physics calculations, and is scaled by the current time scale. * - * \return The unscaled fixed delta time in seconds. + * \return The fixed delta time, scaled by time scale, in seconds. */ double get_fixed_delta_time() const; -- cgit v1.2.3 From 66f2777086e4d554067da7400f543fe7cc9e4b0b Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 9 Dec 2024 17:30:35 +0100 Subject: removed unused functions and friend relationss --- src/crepe/facade/SDLContext.cpp | 4 ---- src/crepe/facade/SDLContext.h | 18 ------------------ src/crepe/manager/LoopTimerManager.cpp | 1 - 3 files changed, 23 deletions(-) (limited to 'src') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 6becf60..abdfed0 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -340,8 +340,6 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { return ret_cam; } -uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } - std::unique_ptr> SDLContext::texture_from_path(const std::string & path) { @@ -372,8 +370,6 @@ ivec2 SDLContext::get_size(const Texture & ctx) { return size; } -void SDLContext::delay(int ms) const { SDL_Delay(ms); } - std::vector SDLContext::get_events() { std::vector event_list; SDL_Event event; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index e232511..31646a6 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -139,24 +139,6 @@ private: */ MouseButton sdl_to_mousebutton(Uint8 sdl_button); -private: - //! Will only use delay - friend class LoopTimer; - /** - * \brief Gets the current SDL ticks since the program started. - * \return Current ticks in milliseconds as a constant uint64_t. - */ - uint64_t get_ticks() const; - /** - * \brief Pauses the execution for a specified duration. - * - * This function uses SDL's delay function to halt the program execution for a given number - * of milliseconds, allowing for frame rate control or other timing-related functionality. - * - * \param ms Duration of the delay in milliseconds. - */ - void delay(int ms) const; - private: /** * \brief Constructs an SDLContext instance. diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 597b214..37b9c44 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -1,7 +1,6 @@ #include #include -#include "../facade/SDLContext.h" #include "../util/Log.h" #include "LoopTimerManager.h" -- cgit v1.2.3 From 7893342e1ba5fd01be7448244db967fda3c5cfe9 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 9 Dec 2024 17:35:20 +0100 Subject: checked for 0 division --- src/crepe/manager/LoopTimerManager.cpp | 7 +++++++ src/test/CMakeLists.txt | 1 - 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 37b9c44..70a57d2 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -31,6 +31,9 @@ void LoopTimerManager::update() { if (this->delta_time > this->maximum_delta_time) { this->delta_time = this->maximum_delta_time; } + if(this->delta_time.count() <= 0){ + this->delta_time = std::chrono::duration(0.0); + } this->actual_fps = 1.0 / this->delta_time.count(); this->elapsed_time += this->delta_time; @@ -49,6 +52,10 @@ void LoopTimerManager::advance_fixed_update() { void LoopTimerManager::set_target_fps(int fps) { this->target_fps = fps; + //check if fps is lower or equals 0 + if(fps <= 0){ + return; + } // target time per frame in seconds this->frame_target_time = std::chrono::duration(1.0) / this->target_fps; } diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 1c963d8..a6d3387 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -15,7 +15,6 @@ target_sources(test_main PUBLIC Vector2Test.cpp LoopManagerTest.cpp LoopTimerTest.cpp - InputTest.cpp ScriptEventTest.cpp ScriptSceneTest.cpp -- cgit v1.2.3 From 3ace1a9a7af4141715a8454d974ecd65208f405a Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 9 Dec 2024 17:36:46 +0100 Subject: removed auto --- src/crepe/manager/LoopTimerManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 70a57d2..e496a44 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -23,7 +23,7 @@ void LoopTimerManager::start() { } void LoopTimerManager::update() { - auto current_frame_time = std::chrono::steady_clock::now(); + std::chrono::steady_clock::time_point current_frame_time = std::chrono::steady_clock::now(); // Convert to duration in seconds for delta time this->delta_time = std::chrono::duration_cast>( current_frame_time - last_frame_time); -- cgit v1.2.3 From 8a3dd5ac472a0c749338ac78dd5198d3819b629f Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 10 Dec 2024 09:56:22 +0100 Subject: fixed 0 division and duplicate start --- src/crepe/api/LoopManager.cpp | 1 - src/crepe/manager/LoopTimerManager.cpp | 7 ++++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 15fa52d..9bb2183 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -63,7 +63,6 @@ void LoopManager::setup() { this->game_running = true; this->loop_timer.start(); this->scene_manager.load_next_scene(); - this->loop_timer.start(); } void LoopManager::render() { diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index e496a44..1d864ad 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -31,10 +31,11 @@ void LoopTimerManager::update() { if (this->delta_time > this->maximum_delta_time) { this->delta_time = this->maximum_delta_time; } - if(this->delta_time.count() <= 0){ - this->delta_time = std::chrono::duration(0.0); + if (this->delta_time.count() > 0.0) { + this->actual_fps = 1.0 / this->delta_time.count(); + } else { + this->actual_fps = INFINITY; } - this->actual_fps = 1.0 / this->delta_time.count(); this->elapsed_time += this->delta_time; this->last_frame_time = current_frame_time; -- cgit v1.2.3 From aae935bfa77f5bb9c4d2e245ac4fac3c7f4beeb7 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 10 Dec 2024 09:57:12 +0100 Subject: changed infinity to 0 --- src/crepe/manager/LoopTimerManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 1d864ad..366d48a 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -34,7 +34,7 @@ void LoopTimerManager::update() { if (this->delta_time.count() > 0.0) { this->actual_fps = 1.0 / this->delta_time.count(); } else { - this->actual_fps = INFINITY; + this->actual_fps = 0; } this->elapsed_time += this->delta_time; -- cgit v1.2.3 From fb3edf33aedc1b0c37c3f74a5eaac05bf48f491a Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 10 Dec 2024 10:19:49 +0100 Subject: clarified doxygen and changed function name --- src/crepe/api/LoopManager.cpp | 2 +- src/crepe/manager/LoopTimerManager.cpp | 2 +- src/crepe/manager/LoopTimerManager.h | 28 +++++++++++++++++----------- 3 files changed, 19 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 9bb2183..492b982 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -47,7 +47,7 @@ void LoopManager::loop() { while (game_running) { this->loop_timer.update(); - while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_loop_interval()) { + while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_delta_time()) { this->process_input(); this->fixed_update(); this->loop_timer.advance_fixed_update(); diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 366d48a..d2e40fe 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -92,6 +92,6 @@ void LoopTimerManager::set_fixed_delta_time(double seconds) { this->fixed_delta_time = std::chrono::duration(seconds); } -double LoopTimerManager::get_fixed_loop_interval() const { +double LoopTimerManager::get_fixed_delta_time() const { return this->fixed_delta_time.count(); } diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index 5a9eda8..46a0dcb 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -22,7 +22,10 @@ public: LoopTimerManager(Mediator &mediator); /** * \brief Get the current delta time for the current frame. - * + * + * This value represents the estimated frame duration of the current frame. + * This value can be used in the frame_update to convert pixel based values to time based values. + * * \return Delta time in seconds since the last frame. */ double get_delta_time() const; @@ -73,19 +76,10 @@ public: * * This value is used in the LoopManager to determine how many times * the fixed_update should be called within a given interval. + * This value is also the timing value which is used in the fixed_loop to convert pixels to time. * * \return The unscaled fixed delta time in seconds. */ - double get_fixed_loop_interval() const; - - /** - * \brief Get the scaled fixed delta time in seconds. - * - * The fixed delta time is used for operations that require uniform time steps, - * such as physics calculations, and is scaled by the current time scale. - * - * \return The fixed delta time, scaled by time scale, in seconds. - */ double get_fixed_delta_time() const; /** @@ -94,9 +88,21 @@ public: * \param seconds fixed_delta_time in seconds. * * The fixed_delta_time value is used to determine how many times per second the fixed_update and process_input functions are called. + * This value is also the timing value which is used in the fixed_loop to convert pixels to time. */ void set_fixed_delta_time(double seconds); + /** + * \brief Retrieves the scaled fixed delta time in seconds. + * + * The scaled fixed delta time is the timing value used within the `fixed_update` function. + * It is adjusted by the time_scale to account for any changes in the simulation's + * speed. + * + * \return The fixed delta time, scaled by the current time scale, in seconds. + */ + double get_scaled_fixed_delta_time() const; + private: //! Friend relation to use start,enforce_frame_rate,get_lag,update,advance_fixed_update. friend class LoopManager; -- cgit v1.2.3 From 3cf434961be522dc94e91e52361af15e370406c0 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 10 Dec 2024 13:47:44 +0100 Subject: feedback changes --- src/crepe/api/LoopManager.cpp | 2 +- src/crepe/manager/LoopTimerManager.cpp | 14 ++++++++------ src/crepe/manager/LoopTimerManager.h | 9 +++++---- 3 files changed, 14 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 492b982..6ebf280 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -50,7 +50,7 @@ void LoopManager::loop() { while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_delta_time()) { this->process_input(); this->fixed_update(); - this->loop_timer.advance_fixed_update(); + this->loop_timer.advance_fixed_elapsed_time(); } this->frame_update(); diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index d2e40fe..057a18e 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -47,7 +47,7 @@ double LoopTimerManager::get_delta_time() const { double LoopTimerManager::get_current_time() const { return this->elapsed_time.count(); } -void LoopTimerManager::advance_fixed_update() { +void LoopTimerManager::advance_fixed_elapsed_time() { this->elapsed_fixed_time += this->fixed_delta_time; } @@ -68,13 +68,15 @@ void LoopTimerManager::set_time_scale(double value) { this->time_scale = value; double LoopTimerManager::get_time_scale() const { return this->time_scale; } void LoopTimerManager::enforce_frame_rate() { - auto current_frame_time = std::chrono::steady_clock::now(); - auto frame_duration = current_frame_time - this->last_frame_time; + std::chrono::steady_clock::time_point current_frame_time = std::chrono::steady_clock::now(); + std::chrono::duration 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) { - auto delay_time = std::chrono::duration_cast( - this->frame_target_time - frame_duration); + std::chrono::microseconds delay_time = std::chrono::duration_cast( + this->frame_target_time - frame_duration); + if (delay_time.count() > 0) { std::this_thread::sleep_for(delay_time); @@ -85,7 +87,7 @@ void LoopTimerManager::enforce_frame_rate() { double LoopTimerManager::get_lag() const { return (this->elapsed_time - this->elapsed_fixed_time).count(); } -double LoopTimerManager::get_fixed_delta_time() const { +double LoopTimerManager::get_scaled_fixed_delta_time() const { return this->fixed_delta_time.count() * this->time_scale; } void LoopTimerManager::set_fixed_delta_time(double seconds) { diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index 46a0dcb..9336520 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -139,12 +139,13 @@ private: void update(); /** - * \brief Advance the game loop by a fixed update interval. + * \brief Progress the elapsed fixed time by the fixed delta time interval. * - * This method progresses the game state by a consistent, fixed time step, allowing for - * stable updates independent of frame rate fluctuations. + * This method advances the game's fixed update loop by adding the fixed_delta_time + * to elapsed_fixed_time, ensuring the fixed update catches up with the elapsed time. */ - void advance_fixed_update(); + void advance_fixed_elapsed_time(); + private: //! Target frames per second -- cgit v1.2.3 From 4ea6ea704ba59f0d56718e1ed903f49ca250ab5d Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 10 Dec 2024 13:48:06 +0100 Subject: make format --- src/crepe/api/LoopManager.cpp | 4 +--- src/crepe/manager/LoopTimerManager.cpp | 15 ++++++++------- src/crepe/manager/LoopTimerManager.h | 3 +-- 3 files changed, 10 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 6ebf280..46a7635 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -77,6 +77,4 @@ bool LoopManager::on_shutdown(const ShutDownEvent & e) { return false; } -void LoopManager::frame_update() { - this->scene_manager.load_next_scene(); -} +void LoopManager::frame_update() { this->scene_manager.load_next_scene(); } diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 057a18e..e57ec6f 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -23,7 +23,8 @@ void LoopTimerManager::start() { } void LoopTimerManager::update() { - std::chrono::steady_clock::time_point current_frame_time = std::chrono::steady_clock::now(); + std::chrono::steady_clock::time_point current_frame_time + = std::chrono::steady_clock::now(); // Convert to duration in seconds for delta time this->delta_time = std::chrono::duration_cast>( current_frame_time - last_frame_time); @@ -54,7 +55,7 @@ void LoopTimerManager::advance_fixed_elapsed_time() { void LoopTimerManager::set_target_fps(int fps) { this->target_fps = fps; //check if fps is lower or equals 0 - if(fps <= 0){ + if (fps <= 0) { return; } // target time per frame in seconds @@ -68,15 +69,15 @@ void LoopTimerManager::set_time_scale(double value) { this->time_scale = value; double LoopTimerManager::get_time_scale() const { return this->time_scale; } void LoopTimerManager::enforce_frame_rate() { - std::chrono::steady_clock::time_point current_frame_time = std::chrono::steady_clock::now(); + std::chrono::steady_clock::time_point current_frame_time + = std::chrono::steady_clock::now(); std::chrono::duration 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); - + std::chrono::microseconds delay_time + = std::chrono::duration_cast(this->frame_target_time + - frame_duration); if (delay_time.count() > 0) { std::this_thread::sleep_for(delay_time); diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index 9336520..ec44d52 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -19,7 +19,7 @@ public: /** * \param mediator A reference to a Mediator object used for transfering managers. */ - LoopTimerManager(Mediator &mediator); + LoopTimerManager(Mediator & mediator); /** * \brief Get the current delta time for the current frame. * @@ -146,7 +146,6 @@ private: */ void advance_fixed_elapsed_time(); - private: //! Target frames per second int target_fps = 60; -- cgit v1.2.3 From 6cd6ed3eb2a14e703e97bf95fe1b73fb8d3d91f4 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 10 Dec 2024 14:08:42 +0100 Subject: changed function name and removed auto --- src/crepe/manager/LoopTimerManager.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src') diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index e57ec6f..8725c33 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -55,9 +55,7 @@ void LoopTimerManager::advance_fixed_elapsed_time() { void LoopTimerManager::set_target_fps(int fps) { this->target_fps = fps; //check if fps is lower or equals 0 - if (fps <= 0) { - return; - } + if (fps <= 0) return; // target time per frame in seconds this->frame_target_time = std::chrono::duration(1.0) / this->target_fps; } -- cgit v1.2.3 From f64b793ad8e796458c8e175f298e8d13eb3b3459 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 11 Dec 2024 14:16:40 +0100 Subject: feedback --- src/crepe/api/LoopManager.cpp | 45 ++++++++++++------------ src/crepe/api/LoopManager.h | 18 ++-------- src/crepe/manager/LoopTimerManager.cpp | 56 ++++++++++++++--------------- src/crepe/manager/LoopTimerManager.h | 64 +++++++++++++++++----------------- src/crepe/manager/Mediator.h | 1 + src/crepe/system/AnimatorSystem.cpp | 2 +- src/test/LoopManagerTest.cpp | 12 ++----- src/test/LoopTimerTest.cpp | 20 +++++------ 8 files changed, 99 insertions(+), 119 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 46a7635..a1da8be 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -7,7 +7,8 @@ #include "../system/PhysicsSystem.h" #include "../system/RenderSystem.h" #include "../system/ScriptSystem.h" -#include "manager/EventManager.h" +#include "../manager/EventManager.h" +#include "../util/Log.h" #include "LoopManager.h" @@ -25,56 +26,56 @@ LoopManager::LoopManager() { this->event_manager.subscribe( [this](const ShutDownEvent & event) { return this->on_shutdown(event); }); } - -void LoopManager::process_input() { - this->get_system().update(); - this->event_manager.dispatch_events(); -} - void LoopManager::start() { this->setup(); this->loop(); } -void LoopManager::fixed_update() { - this->get_system().update(); - this->get_system().update(); - this->get_system().update(); +void LoopManager::setup() { + this->game_running = true; + this->loop_timer.start(); + this->scene_manager.load_next_scene(); } void LoopManager::loop() { + try { while (game_running) { this->loop_timer.update(); while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_delta_time()) { - this->process_input(); this->fixed_update(); this->loop_timer.advance_fixed_elapsed_time(); } this->frame_update(); - this->render(); this->loop_timer.enforce_frame_rate(); } + }catch(const exception & e){ + Log::logf(Log::Level::ERROR, "Exception caught in main loop: %s", e.what()); + this->event_manager.trigger_event(ShutDownEvent{}); + } } -void LoopManager::setup() { - this->game_running = true; - this->loop_timer.start(); - this->scene_manager.load_next_scene(); +// will be called at a fixed interval +void LoopManager::fixed_update() { + this->get_system().update(); + this->event_manager.dispatch_events(); + this->get_system().update(); + this->get_system().update(); + this->get_system().update(); } -void LoopManager::render() { - if (!this->game_running) return; - +// will be called every frame +void LoopManager::frame_update() { + this->scene_manager.load_next_scene(); this->get_system().update(); + //render this->get_system().update(); } bool LoopManager::on_shutdown(const ShutDownEvent & e) { this->game_running = false; + // propagate to possible user ShutDownEvent listeners return false; } - -void LoopManager::frame_update() { this->scene_manager.load_next_scene(); } diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index f94cea1..2319d65 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -25,7 +25,7 @@ public: * \brief Start the gameloop * * This is the start of the engine where the setup is called and then the loop keeps running until the game stops running. - * Developers need to call this function to run the game. + * The Game programmer needs to call this function to run the game. This should be done after creating and adding all scenes. */ void start(); @@ -51,13 +51,6 @@ private: */ void loop(); - /** - * \brief Function for handling input-related system calls. - * - * Processes user inputs from keyboard and mouse. - */ - void process_input(); - /** * \brief Per-frame update. * @@ -71,15 +64,9 @@ private: * This function updates physics and game logic based on LoopTimer's fixed_delta_time. */ virtual void fixed_update(); - /** - * \brief Function for executing render-related systems. - * - * Renders the current state of the game to the screen. - */ - virtual void render(); + //! Indicates whether the game is running. bool game_running = false; - private: //! Global context Mediator mediator; @@ -97,6 +84,7 @@ 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 8725c33..a306eb7 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -6,7 +6,7 @@ #include "LoopTimerManager.h" using namespace crepe; - +using namespace std::chrono_literals; LoopTimerManager::LoopTimerManager(Mediator & mediator) : Manager(mediator) { this->mediator.loop_timer = *this; dbg_trace(); @@ -15,62 +15,57 @@ LoopTimerManager::LoopTimerManager(Mediator & mediator) : Manager(mediator) { void LoopTimerManager::start() { this->last_frame_time = std::chrono::steady_clock::now(); - this->elapsed_time = std::chrono::milliseconds(0); + this->elapsed_time = 0s; // 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(fixed_delta_time); - this->delta_time = std::chrono::milliseconds(0); + = -std::chrono::duration_cast(this->fixed_delta_time); + this->delta_time = 0s; } void LoopTimerManager::update() { - std::chrono::steady_clock::time_point current_frame_time + TimePoint_t current_frame_time = std::chrono::steady_clock::now(); // Convert to duration in seconds for delta time - this->delta_time = std::chrono::duration_cast>( - current_frame_time - last_frame_time); + this->delta_time = current_frame_time - last_frame_time; if (this->delta_time > this->maximum_delta_time) { this->delta_time = this->maximum_delta_time; } - if (this->delta_time.count() > 0.0) { + if (this->delta_time > 0s) { this->actual_fps = 1.0 / this->delta_time.count(); } else { this->actual_fps = 0; } - - this->elapsed_time += this->delta_time; + this->elapsed_time += std::chrono::duration_cast(this->delta_time); this->last_frame_time = current_frame_time; } -double LoopTimerManager::get_delta_time() const { - return this->delta_time.count() * this->time_scale; -} +Duration_t LoopTimerManager::get_delta_time() const {return this->delta_time * this->time_scale;} -double LoopTimerManager::get_current_time() const { return this->elapsed_time.count(); } +ElapsedTime_t LoopTimerManager::get_elapsed_time() const { return this->elapsed_time; } void LoopTimerManager::advance_fixed_elapsed_time() { - this->elapsed_fixed_time += this->fixed_delta_time; + this->elapsed_fixed_time += std::chrono::duration_cast(this->fixed_delta_time); } -void LoopTimerManager::set_target_fps(int fps) { +void LoopTimerManager::set_target_framerate(unsigned fps) { this->target_fps = fps; //check if fps is lower or equals 0 if (fps <= 0) return; // target time per frame in seconds - this->frame_target_time = std::chrono::duration(1.0) / this->target_fps; + this->frame_target_time = Duration_t(1s) / this->target_fps; } -int LoopTimerManager::get_fps() const { return this->actual_fps; } +unsigned LoopTimerManager::get_fps() const { return this->actual_fps; } void LoopTimerManager::set_time_scale(double value) { this->time_scale = value; } -double LoopTimerManager::get_time_scale() const { return this->time_scale; } +float LoopTimerManager::get_time_scale() const { return this->time_scale; } void LoopTimerManager::enforce_frame_rate() { - std::chrono::steady_clock::time_point current_frame_time + TimePoint_t current_frame_time = std::chrono::steady_clock::now(); - std::chrono::duration frame_duration = current_frame_time - this->last_frame_time; - + 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 @@ -83,16 +78,17 @@ void LoopTimerManager::enforce_frame_rate() { } } -double LoopTimerManager::get_lag() const { - return (this->elapsed_time - this->elapsed_fixed_time).count(); +Duration_t LoopTimerManager::get_lag() const { + return (this->elapsed_time - this->elapsed_fixed_time); } -double LoopTimerManager::get_scaled_fixed_delta_time() const { - return this->fixed_delta_time.count() * this->time_scale; + +Duration_t LoopTimerManager::get_scaled_fixed_delta_time() const { + return this->fixed_delta_time * this->time_scale; } -void LoopTimerManager::set_fixed_delta_time(double seconds) { - this->fixed_delta_time = std::chrono::duration(seconds); +void LoopTimerManager::set_fixed_delta_time(float seconds) { + this->fixed_delta_time = Duration_t(seconds); } -double LoopTimerManager::get_fixed_delta_time() const { - return this->fixed_delta_time.count(); +Duration_t LoopTimerManager::get_fixed_delta_time() const { + return this->fixed_delta_time; } diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index ec44d52..c5f3cb0 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -5,7 +5,9 @@ #include "Manager.h" namespace crepe { - +typedef std::chrono::duration Duration_t; +typedef std::chrono::duration ElapsedTime_t; +typedef std::chrono::steady_clock::time_point TimePoint_t; /** * \brief Manages timing and frame rate for the game loop. * @@ -28,31 +30,31 @@ public: * * \return Delta time in seconds since the last frame. */ - double get_delta_time() const; + Duration_t get_delta_time() const; /** - * \brief Get the current game time. + * \brief Get the current elapsed time (total time passed ) * * \note The current game time may vary from real-world elapsed time. It is the cumulative * sum of each frame's delta time. * * \return Elapsed game time in seconds. */ - double get_current_time() const; + ElapsedTime_t get_elapsed_time() const; /** * \brief Set the target frames per second (FPS). * * \param fps The desired frames rendered per second. */ - void set_target_fps(int fps); + void set_target_framerate(unsigned fps); /** * \brief Get the current frames per second (FPS). * * \return Current FPS. */ - int get_fps() const; + unsigned get_fps() const; /** * \brief Get the current time scale. @@ -60,7 +62,7 @@ public: * \return The current time scale, where (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up). * up the game. */ - double get_time_scale() const; + float get_time_scale() const; /** * \brief Set the time scale. @@ -76,11 +78,10 @@ public: * * This value is used in the LoopManager to determine how many times * the fixed_update should be called within a given interval. - * This value is also the timing value which is used in the fixed_loop to convert pixels to time. * * \return The unscaled fixed delta time in seconds. */ - double get_fixed_delta_time() const; + Duration_t get_fixed_delta_time() const; /** * \brief Set the fixed_delta_time in seconds. @@ -88,9 +89,9 @@ public: * \param seconds fixed_delta_time in seconds. * * The fixed_delta_time value is used to determine how many times per second the fixed_update and process_input functions are called. - * This value is also the timing value which is used in the fixed_loop to convert pixels to time. + * */ - void set_fixed_delta_time(double seconds); + void set_fixed_delta_time(float seconds); /** * \brief Retrieves the scaled fixed delta time in seconds. @@ -101,12 +102,11 @@ public: * * \return The fixed delta time, scaled by the current time scale, in seconds. */ - double get_scaled_fixed_delta_time() const; + Duration_t get_scaled_fixed_delta_time() const; private: //! Friend relation to use start,enforce_frame_rate,get_lag,update,advance_fixed_update. friend class LoopManager; - /** * \brief Start the loop timer. * @@ -128,7 +128,7 @@ private: * * \return Accumulated lag in seconds. */ - double get_lag() const; + Duration_t get_lag() const; /** * \brief Update the timer to the current frame. @@ -147,27 +147,27 @@ private: void advance_fixed_elapsed_time(); private: - //! Target frames per second + //! Target frames per second. int target_fps = 60; - //! Actual frames per second + //! Actual frames per second. int actual_fps = 0; - //! Time scale for speeding up or slowing down the game (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up) - double time_scale = 1; - //! Maximum delta time in seconds to avoid large jumps - std::chrono::duration maximum_delta_time{0.25}; - //! Delta time for the current frame in seconds - std::chrono::duration delta_time{0.0}; + //! Time scale for speeding up or slowing down the game (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up). + float time_scale = 1; + //! Maximum delta time in seconds to avoid large jumps. + Duration_t maximum_delta_time{0.25}; + //! Delta time for the current frame in seconds. + Duration_t delta_time{0.0}; //! Target time per frame in seconds - std::chrono::duration frame_target_time - = std::chrono::duration(1.0) / target_fps; - //! Fixed delta time for fixed updates in seconds - std::chrono::duration fixed_delta_time = std::chrono::duration(1.0) / 50.0; - //! Total elapsed game time in seconds - std::chrono::duration elapsed_time{0.0}; - //! Total elapsed time for fixed updates in seconds - std::chrono::duration elapsed_fixed_time{0.0}; - //! Time of the last frame - std::chrono::steady_clock::time_point last_frame_time; + Duration_t frame_target_time + = Duration_t(1.0) / target_fps; + //! Fixed delta time for fixed updates in seconds. + Duration_t fixed_delta_time = Duration_t(1.0) / 50.0; + //! Total elapsed game time in microseconds. + ElapsedTime_t elapsed_time{0}; + //! Total elapsed time for fixed updates in microseconds. + ElapsedTime_t elapsed_fixed_time{0}; + //! Time of the last frame. + TimePoint_t last_frame_time; }; } // namespace crepe diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index eb8a7a5..ad51cc6 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -11,6 +11,7 @@ namespace crepe { class ComponentManager; class SceneManager; + class LoopTimerManager; class EventManager; /** diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 499f618..690b45b 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -13,7 +13,7 @@ void AnimatorSystem::update() { LoopTimerManager & timer = this->mediator.loop_timer; RefVector animations = mgr.get_components_by_type(); - double elapsed_time = timer.get_current_time(); + unsigned long long elapsed_time = timer.get_elapsed_time().count(); for (Animator & a : animations) { if (!a.active) continue; diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp index c44ebda..4e0ecdc 100644 --- a/src/test/LoopManagerTest.cpp +++ b/src/test/LoopManagerTest.cpp @@ -16,7 +16,6 @@ protected: public: MOCK_METHOD(void, fixed_update, (), (override)); MOCK_METHOD(void, frame_update, (), (override)); - MOCK_METHOD(void, render, (), (override)); }; TestGameLoop test_loop; @@ -25,10 +24,9 @@ protected: TEST_F(LoopManagerTest, FixedUpdate) { // Arrange - test_loop.loop_timer.set_target_fps(60); + test_loop.loop_timer.set_target_framerate(60); // Set expectations for the mock calls - EXPECT_CALL(test_loop, render).Times(::testing::Exactly(60)); EXPECT_CALL(test_loop, frame_update).Times(::testing::Exactly(60)); EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(50)); @@ -47,10 +45,9 @@ TEST_F(LoopManagerTest, FixedUpdate) { } TEST_F(LoopManagerTest, ScaledFixedUpdate) { // Arrange - test_loop.loop_timer.set_target_fps(60); + test_loop.loop_timer.set_target_framerate(60); // Set expectations for the mock calls - EXPECT_CALL(test_loop, render).Times(::testing::Exactly(60)); EXPECT_CALL(test_loop, frame_update).Times(::testing::Exactly(60)); EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(50)); @@ -69,9 +66,8 @@ TEST_F(LoopManagerTest, ScaledFixedUpdate) { } TEST_F(LoopManagerTest, ShutDown) { // Arrange - test_loop.loop_timer.set_target_fps(60); + test_loop.loop_timer.set_target_framerate(60); - EXPECT_CALL(test_loop, render).Times(::testing::AtLeast(1)); EXPECT_CALL(test_loop, frame_update).Times(::testing::AtLeast(1)); EXPECT_CALL(test_loop, fixed_update).Times(::testing::AtLeast(1)); // Start the loop in a separate thread @@ -80,6 +76,4 @@ TEST_F(LoopManagerTest, ShutDown) { test_loop.event_manager.trigger_event(ShutDownEvent{}); // Wait for the loop thread to finish loop_thread.join(); - - // Test finished } diff --git a/src/test/LoopTimerTest.cpp b/src/test/LoopTimerTest.cpp index c6655d9..1216e5e 100644 --- a/src/test/LoopTimerTest.cpp +++ b/src/test/LoopTimerTest.cpp @@ -17,7 +17,7 @@ protected: }; TEST_F(LoopTimerTest, EnforcesTargetFrameRate) { // Set the target FPS to 60 (which gives a target time per frame of ~16.67 ms) - loop_timer.set_target_fps(60); + loop_timer.set_target_framerate(60); auto start_time = steady_clock::now(); loop_timer.enforce_frame_rate(); @@ -30,33 +30,33 @@ TEST_F(LoopTimerTest, EnforcesTargetFrameRate) { } TEST_F(LoopTimerTest, SetTargetFps) { // Set the target FPS to 120 - loop_timer.set_target_fps(120); + loop_timer.set_target_framerate(120); // Calculate the expected frame time (~8.33ms per frame) - auto expected_frame_time = std::chrono::duration(1.0 / 120.0); + Duration_t expected_frame_time = std::chrono::duration(1.0 / 120.0); ASSERT_NEAR(loop_timer.frame_target_time.count(), expected_frame_time.count(), 0.001); } TEST_F(LoopTimerTest, DeltaTimeCalculation) { // Set the target FPS to 60 (16.67 ms per frame) - loop_timer.set_target_fps(60); + loop_timer.set_target_framerate(60); auto start_time = steady_clock::now(); loop_timer.update(); auto end_time = steady_clock::now(); // Check the delta time - double delta_time = loop_timer.get_delta_time(); + Duration_t delta_time = loop_timer.get_delta_time(); auto elapsed_time = duration_cast(end_time - start_time).count(); // Assert that delta_time is close to the elapsed time - ASSERT_NEAR(delta_time, elapsed_time, 1); + ASSERT_NEAR(delta_time.count(), elapsed_time, 1); } TEST_F(LoopTimerTest, getCurrentTime) { // Set the target FPS to 60 (16.67 ms per frame) - loop_timer.set_target_fps(60); + loop_timer.set_target_framerate(60); auto start_time = steady_clock::now(); @@ -68,8 +68,8 @@ TEST_F(LoopTimerTest, getCurrentTime) { auto end_time = steady_clock::now(); // Get the elapsed time in seconds as a double - auto elapsed_time - = duration_cast>(end_time - start_time).count(); + auto elapsed_time = std::chrono::duration_cast(end_time - start_time).count(); - ASSERT_NEAR(loop_timer.get_current_time(), elapsed_time, 0.001); + + ASSERT_NEAR(loop_timer.get_elapsed_time().count(), elapsed_time, 5); } -- cgit v1.2.3 From dfa8ffbc03c4c1acd74fd14e54c6ee566a3c445c Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 11 Dec 2024 14:33:31 +0100 Subject: make format --- src/crepe/api/LoopManager.cpp | 26 +++++++++++++------------- src/crepe/api/LoopManager.h | 2 +- src/crepe/manager/LoopTimerManager.cpp | 17 ++++++++--------- src/crepe/manager/LoopTimerManager.h | 3 +-- src/test/LoopTimerTest.cpp | 4 ++-- 5 files changed, 25 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 4ca9928..b335cad 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" @@ -44,18 +44,18 @@ void LoopManager::setup() { void LoopManager::loop() { try { - while (game_running) { - this->loop_timer.update(); + while (game_running) { + this->loop_timer.update(); - while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_delta_time()) { - this->fixed_update(); - this->loop_timer.advance_fixed_elapsed_time(); - } + while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_delta_time()) { + this->fixed_update(); + this->loop_timer.advance_fixed_elapsed_time(); + } - this->frame_update(); - this->loop_timer.enforce_frame_rate(); - } - }catch(const exception & e){ + this->frame_update(); + this->loop_timer.enforce_frame_rate(); + } + } catch (const exception & e) { Log::logf(Log::Level::ERROR, "Exception caught in main loop: %s", e.what()); this->event_manager.trigger_event(ShutDownEvent{}); } @@ -73,7 +73,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 a306eb7..71a72e2 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -23,8 +23,7 @@ void LoopTimerManager::start() { } void LoopTimerManager::update() { - TimePoint_t current_frame_time - = std::chrono::steady_clock::now(); + TimePoint_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; @@ -40,12 +39,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; +} ElapsedTime_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) { @@ -63,8 +65,7 @@ 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(); + TimePoint_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) { @@ -89,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/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index c5f3cb0..61ae6ef 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -158,8 +158,7 @@ private: //! Delta time for the current frame in seconds. Duration_t delta_time{0.0}; //! Target time per frame in seconds - Duration_t frame_target_time - = Duration_t(1.0) / target_fps; + Duration_t frame_target_time = Duration_t(1.0) / target_fps; //! Fixed delta time for fixed updates in seconds. Duration_t fixed_delta_time = Duration_t(1.0) / 50.0; //! Total elapsed game time in microseconds. diff --git a/src/test/LoopTimerTest.cpp b/src/test/LoopTimerTest.cpp index 1216e5e..e4f8477 100644 --- a/src/test/LoopTimerTest.cpp +++ b/src/test/LoopTimerTest.cpp @@ -68,8 +68,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 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/api/LoopManager.cpp | 19 +++++++------- src/crepe/manager/LoopTimerManager.cpp | 47 +++++++++++++++++----------------- src/crepe/manager/LoopTimerManager.h | 34 ++++++++++++------------ src/crepe/manager/Mediator.h | 6 ++--- src/crepe/system/AISystem.cpp | 8 +++--- src/test/LoopTimerTest.cpp | 10 +++++--- 6 files changed, 64 insertions(+), 60 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 4ca9928..aa4a21a 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -43,20 +43,19 @@ void LoopManager::setup() { void LoopManager::loop() { try { + while (game_running) { + this->loop_timer.update(); - while (game_running) { - this->loop_timer.update(); + while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_delta_time()) { + this->fixed_update(); + this->loop_timer.advance_fixed_elapsed_time(); + } - while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_delta_time()) { - this->fixed_update(); - this->loop_timer.advance_fixed_elapsed_time(); + this->frame_update(); + this->loop_timer.enforce_frame_rate(); } - - this->frame_update(); - this->loop_timer.enforce_frame_rate(); - } }catch(const exception & e){ - Log::logf(Log::Level::ERROR, "Exception caught in main loop: %s", e.what()); + Log::logf(Log::Level::ERROR, "Exception caught in main loop: {}", e.what()); this->event_manager.trigger_event(ShutDownEvent{}); } } 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; } diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index c5f3cb0..ad522f7 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -5,9 +5,10 @@ #include "Manager.h" namespace crepe { -typedef std::chrono::duration Duration_t; -typedef std::chrono::duration ElapsedTime_t; -typedef std::chrono::steady_clock::time_point TimePoint_t; + +typedef std::chrono::duration duration_t; +typedef std::chrono::duration elapsed_time_t; + /** * \brief Manages timing and frame rate for the game loop. * @@ -30,7 +31,7 @@ public: * * \return Delta time in seconds since the last frame. */ - Duration_t get_delta_time() const; + duration_t get_delta_time() const; /** * \brief Get the current elapsed time (total time passed ) @@ -40,7 +41,7 @@ public: * * \return Elapsed game time in seconds. */ - ElapsedTime_t get_elapsed_time() const; + elapsed_time_t get_elapsed_time() const; /** * \brief Set the target frames per second (FPS). @@ -81,7 +82,7 @@ public: * * \return The unscaled fixed delta time in seconds. */ - Duration_t get_fixed_delta_time() const; + duration_t get_fixed_delta_time() const; /** * \brief Set the fixed_delta_time in seconds. @@ -102,7 +103,7 @@ public: * * \return The fixed delta time, scaled by the current time scale, in seconds. */ - Duration_t get_scaled_fixed_delta_time() const; + duration_t get_scaled_fixed_delta_time() const; private: //! Friend relation to use start,enforce_frame_rate,get_lag,update,advance_fixed_update. @@ -128,7 +129,7 @@ private: * * \return Accumulated lag in seconds. */ - Duration_t get_lag() const; + duration_t get_lag() const; /** * \brief Update the timer to the current frame. @@ -154,20 +155,21 @@ private: //! Time scale for speeding up or slowing down the game (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up). float time_scale = 1; //! Maximum delta time in seconds to avoid large jumps. - Duration_t maximum_delta_time{0.25}; + duration_t maximum_delta_time{0.25}; //! Delta time for the current frame in seconds. - Duration_t delta_time{0.0}; + duration_t delta_time{0.0}; //! Target time per frame in seconds - Duration_t frame_target_time - = Duration_t(1.0) / target_fps; + duration_t frame_target_time{1.0 / target_fps}; //! Fixed delta time for fixed updates in seconds. - Duration_t fixed_delta_time = Duration_t(1.0) / 50.0; + duration_t fixed_delta_time{1.0 / 50.0}; //! Total elapsed game time in microseconds. - ElapsedTime_t elapsed_time{0}; + elapsed_time_t elapsed_time{0}; //! Total elapsed time for fixed updates in microseconds. - ElapsedTime_t elapsed_fixed_time{0}; + elapsed_time_t elapsed_fixed_time{0}; + + typedef std::chrono::steady_clock::time_point time_point_t; //! Time of the last frame. - TimePoint_t last_frame_time; + time_point_t last_frame_time; }; } // namespace crepe diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index dfb37dc..b529f2c 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -5,17 +5,15 @@ // TODO: remove these singletons: #include "../facade/SDLContext.h" -#include "SaveManager.h" - namespace crepe { class ComponentManager; class SceneManager; +class EventManager; +class LoopTimerManager; class SaveManager; class ResourceManager; -class LoopTimerManager; -class EventManager; /** * Struct to pass references to classes that would otherwise need to be singletons down to * other classes within the engine hierarchy. Made to prevent constant changes to subclasses to diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp index e2e36a5..ffb1bcd 100644 --- a/src/crepe/system/AISystem.cpp +++ b/src/crepe/system/AISystem.cpp @@ -1,21 +1,23 @@ #include #include -#include "api/LoopTimer.h" +#include "manager/LoopTimerManager.h" #include "manager/ComponentManager.h" #include "manager/Mediator.h" #include "AISystem.h" using namespace crepe; +using namespace std::chrono; void AISystem::update() { const Mediator & mediator = this->mediator; ComponentManager & mgr = mediator.component_manager; RefVector ai_components = mgr.get_components_by_type(); + LoopTimerManager & loop_timer = mediator.loop_timer; //TODO: Use fixed loop dt (this is not available at master at the moment) - double dt = LoopTimer::get_instance().get_delta_time(); + duration_t dt = loop_timer.get_delta_time(); // Loop through all AI components for (AI & ai : ai_components) { @@ -42,7 +44,7 @@ void AISystem::update() { // Calculate the acceleration (using the above calculated force) vec2 acceleration = force / rigidbody.data.mass; // Finally, update Rigidbody's velocity - rigidbody.data.linear_velocity += acceleration * dt; + rigidbody.data.linear_velocity += acceleration * duration_cast(dt).count(); } } diff --git a/src/test/LoopTimerTest.cpp b/src/test/LoopTimerTest.cpp index 1216e5e..f99f109 100644 --- a/src/test/LoopTimerTest.cpp +++ b/src/test/LoopTimerTest.cpp @@ -15,6 +15,7 @@ protected: void SetUp() override { loop_timer.start(); } }; + TEST_F(LoopTimerTest, EnforcesTargetFrameRate) { // Set the target FPS to 60 (which gives a target time per frame of ~16.67 ms) loop_timer.set_target_framerate(60); @@ -28,15 +29,17 @@ TEST_F(LoopTimerTest, EnforcesTargetFrameRate) { // For 60 FPS, the target frame time is around 16.67ms ASSERT_NEAR(elapsed_ms, 16.7, 1); } + TEST_F(LoopTimerTest, SetTargetFps) { // Set the target FPS to 120 loop_timer.set_target_framerate(120); // Calculate the expected frame time (~8.33ms per frame) - Duration_t expected_frame_time = std::chrono::duration(1.0 / 120.0); + duration_t expected_frame_time = std::chrono::duration(1.0 / 120.0); ASSERT_NEAR(loop_timer.frame_target_time.count(), expected_frame_time.count(), 0.001); } + TEST_F(LoopTimerTest, DeltaTimeCalculation) { // Set the target FPS to 60 (16.67 ms per frame) loop_timer.set_target_framerate(60); @@ -46,7 +49,7 @@ TEST_F(LoopTimerTest, DeltaTimeCalculation) { auto end_time = steady_clock::now(); // Check the delta time - Duration_t delta_time = loop_timer.get_delta_time(); + duration_t delta_time = loop_timer.get_delta_time(); auto elapsed_time = duration_cast(end_time - start_time).count(); @@ -68,8 +71,9 @@ 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 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') 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 From 9390bc838dbcb7126b07d8b109697f96d713f34d Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 11 Dec 2024 15:14:34 +0100 Subject: feedback changed and merge working --- src/crepe/manager/LoopTimerManager.cpp | 15 +++++++-------- src/crepe/manager/LoopTimerManager.h | 2 +- src/test/LoopManagerTest.cpp | 7 ++----- 3 files changed, 10 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 5a6cc76..ea4619f 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -18,13 +18,12 @@ void LoopTimerManager::start() { this->last_frame_time = std::chrono::steady_clock::now(); 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 = -this->fixed_delta_time; + this->elapsed_fixed_time = elapsed_time_t{0}; 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; @@ -41,12 +40,12 @@ 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) { @@ -64,9 +63,9 @@ 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 + time_point_t current_frame_time = std::chrono::steady_clock::now(); - Duration_t frame_duration = current_frame_time - this->last_frame_time; + 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) { duration_t delay_time = this->frame_target_time - frame_duration; @@ -88,6 +87,6 @@ void LoopTimerManager::set_fixed_delta_time(float 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; } diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index ad522f7..0eda156 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -6,7 +6,7 @@ namespace crepe { -typedef std::chrono::duration duration_t; +typedef std::chrono::duration duration_t; typedef std::chrono::duration elapsed_time_t; /** diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp index 4e0ecdc..cf7a454 100644 --- a/src/test/LoopManagerTest.cpp +++ b/src/test/LoopManagerTest.cpp @@ -28,7 +28,7 @@ TEST_F(LoopManagerTest, FixedUpdate) { // Set expectations for the mock calls EXPECT_CALL(test_loop, frame_update).Times(::testing::Exactly(60)); - EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(50)); + EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(49)); // Start the loop in a separate thread std::thread loop_thread([&]() { test_loop.start(); }); @@ -49,7 +49,7 @@ TEST_F(LoopManagerTest, ScaledFixedUpdate) { // Set expectations for the mock calls EXPECT_CALL(test_loop, frame_update).Times(::testing::Exactly(60)); - EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(50)); + EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(49)); // Start the loop in a separate thread std::thread loop_thread([&]() { test_loop.start(); }); @@ -67,9 +67,6 @@ TEST_F(LoopManagerTest, ScaledFixedUpdate) { TEST_F(LoopManagerTest, ShutDown) { // Arrange test_loop.loop_timer.set_target_framerate(60); - - EXPECT_CALL(test_loop, frame_update).Times(::testing::AtLeast(1)); - EXPECT_CALL(test_loop, fixed_update).Times(::testing::AtLeast(1)); // Start the loop in a separate thread std::thread loop_thread([&]() { test_loop.start(); }); std::this_thread::sleep_for(std::chrono::milliseconds(1)); -- cgit v1.2.3 From 98a850627aaf9f506ba60bbd4b33e91f9d0471a1 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 11 Dec 2024 15:16:19 +0100 Subject: make format --- src/crepe/manager/LoopTimerManager.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index ea4619f..9819632 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -23,8 +23,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; @@ -40,12 +39,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) { @@ -63,8 +65,7 @@ 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() { - time_point_t current_frame_time - = std::chrono::steady_clock::now(); + 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) { @@ -87,6 +88,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; } -- cgit v1.2.3 From 1087daecae46cd7acc3f64c5169e56f59953b599 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 11 Dec 2024 18:33:06 +0100 Subject: fps to unsigned --- src/crepe/manager/LoopTimerManager.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index 0eda156..91403e4 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -149,9 +149,9 @@ private: private: //! Target frames per second. - int target_fps = 60; + unsigned target_fps = 60; //! Actual frames per second. - int actual_fps = 0; + unsigned actual_fps = 0; //! Time scale for speeding up or slowing down the game (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up). float time_scale = 1; //! Maximum delta time in seconds to avoid large jumps. -- cgit v1.2.3 From 37f352c20cdf3c972ad99b076bb091f698132312 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 11 Dec 2024 19:16:03 +0100 Subject: testing less strict --- src/test/LoopManagerTest.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp index cf7a454..af89d64 100644 --- a/src/test/LoopManagerTest.cpp +++ b/src/test/LoopManagerTest.cpp @@ -27,8 +27,8 @@ TEST_F(LoopManagerTest, FixedUpdate) { test_loop.loop_timer.set_target_framerate(60); // Set expectations for the mock calls - EXPECT_CALL(test_loop, frame_update).Times(::testing::Exactly(60)); - EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(49)); + EXPECT_CALL(test_loop, frame_update).Times(::testing::Between(55, 65)); + EXPECT_CALL(test_loop, fixed_update).Times(::testing::Between(48, 52)); // Start the loop in a separate thread std::thread loop_thread([&]() { test_loop.start(); }); @@ -48,8 +48,8 @@ TEST_F(LoopManagerTest, ScaledFixedUpdate) { test_loop.loop_timer.set_target_framerate(60); // Set expectations for the mock calls - EXPECT_CALL(test_loop, frame_update).Times(::testing::Exactly(60)); - EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(49)); + EXPECT_CALL(test_loop, frame_update).Times(::testing::Between(55, 65)); + EXPECT_CALL(test_loop, fixed_update).Times(::testing::Between(48, 52)); // Start the loop in a separate thread std::thread loop_thread([&]() { test_loop.start(); }); -- cgit v1.2.3 From a464096dfff2ee4605e77852020e6a5c0fa5aa58 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 11 Dec 2024 19:32:22 +0100 Subject: last changes --- src/crepe/api/LoopManager.h | 1 - src/crepe/api/LoopTimer.cpp | 79 -------------------------------------------- src/crepe/manager/Mediator.h | 1 - 3 files changed, 81 deletions(-) delete mode 100644 src/crepe/api/LoopTimer.cpp (limited to 'src') diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 50d91d2..2915315 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -12,7 +12,6 @@ #include "../manager/SceneManager.h" #include "../system/System.h" -#include "LoopTimer.h" namespace crepe { /** diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp deleted file mode 100644 index 56e48d3..0000000 --- a/src/crepe/api/LoopTimer.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include - -#include "../util/Log.h" -#include "facade/SDLContext.h" -#include "manager/Manager.h" - -#include "LoopTimer.h" - -using namespace crepe; - -LoopTimer::LoopTimer(Mediator & mediator) : Manager(mediator) { - dbg_trace(); - mediator.timer = *this; -} - -void LoopTimer::start() { - this->last_frame_time = std::chrono::steady_clock::now(); - this->elapsed_time = std::chrono::milliseconds(0); - this->elapsed_fixed_time = std::chrono::milliseconds(0); - this->delta_time = std::chrono::milliseconds(0); -} - -void LoopTimer::update() { - auto current_frame_time = std::chrono::steady_clock::now(); - // Convert to duration in seconds for delta time - this->delta_time = std::chrono::duration_cast>( - current_frame_time - last_frame_time); - - if (this->delta_time > this->maximum_delta_time) { - this->delta_time = this->maximum_delta_time; - } - - this->delta_time *= this->game_scale; - this->elapsed_time += this->delta_time; - this->last_frame_time = current_frame_time; -} - -double LoopTimer::get_delta_time() const { return this->delta_time.count(); } - -double LoopTimer::get_current_time() const { return this->elapsed_time.count(); } - -void LoopTimer::advance_fixed_update() { this->elapsed_fixed_time += this->fixed_delta_time; } - -double LoopTimer::get_fixed_delta_time() const { return this->fixed_delta_time.count(); } - -void LoopTimer::set_fps(int fps) { - this->fps = fps; - // target time per frame in seconds - this->frame_target_time = std::chrono::duration(1.0) / fps; -} - -int LoopTimer::get_fps() const { return this->fps; } - -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(current_frame_time - - this->last_frame_time); - - if (frame_duration < this->frame_target_time) { - std::chrono::milliseconds delay_time - = std::chrono::duration_cast(this->frame_target_time - - frame_duration); - if (delay_time.count() > 0) { - SDLContext & ctx = this->mediator.sdl_context; - ctx.delay(delay_time.count()); - } - } - - this->last_frame_time = current_frame_time; -} - -double LoopTimer::get_lag() const { - return (this->elapsed_time - this->elapsed_fixed_time).count(); -} diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index a055bb9..a336410 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -11,7 +11,6 @@ class LoopTimerManager; class SaveManager; class ResourceManager; class SDLContext; -class LoopTimer; /** * Struct to pass references to classes that would otherwise need to be singletons down to -- cgit v1.2.3