From 000062b462a3af86db4dac4d8c9e5ef32feb2996 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 11 Dec 2024 21:19:57 +0100 Subject: split up loopmanager into SystemManager and Engine --- src/crepe/manager/CMakeLists.txt | 3 ++ src/crepe/manager/Mediator.h | 4 +-- src/crepe/manager/SystemManager.cpp | 45 ++++++++++++++++++++++++++++++ src/crepe/manager/SystemManager.h | 55 +++++++++++++++++++++++++++++++++++++ src/crepe/manager/SystemManager.hpp | 40 +++++++++++++++++++++++++++ 5 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 src/crepe/manager/SystemManager.cpp create mode 100644 src/crepe/manager/SystemManager.h create mode 100644 src/crepe/manager/SystemManager.hpp (limited to 'src/crepe/manager') diff --git a/src/crepe/manager/CMakeLists.txt b/src/crepe/manager/CMakeLists.txt index 459bbfa..aa19f3e 100644 --- a/src/crepe/manager/CMakeLists.txt +++ b/src/crepe/manager/CMakeLists.txt @@ -6,6 +6,7 @@ target_sources(crepe PUBLIC SceneManager.cpp ResourceManager.cpp ReplayManager.cpp + SystemManager.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -21,5 +22,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES ResourceManager.h ResourceManager.hpp ReplayManager.h + SystemManager.h + SystemManager.hpp ) diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index f5864e7..95ca4b5 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -14,7 +14,7 @@ class ResourceManager; class SDLContext; class LoopTimer; class ReplayManager; -class LoopManager; +class SystemManager; /** * Struct to pass references to classes that would otherwise need to be singletons down to @@ -37,7 +37,7 @@ struct Mediator { OptionalRef resource_manager; OptionalRef timer; OptionalRef replay_manager; - OptionalRef loop_manager; + OptionalRef system_manager; }; } // namespace crepe diff --git a/src/crepe/manager/SystemManager.cpp b/src/crepe/manager/SystemManager.cpp new file mode 100644 index 0000000..8fd80a7 --- /dev/null +++ b/src/crepe/manager/SystemManager.cpp @@ -0,0 +1,45 @@ +#include "../system/AISystem.h" +#include "../system/AnimatorSystem.h" +#include "../system/AudioSystem.h" +#include "../system/CollisionSystem.h" +#include "../system/InputSystem.h" +#include "../system/ParticleSystem.h" +#include "../system/PhysicsSystem.h" +#include "../system/RenderSystem.h" +#include "../system/ScriptSystem.h" +#include "../system/EventSystem.h" + +#include "SystemManager.h" + +using namespace crepe; +using namespace std; + +SystemManager::SystemManager(Mediator & mediator) : Manager(mediator) { + this->load_system(); + this->load_system(); + this->load_system(); + this->load_system(); + this->load_system(); + this->load_system(); + this->load_system(); + this->load_system(); + this->load_system(); + this->load_system(); + + this->mediator.system_manager = *this; +} + +void SystemManager::fixed_update() { + for (auto & [type, system] : this->systems) { + if (!system->active) continue; + system->fixed_update(); + } +} + +void SystemManager::frame_update() { + for (auto & [type, system] : this->systems) { + if (!system->active) continue; + system->frame_update(); + } +} + diff --git a/src/crepe/manager/SystemManager.h b/src/crepe/manager/SystemManager.h new file mode 100644 index 0000000..5726a5c --- /dev/null +++ b/src/crepe/manager/SystemManager.h @@ -0,0 +1,55 @@ +#pragma once + +#include "../system/System.h" + +#include "Manager.h" + +namespace crepe { + +class SystemManager : public Manager { +public: + SystemManager(Mediator &); + + /** + * \brief Per-frame update. + * + * Updates the game state based on the elapsed time since the last frame. + */ + void frame_update(); + + /** + * \brief Fixed update executed at a fixed rate. + * + * This function updates physics and game logic based on LoopTimer's fixed_delta_time. + */ + void fixed_update(); + +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 SystemManager using SystemManager::load_system. + */ + std::unordered_map> systems; + /** + * \brief Initialize a system + * \tparam T System type (must be derivative of \c System) + */ + template + void load_system(); + +public: + /** + * \brief Retrieve a reference to ECS system + * \tparam T System type + * \returns Reference to system instance + * \throws std::runtime_error if the System is not initialized + */ + template + T & get_system(); +}; + +} // namespace crepe + +#include "SystemManager.hpp" diff --git a/src/crepe/manager/SystemManager.hpp b/src/crepe/manager/SystemManager.hpp new file mode 100644 index 0000000..46ada5f --- /dev/null +++ b/src/crepe/manager/SystemManager.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include +#include +#include + +#include "SystemManager.h" + +namespace crepe { + +template +T & SystemManager::get_system() { + using namespace std; + static_assert(is_base_of::value, "get_system must recieve a derivative class of System"); + + const type_info & type = typeid(T); + if (!this->systems.contains(type)) + throw runtime_error(format("SystemManager: {} is not initialized", type.name())); + + System * system = this->systems.at(type).get(); + T * concrete_system = dynamic_cast(system); + assert(concrete_system != nullptr); + + return *concrete_system; +} + +template +void SystemManager::load_system() { + using namespace std; + static_assert(is_base_of::value, + "load_system must recieve a derivative class of System"); + + const type_info & type = typeid(T); + if (this->systems.contains(type)) + throw runtime_error(format("SystemManager: {} is already initialized", type.name())); + System * system = new T(this->mediator); + this->systems[type] = unique_ptr(system); +} + +} // namespace crepe -- cgit v1.2.3