From 6fd7cec7d4bbf5aeb361b3f1337671bb0f9af61b Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 28 Nov 2024 10:13:11 +0100 Subject: manager mediator refactor --- src/crepe/manager/Mediator.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/crepe/manager/Mediator.h (limited to 'src/crepe/manager/Mediator.h') diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h new file mode 100644 index 0000000..ce35d5c --- /dev/null +++ b/src/crepe/manager/Mediator.h @@ -0,0 +1,29 @@ +#pragma once + +#include "../util/OptionalRef.h" + +// TODO: remove these singletons: +#include "SaveManager.h" +#include "EventManager.h" + +namespace crepe { + +class ComponentManager; +class SceneManager; + +/** + * 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 + * pass specific references through dependency injection. All references on this struct + * *should* be explicitly checked for availability as this struct does not guarantee anything. + * + * \todo Find better solution + */ +struct Mediator { + OptionalRef component_manager; + OptionalRef scene_manager; + OptionalRef save_manager = SaveManager::get_instance(); + OptionalRef event_manager = EventManager::get_instance(); +}; + +} -- cgit v1.2.3 From 7a07c56d572a6f30d0aa611bd566197bc04c3b33 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 28 Nov 2024 13:13:54 +0100 Subject: add more documentation to Mediator + fix system mediator use --- src/crepe/manager/Mediator.h | 6 +++++- src/crepe/system/AnimatorSystem.cpp | 2 +- src/crepe/system/ParticleSystem.cpp | 2 +- src/crepe/system/PhysicsSystem.cpp | 2 +- src/crepe/system/RenderSystem.cpp | 6 +++--- src/crepe/system/ScriptSystem.cpp | 2 +- src/crepe/system/System.cpp | 2 +- src/crepe/system/System.h | 2 +- 8 files changed, 14 insertions(+), 10 deletions(-) (limited to 'src/crepe/manager/Mediator.h') diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index ce35d5c..a91509e 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -17,7 +17,11 @@ class SceneManager; * pass specific references through dependency injection. All references on this struct * *should* be explicitly checked for availability as this struct does not guarantee anything. * - * \todo Find better solution + * \note Dereferencing members of this struct should be deferred. If you are a user of this + * class, keep a reference to this mediator instead of just picking references from it when you + * receive an instance. + * + * \warning This class should never be directly accessible from the API */ struct Mediator { OptionalRef component_manager; diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 57ee6e2..8bb6465 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -9,7 +9,7 @@ using namespace crepe; void AnimatorSystem::update() { - ComponentManager & mgr = this->component_manager; + ComponentManager & mgr = this->mediator.component_manager; RefVector animations = mgr.get_components_by_type(); diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index ebae56b..b14c52f 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -12,7 +12,7 @@ using namespace crepe; void ParticleSystem::update() { // Get all emitters - ComponentManager & mgr = this->component_manager; + ComponentManager & mgr = this->mediator.component_manager; RefVector emitters = mgr.get_components_by_type(); for (ParticleEmitter & emitter : emitters) { diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index 071ca21..eba9dfa 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -11,7 +11,7 @@ using namespace crepe; void PhysicsSystem::update() { - ComponentManager & mgr = this->component_manager; + ComponentManager & mgr = this->mediator.component_manager; RefVector rigidbodies = mgr.get_components_by_type(); RefVector transforms = mgr.get_components_by_type(); diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index ea60a95..4e97b3e 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -22,7 +22,7 @@ void RenderSystem::clear_screen() { this->context.clear_screen(); } void RenderSystem::present_screen() { this->context.present_screen(); } const Camera & RenderSystem::update_camera() { - ComponentManager & mgr = this->component_manager; + ComponentManager & mgr = this->mediator.component_manager; RefVector cameras = mgr.get_components_by_type(); @@ -62,7 +62,7 @@ void RenderSystem::update() { bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, const double & scale) { - ComponentManager & mgr = this->component_manager; + ComponentManager & mgr = this->mediator.component_manager; vector> emitters = mgr.get_components_by_id(sprite.game_object_id); @@ -102,7 +102,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, } void RenderSystem::render() { - ComponentManager & mgr = this->component_manager; + ComponentManager & mgr = this->mediator.component_manager; const Camera & cam = this->update_camera(); RefVector sprites = mgr.get_components_by_type(); diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index 7ecbe78..2e16eb0 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -10,7 +10,7 @@ using namespace crepe; void ScriptSystem::update() { dbg_trace(); - ComponentManager & mgr = this->component_manager; + ComponentManager & mgr = this->mediator.component_manager; RefVector behavior_scripts = mgr.get_components_by_type(); for (BehaviorScript & behavior_script : behavior_scripts) { diff --git a/src/crepe/system/System.cpp b/src/crepe/system/System.cpp index 392d920..f68549b 100644 --- a/src/crepe/system/System.cpp +++ b/src/crepe/system/System.cpp @@ -4,4 +4,4 @@ using namespace crepe; -System::System(const Mediator & mediator) : component_manager(mediator.component_manager) { dbg_trace(); } +System::System(const Mediator & mediator) : mediator(mediator) { dbg_trace(); } diff --git a/src/crepe/system/System.h b/src/crepe/system/System.h index 92bfd50..4e7fc6d 100644 --- a/src/crepe/system/System.h +++ b/src/crepe/system/System.h @@ -25,7 +25,7 @@ public: virtual ~System() = default; protected: - ComponentManager & component_manager; + const Mediator & mediator; }; } // namespace crepe -- cgit v1.2.3 From 2d5721820063031b65d64b1b2b1b4797b71935af Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 30 Nov 2024 16:41:22 +0100 Subject: `make format` --- src/crepe/api/LoopManager.cpp | 5 +---- src/crepe/api/LoopManager.h | 2 +- src/crepe/api/Scene.h | 3 ++- src/crepe/api/Script.cpp | 1 - src/crepe/api/Script.h | 4 ++-- src/crepe/manager/ComponentManager.cpp | 2 +- src/crepe/manager/Manager.cpp | 3 +-- src/crepe/manager/Manager.h | 3 +-- src/crepe/manager/Mediator.h | 4 ++-- src/crepe/manager/SaveManager.cpp | 4 ++-- src/crepe/system/PhysicsSystem.cpp | 2 +- src/crepe/system/RenderSystem.cpp | 2 +- src/crepe/system/ScriptSystem.cpp | 2 +- src/test/ECSTest.cpp | 3 ++- src/test/EventTest.cpp | 6 ++---- src/test/ParticleTest.cpp | 3 ++- src/test/PhysicsTest.cpp | 3 ++- src/test/RenderSystemTest.cpp | 3 ++- src/test/SceneManagerTest.cpp | 7 ++++--- src/test/ScriptEventTest.cpp | 7 +++---- src/test/ScriptSceneTest.cpp | 3 +-- src/test/ScriptTest.cpp | 3 +-- src/test/ScriptTest.h | 8 +++++--- 23 files changed, 40 insertions(+), 43 deletions(-) (limited to 'src/crepe/manager/Mediator.h') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index b277185..731cfb7 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -22,9 +22,7 @@ LoopManager::LoopManager() { this->load_system(); } -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(); @@ -69,4 +67,3 @@ void LoopManager::render() { } void LoopManager::update() {} - diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 6ea5ccc..d8910a0 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -2,10 +2,10 @@ #include +#include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" #include "../manager/SceneManager.h" #include "../system/System.h" -#include "../facade/SDLContext.h" #include "LoopTimer.h" diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index 66dad17..9f1e8ce 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -2,8 +2,8 @@ #include -#include "../util/OptionalRef.h" #include "../manager/Mediator.h" +#include "../util/OptionalRef.h" namespace crepe { @@ -37,6 +37,7 @@ public: // TODO: Late references should ALWAYS be private! This is currently kept as-is so unit tests // keep passing, but this reference should not be directly accessible by the user!!! + protected: /** * \name Late references diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp index a27838e..4091fd4 100644 --- a/src/crepe/api/Script.cpp +++ b/src/crepe/api/Script.cpp @@ -25,4 +25,3 @@ void Script::set_next_scene(const string & name) { SceneManager & mgr = mediator.scene_manager; mgr.set_next_scene(name); } - diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index e1f86b2..1b339b0 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -2,10 +2,10 @@ #include +#include "../manager/EventManager.h" +#include "../manager/Mediator.h" #include "../types.h" #include "../util/OptionalRef.h" -#include "../manager/Mediator.h" -#include "../manager/EventManager.h" namespace crepe { diff --git a/src/crepe/manager/ComponentManager.cpp b/src/crepe/manager/ComponentManager.cpp index 5a96158..80cf8b4 100644 --- a/src/crepe/manager/ComponentManager.cpp +++ b/src/crepe/manager/ComponentManager.cpp @@ -1,6 +1,6 @@ #include "../api/GameObject.h" -#include "../util/Log.h" #include "../types.h" +#include "../util/Log.h" #include "ComponentManager.h" diff --git a/src/crepe/manager/Manager.cpp b/src/crepe/manager/Manager.cpp index fe7c936..1182785 100644 --- a/src/crepe/manager/Manager.cpp +++ b/src/crepe/manager/Manager.cpp @@ -2,5 +2,4 @@ using namespace crepe; -Manager::Manager(Mediator & mediator) : mediator(mediator) { } - +Manager::Manager(Mediator & mediator) : mediator(mediator) {} diff --git a/src/crepe/manager/Manager.h b/src/crepe/manager/Manager.h index 9adfd0b..4f21ef4 100644 --- a/src/crepe/manager/Manager.h +++ b/src/crepe/manager/Manager.h @@ -13,5 +13,4 @@ protected: Mediator & mediator; }; -} - +} // namespace crepe diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index a91509e..71bd1c9 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -3,8 +3,8 @@ #include "../util/OptionalRef.h" // TODO: remove these singletons: -#include "SaveManager.h" #include "EventManager.h" +#include "SaveManager.h" namespace crepe { @@ -30,4 +30,4 @@ struct Mediator { OptionalRef event_manager = EventManager::get_instance(); }; -} +} // namespace crepe diff --git a/src/crepe/manager/SaveManager.cpp b/src/crepe/manager/SaveManager.cpp index 121d017..d4ed1c1 100644 --- a/src/crepe/manager/SaveManager.cpp +++ b/src/crepe/manager/SaveManager.cpp @@ -1,7 +1,7 @@ +#include "../ValueBroker.h" +#include "../api/Config.h" #include "../facade/DB.h" #include "../util/Log.h" -#include "../api/Config.h" -#include "../ValueBroker.h" #include "SaveManager.h" diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index eba9dfa..bebcf3d 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -1,10 +1,10 @@ #include -#include "../manager/ComponentManager.h" #include "../api/Config.h" #include "../api/Rigidbody.h" #include "../api/Transform.h" #include "../api/Vector2.h" +#include "../manager/ComponentManager.h" #include "PhysicsSystem.h" diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 4e97b3e..0ad685c 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -5,12 +5,12 @@ #include #include -#include "../manager/ComponentManager.h" #include "../api/Camera.h" #include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" +#include "../manager/ComponentManager.h" #include "RenderSystem.h" diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index 2e16eb0..d6b2ca1 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -1,6 +1,6 @@ -#include "../manager/ComponentManager.h" #include "../api/BehaviorScript.h" #include "../api/Script.h" +#include "../manager/ComponentManager.h" #include "ScriptSystem.h" diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp index 22c4fe7..3e6c61c 100644 --- a/src/test/ECSTest.cpp +++ b/src/test/ECSTest.cpp @@ -2,17 +2,18 @@ #define protected public -#include #include #include #include #include +#include using namespace std; using namespace crepe; class ECSTest : public ::testing::Test { Mediator m; + public: ComponentManager mgr{m}; }; diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp index 350dd07..dccd554 100644 --- a/src/test/EventTest.cpp +++ b/src/test/EventTest.cpp @@ -2,9 +2,9 @@ #include #include -#include #include #include +#include using namespace std; using namespace std::chrono_literals; @@ -37,9 +37,7 @@ public: }; TEST_F(EventManagerTest, EventSubscription) { - EventHandler key_handler = [](const KeyPressEvent & e) { - return true; - }; + EventHandler key_handler = [](const KeyPressEvent & e) { return true; }; // Subscribe to KeyPressEvent EventManager::get_instance().subscribe(key_handler, 1); diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 4e9fa4e..a659fe5 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -7,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -17,6 +17,7 @@ using namespace crepe; class ParticlesTest : public ::testing::Test { Mediator m; + public: ComponentManager component_manager{m}; ParticleSystem particle_system{m}; diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp index 01b7c51..43af8e4 100644 --- a/src/test/PhysicsTest.cpp +++ b/src/test/PhysicsTest.cpp @@ -1,8 +1,8 @@ -#include #include #include #include #include +#include #include #include @@ -12,6 +12,7 @@ using namespace crepe; class PhysicsTest : public ::testing::Test { Mediator m; + public: ComponentManager component_manager{m}; PhysicsSystem system{m}; diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp index 3528e46..c105dcb 100644 --- a/src/test/RenderSystemTest.cpp +++ b/src/test/RenderSystemTest.cpp @@ -7,11 +7,11 @@ #define protected public #include -#include #include #include #include #include +#include #include @@ -21,6 +21,7 @@ using namespace testing; class RenderSystemTest : public Test { Mediator m; + public: ComponentManager mgr{m}; RenderSystem sys{m}; diff --git a/src/test/SceneManagerTest.cpp b/src/test/SceneManagerTest.cpp index d027d89..9bb260c 100644 --- a/src/test/SceneManagerTest.cpp +++ b/src/test/SceneManagerTest.cpp @@ -1,13 +1,13 @@ #include -#include -#include -#include #include #include #include #include #include +#include +#include +#include using namespace std; using namespace crepe; @@ -57,6 +57,7 @@ private: class SceneManagerTest : public ::testing::Test { Mediator m; + public: ComponentManager component_mgr{m}; SceneManager scene_mgr{m}; diff --git a/src/test/ScriptEventTest.cpp b/src/test/ScriptEventTest.cpp index 7a9abbb..5da31e7 100644 --- a/src/test/ScriptEventTest.cpp +++ b/src/test/ScriptEventTest.cpp @@ -4,13 +4,13 @@ #define private public #define protected public -#include -#include #include #include #include #include #include +#include +#include #include #include "ScriptTest.h" @@ -32,7 +32,7 @@ TEST_F(ScriptEventTest, Inactive) { EventManager & evmgr = this->event_manager; unsigned event_count = 0; - script.subscribe([&](const MyEvent &){ + script.subscribe([&](const MyEvent &) { event_count++; return true; }); @@ -48,4 +48,3 @@ TEST_F(ScriptEventTest, Inactive) { evmgr.trigger_event(); EXPECT_EQ(1, event_count); } - diff --git a/src/test/ScriptSceneTest.cpp b/src/test/ScriptSceneTest.cpp index f96ae8b..9ee1e52 100644 --- a/src/test/ScriptSceneTest.cpp +++ b/src/test/ScriptSceneTest.cpp @@ -4,8 +4,8 @@ #define private public #define protected public -#include #include "ScriptTest.h" +#include using namespace std; using namespace crepe; @@ -28,4 +28,3 @@ TEST_F(ScriptSceneTest, Inactive) { script.set_next_scene(non_default_value); EXPECT_EQ(non_default_value, scene_manager.next_scene); } - diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp index 6d0d5fb..1d2d6dd 100644 --- a/src/test/ScriptTest.cpp +++ b/src/test/ScriptTest.cpp @@ -1,5 +1,5 @@ -#include #include +#include // stupid hack to allow access to private/protected members under test #define private public @@ -75,4 +75,3 @@ TEST_F(ScriptTest, UpdateInactive) { system.update(); } } - diff --git a/src/test/ScriptTest.h b/src/test/ScriptTest.h index 9a71ba7..1bbfdd3 100644 --- a/src/test/ScriptTest.h +++ b/src/test/ScriptTest.h @@ -1,22 +1,24 @@ #pragma once -#include #include +#include -#include -#include #include #include +#include +#include class ScriptTest : public testing::Test { protected: crepe::Mediator mediator; + public: crepe::ComponentManager component_manager{mediator}; crepe::ScriptSystem system{mediator}; class MyScript : public crepe::Script { // NOTE: explicitly stating `public:` is not required on actual scripts + public: MOCK_METHOD(void, init, (), (override)); MOCK_METHOD(void, update, (), (override)); -- cgit v1.2.3