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/scene_manager.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/example/scene_manager.cpp (limited to 'src/example/scene_manager.cpp') 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/example/scene_manager.cpp') 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 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/example/scene_manager.cpp') 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 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/example/scene_manager.cpp') 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/example/scene_manager.cpp') 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