From fac06d488e635583307cd4a916a611500fb64c16 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 13 Nov 2024 13:10:25 +0100 Subject: cleanup + fix examples --- src/example/scene_manager.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/example/scene_manager.cpp') diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index f46dc36..5cd7336 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -34,7 +34,8 @@ public: }; int main() { - SceneManager & scene_mgr = SceneManager::get_instance(); + ComponentManager component_mgr{}; + SceneManager scene_mgr{component_mgr}; // Add the scenes to the scene manager scene_mgr.add_scene("scene1"); @@ -45,7 +46,6 @@ int main() { scene_mgr.load_next_scene(); // Get the Metadata components of each GameObject of Scene1 - ComponentManager & component_mgr = ComponentManager::get_instance(); vector> metadata = component_mgr.get_components_by_type(); -- cgit v1.2.3 From 0aa97807cb9b7f1d1850449dd5a5e45b961445ce Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 13 Nov 2024 18:46:06 +0100 Subject: fix scene_manager example --- src/crepe/ComponentManager.cpp | 8 +++----- src/crepe/ComponentManager.h | 2 +- src/crepe/api/Scene.cpp | 2 +- src/crepe/api/Scene.h | 10 ++++++++-- src/crepe/api/SceneManager.hpp | 9 ++++++--- src/example/components_internal.cpp | 2 +- src/example/ecs.cpp | 15 +++++---------- src/example/physics.cpp | 3 +-- src/example/rendering.cpp | 6 +++--- src/example/scene_manager.cpp | 25 +++++++++++++++---------- src/example/script.cpp | 2 +- 11 files changed, 45 insertions(+), 39 deletions(-) (limited to 'src/example/scene_manager.cpp') diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index c6b658c..1e23609 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -25,13 +25,11 @@ void ComponentManager::delete_all_components() { ComponentManager::ComponentManager() { dbg_trace(); } ComponentManager::~ComponentManager() { dbg_trace(); } -GameObject & ComponentManager::new_object(const string & name, +GameObject ComponentManager::new_object(const string & name, const string & tag, const Vector2 & position, double rotation, double scale) { - GameObject * object = new GameObject(*this, this->next_id, name, tag, - position, rotation, scale); - this->objects.push_front(unique_ptr(object)); + GameObject object{*this, this->next_id, name, tag, position, rotation, scale}; this->next_id++; - return *object; + return object; } diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 75606e0..31a8bfa 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -101,7 +101,7 @@ public: std::vector> get_components_by_type() const; // TODO: doxygen - GameObject & new_object(const std::string & name, + GameObject new_object(const std::string & name, const std::string & tag = "", const Vector2 & position = {0, 0}, double rotation = 0, double scale = 0); diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp index 933edf4..008d689 100644 --- a/src/crepe/api/Scene.cpp +++ b/src/crepe/api/Scene.cpp @@ -2,4 +2,4 @@ using namespace crepe; -Scene::Scene(const std::string & name) : name(name) {} +Scene::Scene(ComponentManager & mgr, const std::string & name) : component_manager(mgr), name(name) {} diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index f8bcc3d..334f306 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -4,14 +4,20 @@ namespace crepe { +class ComponentManager; + class Scene { public: - Scene(const std::string & name); + Scene(ComponentManager & mgr, const std::string & name); virtual ~Scene() = default; + virtual void load_scene() = 0; public: - std::string name; + const std::string name; + +protected: + ComponentManager & component_manager; }; } // namespace crepe diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/api/SceneManager.hpp index 8bad7b2..714f690 100644 --- a/src/crepe/api/SceneManager.hpp +++ b/src/crepe/api/SceneManager.hpp @@ -1,13 +1,16 @@ +#pragma once + #include "SceneManager.h" namespace crepe { template void SceneManager::add_scene(const std::string & name) { - static_assert(std::is_base_of::value, - "T must be derived from Scene"); + using namespace std; + static_assert(is_base_of::value, "T must be derived from Scene"); - scenes.emplace_back(make_unique(name)); + Scene * scene = new T(this->component_manager, name); + this->scenes.emplace_back(unique_ptr(scene)); // The first scene added, is the one that will be loaded at the beginning if (next_scene.empty()) { diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp index 7a8a08a..eeecdc0 100644 --- a/src/example/components_internal.cpp +++ b/src/example/components_internal.cpp @@ -30,7 +30,7 @@ int main() { GameObject * game_object[OBJ_COUNT]; for (int i = 0; i < OBJ_COUNT; ++i) { - GameObject & obj = mgr.new_object("Name", "Tag"); + GameObject obj = mgr.new_object("Name", "Tag"); obj.add_component("test"); obj.add_component(0, 0, i); } diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index 9a008ea..06fc563 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -13,16 +13,11 @@ int main() { // Create a few GameObjects try { - GameObject & body - = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); - GameObject & right_leg - = mgr.new_object("rightLeg", "person", Vector2{1, 1}, 0, 1); - GameObject & left_leg - = mgr.new_object("leftLeg", "person", Vector2{1, 1}, 0, 1); - GameObject & right_foot - = mgr.new_object("rightFoot", "person", Vector2{2, 2}, 0, 1); - GameObject & left_foot - = mgr.new_object("leftFoot", "person", Vector2{2, 2}, 0, 1); + GameObject body = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + GameObject right_leg = mgr.new_object("rightLeg", "person", Vector2{1, 1}, 0, 1); + GameObject left_leg = mgr.new_object("leftLeg", "person", Vector2{1, 1}, 0, 1); + GameObject right_foot = mgr.new_object("rightFoot", "person", Vector2{2, 2}, 0, 1); + GameObject left_foot = mgr.new_object("leftFoot", "person", Vector2{2, 2}, 0, 1); // Set the parent of each GameObject right_foot.set_parent(right_leg); diff --git a/src/example/physics.cpp b/src/example/physics.cpp index 2ebf779..ad663a0 100644 --- a/src/example/physics.cpp +++ b/src/example/physics.cpp @@ -11,8 +11,7 @@ using namespace std; int main(int argc, char * argv[]) { ComponentManager mgr{}; - GameObject & game_object - = mgr.new_object("Name", "Tag", Vector2{0, 0}, 0, 0); + GameObject game_object = mgr.new_object("Name", "Tag", Vector2{0, 0}, 0, 0); game_object.add_component(Rigidbody::Data{ .mass = 1, .gravity_scale = 1, diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 74c00e2..abd11b1 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -23,9 +23,9 @@ int main() { ComponentManager mgr{}; RenderSystem sys{mgr}; - auto & obj = mgr.new_object("name", "tag", Vector2{0, 0}, 1, 1); - auto & obj1 = mgr.new_object("name", "tag", Vector2{500, 0}, 1, 0.1); - auto & obj2 = mgr.new_object("name", "tag", Vector2{800, 0}, 1, 0.1); + GameObject obj = mgr.new_object("name", "tag", Vector2{0, 0}, 1, 1); + GameObject obj1 = mgr.new_object("name", "tag", Vector2{500, 0}, 1, 0.1); + GameObject obj2 = mgr.new_object("name", "tag", Vector2{800, 0}, 1, 0.1); // Normal adding components { diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index 5cd7336..1c982aa 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -12,24 +12,26 @@ using namespace std; class ConcreteScene1 : public Scene { public: - ConcreteScene1(string name) : Scene(name) {} + using Scene::Scene; void load_scene() { - GameObject object1(0, "scene_1", "tag_scene_1", Vector2{0, 0}, 0, 1); - GameObject object2(1, "scene_1", "tag_scene_1", Vector2{1, 0}, 0, 1); - GameObject object3(2, "scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1); + auto & mgr = this->component_manager; + GameObject object1 = mgr.new_object("scene_1", "tag_scene_1", Vector2{0, 0}, 0, 1); + GameObject object2 = mgr.new_object("scene_1", "tag_scene_1", Vector2{1, 0}, 0, 1); + GameObject object3 = mgr.new_object("scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1); } }; class ConcreteScene2 : public Scene { public: - ConcreteScene2(string name) : Scene(name) {} + using Scene::Scene; void load_scene() { - GameObject object1(0, "scene_2", "tag_scene_2", Vector2{0, 0}, 0, 1); - GameObject object2(1, "scene_2", "tag_scene_2", Vector2{0, 1}, 0, 1); - GameObject object3(2, "scene_2", "tag_scene_2", Vector2{0, 2}, 0, 1); - GameObject object4(3, "scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1); + auto & mgr = this->component_manager; + GameObject object1 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 0}, 0, 1); + GameObject object2 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 1}, 0, 1); + GameObject object3 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 2}, 0, 1); + GameObject object4 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1); } }; @@ -41,7 +43,9 @@ int main() { scene_mgr.add_scene("scene1"); scene_mgr.add_scene("scene2"); - // There is no need to call set_next_scene() at the beginnen, because the first scene will be automatically set as the next scene + // There is no need to call set_next_scene() at the beginning because the + // first scene will be automatically set as the next scene + // Load scene1 (the first scene added) scene_mgr.load_next_scene(); @@ -73,3 +77,4 @@ int main() { return 0; } + diff --git a/src/example/script.cpp b/src/example/script.cpp index c82764c..a23295b 100644 --- a/src/example/script.cpp +++ b/src/example/script.cpp @@ -39,7 +39,7 @@ int main() { ScriptSystem system{component_manager}; // Create game object with Transform and BehaviorScript components - auto & obj = component_manager.new_object("name"); + GameObject obj = component_manager.new_object("name"); obj.add_component().set_script(); // Update all scripts. This should result in MyScript::update being called -- cgit v1.2.3 From d69f1642cb397d68d591718f83028e14620ab340 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 13 Nov 2024 19:22:23 +0100 Subject: `make format` --- src/crepe/ComponentManager.cpp | 10 +++++----- src/crepe/ComponentManager.h | 6 +++--- src/crepe/api/Scene.cpp | 4 +++- src/example/ecs.cpp | 12 ++++++++---- src/example/scene_manager.cpp | 22 ++++++++++++++-------- 5 files changed, 33 insertions(+), 21 deletions(-) (limited to 'src/example/scene_manager.cpp') diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 1e23609..7af0380 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -25,11 +25,11 @@ void ComponentManager::delete_all_components() { ComponentManager::ComponentManager() { dbg_trace(); } ComponentManager::~ComponentManager() { dbg_trace(); } -GameObject ComponentManager::new_object(const string & name, - const string & tag, - const Vector2 & position, - double rotation, double scale) { - GameObject object{*this, this->next_id, name, tag, position, rotation, scale}; +GameObject ComponentManager::new_object(const string & name, const string & tag, + const Vector2 & position, + double rotation, double scale) { + GameObject object{*this, this->next_id, name, tag, + position, rotation, scale}; this->next_id++; return object; } diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 31a8bfa..51c84a4 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -102,9 +102,9 @@ public: // TODO: doxygen GameObject new_object(const std::string & name, - const std::string & tag = "", - const Vector2 & position = {0, 0}, - double rotation = 0, double scale = 0); + const std::string & tag = "", + const Vector2 & position = {0, 0}, + double rotation = 0, double scale = 0); private: /** diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp index 008d689..88aa82d 100644 --- a/src/crepe/api/Scene.cpp +++ b/src/crepe/api/Scene.cpp @@ -2,4 +2,6 @@ using namespace crepe; -Scene::Scene(ComponentManager & mgr, const std::string & name) : component_manager(mgr), name(name) {} +Scene::Scene(ComponentManager & mgr, const std::string & name) + : component_manager(mgr), + name(name) {} diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index 06fc563..5f83da1 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -14,10 +14,14 @@ int main() { // Create a few GameObjects try { GameObject body = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); - GameObject right_leg = mgr.new_object("rightLeg", "person", Vector2{1, 1}, 0, 1); - GameObject left_leg = mgr.new_object("leftLeg", "person", Vector2{1, 1}, 0, 1); - GameObject right_foot = mgr.new_object("rightFoot", "person", Vector2{2, 2}, 0, 1); - GameObject left_foot = mgr.new_object("leftFoot", "person", Vector2{2, 2}, 0, 1); + GameObject right_leg + = mgr.new_object("rightLeg", "person", Vector2{1, 1}, 0, 1); + GameObject left_leg + = mgr.new_object("leftLeg", "person", Vector2{1, 1}, 0, 1); + GameObject right_foot + = mgr.new_object("rightFoot", "person", Vector2{2, 2}, 0, 1); + GameObject left_foot + = mgr.new_object("leftFoot", "person", Vector2{2, 2}, 0, 1); // Set the parent of each GameObject right_foot.set_parent(right_leg); diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index 1c982aa..24ab72e 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -16,9 +16,12 @@ public: void load_scene() { auto & mgr = this->component_manager; - GameObject object1 = mgr.new_object("scene_1", "tag_scene_1", Vector2{0, 0}, 0, 1); - GameObject object2 = mgr.new_object("scene_1", "tag_scene_1", Vector2{1, 0}, 0, 1); - GameObject object3 = mgr.new_object("scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1); + GameObject object1 + = mgr.new_object("scene_1", "tag_scene_1", Vector2{0, 0}, 0, 1); + GameObject object2 + = mgr.new_object("scene_1", "tag_scene_1", Vector2{1, 0}, 0, 1); + GameObject object3 + = mgr.new_object("scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1); } }; @@ -28,10 +31,14 @@ public: void load_scene() { auto & mgr = this->component_manager; - GameObject object1 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 0}, 0, 1); - GameObject object2 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 1}, 0, 1); - GameObject object3 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 2}, 0, 1); - GameObject object4 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1); + GameObject object1 + = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 0}, 0, 1); + GameObject object2 + = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 1}, 0, 1); + GameObject object3 + = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 2}, 0, 1); + GameObject object4 + = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1); } }; @@ -77,4 +84,3 @@ int main() { return 0; } - -- cgit v1.2.3