diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-12-11 21:19:57 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-12-11 21:19:57 +0100 |
commit | 000062b462a3af86db4dac4d8c9e5ef32feb2996 (patch) | |
tree | d766704f5862520ead6a03656103dd2fbcce99e9 /src | |
parent | 359ad8db97305856f4cfdade1cd1dada78a7a635 (diff) |
split up loopmanager into SystemManager and Engine
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/crepe/api/Engine.cpp | 37 | ||||
-rw-r--r-- | src/crepe/api/Engine.h (renamed from src/crepe/api/LoopManager.h) | 48 | ||||
-rw-r--r-- | src/crepe/api/Engine.hpp | 12 | ||||
-rw-r--r-- | src/crepe/api/LoopTimer.h | 2 | ||||
-rw-r--r-- | src/crepe/manager/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/crepe/manager/Mediator.h | 4 | ||||
-rw-r--r-- | src/crepe/manager/SystemManager.cpp (renamed from src/crepe/api/LoopManager.cpp) | 40 | ||||
-rw-r--r-- | src/crepe/manager/SystemManager.h | 55 | ||||
-rw-r--r-- | src/crepe/manager/SystemManager.hpp (renamed from src/crepe/api/LoopManager.hpp) | 16 | ||||
-rw-r--r-- | src/crepe/system/ReplaySystem.cpp | 11 | ||||
-rw-r--r-- | src/example/replay.cpp | 9 |
12 files changed, 140 insertions, 102 deletions
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 46deb67..6e062f9 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -15,7 +15,7 @@ target_sources(crepe PUBLIC CircleCollider.cpp IKeyListener.cpp IMouseListener.cpp - LoopManager.cpp + Engine.cpp LoopTimer.cpp Asset.cpp EventHandler.cpp @@ -49,7 +49,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Event.h IKeyListener.h IMouseListener.h - LoopManager.h + Engine.h + Engine.hpp LoopTimer.h Asset.h Button.h diff --git a/src/crepe/api/Engine.cpp b/src/crepe/api/Engine.cpp new file mode 100644 index 0000000..8ed55fa --- /dev/null +++ b/src/crepe/api/Engine.cpp @@ -0,0 +1,37 @@ +#include "Engine.h" + +using namespace crepe; +using namespace std; + +void Engine::start() { + this->setup(); + this->loop(); +} + +void Engine::setup() { + LoopTimer & timer = this->loop_timer; + this->game_running = true; + this->scene_manager.load_next_scene(); + timer.start(); + timer.set_fps(200); +} + +void Engine::loop() { + LoopTimer & timer = this->loop_timer; + SystemManager & systems = this->system_manager; + + timer.start(); + + while (game_running) { + timer.update(); + + while (timer.get_lag() >= timer.get_fixed_delta_time()) { + systems.fixed_update(); + timer.advance_fixed_update(); + } + + systems.frame_update(); + timer.enforce_frame_rate(); + } +} + diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/Engine.h index f34a5a0..7601015 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/Engine.h @@ -7,21 +7,20 @@ #include "../manager/ResourceManager.h" #include "../manager/SaveManager.h" #include "../manager/SceneManager.h" -#include "../system/System.h" +#include "../manager/SystemManager.h" #include "LoopTimer.h" namespace crepe { /** - * \brief Main game loop manager + * \brief Main game entrypoint * * This class is responsible for managing the game loop, including initialization and updating. */ -class LoopManager { +class Engine { public: void start(); - LoopManager(); /** * \brief Add a new concrete scene to the scene manager @@ -45,20 +44,6 @@ private: */ void loop(); - /** - * \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(); - bool game_running = false; private: @@ -79,31 +64,10 @@ private: LoopTimer loop_timer{mediator}; //! ReplayManager instance ReplayManager replay_manager{mediator}; - -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. - */ - std::unordered_map<std::type_index, std::unique_ptr<System>> systems; - /** - * \brief Initialize a system - * \tparam T System type (must be derivative of \c System) - */ - template <class T> - void load_system(); - /** - * \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 <class T> - T & get_system(); + //! SystemManager + SystemManager system_manager{mediator}; }; } // namespace crepe -#include "LoopManager.hpp" +#include "Engine.hpp" diff --git a/src/crepe/api/Engine.hpp b/src/crepe/api/Engine.hpp new file mode 100644 index 0000000..f2fdc0a --- /dev/null +++ b/src/crepe/api/Engine.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "Engine.h" + +namespace crepe { + +template <class T> +void Engine::add_scene() { + this->scene_manager.add_scene<T>(); +} + +} // namespace crepe diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index 0a73a4c..fb52a10 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -54,7 +54,7 @@ public: void set_game_scale(double game_scale); private: - friend class LoopManager; + friend class Engine; /** * \brief Start the loop timer. 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<ResourceManager> resource_manager; OptionalRef<LoopTimer> timer; OptionalRef<ReplayManager> replay_manager; - OptionalRef<LoopManager> loop_manager; + OptionalRef<SystemManager> system_manager; }; } // namespace crepe diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/manager/SystemManager.cpp index 2855455..8fd80a7 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/manager/SystemManager.cpp @@ -9,12 +9,12 @@ #include "../system/ScriptSystem.h" #include "../system/EventSystem.h" -#include "LoopManager.h" +#include "SystemManager.h" using namespace crepe; using namespace std; -LoopManager::LoopManager() { +SystemManager::SystemManager(Mediator & mediator) : Manager(mediator) { this->load_system<ScriptSystem>(); this->load_system<AISystem>(); this->load_system<PhysicsSystem>(); @@ -26,50 +26,20 @@ LoopManager::LoopManager() { this->load_system<EventSystem>(); this->load_system<AudioSystem>(); - this->mediator.loop_manager = *this; + this->mediator.system_manager = *this; } -void LoopManager::start() { - this->setup(); - this->loop(); -} - -void LoopManager::fixed_update() { +void SystemManager::fixed_update() { for (auto & [type, system] : this->systems) { if (!system->active) continue; system->fixed_update(); } } -void LoopManager::frame_update() { +void SystemManager::frame_update() { for (auto & [type, system] : this->systems) { if (!system->active) continue; system->frame_update(); } } -void LoopManager::setup() { - LoopTimer & timer = this->loop_timer; - this->game_running = true; - this->scene_manager.load_next_scene(); - timer.start(); - timer.set_fps(200); -} - -void LoopManager::loop() { - LoopTimer & timer = this->loop_timer; - timer.start(); - - while (game_running) { - timer.update(); - - while (timer.get_lag() >= timer.get_fixed_delta_time()) { - this->fixed_update(); - timer.advance_fixed_update(); - } - - this->frame_update(); - timer.enforce_frame_rate(); - } -} - 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<std::type_index, std::unique_ptr<System>> systems; + /** + * \brief Initialize a system + * \tparam T System type (must be derivative of \c System) + */ + template <class T> + 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 <class T> + T & get_system(); +}; + +} // namespace crepe + +#include "SystemManager.hpp" diff --git a/src/crepe/api/LoopManager.hpp b/src/crepe/manager/SystemManager.hpp index 627b281..46ada5f 100644 --- a/src/crepe/api/LoopManager.hpp +++ b/src/crepe/manager/SystemManager.hpp @@ -4,24 +4,18 @@ #include <cassert> #include <format> -#include "../system/System.h" -#include "LoopManager.h" +#include "SystemManager.h" namespace crepe { template <class T> -void LoopManager::add_scene() { - this->scene_manager.add_scene<T>(); -} - -template <class T> -T & LoopManager::get_system() { +T & SystemManager::get_system() { using namespace std; static_assert(is_base_of<System, T>::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("LoopManager: {} is not initialized", type.name())); + throw runtime_error(format("SystemManager: {} is not initialized", type.name())); System * system = this->systems.at(type).get(); T * concrete_system = dynamic_cast<T *>(system); @@ -31,14 +25,14 @@ T & LoopManager::get_system() { } template <class T> -void LoopManager::load_system() { +void SystemManager::load_system() { using namespace std; static_assert(is_base_of<System, T>::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("LoopManager: {} is already initialized", type.name())); + throw runtime_error(format("SystemManager: {} is already initialized", type.name())); System * system = new T(this->mediator); this->systems[type] = unique_ptr<System>(system); } diff --git a/src/crepe/system/ReplaySystem.cpp b/src/crepe/system/ReplaySystem.cpp index 85595a2..5a90752 100644 --- a/src/crepe/system/ReplaySystem.cpp +++ b/src/crepe/system/ReplaySystem.cpp @@ -1,11 +1,10 @@ -#include "system/ScriptSystem.h" +#include "ScriptSystem.h" #include "../manager/ReplayManager.h" +#include "../manager/SystemManager.h" #include "ReplaySystem.h" -#include "../api/LoopManager.h" - using namespace crepe; using namespace std; @@ -58,7 +57,8 @@ void ReplaySystem::update_playing() { } void ReplaySystem::playback_begin() { - LoopManager & loop_manager = this->mediator.loop_manager; + SystemManager & systems = this->mediator.system_manager; + systems.get_system<ScriptSystem>().active = false; // TODO: store system active state // TODO: disable most systems // TODO: store components snapshot @@ -69,7 +69,8 @@ void ReplaySystem::playback_end() { replay.state = ReplayManager::IDLE; - LoopManager & loop_manager = this->mediator.loop_manager; + SystemManager & systems = this->mediator.system_manager; + systems.get_system<ScriptSystem>().active = true; // TODO: restore system active state snapshot // TODO: restore components snapshot diff --git a/src/example/replay.cpp b/src/example/replay.cpp index 2f9c63e..11413fa 100644 --- a/src/example/replay.cpp +++ b/src/example/replay.cpp @@ -5,7 +5,7 @@ #include <crepe/api/Config.h> #include <crepe/api/Event.h> #include <crepe/api/GameObject.h> -#include <crepe/api/LoopManager.h> +#include <crepe/api/Engine.h> #include <crepe/api/Rigidbody.h> #include <crepe/api/Scene.h> #include <crepe/api/Script.h> @@ -95,9 +95,10 @@ public: int main(int argc, char * argv[]) { Config & cfg = Config::get_instance(); cfg.log.level = Log::Level::DEBUG; - LoopManager gameloop; - gameloop.add_scene<TestScene>(); - gameloop.start(); + Engine engine; + + engine.add_scene<TestScene>(); + engine.start(); return 0; } |