diff options
Diffstat (limited to 'src/crepe')
28 files changed, 148 insertions, 148 deletions
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 3e9e21c..637fbe1 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -6,40 +6,51 @@ #include "../system/PhysicsSystem.h" #include "../system/RenderSystem.h" #include "../system/ScriptSystem.h" -#include "manager/EventManager.h" +#include "../system/EventSystem.h" #include "LoopManager.h" using namespace crepe; using namespace std; -LoopManager::LoopManager() { - this->load_system<AnimatorSystem>(); - this->load_system<CollisionSystem>(); - this->load_system<ParticleSystem>(); - this->load_system<PhysicsSystem>(); - this->load_system<RenderSystem>(); - this->load_system<ScriptSystem>(); - this->load_system<InputSystem>(); - this->load_system<AudioSystem>(); -} - -void LoopManager::process_input() { this->get_system<InputSystem>().update(); } +LoopManager::LoopManager() + : systems({ + ScriptSystem{this->mediator}, + PhysicsSystem{this->mediator}, + CollisionSystem{this->mediator}, + AnimatorSystem{this->mediator}, + ParticleSystem{this->mediator}, + RenderSystem{this->mediator}, + InputSystem{this->mediator}, + EventSystem{this->mediator}, + AudioSystem{this->mediator}, + }) { } void LoopManager::start() { this->setup(); this->loop(); } -void LoopManager::set_running(bool running) { this->game_running = running; } void LoopManager::fixed_update() { - // TODO: retrieve EventManager from direct member after singleton refactor - EventManager & ev = this->mediator.event_manager; - ev.dispatch_events(); - this->get_system<ScriptSystem>().update(); - this->get_system<PhysicsSystem>().update(); - this->get_system<CollisionSystem>().update(); - this->get_system<AudioSystem>().update(); + for (System & system : this->systems) { + if (!system.active) continue; + system.fixed_update(); + } +} + +void LoopManager::frame_update() { + for (System & 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() { @@ -50,31 +61,12 @@ void LoopManager::loop() { timer.update(); while (timer.get_lag() >= timer.get_fixed_delta_time()) { - this->process_input(); this->fixed_update(); timer.advance_fixed_update(); } - this->update(); - this->render(); - + this->frame_update(); timer.enforce_frame_rate(); } } -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::render() { - if (!this->game_running) return; - - this->get_system<AnimatorSystem>().update(); - this->get_system<RenderSystem>().update(); -} - -void LoopManager::update() {} diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 0947f94..1ac6a39 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -1,7 +1,5 @@ #pragma once -#include <memory> - #include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" #include "../manager/ResourceManager.h" @@ -45,25 +43,11 @@ private: void loop(); /** - * \brief Function for handling input-related system calls. - * - * Processes user inputs from keyboard and mouse. - */ - void process_input(); - - /** * \brief Per-frame update. * * Updates the game state based on the elapsed time since the last frame. */ - void update(); - - /** - * \brief Late update which is called after update(). - * - * This function can be used for final adjustments before rendering. - */ - void late_update(); + void frame_update(); /** * \brief Fixed update executed at a fixed rate. @@ -72,20 +56,6 @@ private: */ void fixed_update(); - /** - * \brief Set game running variable - * - * \param running running (false = game shutdown, true = game running) - */ - void set_running(bool running); - - /** - * \brief Function for executing render-related systems. - * - * Renders the current state of the game to the screen. - */ - void render(); - bool game_running = false; private: @@ -107,25 +77,8 @@ private: 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(); + std::vector<System> systems; }; } // namespace crepe diff --git a/src/crepe/api/LoopManager.hpp b/src/crepe/api/LoopManager.hpp index 266758a..51afa70 100644 --- a/src/crepe/api/LoopManager.hpp +++ b/src/crepe/api/LoopManager.hpp @@ -1,11 +1,5 @@ #pragma once -#include <cassert> -#include <format> -#include <memory> - -#include "../system/System.h" - #include "LoopManager.h" namespace crepe { @@ -15,34 +9,4 @@ 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/manager/CMakeLists.txt b/src/crepe/manager/CMakeLists.txt index 480c8ee..459bbfa 100644 --- a/src/crepe/manager/CMakeLists.txt +++ b/src/crepe/manager/CMakeLists.txt @@ -5,6 +5,7 @@ target_sources(crepe PUBLIC SaveManager.cpp SceneManager.cpp ResourceManager.cpp + ReplayManager.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -19,5 +20,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SceneManager.hpp ResourceManager.h ResourceManager.hpp + ReplayManager.h ) diff --git a/src/crepe/manager/ReplayManager.cpp b/src/crepe/manager/ReplayManager.cpp new file mode 100644 index 0000000..3643a8e --- /dev/null +++ b/src/crepe/manager/ReplayManager.cpp @@ -0,0 +1,8 @@ +#include "ReplayManager.h" +#include "manager/Manager.h" + +using namespace crepe; + +ReplayManager::ReplayManager(Mediator & mediator) : Manager(mediator) { +} + diff --git a/src/crepe/manager/ReplayManager.h b/src/crepe/manager/ReplayManager.h new file mode 100644 index 0000000..242eae4 --- /dev/null +++ b/src/crepe/manager/ReplayManager.h @@ -0,0 +1,32 @@ +#pragma once + +#include "Manager.h" + +namespace crepe { + +class ReplaySystem; +class Memento; + +typedef size_t recording_t; + +class ReplayManager : public Manager { +public: + ReplayManager(Mediator & mediator); + friend class ReplaySystem; + +protected: + void record_frame(); + +private: + bool recording; + struct Recording { + recording_t id; + std::vector<Memento> frames; + }; +public: + void record_start(); + recording_t record_end(); + +}; + +} // namespace crepe diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 549c35d..8d6d8fa 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -1,5 +1,3 @@ - - #include "../api/Animator.h" #include "../manager/ComponentManager.h" #include "api/LoopTimer.h" @@ -8,7 +6,7 @@ using namespace crepe; -void AnimatorSystem::update() { +void AnimatorSystem::frame_update() { ComponentManager & mgr = this->mediator.component_manager; LoopTimer & timer = this->mediator.timer; RefVector<Animator> animations = mgr.get_components_by_type<Animator>(); diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index 7d3f565..092e131 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -22,7 +22,7 @@ public: * Animator components, moving the animations forward and managing their behavior (e.g., * looping). */ - void update() override; + void frame_update() override; }; } // namespace crepe diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index ddba268..9b86069 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -7,7 +7,7 @@ using namespace crepe; using namespace std; -void AudioSystem::update() { +void AudioSystem::fixed_update() { ComponentManager & component_manager = this->mediator.component_manager; ResourceManager & resource_manager = this->mediator.resource_manager; RefVector<AudioSource> components diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h index 2ddc443..56fc98c 100644 --- a/src/crepe/system/AudioSystem.h +++ b/src/crepe/system/AudioSystem.h @@ -11,7 +11,7 @@ namespace crepe { class AudioSystem : public System { public: using System::System; - void update() override; + void fixed_update() override; private: /** diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt index 6b2e099..1336114 100644 --- a/src/crepe/system/CMakeLists.txt +++ b/src/crepe/system/CMakeLists.txt @@ -8,6 +8,8 @@ target_sources(crepe PUBLIC AudioSystem.cpp AnimatorSystem.cpp InputSystem.cpp + EventSystem.cpp + ReplaySystem.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -19,4 +21,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES AudioSystem.h AnimatorSystem.h InputSystem.h + EventSystem.h ) diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 44a0431..2db592f 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -23,7 +23,7 @@ using namespace crepe; -void CollisionSystem::update() { +void CollisionSystem::fixed_update() { std::vector<CollisionInternal> all_colliders; game_object_id_t id = 0; ComponentManager & mgr = this->mediator.component_manager; diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h index 5b136c6..48a8f86 100644 --- a/src/crepe/system/CollisionSystem.h +++ b/src/crepe/system/CollisionSystem.h @@ -85,7 +85,7 @@ public: public: //! Updates the collision system by checking for collisions between colliders and handling them. - void update() override; + void fixed_update() override; private: /** diff --git a/src/crepe/system/EventSystem.cpp b/src/crepe/system/EventSystem.cpp new file mode 100644 index 0000000..bf2b6bc --- /dev/null +++ b/src/crepe/system/EventSystem.cpp @@ -0,0 +1,9 @@ +#include "EventSystem.h" + +using namespace crepe; + +void EventSystem::fixed_update() { + EventManager & ev = this->mediator.event_manager; + ev.dispatch_events(); +} + diff --git a/src/crepe/system/EventSystem.h b/src/crepe/system/EventSystem.h new file mode 100644 index 0000000..a179d00 --- /dev/null +++ b/src/crepe/system/EventSystem.h @@ -0,0 +1,15 @@ +#pragma once + +#include "System.h" + +namespace crepe { + +class EventSystem : public System { +public: + using System::System; + + void fixed_update() override; +}; + +} + diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index aaa8bdf..b31bd34 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -6,7 +6,7 @@ using namespace crepe; -void InputSystem::update() { +void InputSystem::fixed_update() { ComponentManager & mgr = this->mediator.component_manager; EventManager & event_mgr = this->mediator.event_manager; std::vector<SDLContext::EventData> event_list = SDLContext::get_instance().get_events(); diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 87e86f8..62b0fcd 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -27,7 +27,7 @@ public: * \brief Updates the system, processing all input events. * This method processes all events and triggers corresponding actions. */ - void update() override; + void fixed_update() override; private: //! Stores the last position of the mouse when the button was pressed. diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index b14c52f..5ccd128 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -10,7 +10,7 @@ using namespace crepe; -void ParticleSystem::update() { +void ParticleSystem::frame_update() { // Get all emitters ComponentManager & mgr = this->mediator.component_manager; RefVector<ParticleEmitter> emitters = mgr.get_components_by_type<ParticleEmitter>(); diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index 068f01c..6c631ea 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -20,7 +20,7 @@ public: * \brief Updates all particle emitters by emitting particles, updating particle states, and * checking bounds. */ - void update() override; + void frame_update() override; private: /** diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index ebf4439..714e531 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -10,7 +10,7 @@ using namespace crepe; -void PhysicsSystem::update() { +void PhysicsSystem::fixed_update() { ComponentManager & mgr = this->mediator.component_manager; RefVector<Rigidbody> rigidbodies = mgr.get_components_by_type<Rigidbody>(); RefVector<Transform> transforms = mgr.get_components_by_type<Transform>(); diff --git a/src/crepe/system/PhysicsSystem.h b/src/crepe/system/PhysicsSystem.h index 26152a5..5ed624f 100644 --- a/src/crepe/system/PhysicsSystem.h +++ b/src/crepe/system/PhysicsSystem.h @@ -18,7 +18,7 @@ public: * * It calculates new velocties and changes the postion in the transform. */ - void update() override; + void fixed_update() override; }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 26f2c85..e147b9f 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -61,7 +61,7 @@ RefVector<Sprite> RenderSystem::sort(RefVector<Sprite> & objs) const { return sorted_objs; } -void RenderSystem::update() { +void RenderSystem::frame_update() { this->clear_screen(); this->render(); this->present_screen(); diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index e270a6b..a01923d 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -27,7 +27,7 @@ public: * \brief Updates the RenderSystem for the current frame. * This method is called to perform all rendering operations for the current game frame. */ - void update() override; + void frame_update() override; private: //! Clears the screen in preparation for rendering. diff --git a/src/crepe/system/ReplaySystem.cpp b/src/crepe/system/ReplaySystem.cpp new file mode 100644 index 0000000..3aabb58 --- /dev/null +++ b/src/crepe/system/ReplaySystem.cpp @@ -0,0 +1,7 @@ +#include "ReplaySystem.h" + +using namespace crepe; + +void ReplaySystem::fixed_update() { +} + diff --git a/src/crepe/system/ReplaySystem.h b/src/crepe/system/ReplaySystem.h new file mode 100644 index 0000000..15ef3fc --- /dev/null +++ b/src/crepe/system/ReplaySystem.h @@ -0,0 +1,15 @@ +#pragma once + +#include "System.h" + +namespace crepe { + +class ReplaySystem : public System { +public: + using System::System; + + void fixed_update() override; +}; + +} + diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index d6b2ca1..ab79fc3 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -7,7 +7,7 @@ using namespace std; using namespace crepe; -void ScriptSystem::update() { +void ScriptSystem::fixed_update() { dbg_trace(); ComponentManager & mgr = this->mediator.component_manager; diff --git a/src/crepe/system/ScriptSystem.h b/src/crepe/system/ScriptSystem.h index 3db1b1e..612c2ae 100644 --- a/src/crepe/system/ScriptSystem.h +++ b/src/crepe/system/ScriptSystem.h @@ -22,7 +22,7 @@ public: * method. It also calls Script::init() if this has not been done before on * the \c BehaviorScript instance. */ - void update() override; + void fixed_update() override; }; } // namespace crepe diff --git a/src/crepe/system/System.h b/src/crepe/system/System.h index 063dfbf..e2ce7eb 100644 --- a/src/crepe/system/System.h +++ b/src/crepe/system/System.h @@ -14,10 +14,12 @@ class ComponentManager; */ class System { public: - /** - * \brief Process all components this system is responsible for. - */ - virtual void update() = 0; + //! Code that runs in the fixed loop + virtual void fixed_update() {}; + //! Code that runs in the frame loop + virtual void frame_update() {}; + //! Indicates that the update functions of this system should be run + bool active = true; public: System(const Mediator & m); |