From 02c4c796888397496452aaa46c52691eef8707eb Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 16:11:54 +0100 Subject: Added abstract Scene class --- src/crepe/api/Scene.cpp | 5 +++++ src/crepe/api/Scene.h | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/crepe/api/Scene.cpp create mode 100644 src/crepe/api/Scene.h (limited to 'src') diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp new file mode 100644 index 0000000..189650f --- /dev/null +++ b/src/crepe/api/Scene.cpp @@ -0,0 +1,5 @@ +#include "Scene.h" + +using namespace crepe::api; + +Scene::Scene(std::string name) : name(name) {} diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h new file mode 100644 index 0000000..35fe9db --- /dev/null +++ b/src/crepe/api/Scene.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +namespace crepe::api { + +class Scene { +public: + Scene(std::string name); + virtual ~Scene() = default; + virtual void load_scene() = 0; + +public: + std::string name; +}; + +} // namespace crepe::api -- cgit v1.2.3 From d478c8b76d6155bd9653f81787f73db07c55d9f3 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 16:12:10 +0100 Subject: Added SceneManager --- src/crepe/api/CMakeLists.txt | 5 +++++ src/crepe/api/SceneManager.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/crepe/api/SceneManager.h | 36 ++++++++++++++++++++++++++++++++++++ src/crepe/api/SceneManager.hpp | 11 +++++++++++ 4 files changed, 88 insertions(+) create mode 100644 src/crepe/api/SceneManager.cpp create mode 100644 src/crepe/api/SceneManager.h create mode 100644 src/crepe/api/SceneManager.hpp (limited to 'src') diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 0bb1263..7fd8ffd 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -11,6 +11,8 @@ target_sources(crepe PUBLIC Texture.cpp AssetManager.cpp Sprite.cpp + Scene.cpp + SceneManager.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -28,4 +30,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Texture.h AssetManager.h AssetManager.hpp + Scene.h + SceneManager.h + SceneManager.hpp ) diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/api/SceneManager.cpp new file mode 100644 index 0000000..773303b --- /dev/null +++ b/src/crepe/api/SceneManager.cpp @@ -0,0 +1,36 @@ +#include "../ComponentManager.h" + +#include "SceneManager.h" + +using namespace crepe::api; + +SceneManager::SceneManager() {} + +SceneManager & SceneManager::get_instance() { + static SceneManager instance; + return instance; +} + +// Push the next scene onto the queue +void SceneManager::load_scene(std::string name) { + next_scene.push(name); +} + +// Load a new scene from the queue (if there is one) +void SceneManager::empty_queue() { + while (!next_scene.empty()) { + string name = next_scene.front(); + next_scene.pop(); + + for (auto & scene : scenes) { + if (scene->name == name) { + // Delete all components of the current scene + ComponentManager & mgr = ComponentManager::get_instance(); + mgr.delete_all_components(); + // Load the new scene + scene->load_scene(); + break; + } + } + } +} diff --git a/src/crepe/api/SceneManager.h b/src/crepe/api/SceneManager.h new file mode 100644 index 0000000..97441a5 --- /dev/null +++ b/src/crepe/api/SceneManager.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include + +#include "Scene.h" + +namespace crepe::api { + +class SceneManager { +public: + // Singleton + static SceneManager & get_instance(); + SceneManager(const SceneManager &) = delete; + SceneManager(SceneManager &&) = delete; + SceneManager & operator=(const SceneManager &) = delete; + SceneManager & operator=(SceneManager &&) = delete; + +public: + template + void add_scene(std::string name); + void load_scene(std::string name); + void empty_queue(); + +private: + SceneManager(); + +private: + std::vector> scenes; + std::queue next_scene; +}; + +} // namespace crepe::api + +#include "SceneManager.hpp" diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/api/SceneManager.hpp new file mode 100644 index 0000000..120640f --- /dev/null +++ b/src/crepe/api/SceneManager.hpp @@ -0,0 +1,11 @@ +#include "SceneManager.h" + +using namespace crepe::api; +using namespace std; + +// Add a new concrete scene to the scene manager +template +void SceneManager::add_scene(string name) { + static_assert(is_base_of::value, "T must be derived from Scene"); + scenes.emplace_back(make_unique(name)); +} -- cgit v1.2.3 From 6071387c2bbe6d36a95c113ad137e8e2ce80be27 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 16:12:23 +0100 Subject: Added examples for the SceneManager --- src/example/CMakeLists.txt | 1 + src/example/scene_manager.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/example/scene_manager.cpp (limited to 'src') diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 81df8d1..023d0ad 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -25,3 +25,4 @@ add_example(asset_manager) add_example(particle) add_example(physics) add_example(ecs) +add_example(scene_manager) diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp new file mode 100644 index 0000000..417a188 --- /dev/null +++ b/src/example/scene_manager.cpp @@ -0,0 +1,75 @@ +#include + +#include "../crepe/api/SceneManager.h" +#include "../crepe/api/Scene.h" +#include "../crepe/api/GameObject.h" +#include "../crepe/Metadata.h" +#include "../crepe/ComponentManager.h" + +using namespace crepe::api; +using namespace crepe; +using namespace std; + +class concreteScene1 : public Scene { +public: + concreteScene1(string name) : Scene(name) {} + + void load_scene() { + GameObject object1(0, "scene_1", "tag_scene_1", Point{0, 0}, 0, 1); + GameObject object2(1, "scene_1", "tag_scene_1", Point{1, 0}, 0, 1); + GameObject object3(2, "scene_1", "tag_scene_1", Point{2, 0}, 0, 1); + } +}; + +class concreteScene2 : public Scene { +public: + concreteScene2(string name) : Scene(name) {} + + void load_scene() { + GameObject object1(0, "scene_2", "tag_scene_2", Point{0, 0}, 0, 1); + GameObject object2(1, "scene_2", "tag_scene_2", Point{0, 1}, 0, 1); + GameObject object3(2, "scene_2", "tag_scene_2", Point{0, 2}, 0, 1); + GameObject object4(3, "scene_2", "tag_scene_2", Point{0, 3}, 0, 1); + } +}; + +int main() { + SceneManager & scene_mgr = SceneManager::get_instance(); + + // Add the scenes to the scene manager + scene_mgr.add_scene("scene1"); + scene_mgr.add_scene("scene2"); + + // Load scene1 to the queue + scene_mgr.load_scene("scene1"); + // Empty the queue (now scene1 is loaded) + scene_mgr.empty_queue(); + + // Get the Metadata components of each GameObject of Scene1 + ComponentManager & component_mgr = ComponentManager::get_instance(); + vector> metadata = component_mgr.get_components_by_type(); + + cout << "Metadata components of Scene1:" << endl; + // Print the Metadata + for (auto & m : metadata) { + cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name + << " Tag: " << m.get().tag << endl; + } + + // Load scene2 to the queue + scene_mgr.load_scene("scene2"); + // Empty the queue (now scene2 is loaded) + scene_mgr.empty_queue(); + + // Get the Metadata components of each GameObject of Scene2 + metadata = component_mgr.get_components_by_type(); + + cout << "Metadata components of Scene2:" << endl; + // Print the Metadata + for (auto & m : metadata) { + cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name + << " Tag: " << m.get().tag << endl; + } + + return 0; +} -- cgit v1.2.3 From 84116f205b7eefb0f437b8479f7b0384b2ae8314 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 16:35:38 +0100 Subject: Merge --- src/example/scene_manager.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index 417a188..499a727 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -6,7 +6,6 @@ #include "../crepe/Metadata.h" #include "../crepe/ComponentManager.h" -using namespace crepe::api; using namespace crepe; using namespace std; -- cgit v1.2.3 From 9752e2d27b06db25f0e2bc170341901201b4e4c1 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 16:42:08 +0100 Subject: Replaced crepe::api to crepe --- src/crepe/api/Scene.cpp | 2 +- src/crepe/api/Scene.h | 4 ++-- src/crepe/api/SceneManager.cpp | 2 +- src/crepe/api/SceneManager.h | 4 ++-- src/crepe/api/SceneManager.hpp | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp index 189650f..092dc62 100644 --- a/src/crepe/api/Scene.cpp +++ b/src/crepe/api/Scene.cpp @@ -1,5 +1,5 @@ #include "Scene.h" -using namespace crepe::api; +using namespace crepe; Scene::Scene(std::string name) : name(name) {} diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index 35fe9db..8bb487a 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -2,7 +2,7 @@ #include -namespace crepe::api { +namespace crepe { class Scene { public: @@ -14,4 +14,4 @@ public: std::string name; }; -} // namespace crepe::api +} // namespace crepe diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/api/SceneManager.cpp index 773303b..1680dd1 100644 --- a/src/crepe/api/SceneManager.cpp +++ b/src/crepe/api/SceneManager.cpp @@ -2,7 +2,7 @@ #include "SceneManager.h" -using namespace crepe::api; +using namespace crepe; SceneManager::SceneManager() {} diff --git a/src/crepe/api/SceneManager.h b/src/crepe/api/SceneManager.h index 97441a5..76fe92e 100644 --- a/src/crepe/api/SceneManager.h +++ b/src/crepe/api/SceneManager.h @@ -6,7 +6,7 @@ #include "Scene.h" -namespace crepe::api { +namespace crepe { class SceneManager { public: @@ -31,6 +31,6 @@ private: std::queue next_scene; }; -} // namespace crepe::api +} // namespace crepe #include "SceneManager.hpp" diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/api/SceneManager.hpp index 120640f..7789823 100644 --- a/src/crepe/api/SceneManager.hpp +++ b/src/crepe/api/SceneManager.hpp @@ -1,6 +1,6 @@ #include "SceneManager.h" -using namespace crepe::api; +using namespace crepe; using namespace std; // Add a new concrete scene to the scene manager -- cgit v1.2.3 From bf4c172f2709adf5a6f210bae60e16972e8decad Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 16:42:57 +0100 Subject: Make format --- src/crepe/api/SceneManager.cpp | 4 +--- src/crepe/api/SceneManager.h | 2 +- src/example/scene_manager.cpp | 15 ++++++++------- 3 files changed, 10 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/api/SceneManager.cpp index 1680dd1..c8061c9 100644 --- a/src/crepe/api/SceneManager.cpp +++ b/src/crepe/api/SceneManager.cpp @@ -12,9 +12,7 @@ SceneManager & SceneManager::get_instance() { } // Push the next scene onto the queue -void SceneManager::load_scene(std::string name) { - next_scene.push(name); -} +void SceneManager::load_scene(std::string name) { next_scene.push(name); } // Load a new scene from the queue (if there is one) void SceneManager::empty_queue() { diff --git a/src/crepe/api/SceneManager.h b/src/crepe/api/SceneManager.h index 76fe92e..87db8d7 100644 --- a/src/crepe/api/SceneManager.h +++ b/src/crepe/api/SceneManager.h @@ -1,8 +1,8 @@ #pragma once -#include #include #include +#include #include "Scene.h" diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index 499a727..f1fe86a 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -1,10 +1,10 @@ #include -#include "../crepe/api/SceneManager.h" -#include "../crepe/api/Scene.h" -#include "../crepe/api/GameObject.h" -#include "../crepe/Metadata.h" #include "../crepe/ComponentManager.h" +#include "../crepe/Metadata.h" +#include "../crepe/api/GameObject.h" +#include "../crepe/api/Scene.h" +#include "../crepe/api/SceneManager.h" using namespace crepe; using namespace std; @@ -46,8 +46,9 @@ int main() { // Get the Metadata components of each GameObject of Scene1 ComponentManager & component_mgr = ComponentManager::get_instance(); - vector> metadata = component_mgr.get_components_by_type(); - + vector> metadata + = component_mgr.get_components_by_type(); + cout << "Metadata components of Scene1:" << endl; // Print the Metadata for (auto & m : metadata) { @@ -62,7 +63,7 @@ int main() { // Get the Metadata components of each GameObject of Scene2 metadata = component_mgr.get_components_by_type(); - + cout << "Metadata components of Scene2:" << endl; // Print the Metadata for (auto & m : metadata) { -- cgit v1.2.3 From 452944cd6dc666a58f5f8441b6cde0aaf8cc48aa Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 09:56:59 +0100 Subject: Make format --- src/crepe/Component.cpp | 1 - src/crepe/Metadata.cpp | 4 ++-- src/crepe/Metadata.h | 3 ++- src/crepe/api/GameObject.cpp | 11 +++++++---- src/crepe/api/GameObject.h | 3 ++- src/crepe/api/Transform.cpp | 4 ++-- 6 files changed, 15 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 41e7273..358ce31 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -3,4 +3,3 @@ using namespace crepe; Component::Component(uint32_t id) : game_object_id(id), active(true) {} - diff --git a/src/crepe/Metadata.cpp b/src/crepe/Metadata.cpp index 1ba150a..53d93da 100644 --- a/src/crepe/Metadata.cpp +++ b/src/crepe/Metadata.cpp @@ -3,6 +3,6 @@ using namespace crepe; using namespace std; -Metadata::Metadata(uint32_t game_object_id, const string & name, const string & tag) +Metadata::Metadata(uint32_t game_object_id, const string & name, + const string & tag) : Component(game_object_id), name(name), tag(tag) {} - diff --git a/src/crepe/Metadata.h b/src/crepe/Metadata.h index b946fd0..1577987 100644 --- a/src/crepe/Metadata.h +++ b/src/crepe/Metadata.h @@ -9,7 +9,8 @@ namespace crepe { class Metadata : public Component { public: - Metadata(uint32_t game_object_id, const std::string & name, const std::string & tag); + Metadata(uint32_t game_object_id, const std::string & name, + const std::string & tag); virtual int get_instances_max() const { return 1; } public: diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 8a1a235..51cd08f 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -6,7 +6,9 @@ using namespace crepe; using namespace std; -GameObject::GameObject(uint32_t id, std::string name, std::string tag, const Point & position, double rotation, double scale) : id(id) { +GameObject::GameObject(uint32_t id, std::string name, std::string tag, + const Point & position, double rotation, double scale) + : id(id) { ComponentManager & mgr = ComponentManager::get_instance(); mgr.add_component(this->id, position, rotation, scale); mgr.add_component(this->id, name, tag); @@ -16,11 +18,12 @@ void GameObject::set_parent(const GameObject & parent) { auto & mgr = ComponentManager::get_instance(); // set parent on own Metadata component - vector> this_metadata = mgr.get_components_by_id(this->id); + vector> this_metadata + = mgr.get_components_by_id(this->id); this_metadata.at(0).get().parent = parent.id; // add own id to children list of parent's Metadata component - vector> parent_metadata = mgr.get_components_by_id(parent.id); + vector> parent_metadata + = mgr.get_components_by_id(parent.id); parent_metadata.at(0).get().children.push_back(this->id); } - diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index 4c87639..602f33c 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -9,7 +9,8 @@ namespace crepe { class GameObject { public: - GameObject(uint32_t id, std::string name, std::string tag, const Point & position, double rotation, double scale); + GameObject(uint32_t id, std::string name, std::string tag, + const Point & position, double rotation, double scale); void set_parent(const GameObject & parent); template diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index 9c9bb06..1d8d401 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -8,10 +8,10 @@ using namespace crepe; -Transform::Transform(uint32_t game_id, const Point & point, double rot, double scale) +Transform::Transform(uint32_t game_id, const Point & point, double rot, + double scale) : Component(game_id), position(point), rotation(rot), scale(scale) { dbg_trace(); } Transform::~Transform() { dbg_trace(); } - -- cgit v1.2.3 From 2fce3e3985302544827f6bf4dcc3a46cae3f2fd1 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 10:06:15 +0100 Subject: Used const references instead of pass by value --- src/crepe/api/Scene.cpp | 2 +- src/crepe/api/Scene.h | 2 +- src/crepe/api/SceneManager.cpp | 3 ++- src/crepe/api/SceneManager.h | 4 ++-- src/crepe/api/SceneManager.hpp | 9 +++++---- 5 files changed, 11 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp index 092dc62..933edf4 100644 --- a/src/crepe/api/Scene.cpp +++ b/src/crepe/api/Scene.cpp @@ -2,4 +2,4 @@ using namespace crepe; -Scene::Scene(std::string name) : name(name) {} +Scene::Scene(const std::string & name) : name(name) {} diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index 8bb487a..f8bcc3d 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -6,7 +6,7 @@ namespace crepe { class Scene { public: - Scene(std::string name); + Scene(const std::string & name); virtual ~Scene() = default; virtual void load_scene() = 0; diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/api/SceneManager.cpp index c8061c9..6fc8f9b 100644 --- a/src/crepe/api/SceneManager.cpp +++ b/src/crepe/api/SceneManager.cpp @@ -3,6 +3,7 @@ #include "SceneManager.h" using namespace crepe; +using namespace std; SceneManager::SceneManager() {} @@ -12,7 +13,7 @@ SceneManager & SceneManager::get_instance() { } // Push the next scene onto the queue -void SceneManager::load_scene(std::string name) { next_scene.push(name); } +void SceneManager::load_scene(const std::string & name) { next_scene.push(name); } // Load a new scene from the queue (if there is one) void SceneManager::empty_queue() { diff --git a/src/crepe/api/SceneManager.h b/src/crepe/api/SceneManager.h index 87db8d7..b7d5150 100644 --- a/src/crepe/api/SceneManager.h +++ b/src/crepe/api/SceneManager.h @@ -19,8 +19,8 @@ public: public: template - void add_scene(std::string name); - void load_scene(std::string name); + void add_scene(const std::string & name); + void load_scene(const std::string & name); void empty_queue(); private: diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/api/SceneManager.hpp index 7789823..82787ed 100644 --- a/src/crepe/api/SceneManager.hpp +++ b/src/crepe/api/SceneManager.hpp @@ -1,11 +1,12 @@ #include "SceneManager.h" -using namespace crepe; -using namespace std; +namespace crepe { // Add a new concrete scene to the scene manager template -void SceneManager::add_scene(string name) { - static_assert(is_base_of::value, "T must be derived from Scene"); +void SceneManager::add_scene(const std::string & name) { + static_assert(std::is_base_of::value, "T must be derived from Scene"); scenes.emplace_back(make_unique(name)); } + +} // namespace crepe -- cgit v1.2.3 From cd7f08320990f1a37e2d012b7e3ae3761c5a7267 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 10:21:39 +0100 Subject: Renamed methods and replaced the queue with a simple string --- src/crepe/api/SceneManager.cpp | 17 ++++++++--------- src/crepe/api/SceneManager.h | 6 +++--- src/crepe/api/SceneManager.hpp | 8 +++++++- src/example/scene_manager.cpp | 8 ++++---- 4 files changed, 22 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/api/SceneManager.cpp index 6fc8f9b..08f036d 100644 --- a/src/crepe/api/SceneManager.cpp +++ b/src/crepe/api/SceneManager.cpp @@ -12,17 +12,16 @@ SceneManager & SceneManager::get_instance() { return instance; } -// Push the next scene onto the queue -void SceneManager::load_scene(const std::string & name) { next_scene.push(name); } - -// Load a new scene from the queue (if there is one) -void SceneManager::empty_queue() { - while (!next_scene.empty()) { - string name = next_scene.front(); - next_scene.pop(); +// Set the next scene (this scene will be loaded at the end of the frame) +void SceneManager::set_next_scene(const std::string & name) { + next_scene = name; +} +// Load a new scene (if there is one) +void SceneManager::load_next_scene() { + if (!next_scene.empty()) { for (auto & scene : scenes) { - if (scene->name == name) { + if (scene->name == next_scene) { // Delete all components of the current scene ComponentManager & mgr = ComponentManager::get_instance(); mgr.delete_all_components(); diff --git a/src/crepe/api/SceneManager.h b/src/crepe/api/SceneManager.h index b7d5150..eaf1ba1 100644 --- a/src/crepe/api/SceneManager.h +++ b/src/crepe/api/SceneManager.h @@ -20,15 +20,15 @@ public: public: template void add_scene(const std::string & name); - void load_scene(const std::string & name); - void empty_queue(); + void set_next_scene(const std::string & name); + void load_next_scene(); private: SceneManager(); private: std::vector> scenes; - std::queue next_scene; + std::string next_scene; }; } // namespace crepe diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/api/SceneManager.hpp index 82787ed..e2ba2da 100644 --- a/src/crepe/api/SceneManager.hpp +++ b/src/crepe/api/SceneManager.hpp @@ -5,8 +5,14 @@ namespace crepe { // Add a new concrete scene to the scene manager template void SceneManager::add_scene(const std::string & name) { - static_assert(std::is_base_of::value, "T must be derived from Scene"); + static_assert(std::is_base_of::value, + "T must be derived from Scene"); scenes.emplace_back(make_unique(name)); + + // The first scene added, is the one that will be loaded at the beginning + if (next_scene.empty()) { + next_scene = name; + } } } // namespace crepe diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index f1fe86a..9d832eb 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -40,9 +40,9 @@ int main() { scene_mgr.add_scene("scene2"); // Load scene1 to the queue - scene_mgr.load_scene("scene1"); + scene_mgr.set_next_scene("scene1"); // Empty the queue (now scene1 is loaded) - scene_mgr.empty_queue(); + scene_mgr.load_next_scene(); // Get the Metadata components of each GameObject of Scene1 ComponentManager & component_mgr = ComponentManager::get_instance(); @@ -57,9 +57,9 @@ int main() { } // Load scene2 to the queue - scene_mgr.load_scene("scene2"); + scene_mgr.set_next_scene("scene2"); // Empty the queue (now scene2 is loaded) - scene_mgr.empty_queue(); + scene_mgr.load_next_scene(); // Get the Metadata components of each GameObject of Scene2 metadata = component_mgr.get_components_by_type(); -- cgit v1.2.3 From c40c89f0c1f72e67b4036792b4b2a9b28eb73e9a Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 10:24:51 +0100 Subject: Improved test --- src/example/scene_manager.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index 9d832eb..2fcdbec 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -39,9 +39,8 @@ int main() { scene_mgr.add_scene("scene1"); scene_mgr.add_scene("scene2"); - // Load scene1 to the queue - scene_mgr.set_next_scene("scene1"); - // Empty the queue (now scene1 is loaded) + // There is no need to call set_next_scene() at the beginnen, because the first scene will be automatically set as the next scene + // Load scene1 (the first scene added) scene_mgr.load_next_scene(); // Get the Metadata components of each GameObject of Scene1 @@ -56,9 +55,9 @@ int main() { << " Tag: " << m.get().tag << endl; } - // Load scene2 to the queue + // Set scene2 as the next scene scene_mgr.set_next_scene("scene2"); - // Empty the queue (now scene2 is loaded) + // Load scene2 scene_mgr.load_next_scene(); // Get the Metadata components of each GameObject of Scene2 -- cgit v1.2.3 From 3a5201961ce31d415042c6273d03e46aed7e6eb8 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 6 Nov 2024 18:38:45 +0100 Subject: fix code standard and merge #19 --- src/crepe/api/SceneManager.cpp | 41 +++++++++++++++++++++++++---------------- src/crepe/api/SceneManager.h | 16 +++++++++++++++- src/crepe/api/SceneManager.hpp | 2 +- 3 files changed, 41 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/api/SceneManager.cpp index 08f036d..57ec302 100644 --- a/src/crepe/api/SceneManager.cpp +++ b/src/crepe/api/SceneManager.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "../ComponentManager.h" #include "SceneManager.h" @@ -5,30 +8,36 @@ using namespace crepe; using namespace std; -SceneManager::SceneManager() {} - SceneManager & SceneManager::get_instance() { static SceneManager instance; return instance; } -// Set the next scene (this scene will be loaded at the end of the frame) -void SceneManager::set_next_scene(const std::string & name) { +void SceneManager::set_next_scene(const string & name) { next_scene = name; } -// Load a new scene (if there is one) void SceneManager::load_next_scene() { - if (!next_scene.empty()) { - for (auto & scene : scenes) { - if (scene->name == next_scene) { - // Delete all components of the current scene - ComponentManager & mgr = ComponentManager::get_instance(); - mgr.delete_all_components(); - // Load the new scene - scene->load_scene(); - break; - } + // next scene not set + if (this->next_scene.empty()) + return; + + auto it = find_if(this->scenes.begin(), this->scenes.end(), + [&next_scene = this->next_scene] (unique_ptr & scene) { + return scene->name == next_scene; } - } + ); + + // next scene not found + if (it == this->scenes.end()) + return; + unique_ptr & scene = *it; + + // Delete all components of the current scene + ComponentManager & mgr = ComponentManager::get_instance(); + mgr.delete_all_components(); + + // Load the new scene + scene->load_scene(); } + diff --git a/src/crepe/api/SceneManager.h b/src/crepe/api/SceneManager.h index eaf1ba1..1e0e670 100644 --- a/src/crepe/api/SceneManager.h +++ b/src/crepe/api/SceneManager.h @@ -18,13 +18,27 @@ public: SceneManager & operator=(SceneManager &&) = delete; public: + /** + * \brief Add a new concrete scene to the scene manager + * + * \tparam T Type of concrete scene + * \param name Name of new scene + */ template void add_scene(const std::string & name); + /** + * \brief Set the next scene + * + * This scene will be loaded at the end of the frame + * + * \param name Name of the next scene + */ void set_next_scene(const std::string & name); + //! Load a new scene (if there is one) void load_next_scene(); private: - SceneManager(); + SceneManager() = default; private: std::vector> scenes; diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/api/SceneManager.hpp index e2ba2da..8bad7b2 100644 --- a/src/crepe/api/SceneManager.hpp +++ b/src/crepe/api/SceneManager.hpp @@ -2,11 +2,11 @@ namespace crepe { -// Add a new concrete scene to the scene manager template void SceneManager::add_scene(const std::string & name) { static_assert(std::is_base_of::value, "T must be derived from Scene"); + scenes.emplace_back(make_unique(name)); // The first scene added, is the one that will be loaded at the beginning -- cgit v1.2.3