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/manager/EventManager.h | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) (limited to 'src/crepe/manager/EventManager.h') 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 -- 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/crepe/manager/EventManager.h') 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 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/crepe/manager/EventManager.h') 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