diff options
Diffstat (limited to 'src/crepe/api')
-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/LoopManager.cpp | 75 | ||||
-rw-r--r-- | src/crepe/api/LoopManager.hpp | 46 | ||||
-rw-r--r-- | src/crepe/api/LoopTimer.h | 2 |
7 files changed, 59 insertions, 166 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/LoopManager.cpp b/src/crepe/api/LoopManager.cpp deleted file mode 100644 index 2855455..0000000 --- a/src/crepe/api/LoopManager.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#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 "LoopManager.h" - -using namespace crepe; -using namespace std; - -LoopManager::LoopManager() { - this->load_system<ScriptSystem>(); - this->load_system<AISystem>(); - this->load_system<PhysicsSystem>(); - this->load_system<CollisionSystem>(); - this->load_system<AnimatorSystem>(); - this->load_system<ParticleSystem>(); - this->load_system<RenderSystem>(); - this->load_system<InputSystem>(); - this->load_system<EventSystem>(); - this->load_system<AudioSystem>(); - - this->mediator.loop_manager = *this; -} - -void LoopManager::start() { - this->setup(); - this->loop(); -} - -void LoopManager::fixed_update() { - for (auto & [type, system] : this->systems) { - if (!system->active) continue; - system->fixed_update(); - } -} - -void LoopManager::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/api/LoopManager.hpp b/src/crepe/api/LoopManager.hpp deleted file mode 100644 index 627b281..0000000 --- a/src/crepe/api/LoopManager.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -#include <memory> -#include <cassert> -#include <format> - -#include "../system/System.h" -#include "LoopManager.h" - -namespace crepe { - -template <class T> -void LoopManager::add_scene() { - this->scene_manager.add_scene<T>(); -} - -template <class T> -T & LoopManager::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())); - - System * system = this->systems.at(type).get(); - T * concrete_system = dynamic_cast<T *>(system); - assert(concrete_system != nullptr); - - return *concrete_system; -} - -template <class T> -void LoopManager::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())); - System * system = new T(this->mediator); - this->systems[type] = unique_ptr<System>(system); -} - -} // 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. |