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/test/EventTest.cpp | 88 ++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 53 deletions(-) (limited to 'src/test/EventTest.cpp') 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 } -- 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/test/EventTest.cpp') 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