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/SceneManager.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/crepe/api/SceneManager.h (limited to 'src/crepe/api/SceneManager.h') 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" -- 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/crepe/api/SceneManager.h') 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/crepe/api/SceneManager.h') 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 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/crepe/api/SceneManager.h') 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/crepe/api/SceneManager.h') 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 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/crepe/api/SceneManager.h') 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