From 770496ee9d0e45480c0e0f8951adb8eee247bfe1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 10 Dec 2024 19:50:26 +0100 Subject: big WIP --- src/crepe/manager/CMakeLists.txt | 2 ++ src/crepe/manager/ReplayManager.cpp | 8 ++++++++ src/crepe/manager/ReplayManager.h | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/crepe/manager/ReplayManager.cpp create mode 100644 src/crepe/manager/ReplayManager.h (limited to 'src/crepe/manager') 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 frames; + }; +public: + void record_start(); + recording_t record_end(); + +}; + +} // namespace crepe -- cgit v1.2.3 From f0ecbea57a4d75905c4ee79608807187cd8f3e72 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 11 Dec 2024 16:51:03 +0100 Subject: WIP --- src/crepe/Component.cpp | 5 ++++ src/crepe/Component.h | 8 +++++-- src/crepe/api/LoopManager.cpp | 35 ++++++++++++++-------------- src/crepe/api/LoopManager.h | 21 +++++++++++++++-- src/crepe/api/LoopManager.hpp | 34 +++++++++++++++++++++++++++ src/crepe/manager/ComponentManager.h | 24 +++++++++---------- src/crepe/manager/ReplayManager.h | 17 +++++++++----- src/example/replay.cpp | 45 ++++++++++++++++++++++++++++++------ 8 files changed, 141 insertions(+), 48 deletions(-) (limited to 'src/crepe/manager') diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index acfd35c..141e1a8 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -3,3 +3,8 @@ using namespace crepe; Component::Component(game_object_id_t id) : game_object_id(id) {} + +Component & Component::operator=(const Component &) { + return *this; +} + diff --git a/src/crepe/Component.h b/src/crepe/Component.h index eff5a58..47c5c34 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -32,9 +32,13 @@ protected: //! Only ComponentManager can create components friend class ComponentManager; - Component(const Component &) = delete; + // create snapshot + Component(const Component &) = default; + // restore snapshot + virtual Component & operator=(const Component &); + + // components are never moved Component(Component &&) = delete; - virtual Component & operator=(const Component &) = delete; virtual Component & operator=(Component &&) = delete; public: diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 637fbe1..ff428fd 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -13,18 +13,17 @@ using namespace crepe; using namespace std; -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}, - }) { } +LoopManager::LoopManager() { + 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(); +} void LoopManager::start() { this->setup(); @@ -32,16 +31,16 @@ void LoopManager::start() { } void LoopManager::fixed_update() { - for (System & system : this->systems) { - if (!system.active) continue; - system.fixed_update(); + for (auto & [type, 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(); + for (auto & [type, system] : this->systems) { + if (!system->active) continue; + system->frame_update(); } } diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 1183a4d..4b1fc1e 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -73,15 +73,32 @@ private: SaveManager save_manager{mediator}; //! SDL context \todo no more singletons! - SDLContext & sdl_context = SDLContext::get_instance(); + SDLContext & sdl_context = mediator.sdl_context; //! Loop timer \todo no more singletons! LoopTimer & loop_timer = LoopTimer::get_instance(); 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> systems; + /** + * \brief Initialize a system + * \tparam T System type (must be derivative of \c System) + */ + template + 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 */ - std::vector systems; + template + T & get_system(); }; } // namespace crepe diff --git a/src/crepe/api/LoopManager.hpp b/src/crepe/api/LoopManager.hpp index 51afa70..627b281 100644 --- a/src/crepe/api/LoopManager.hpp +++ b/src/crepe/api/LoopManager.hpp @@ -1,5 +1,10 @@ #pragma once +#include +#include +#include + +#include "../system/System.h" #include "LoopManager.h" namespace crepe { @@ -9,4 +14,33 @@ void LoopManager::add_scene() { this->scene_manager.add_scene(); } +template +T & LoopManager::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("LoopManager: {} 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 LoopManager::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("LoopManager: {} is already initialized", type.name())); + System * system = new T(this->mediator); + this->systems[type] = unique_ptr(system); +} + } // namespace crepe diff --git a/src/crepe/manager/ComponentManager.h b/src/crepe/manager/ComponentManager.h index 19a8e81..94fd94f 100644 --- a/src/crepe/manager/ComponentManager.h +++ b/src/crepe/manager/ComponentManager.h @@ -21,13 +21,6 @@ class GameObject; * This class manages all components. It provides methods to add, delete and get components. */ class ComponentManager : public Manager { - // TODO: This relation should be removed! I (loek) believe that the scene manager should - // create/destroy components because the GameObject's are stored in concrete Scene classes, - // which will in turn call GameObject's destructor, which will in turn call - // ComponentManager::delete_components_by_id or something. This is a pretty major change, so - // here is a comment and temporary fix instead :tada: - friend class SceneManager; - public: ComponentManager(Mediator & mediator); ~ComponentManager(); // dbg_trace @@ -49,12 +42,7 @@ public: const vec2 & position = {0, 0}, double rotation = 0, double scale = 1); -protected: - /** - * GameObject is used as an interface to add/remove components, and the game programmer is - * supposed to use it instead of interfacing with the component manager directly. - */ - friend class GameObject; +public: /** * \brief Add a component to the ComponentManager * @@ -154,6 +142,16 @@ public: template RefVector get_components_by_tag(const std::string & tag) const; + struct SnapshotComponent { + by_type>>> components; + Component component; + }; + struct Snapshot { + + }; + Snapshot save(); + void restore(const Snapshot &); + private: /** * \brief Get object IDs by predicate function diff --git a/src/crepe/manager/ReplayManager.h b/src/crepe/manager/ReplayManager.h index 242eae4..c50196c 100644 --- a/src/crepe/manager/ReplayManager.h +++ b/src/crepe/manager/ReplayManager.h @@ -1,6 +1,8 @@ #pragma once #include "Manager.h" +#include "ComponentManager.h" +#include namespace crepe { @@ -18,15 +20,18 @@ protected: void record_frame(); private: - bool recording; - struct Recording { - recording_t id; - std::vector frames; - }; + typedef std::vector Recording; + + bool recording = false; + recording_t current_recording = -1; + + + std::unordered_map> memory; public: void record_start(); recording_t record_end(); - + void play(recording_t handle); + void release(recording_t handle); }; } // namespace crepe diff --git a/src/example/replay.cpp b/src/example/replay.cpp index 6b1ed46..a3c7fba 100644 --- a/src/example/replay.cpp +++ b/src/example/replay.cpp @@ -1,4 +1,4 @@ -#include "util/OptionalRef.h" +#include #include #include #include @@ -13,6 +13,7 @@ #include #include #include +#include using namespace crepe; using namespace std; @@ -22,27 +23,55 @@ class AnimationScript : public Script { float t = 0; void init() { - Log::logf("AnimationScript init"); transform = &get_component(); } void update() { - Log::logf("AnimationScript update"); - t += 0.01; + t += 0.05; transform->position = { sin(t), cos(t) }; } }; +class Timeline : public Script { + unsigned i = 0; + OptionalRef mgr; + recording_t recording; + void update() { + ReplayManager & mgr = this->mgr; + switch (i++) { + default: break; + case 10: + mgr.record_start(); + Log::logf("start"); + break; + case 60: + this->recording = mgr.record_end(); + Log::logf("stop"); + break; + case 70: + mgr.play(this->recording); + Log::logf("play"); + break; + case 71: + mgr.release(this->recording); + Log::logf("end"); + break; + case 72: + Log::logf("exit"); + throw; + break; + }; + } +}; + class TestScene : public Scene { public: using Scene::Scene; void load_scene() { - Log::logf("Initializing scene..."); Mediator & m = this->mediator; ComponentManager & mgr = m.component_manager; - GameObject cam = mgr.new_object("cam"); cam.add_component(ivec2{640,480},vec2{3,3}, Camera::Data{ .bg_color = Color::WHITE, @@ -54,7 +83,9 @@ public: .size = { 0.5, 0.5 }, }); square.add_component().set_script(); - Log::logf("Done initializing scene"); + + GameObject scapegoat = mgr.new_object(""); + scapegoat.add_component().set_script(); } string get_name() const { return "scene1"; } -- cgit v1.2.3 From a2b67f879e2073c5b094c9a480bf637c98703001 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 11 Dec 2024 18:42:38 +0100 Subject: example working again --- src/crepe/api/LoopManager.h | 5 ++++- src/crepe/manager/Mediator.h | 2 ++ src/crepe/manager/ReplayManager.cpp | 17 +++++++++++++++++ src/example/replay.cpp | 22 +++++++++++++--------- 4 files changed, 36 insertions(+), 10 deletions(-) (limited to 'src/crepe/manager') diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 28aa423..f34a5a0 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -2,13 +2,14 @@ #include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" +#include "../manager/ReplayManager.h" +#include "../manager/ResourceManager.h" #include "../manager/ResourceManager.h" #include "../manager/SaveManager.h" #include "../manager/SceneManager.h" #include "../system/System.h" #include "LoopTimer.h" -#include "manager/ResourceManager.h" namespace crepe { @@ -76,6 +77,8 @@ private: SDLContext sdl_context{mediator}; //! LoopTimer instance LoopTimer loop_timer{mediator}; + //! ReplayManager instance + ReplayManager replay_manager{mediator}; private: /** diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index 628154a..eef4432 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -13,6 +13,7 @@ class SaveManager; class ResourceManager; class SDLContext; class LoopTimer; +class ReplayManager; /** * Struct to pass references to classes that would otherwise need to be singletons down to @@ -34,6 +35,7 @@ struct Mediator { OptionalRef event_manager = EventManager::get_instance(); OptionalRef resource_manager; OptionalRef timer; + OptionalRef replay_manager; }; } // namespace crepe diff --git a/src/crepe/manager/ReplayManager.cpp b/src/crepe/manager/ReplayManager.cpp index 3643a8e..40d34a4 100644 --- a/src/crepe/manager/ReplayManager.cpp +++ b/src/crepe/manager/ReplayManager.cpp @@ -4,5 +4,22 @@ using namespace crepe; ReplayManager::ReplayManager(Mediator & mediator) : Manager(mediator) { + mediator.replay_manager = *this; +} + +void ReplayManager::record_start() { + +} + +recording_t ReplayManager::record_end() { + return 0; +} + +void ReplayManager::play(recording_t handle) { + +} + +void ReplayManager::release(recording_t handle) { + } diff --git a/src/example/replay.cpp b/src/example/replay.cpp index 0c4f9b3..67acbd8 100644 --- a/src/example/replay.cpp +++ b/src/example/replay.cpp @@ -33,26 +33,30 @@ class AnimationScript : public Script { class Timeline : public Script { unsigned i = 0; - OptionalRef mgr; + ReplayManager & mgr; recording_t recording; + +public: + Timeline(ReplayManager & mgr) + : mgr(mgr) {} + void update() { - ReplayManager & mgr = this->mgr; switch (i++) { default: break; case 10: - // mgr.record_start(); + mgr.record_start(); Log::logf("start"); break; case 60: - // this->recording = mgr.record_end(); + this->recording = mgr.record_end(); Log::logf("stop"); break; case 70: - // mgr.play(this->recording); + mgr.play(this->recording); Log::logf("play"); break; case 71: - // mgr.release(this->recording); + mgr.release(this->recording); Log::logf("end"); break; case 72: @@ -68,8 +72,8 @@ public: using Scene::Scene; void load_scene() { - Mediator & m = this->mediator; - ComponentManager & mgr = m.component_manager; + Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; GameObject cam = mgr.new_object("cam"); cam.add_component(ivec2{640,480},vec2{3,3}, Camera::Data{ @@ -86,7 +90,7 @@ public: square.add_component().set_script(); GameObject scapegoat = mgr.new_object(""); - scapegoat.add_component().set_script(); + scapegoat.add_component().set_script(mediator.replay_manager); } string get_name() const { return "scene1"; } -- cgit v1.2.3 From 313da47b72aafa0b40ac2cd2b586d525ed17fbd4 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 11 Dec 2024 18:51:58 +0100 Subject: more WIP --- src/crepe/manager/ReplayManager.cpp | 17 ++++++++++++++--- src/example/replay.cpp | 8 +++----- 2 files changed, 17 insertions(+), 8 deletions(-) (limited to 'src/crepe/manager') diff --git a/src/crepe/manager/ReplayManager.cpp b/src/crepe/manager/ReplayManager.cpp index 40d34a4..82c2275 100644 --- a/src/crepe/manager/ReplayManager.cpp +++ b/src/crepe/manager/ReplayManager.cpp @@ -1,25 +1,36 @@ +#include "../util/Log.h" + #include "ReplayManager.h" -#include "manager/Manager.h" +#include "Manager.h" using namespace crepe; +using namespace std; ReplayManager::ReplayManager(Mediator & mediator) : Manager(mediator) { mediator.replay_manager = *this; } void ReplayManager::record_start() { - + if (this->recording) this->release(this->current_recording); + this->current_recording++; + this->recording = true; } recording_t ReplayManager::record_end() { - return 0; + this->recording = false; + return this->current_recording; } void ReplayManager::play(recording_t handle) { + if (!this->memory.contains(handle)) + throw out_of_range(format("ReplayManager: no recording for handle {}", handle)); + Recording & recording = *this->memory.at(handle); + dbg_log("TODO: magic"); } void ReplayManager::release(recording_t handle) { + dbg_log("release"); } diff --git a/src/example/replay.cpp b/src/example/replay.cpp index 67acbd8..2f9c63e 100644 --- a/src/example/replay.cpp +++ b/src/example/replay.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -45,22 +46,17 @@ public: default: break; case 10: mgr.record_start(); - Log::logf("start"); break; case 60: this->recording = mgr.record_end(); - Log::logf("stop"); break; case 70: mgr.play(this->recording); - Log::logf("play"); break; case 71: mgr.release(this->recording); - Log::logf("end"); break; case 72: - Log::logf("exit"); throw; break; }; @@ -97,6 +93,8 @@ public: }; int main(int argc, char * argv[]) { + Config & cfg = Config::get_instance(); + cfg.log.level = Log::Level::DEBUG; LoopManager gameloop; gameloop.add_scene(); -- cgit v1.2.3 From 359ad8db97305856f4cfdade1cd1dada78a7a635 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 11 Dec 2024 21:04:30 +0100 Subject: more replay system WIP --- src/crepe/Component.cpp | 9 +++++ src/crepe/Component.h | 13 ++++--- src/crepe/api/GameObject.cpp | 10 ++--- src/crepe/api/GameObject.h | 13 +++++-- src/crepe/api/LoopManager.cpp | 2 + src/crepe/api/Transform.cpp | 10 +++++ src/crepe/api/Transform.h | 7 ++++ src/crepe/manager/ComponentManager.cpp | 26 +++++++++++++ src/crepe/manager/ComponentManager.h | 9 ++++- src/crepe/manager/Mediator.h | 2 + src/crepe/manager/ReplayManager.cpp | 25 ++++++------ src/crepe/manager/ReplayManager.h | 24 ++++++++---- src/crepe/system/ReplaySystem.cpp | 70 ++++++++++++++++++++++++++++++++++ src/crepe/system/ReplaySystem.h | 11 ++++++ src/test/ECSTest.cpp | 14 +++++++ 15 files changed, 209 insertions(+), 36 deletions(-) (limited to 'src/crepe/manager') diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 141e1a8..8086492 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -1,6 +1,7 @@ #include "Component.h" using namespace crepe; +using namespace std; Component::Component(game_object_id_t id) : game_object_id(id) {} @@ -8,3 +9,11 @@ Component & Component::operator=(const Component &) { return *this; } +unique_ptr Component::save() const { + return unique_ptr(new Component(*this)); +} + +void Component::restore(const Component & snapshot) { + *this = snapshot; +} + diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 47c5c34..fc0268c 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "types.h" namespace crepe { @@ -32,15 +34,16 @@ protected: //! Only ComponentManager can create components friend class ComponentManager; - // create snapshot - Component(const Component &) = default; - // restore snapshot - virtual Component & operator=(const Component &); - // components are never moved Component(Component &&) = delete; virtual Component & operator=(Component &&) = delete; +protected: + virtual std::unique_ptr save() const; + Component(const Component &) = default; + virtual void restore(const Component & snapshot); + virtual Component & operator=(const Component &); + public: virtual ~Component() = default; diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index ea9c425..68b074e 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -11,13 +11,9 @@ GameObject::GameObject(Mediator & mediator, game_object_id_t id, const std::string & name, const std::string & tag, const vec2 & position, double rotation, double scale) : id(id), - mediator(mediator) { - - // Add Transform and Metadata components - ComponentManager & mgr = this->mediator.component_manager; - mgr.add_component(this->id, position, rotation, scale); - mgr.add_component(this->id, name, tag); -} + mediator(mediator), + transform(mediator.component_manager->add_component(this->id, position, rotation, scale)), + metadata(mediator.component_manager->add_component(this->id, name, tag)) { } void GameObject::set_parent(const GameObject & parent) { ComponentManager & mgr = this->mediator.component_manager; diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index a311c21..6203f81 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -7,6 +7,8 @@ namespace crepe { class Mediator; +class Transform; +class Metadata; /** * \brief Represents a GameObject @@ -34,6 +36,13 @@ private: //! ComponentManager instances GameObject friend class ComponentManager; +public: + //! The id of the GameObject + const game_object_id_t id; + + Transform & transform; + Metadata & metadata; + public: /** * \brief Set the parent of this GameObject @@ -68,10 +77,6 @@ public: */ void set_persistent(bool persistent = true); -public: - //! The id of the GameObject - const game_object_id_t id; - protected: Mediator & mediator; }; diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 3511bca..2855455 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -25,6 +25,8 @@ LoopManager::LoopManager() { this->load_system(); this->load_system(); this->load_system(); + + this->mediator.loop_manager = *this; } void LoopManager::start() { diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index a85b792..5fc886b 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -3,6 +3,7 @@ #include "Transform.h" using namespace crepe; +using namespace std; Transform::Transform(game_object_id_t id, const vec2 & point, double rotation, double scale) : Component(id), @@ -11,3 +12,12 @@ Transform::Transform(game_object_id_t id, const vec2 & point, double rotation, d scale(scale) { dbg_trace(); } + +unique_ptr Transform::save() const { + return unique_ptr{new Transform(*this)}; +} + +void Transform::restore(const Component & snapshot) { + *this = static_cast(snapshot); +} + diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 7ee6d65..bbd23e0 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -35,6 +35,13 @@ protected: virtual int get_instances_max() const { return 1; } //! ComponentManager instantiates all components friend class ComponentManager; + +protected: + virtual std::unique_ptr save() const; + Transform(const Transform &) = default; + virtual void restore(const Component & snapshot); + virtual Transform & operator=(const Transform &) = default; + }; } // namespace crepe diff --git a/src/crepe/manager/ComponentManager.cpp b/src/crepe/manager/ComponentManager.cpp index 24ba0d7..5f5c050 100644 --- a/src/crepe/manager/ComponentManager.cpp +++ b/src/crepe/manager/ComponentManager.cpp @@ -72,3 +72,29 @@ set ComponentManager::get_objects_by_tag(const string & tag) c return this->get_objects_by_predicate( [tag](const Metadata & data) { return data.tag == tag; }); } + +ComponentManager::Snapshot ComponentManager::save() { + Snapshot snapshot{}; + for (const auto & [type, by_id_index] : this->components) { + for (game_object_id_t id = 0; id < by_id_index.size(); id++) { + const auto & components = by_id_index[id]; + for (size_t index = 0; index < components.size(); index++) { + const Component & component = *components[index]; + snapshot.components.push_back(SnapshotComponent{ + .type = type, + .id = id, + .index = index, + .component = component.save(), + }); + } + } + } + return snapshot; +} + +void ComponentManager::restore(const Snapshot & snapshot) { + for (const SnapshotComponent & info : snapshot.components) { + this->components[info.type][info.id][info.index]->restore(*info.component); + } +} + diff --git a/src/crepe/manager/ComponentManager.h b/src/crepe/manager/ComponentManager.h index dd7c154..457a196 100644 --- a/src/crepe/manager/ComponentManager.h +++ b/src/crepe/manager/ComponentManager.h @@ -143,10 +143,15 @@ public: RefVector get_components_by_tag(const std::string & tag) const; struct SnapshotComponent { - Component component; + std::type_index type; + game_object_id_t id; + size_t index; + std::unique_ptr component; }; struct Snapshot { - + // TODO: some kind of hash code that ensures components exist in all the same places as + // this snapshot + std::vector components; }; Snapshot save(); void restore(const Snapshot &); diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index eef4432..f5864e7 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -14,6 +14,7 @@ class ResourceManager; class SDLContext; class LoopTimer; class ReplayManager; +class LoopManager; /** * Struct to pass references to classes that would otherwise need to be singletons down to @@ -36,6 +37,7 @@ struct Mediator { OptionalRef resource_manager; OptionalRef timer; OptionalRef replay_manager; + OptionalRef loop_manager; }; } // namespace crepe diff --git a/src/crepe/manager/ReplayManager.cpp b/src/crepe/manager/ReplayManager.cpp index 82c2275..81ff114 100644 --- a/src/crepe/manager/ReplayManager.cpp +++ b/src/crepe/manager/ReplayManager.cpp @@ -1,4 +1,4 @@ -#include "../util/Log.h" +#include #include "ReplayManager.h" #include "Manager.h" @@ -11,26 +11,29 @@ ReplayManager::ReplayManager(Mediator & mediator) : Manager(mediator) { } void ReplayManager::record_start() { - if (this->recording) this->release(this->current_recording); - this->current_recording++; - this->recording = true; + if (this->state == RECORDING) this->release(this->id); + this->id++; + this->memory[this->id] = make_unique(); + this->recording = *this->memory.at(this->id); + this->state = RECORDING; } recording_t ReplayManager::record_end() { - this->recording = false; - return this->current_recording; + this->state = IDLE; + return this->id; } void ReplayManager::play(recording_t handle) { if (!this->memory.contains(handle)) throw out_of_range(format("ReplayManager: no recording for handle {}", handle)); - Recording & recording = *this->memory.at(handle); - - dbg_log("TODO: magic"); + this->recording = *this->memory.at(handle); + this->recording->frame = 0; + this->state = PLAYING; } void ReplayManager::release(recording_t handle) { - dbg_log("release"); - + if (!this->memory.contains(handle)) + return; + this->memory.erase(handle); } diff --git a/src/crepe/manager/ReplayManager.h b/src/crepe/manager/ReplayManager.h index c50196c..672d093 100644 --- a/src/crepe/manager/ReplayManager.h +++ b/src/crepe/manager/ReplayManager.h @@ -1,13 +1,14 @@ #pragma once +#include + #include "Manager.h" #include "ComponentManager.h" -#include +#include "util/OptionalRef.h" namespace crepe { class ReplaySystem; -class Memento; typedef size_t recording_t; @@ -20,11 +21,20 @@ protected: void record_frame(); private: - typedef std::vector Recording; - - bool recording = false; - recording_t current_recording = -1; - + struct Recording { + size_t frame = 0; + std::vector frames; + }; + + enum State { + IDLE, + RECORDING, + PLAYING, + }; + + State state = IDLE; + OptionalRef recording; + recording_t id = -1; std::unordered_map> memory; public: diff --git a/src/crepe/system/ReplaySystem.cpp b/src/crepe/system/ReplaySystem.cpp index 3aabb58..85595a2 100644 --- a/src/crepe/system/ReplaySystem.cpp +++ b/src/crepe/system/ReplaySystem.cpp @@ -1,7 +1,77 @@ +#include "system/ScriptSystem.h" + +#include "../manager/ReplayManager.h" + #include "ReplaySystem.h" +#include "../api/LoopManager.h" + using namespace crepe; +using namespace std; void ReplaySystem::fixed_update() { + ReplayManager & replay = this->mediator.replay_manager; + + switch (replay.state) { + case ReplayManager::IDLE: break; + case ReplayManager::RECORDING: { + this->update_recording(); + break; + } + case ReplayManager::PLAYING: { + this->update_playing(); + break; + } + } + + this->last_state = replay.state; +} + +void ReplaySystem::update_recording() { + ReplayManager & replay = this->mediator.replay_manager; + ComponentManager & components = this->mediator.component_manager; + + ReplayManager::Recording & recording = replay.recording; + recording.frames.push_back(components.save()); + recording.frame++; +} + +void ReplaySystem::update_playing() { + ReplayManager & replay = this->mediator.replay_manager; + + if (this->last_state != ReplayManager::PLAYING) { + this->playback_begin(); + } + + ReplayManager::Recording & recording = replay.recording; + + if (recording.frames.size() == recording.frame) { + this->playback_end(); + return; + } + + ComponentManager & components = this->mediator.component_manager; + ComponentManager::Snapshot & frame = recording.frames.at(recording.frame); + + components.restore(frame); + recording.frame++; +} + +void ReplaySystem::playback_begin() { + LoopManager & loop_manager = this->mediator.loop_manager; + // TODO: store system active state + // TODO: disable most systems + // TODO: store components snapshot +} + +void ReplaySystem::playback_end() { + ReplayManager & replay = this->mediator.replay_manager; + + replay.state = ReplayManager::IDLE; + + LoopManager & loop_manager = this->mediator.loop_manager; + + // TODO: restore system active state snapshot + // TODO: restore components snapshot } diff --git a/src/crepe/system/ReplaySystem.h b/src/crepe/system/ReplaySystem.h index 15ef3fc..fb40176 100644 --- a/src/crepe/system/ReplaySystem.h +++ b/src/crepe/system/ReplaySystem.h @@ -1,5 +1,7 @@ #pragma once +#include "../manager/ReplayManager.h" + #include "System.h" namespace crepe { @@ -9,6 +11,15 @@ public: using System::System; void fixed_update() override; + +private: + ReplayManager::State last_state = ReplayManager::IDLE; + void update_recording(); + void update_playing(); + + std::unordered_map system_active_snapshot; + void playback_begin(); + void playback_end(); }; } diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp index af2b7b0..8f86a91 100644 --- a/src/test/ECSTest.cpp +++ b/src/test/ECSTest.cpp @@ -466,3 +466,17 @@ TEST_F(ECSTest, ComponentsByTag) { EXPECT_EQ(objects.size(), 3); } } + +TEST_F(ECSTest, Snapshot) { + GameObject foo = mgr.new_object("foo"); + + foo.transform.position = {1, 1}; + + ComponentManager::Snapshot snapshot = mgr.save(); + + foo.transform.position = {0, 0}; + + mgr.restore(snapshot); + + EXPECT_EQ(foo.transform.position, (vec2{1, 1})); +} -- cgit v1.2.3 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/api/CMakeLists.txt | 5 +- src/crepe/api/Engine.cpp | 37 ++++++++++++ src/crepe/api/Engine.h | 73 ++++++++++++++++++++++++ src/crepe/api/Engine.hpp | 12 ++++ src/crepe/api/LoopManager.cpp | 75 ------------------------- src/crepe/api/LoopManager.h | 109 ------------------------------------ src/crepe/api/LoopManager.hpp | 46 --------------- src/crepe/api/LoopTimer.h | 2 +- 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 +++++++++++++ src/crepe/system/ReplaySystem.cpp | 11 ++-- src/example/replay.cpp | 9 +-- 15 files changed, 282 insertions(+), 244 deletions(-) create mode 100644 src/crepe/api/Engine.cpp create mode 100644 src/crepe/api/Engine.h create mode 100644 src/crepe/api/Engine.hpp delete mode 100644 src/crepe/api/LoopManager.cpp delete mode 100644 src/crepe/api/LoopManager.h delete mode 100644 src/crepe/api/LoopManager.hpp 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/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/Engine.h b/src/crepe/api/Engine.h new file mode 100644 index 0000000..7601015 --- /dev/null +++ b/src/crepe/api/Engine.h @@ -0,0 +1,73 @@ +#pragma once + +#include "../facade/SDLContext.h" +#include "../manager/ComponentManager.h" +#include "../manager/ReplayManager.h" +#include "../manager/ResourceManager.h" +#include "../manager/ResourceManager.h" +#include "../manager/SaveManager.h" +#include "../manager/SceneManager.h" +#include "../manager/SystemManager.h" + +#include "LoopTimer.h" + +namespace crepe { + +/** + * \brief Main game entrypoint + * + * This class is responsible for managing the game loop, including initialization and updating. + */ +class Engine { +public: + void start(); + + /** + * \brief Add a new concrete scene to the scene manager + * + * \tparam T Type of concrete scene + */ + template + void add_scene(); + +private: + /** + * \brief Setup function for one-time initialization. + * + * This function initializes necessary components for the game. + */ + void setup(); + /** + * \brief Main game loop function. + * + * This function runs the main loop, handling game updates and rendering. + */ + void loop(); + + bool game_running = false; + +private: + //! Global context + Mediator mediator; + + //! Component manager instance + ComponentManager component_manager{mediator}; + //! Scene manager instance + SceneManager scene_manager{mediator}; + //! Resource manager instance + ResourceManager resource_manager{mediator}; + //! Save manager instance + SaveManager save_manager{mediator}; + //! SDLContext instance + SDLContext sdl_context{mediator}; + //! LoopTimer instance + LoopTimer loop_timer{mediator}; + //! ReplayManager instance + ReplayManager replay_manager{mediator}; + //! SystemManager + SystemManager system_manager{mediator}; +}; + +} // namespace crepe + +#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 +void Engine::add_scene() { + this->scene_manager.add_scene(); +} + +} // 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(); - 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.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.h b/src/crepe/api/LoopManager.h deleted file mode 100644 index f34a5a0..0000000 --- a/src/crepe/api/LoopManager.h +++ /dev/null @@ -1,109 +0,0 @@ -#pragma once - -#include "../facade/SDLContext.h" -#include "../manager/ComponentManager.h" -#include "../manager/ReplayManager.h" -#include "../manager/ResourceManager.h" -#include "../manager/ResourceManager.h" -#include "../manager/SaveManager.h" -#include "../manager/SceneManager.h" -#include "../system/System.h" - -#include "LoopTimer.h" - -namespace crepe { - -/** - * \brief Main game loop manager - * - * This class is responsible for managing the game loop, including initialization and updating. - */ -class LoopManager { -public: - void start(); - LoopManager(); - - /** - * \brief Add a new concrete scene to the scene manager - * - * \tparam T Type of concrete scene - */ - template - void add_scene(); - -private: - /** - * \brief Setup function for one-time initialization. - * - * This function initializes necessary components for the game. - */ - void setup(); - /** - * \brief Main game loop function. - * - * This function runs the main loop, handling game updates and rendering. - */ - 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: - //! Global context - Mediator mediator; - - //! Component manager instance - ComponentManager component_manager{mediator}; - //! Scene manager instance - SceneManager scene_manager{mediator}; - //! Resource manager instance - ResourceManager resource_manager{mediator}; - //! Save manager instance - SaveManager save_manager{mediator}; - //! SDLContext instance - SDLContext sdl_context{mediator}; - //! LoopTimer instance - 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> systems; - /** - * \brief Initialize a system - * \tparam T System type (must be derivative of \c System) - */ - template - 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 - T & get_system(); -}; - -} // namespace crepe - -#include "LoopManager.hpp" 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 -#include -#include - -#include "../system/System.h" -#include "LoopManager.h" - -namespace crepe { - -template -void LoopManager::add_scene() { - this->scene_manager.add_scene(); -} - -template -T & LoopManager::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("LoopManager: {} 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 LoopManager::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("LoopManager: {} is already initialized", type.name())); - System * system = new T(this->mediator); - this->systems[type] = unique_ptr(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. 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 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().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().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 #include #include -#include +#include #include #include #include @@ -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(); - gameloop.start(); + Engine engine; + + engine.add_scene(); + engine.start(); return 0; } -- cgit v1.2.3 From b9694e8cc6b85a0089f97ff3c21862adc75a7ee5 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 11 Dec 2024 21:49:32 +0100 Subject: working proof of concept replay system --- src/crepe/manager/SystemManager.cpp | 22 ++++++++++++++++++++++ src/crepe/manager/SystemManager.h | 6 ++++++ src/crepe/system/ReplaySystem.cpp | 33 ++++++++++++++++++++------------- src/crepe/system/ReplaySystem.h | 8 +++++++- src/example/replay.cpp | 20 ++++++++++---------- 5 files changed, 65 insertions(+), 24 deletions(-) (limited to 'src/crepe/manager') diff --git a/src/crepe/manager/SystemManager.cpp b/src/crepe/manager/SystemManager.cpp index 8fd80a7..db7430e 100644 --- a/src/crepe/manager/SystemManager.cpp +++ b/src/crepe/manager/SystemManager.cpp @@ -8,6 +8,7 @@ #include "../system/RenderSystem.h" #include "../system/ScriptSystem.h" #include "../system/EventSystem.h" +#include "../system/ReplaySystem.h" #include "SystemManager.h" @@ -15,6 +16,7 @@ using namespace crepe; using namespace std; SystemManager::SystemManager(Mediator & mediator) : Manager(mediator) { + this->load_system(); this->load_system(); this->load_system(); this->load_system(); @@ -43,3 +45,23 @@ void SystemManager::frame_update() { } } +SystemManager::Snapshot SystemManager::save() { + Snapshot snapshot; + for (auto & [type, system] : this->systems) { + snapshot[type] = system->active; + } + return snapshot; +} + +void SystemManager::restore(const Snapshot & snapshot) { + for (auto & [type, active] : snapshot) { + this->systems[type]->active = active; + } +} + +void SystemManager::disable_all() { + for (auto & [type, system] : this->systems) { + system->active = false; + } +} + diff --git a/src/crepe/manager/SystemManager.h b/src/crepe/manager/SystemManager.h index 5726a5c..6cf7f2b 100644 --- a/src/crepe/manager/SystemManager.h +++ b/src/crepe/manager/SystemManager.h @@ -48,6 +48,12 @@ public: */ template T & get_system(); + +public: + typedef std::unordered_map Snapshot; + Snapshot save(); + void restore(const Snapshot & snapshot); + void disable_all(); }; } // namespace crepe diff --git a/src/crepe/system/ReplaySystem.cpp b/src/crepe/system/ReplaySystem.cpp index 5a90752..a6b8fb1 100644 --- a/src/crepe/system/ReplaySystem.cpp +++ b/src/crepe/system/ReplaySystem.cpp @@ -1,8 +1,8 @@ -#include "ScriptSystem.h" - +#include "../util/Log.h" #include "../manager/ReplayManager.h" #include "../manager/SystemManager.h" +#include "RenderSystem.h" #include "ReplaySystem.h" using namespace crepe; @@ -45,6 +45,7 @@ void ReplaySystem::update_playing() { ReplayManager::Recording & recording = replay.recording; if (recording.frames.size() == recording.frame) { + dbg_log("Finished playback"); this->playback_end(); return; } @@ -52,27 +53,33 @@ void ReplaySystem::update_playing() { ComponentManager & components = this->mediator.component_manager; ComponentManager::Snapshot & frame = recording.frames.at(recording.frame); + dbg_logf("Playing recording frame {}", recording.frame); components.restore(frame); recording.frame++; } void ReplaySystem::playback_begin() { SystemManager & systems = this->mediator.system_manager; - systems.get_system().active = false; - // TODO: store system active state - // TODO: disable most systems - // TODO: store components snapshot -} + ComponentManager & components = this->mediator.component_manager; -void ReplaySystem::playback_end() { - ReplayManager & replay = this->mediator.replay_manager; + this->playback = { + .components = components.save(), + .systems = systems.save(), + }; - replay.state = ReplayManager::IDLE; + systems.disable_all(); + systems.get_system().active = true; + systems.get_system().active = true; +} +void ReplaySystem::playback_end() { SystemManager & systems = this->mediator.system_manager; - systems.get_system().active = true; + ComponentManager & components = this->mediator.component_manager; - // TODO: restore system active state snapshot - // TODO: restore components snapshot + components.restore(this->playback.components); + systems.restore(this->playback.systems); + + ReplayManager & replay = this->mediator.replay_manager; + replay.state = ReplayManager::IDLE; } diff --git a/src/crepe/system/ReplaySystem.h b/src/crepe/system/ReplaySystem.h index fb40176..6f6fce4 100644 --- a/src/crepe/system/ReplaySystem.h +++ b/src/crepe/system/ReplaySystem.h @@ -1,6 +1,7 @@ #pragma once #include "../manager/ReplayManager.h" +#include "../manager/SystemManager.h" #include "System.h" @@ -17,7 +18,12 @@ private: void update_recording(); void update_playing(); - std::unordered_map system_active_snapshot; + struct Snapshot { + ComponentManager::Snapshot components; + SystemManager::Snapshot systems; + }; + Snapshot playback; + void playback_begin(); void playback_end(); }; diff --git a/src/example/replay.cpp b/src/example/replay.cpp index 11413fa..e7f4e6d 100644 --- a/src/example/replay.cpp +++ b/src/example/replay.cpp @@ -45,20 +45,20 @@ public: switch (i++) { default: break; case 10: - mgr.record_start(); - break; + mgr.record_start(); + break; case 60: - this->recording = mgr.record_end(); - break; + this->recording = mgr.record_end(); + break; case 70: - mgr.play(this->recording); - break; + mgr.play(this->recording); + break; case 71: - mgr.release(this->recording); - break; + mgr.release(this->recording); + break; case 72: - throw; - break; + throw; + break; }; } }; -- cgit v1.2.3 From f34daa492c30a0d28b747817a9f2d6fb0186cf80 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 12 Dec 2024 18:46:34 +0100 Subject: clean up ReplayManager API --- src/crepe/api/Script.hpp | 3 +++ src/crepe/manager/ReplayManager.cpp | 28 +++++++++++++++++++++ src/crepe/manager/ReplayManager.h | 33 +++++++++++++------------ src/crepe/system/ReplaySystem.cpp | 49 ++++++------------------------------- 4 files changed, 56 insertions(+), 57 deletions(-) (limited to 'src/crepe/manager') diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index 225a51c..547fb8b 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -1,6 +1,7 @@ #pragma once #include "../manager/ComponentManager.h" +#include "../manager/ReplayManager.h" #include "BehaviorScript.h" #include "Script.h" @@ -36,6 +37,8 @@ void Script::subscribe_internal(const EventHandler & callback, [this, callback](const EventType & data) -> bool { bool & active = this->active; if (!active) return false; + ReplayManager & replay = this->mediator->replay_manager; + if (replay.get_state() == ReplayManager::PLAYING) return false; return callback(data); }, channel); diff --git a/src/crepe/manager/ReplayManager.cpp b/src/crepe/manager/ReplayManager.cpp index 81ff114..ab8a5a0 100644 --- a/src/crepe/manager/ReplayManager.cpp +++ b/src/crepe/manager/ReplayManager.cpp @@ -37,3 +37,31 @@ void ReplayManager::release(recording_t handle) { this->memory.erase(handle); } +void ReplayManager::frame_record() { + ComponentManager & components = this->mediator.component_manager; + Recording & recording = this->recording; + + recording.frames.push_back(components.save()); + recording.frame++; +} + +bool ReplayManager::frame_step() { + ComponentManager & components = this->mediator.component_manager; + Recording & recording = this->recording; + + ComponentManager::Snapshot & frame = recording.frames.at(recording.frame); + + components.restore(frame); + recording.frame++; + + if (recording.frame < recording.frames.size()) return false; + // end of recording + recording.frame = 0; + this->state = IDLE; + return true; +} + +ReplayManager::State ReplayManager::get_state() const { + return this->state; +} + diff --git a/src/crepe/manager/ReplayManager.h b/src/crepe/manager/ReplayManager.h index 672d093..5794761 100644 --- a/src/crepe/manager/ReplayManager.h +++ b/src/crepe/manager/ReplayManager.h @@ -15,33 +15,34 @@ typedef size_t recording_t; class ReplayManager : public Manager { public: ReplayManager(Mediator & mediator); - friend class ReplaySystem; - -protected: - void record_frame(); - -private: - struct Recording { - size_t frame = 0; - std::vector frames; - }; +public: + void record_start(); + recording_t record_end(); + void play(recording_t handle); + void release(recording_t handle); + +public: enum State { IDLE, RECORDING, PLAYING, }; + State get_state() const; +public: + void frame_record(); + bool frame_step(); + +private: + struct Recording { + size_t frame = 0; + std::vector frames; + }; State state = IDLE; OptionalRef recording; recording_t id = -1; - std::unordered_map> memory; -public: - void record_start(); - recording_t record_end(); - void play(recording_t handle); - void release(recording_t handle); }; } // namespace crepe diff --git a/src/crepe/system/ReplaySystem.cpp b/src/crepe/system/ReplaySystem.cpp index a6b8fb1..39b5c14 100644 --- a/src/crepe/system/ReplaySystem.cpp +++ b/src/crepe/system/ReplaySystem.cpp @@ -1,4 +1,3 @@ -#include "../util/Log.h" #include "../manager/ReplayManager.h" #include "../manager/SystemManager.h" @@ -10,52 +9,23 @@ using namespace std; void ReplaySystem::fixed_update() { ReplayManager & replay = this->mediator.replay_manager; + ReplayManager::State state = replay.get_state(); + ReplayManager::State last_state = this->last_state; + this->last_state = state; - switch (replay.state) { + switch (state) { case ReplayManager::IDLE: break; case ReplayManager::RECORDING: { - this->update_recording(); + replay.frame_record(); break; } case ReplayManager::PLAYING: { - this->update_playing(); + if (last_state != ReplayManager::PLAYING) this->playback_begin(); + bool last = replay.frame_step(); + if (last) this->playback_end(); break; } } - - this->last_state = replay.state; -} - -void ReplaySystem::update_recording() { - ReplayManager & replay = this->mediator.replay_manager; - ComponentManager & components = this->mediator.component_manager; - - ReplayManager::Recording & recording = replay.recording; - recording.frames.push_back(components.save()); - recording.frame++; -} - -void ReplaySystem::update_playing() { - ReplayManager & replay = this->mediator.replay_manager; - - if (this->last_state != ReplayManager::PLAYING) { - this->playback_begin(); - } - - ReplayManager::Recording & recording = replay.recording; - - if (recording.frames.size() == recording.frame) { - dbg_log("Finished playback"); - this->playback_end(); - return; - } - - ComponentManager & components = this->mediator.component_manager; - ComponentManager::Snapshot & frame = recording.frames.at(recording.frame); - - dbg_logf("Playing recording frame {}", recording.frame); - components.restore(frame); - recording.frame++; } void ReplaySystem::playback_begin() { @@ -78,8 +48,5 @@ void ReplaySystem::playback_end() { components.restore(this->playback.components); systems.restore(this->playback.systems); - - ReplayManager & replay = this->mediator.replay_manager; - replay.state = ReplayManager::IDLE; } -- cgit v1.2.3 From 45f1f18c78e73280a8a861df2db6237bae6f6d80 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 12 Dec 2024 19:02:40 +0100 Subject: add script api for replaymanager --- src/crepe/Component.cpp | 3 ++- src/crepe/api/Script.cpp | 21 +++++++++++++++++++++ src/crepe/api/Script.h | 18 ++++++++++++++++++ src/crepe/manager/ReplayManager.h | 5 +++++ src/crepe/system/ReplaySystem.h | 2 -- src/example/replay.cpp | 35 ++++++++--------------------------- 6 files changed, 54 insertions(+), 30 deletions(-) (limited to 'src/crepe/manager') diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 8086492..1aed507 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -5,7 +5,8 @@ using namespace std; Component::Component(game_object_id_t id) : game_object_id(id) {} -Component & Component::operator=(const Component &) { +Component & Component::operator=(const Component & other) { + this->active = other.active; return *this; } diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp index 753a9e3..8b95cc9 100644 --- a/src/crepe/api/Script.cpp +++ b/src/crepe/api/Script.cpp @@ -25,3 +25,24 @@ void Script::set_next_scene(const string & name) { } SaveManager & Script::get_save_manager() const { return this->mediator->save_manager; } + +void Script::replay::record_start() { + ReplayManager & mgr = this->mediator->replay_manager; + return mgr.record_start(); +} + +recording_t Script::replay::record_end() { + ReplayManager & mgr = this->mediator->replay_manager; + return mgr.record_end(); +} + +void Script::replay::play(recording_t recording) { + ReplayManager & mgr = this->mediator->replay_manager; + return mgr.play(recording); +} + +void Script::replay::release(recording_t recording) { + ReplayManager & mgr = this->mediator->replay_manager; + return mgr.release(recording); +} + diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 668e5d1..c797db0 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -2,6 +2,7 @@ #include +#include "../manager/ReplayManager.h" #include "../manager/EventManager.h" #include "../manager/Mediator.h" #include "../system/CollisionSystem.h" @@ -135,6 +136,23 @@ protected: //! Retrieve SaveManager reference SaveManager & get_save_manager() const; + //! Replay management functions + struct replay { // NOLINT + //! \copydoc ReplayManager::record_start + void record_start(); + //! \copydoc ReplayManager::record_end + recording_t record_end(); + //! \copydoc ReplayManager::play + void play(recording_t); + //! \copydoc ReplayManager::release + void release(recording_t); + + private: + OptionalRef & mediator; + replay(OptionalRef & mediator) : mediator(mediator) {} + friend class Script; + } replay{mediator}; + //! \} private: diff --git a/src/crepe/manager/ReplayManager.h b/src/crepe/manager/ReplayManager.h index 5794761..7be18f3 100644 --- a/src/crepe/manager/ReplayManager.h +++ b/src/crepe/manager/ReplayManager.h @@ -12,7 +12,12 @@ class ReplaySystem; typedef size_t recording_t; +/** + * \brief Replay manager + * + */ class ReplayManager : public Manager { + // TODO: Delete recordings at end of scene public: ReplayManager(Mediator & mediator); diff --git a/src/crepe/system/ReplaySystem.h b/src/crepe/system/ReplaySystem.h index 6f6fce4..919c554 100644 --- a/src/crepe/system/ReplaySystem.h +++ b/src/crepe/system/ReplaySystem.h @@ -15,8 +15,6 @@ public: private: ReplayManager::State last_state = ReplayManager::IDLE; - void update_recording(); - void update_playing(); struct Snapshot { ComponentManager::Snapshot components; diff --git a/src/example/replay.cpp b/src/example/replay.cpp index e7f4e6d..4c606d7 100644 --- a/src/example/replay.cpp +++ b/src/example/replay.cpp @@ -1,19 +1,6 @@ -#include -#include -#include -#include -#include -#include -#include #include -#include -#include #include -#include -#include -#include -#include -#include +#include using namespace crepe; using namespace std; @@ -34,27 +21,20 @@ class AnimationScript : public Script { class Timeline : public Script { unsigned i = 0; - ReplayManager & mgr; recording_t recording; -public: - Timeline(ReplayManager & mgr) - : mgr(mgr) {} - void update() { switch (i++) { default: break; case 10: - mgr.record_start(); + replay.record_start(); break; case 60: - this->recording = mgr.record_end(); - break; - case 70: - mgr.play(this->recording); + this->recording = replay.record_end(); + replay.play(this->recording); break; - case 71: - mgr.release(this->recording); + case 61: + replay.release(this->recording); break; case 72: throw; @@ -86,7 +66,7 @@ public: square.add_component().set_script(); GameObject scapegoat = mgr.new_object(""); - scapegoat.add_component().set_script(mediator.replay_manager); + scapegoat.add_component().set_script(); } string get_name() const { return "scene1"; } @@ -100,5 +80,6 @@ int main(int argc, char * argv[]) { engine.add_scene(); engine.start(); + return 0; } -- cgit v1.2.3 From 80a80f385874ea109b54454b58e84037aa6092cc Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 12 Dec 2024 19:25:13 +0100 Subject: fix system order in engine class --- src/crepe/manager/SystemManager.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/crepe/manager') diff --git a/src/crepe/manager/SystemManager.cpp b/src/crepe/manager/SystemManager.cpp index db7430e..f67e786 100644 --- a/src/crepe/manager/SystemManager.cpp +++ b/src/crepe/manager/SystemManager.cpp @@ -16,17 +16,17 @@ 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->load_system(); - this->load_system(); - this->load_system(); - this->load_system(); + this->load_system(); this->mediator.system_manager = *this; } -- cgit v1.2.3 From 297d621987c224db26eadfb9bde9235741387496 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 12 Dec 2024 19:46:15 +0100 Subject: more cleanup + script utilities --- src/crepe/api/Animator.cpp | 2 +- src/crepe/api/BehaviorScript.hpp | 3 -- src/crepe/api/Camera.cpp | 2 +- src/crepe/api/Script.h | 78 +++++++++++++++++----------------- src/crepe/api/Script.hpp | 23 ++++++++-- src/crepe/api/Sprite.cpp | 2 +- src/crepe/api/Transform.cpp | 2 +- src/crepe/facade/DB.cpp | 2 +- src/crepe/facade/SDLContext.cpp | 2 +- src/crepe/facade/Sound.cpp | 2 +- src/crepe/facade/SoundContext.cpp | 2 +- src/crepe/facade/Texture.cpp | 2 +- src/crepe/manager/ComponentManager.cpp | 2 +- src/crepe/manager/LoopTimerManager.cpp | 2 +- src/crepe/manager/ResourceManager.cpp | 2 +- src/crepe/system/ScriptSystem.cpp | 1 + src/crepe/util/Log.cpp | 1 + src/crepe/util/Log.h | 21 --------- src/crepe/util/dbg.h | 19 +++++++++ src/example/replay.cpp | 6 ++- 20 files changed, 98 insertions(+), 78 deletions(-) create mode 100644 src/crepe/util/dbg.h (limited to 'src/crepe/manager') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 4ce4bf0..203cef3 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -1,5 +1,5 @@ -#include "util/Log.h" +#include "util/dbg.h" #include "Animator.h" #include "Component.h" diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index b9bb1e2..353d5e2 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -2,8 +2,6 @@ #include -#include "../util/Log.h" - #include "BehaviorScript.h" #include "Script.h" @@ -11,7 +9,6 @@ namespace crepe { template BehaviorScript & BehaviorScript::set_script(Args &&... args) { - dbg_trace(); static_assert(std::is_base_of::value); this->script = std::unique_ptr