From b3e991afa5c3c362317616aad68e30889cf3ef40 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 13:20:44 +0100 Subject: Changed examples a bit to work with new GameObject class --- src/example/particle.cpp | 2 +- src/example/physics.cpp | 9 ++------- src/example/rendering.cpp | 21 +++------------------ src/example/script.cpp | 8 +------- 4 files changed, 7 insertions(+), 33 deletions(-) (limited to 'src/example') diff --git a/src/example/particle.cpp b/src/example/particle.cpp index 53fa213..fe8a43b 100644 --- a/src/example/particle.cpp +++ b/src/example/particle.cpp @@ -25,7 +25,7 @@ int main(int argc, char * argv[]) { } GameObject * game_object[1]; - game_object[0] = new GameObject(0, "Name", "Tag", 0); + game_object[0] = new GameObject(0, "Name", "Tag", Point{0, 0}, 0, 1); // FIXME: all systems are singletons, so this shouldn't even compile. ParticleSystem particle_system; diff --git a/src/example/physics.cpp b/src/example/physics.cpp index db69b9b..54d1807 100644 --- a/src/example/physics.cpp +++ b/src/example/physics.cpp @@ -16,13 +16,8 @@ using namespace std; int main(int argc, char * argv[]) { PhysicsSystem physics_system; GameObject * game_object[2]; - game_object[1] = new GameObject(2, "Name", "Tag", 0); // not found not used - game_object[0] = new GameObject(5, "Name", "Tag", 0); - Point point = { - .x = 0, - .y = 0, - }; - game_object[0]->add_component(point, 0, 0); + game_object[1] = new GameObject(2, "Name", "Tag", Point{0, 0}, 0, 0); // not found not used + game_object[0] = new GameObject(5, "Name", "Tag", Point{0, 0}, 0, 0); game_object[0]->add_component(1, 1, BodyType::DYNAMIC); game_object[0]->add_component(1, 0); physics_system.update(); diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 1bf448c..9624093 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -23,19 +23,14 @@ int main() { dbg_trace(); - auto obj = GameObject(0, "name", "tag", 0); - auto obj1 = GameObject(1, "name", "tag", 0); - auto obj2 = GameObject(2, "name", "tag", 0); + auto obj = GameObject(0, "name", "tag", Point{0, 0}, 1, 1); + auto obj1 = GameObject(1, "name", "tag", Point{500, 0}, 1, 0.1); + auto obj2 = GameObject(2, "name", "tag", Point{800, 0}, 1, 0.1); auto & mgr = AssetManager::get_instance(); // Normal adding components { Color color(0, 0, 0, 0); - Point point = { - .x = 0, - .y = 0, - }; - obj.add_component(point, 1, 1); obj.add_component( make_shared("../asset/texture/img.png"), color, FlipSettings{true, true}); @@ -43,22 +38,12 @@ int main() { { Color color(0, 0, 0, 0); - Point point = { - .x = 500, - .y = 0, - }; - obj1.add_component(point, 0, 0.1); auto img = mgr.cache("../asset/texture/second.png"); obj1.add_component(img, color, FlipSettings{true, true}); } { Color color(0, 0, 0, 0); - Point point = { - .x = 800, - .y = 0, - }; - //obj.add_component(point, 0, 0.1); auto img = mgr.cache("../asset/texture/second.png"); obj2.add_component(img, color, FlipSettings{true, true}); } diff --git a/src/example/script.cpp b/src/example/script.cpp index 5df26e8..e4c8319 100644 --- a/src/example/script.cpp +++ b/src/example/script.cpp @@ -37,13 +37,7 @@ class MyScript : public Script { int main() { // Create game object with Transform and BehaviorScript components - auto obj = GameObject(0, "name", "tag", 0); - obj.add_component( - Point{ - .x = 1.2, - .y = 3.4, - }, - 0, 0); + auto obj = GameObject(0, "name", "tag", Point{1.2, 3.4}, 0, 1); obj.add_component().set_script(); // Get ScriptSystem singleton instance (this would normally be done from the -- cgit v1.2.3 From a016ac2a146ce7c980a2b89d5fe6d079491b790b Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 13:34:43 +0100 Subject: Added example --- src/example/CMakeLists.txt | 1 + src/example/ecs.cpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/example/ecs.cpp (limited to 'src/example') diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index fea6f60..81df8d1 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -24,3 +24,4 @@ add_example(rendering) add_example(asset_manager) add_example(particle) add_example(physics) +add_example(ecs) diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp new file mode 100644 index 0000000..e37e8d4 --- /dev/null +++ b/src/example/ecs.cpp @@ -0,0 +1,35 @@ +#include + +#include "../crepe/api/GameObject.h" +#include "../crepe/ComponentManager.h" +#include "../crepe/Metadata.h" +#include "../crepe/api/Transform.h" + +using namespace crepe::api; +using namespace crepe; +using namespace std; + +int main() { + GameObject body(0, "body", "person", Point{0, 0}, 0, 1); + GameObject leg(1, "leg", "person", Point{1, 1}, 0, 1); + GameObject foot(2, "foot", "person", Point{2, 2}, 0, 1); + + foot.set_parent(leg); + leg.set_parent(body); + + ComponentManager & mgr = ComponentManager::get_instance(); + vector> metadata = mgr.get_components_by_type(); + vector> transform = mgr.get_components_by_type(); + + for(auto & m : metadata) { + cout << m.get().name << " " << m.get().tag << " " << m.get().parent << endl; + for(auto & c : m.get().children) { + cout << " " << c << " " << endl; + } + } + for(auto & t : transform) { + cout << t.get().position.x << " " << t.get().position.y << endl; + } + + return 0; +} -- cgit v1.2.3 From 84d92828e0087d468119b0078d93984186c3cc38 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 13:37:31 +0100 Subject: Improved test --- src/example/ecs.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/example') diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index e37e8d4..2fd4ad8 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -11,11 +11,15 @@ using namespace std; int main() { GameObject body(0, "body", "person", Point{0, 0}, 0, 1); - GameObject leg(1, "leg", "person", Point{1, 1}, 0, 1); - GameObject foot(2, "foot", "person", Point{2, 2}, 0, 1); + GameObject rightLeg(1, "rightLeg", "person", Point{1, 1}, 0, 1); + GameObject leftLeg(2, "leftLeg", "person", Point{1, 1}, 0, 1); + GameObject rightFoot(3, "rightFoot", "person", Point{2, 2}, 0, 1); + GameObject leftFoot(4, "leftFoot", "person", Point{2, 2}, 0, 1); - foot.set_parent(leg); - leg.set_parent(body); + rightFoot.set_parent(rightLeg); + leftFoot.set_parent(leftLeg); + rightLeg.set_parent(body); + leftLeg.set_parent(body); ComponentManager & mgr = ComponentManager::get_instance(); vector> metadata = mgr.get_components_by_type(); -- cgit v1.2.3 From 81d2a26f4f893648d6e549570d117e18e71beae8 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 13:42:26 +0100 Subject: Improved test --- src/example/ecs.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/example') diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index 2fd4ad8..4c0e07d 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -10,29 +10,34 @@ using namespace crepe; using namespace std; int main() { + // Create a few GameObjects GameObject body(0, "body", "person", Point{0, 0}, 0, 1); GameObject rightLeg(1, "rightLeg", "person", Point{1, 1}, 0, 1); GameObject leftLeg(2, "leftLeg", "person", Point{1, 1}, 0, 1); GameObject rightFoot(3, "rightFoot", "person", Point{2, 2}, 0, 1); GameObject leftFoot(4, "leftFoot", "person", Point{2, 2}, 0, 1); + // Set the parent of each GameObject rightFoot.set_parent(rightLeg); leftFoot.set_parent(leftLeg); rightLeg.set_parent(body); leftLeg.set_parent(body); + // Get the Metadata and Transform components of each GameObject ComponentManager & mgr = ComponentManager::get_instance(); vector> metadata = mgr.get_components_by_type(); vector> transform = mgr.get_components_by_type(); + // Print the Metadata and Transform components for(auto & m : metadata) { - cout << m.get().name << " " << m.get().tag << " " << m.get().parent << endl; + cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name << " Tag: " << m.get().tag << " Parent: " << m.get().parent << " Children: "; for(auto & c : m.get().children) { - cout << " " << c << " " << endl; + cout << c << " "; } + cout << endl; } for(auto & t : transform) { - cout << t.get().position.x << " " << t.get().position.y << endl; + cout << "Id: " << t.get().game_object_id << " Position: [" << t.get().position.x << ", " << t.get().position.y << "]" << endl; } return 0; -- cgit v1.2.3 From 5b8ec02b6b4f2356e2d688502298274e372b9fad Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 13:57:49 +0100 Subject: Make format --- mwe/events/include/event.h | 2 +- src/crepe/Component.cpp | 4 +-- src/crepe/Metadata.cpp | 7 +++--- src/crepe/System.h | 4 +-- src/crepe/api/GameObject.cpp | 12 ++++++--- src/crepe/api/GameObject.h | 3 ++- src/crepe/api/Transform.cpp | 7 ++---- src/example/ecs.cpp | 58 ++++++++++++++++++++++++++------------------ src/example/physics.cpp | 3 ++- 9 files changed, 55 insertions(+), 45 deletions(-) (limited to 'src/example') diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index 16c75bf..3e70201 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -152,7 +152,7 @@ private: }; class ShutDownEvent : public Event { public: - ShutDownEvent() : Event("ShutDownEvent") {}; + ShutDownEvent() : Event("ShutDownEvent"){}; REGISTER_EVENT_TYPE(ShutDownEvent) diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 25c6947..78b47fa 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -4,6 +4,4 @@ using namespace crepe; Component::Component(uint32_t id) : game_object_id(id), active(true) {} -int Component::get_instances_max() const { - return -1; -} +int Component::get_instances_max() const { return -1; } diff --git a/src/crepe/Metadata.cpp b/src/crepe/Metadata.cpp index 3d1d910..d362e0a 100644 --- a/src/crepe/Metadata.cpp +++ b/src/crepe/Metadata.cpp @@ -3,8 +3,7 @@ using namespace crepe; using namespace std; -Metadata::Metadata(uint32_t gameObjectId, string name, string tag) : Component(gameObjectId), name(name), tag(tag) {} +Metadata::Metadata(uint32_t gameObjectId, string name, string tag) + : Component(gameObjectId), name(name), tag(tag) {} -int Metadata::get_instances_max() const { - return 1; -} +int Metadata::get_instances_max() const { return 1; } diff --git a/src/crepe/System.h b/src/crepe/System.h index ecbb7f5..8744920 100644 --- a/src/crepe/System.h +++ b/src/crepe/System.h @@ -8,8 +8,8 @@ public: virtual void update() = 0; protected: - System() {}; - virtual ~System() {}; + System(){}; + virtual ~System(){}; private: // singleton diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 388f1c4..5393e39 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -1,12 +1,14 @@ #include "api/Transform.h" -#include "Metadata.h" #include "GameObject.h" +#include "Metadata.h" using namespace crepe::api; using namespace std; -GameObject::GameObject(uint32_t id, std::string name, std::string tag, Point position, double rotation, double scale) : id(id) { +GameObject::GameObject(uint32_t id, std::string name, std::string tag, + 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); @@ -14,8 +16,10 @@ GameObject::GameObject(uint32_t id, std::string name, std::string tag, Point pos void GameObject::set_parent(GameObject & parent) { auto & mgr = ComponentManager::get_instance(); - vector> thisMetadata = mgr.get_components_by_id(this->id); - vector> parentMetadata = mgr.get_components_by_id(parent.id); + vector> thisMetadata + = mgr.get_components_by_id(this->id); + vector> parentMetadata + = mgr.get_components_by_id(parent.id); thisMetadata.at(0).get().parent = parent.id; parentMetadata.at(0).get().children.push_back(this->id); } diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index fd513ec..862fee8 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -9,7 +9,8 @@ namespace crepe::api { class GameObject { public: - GameObject(uint32_t id, std::string name, std::string tag, Point position, double rotation, double scale); + GameObject(uint32_t id, std::string name, std::string tag, Point position, + double rotation, double scale); void set_parent(GameObject & parent); template diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index 64ad81c..4b4da8f 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -8,14 +8,11 @@ using namespace crepe::api; -Transform::Transform(uint32_t game_id, Point point, double rot, - double scale) +Transform::Transform(uint32_t game_id, Point point, double rot, double scale) : Component(game_id), position(point), rotation(rot), scale(scale) { dbg_trace(); } Transform::~Transform() { dbg_trace(); } -int Transform::get_instances_max() const { - return 1; -} +int Transform::get_instances_max() const { return 1; } diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index 4c0e07d..2eb09b2 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -1,8 +1,8 @@ #include -#include "../crepe/api/GameObject.h" #include "../crepe/ComponentManager.h" #include "../crepe/Metadata.h" +#include "../crepe/api/GameObject.h" #include "../crepe/api/Transform.h" using namespace crepe::api; @@ -11,33 +11,43 @@ using namespace std; int main() { // Create a few GameObjects - GameObject body(0, "body", "person", Point{0, 0}, 0, 1); - GameObject rightLeg(1, "rightLeg", "person", Point{1, 1}, 0, 1); - GameObject leftLeg(2, "leftLeg", "person", Point{1, 1}, 0, 1); - GameObject rightFoot(3, "rightFoot", "person", Point{2, 2}, 0, 1); - GameObject leftFoot(4, "leftFoot", "person", Point{2, 2}, 0, 1); + try { + GameObject body(0, "body", "person", Point{0, 0}, 0, 1); + GameObject rightLeg(1, "rightLeg", "person", Point{1, 1}, 0, 1); + GameObject leftLeg(2, "leftLeg", "person", Point{1, 1}, 0, 1); + GameObject rightFoot(3, "rightFoot", "person", Point{2, 2}, 0, 1); + GameObject leftFoot(4, "leftFoot", "person", Point{2, 2}, 0, 1); - // Set the parent of each GameObject - rightFoot.set_parent(rightLeg); - leftFoot.set_parent(leftLeg); - rightLeg.set_parent(body); - leftLeg.set_parent(body); + // Set the parent of each GameObject + rightFoot.set_parent(rightLeg); + leftFoot.set_parent(leftLeg); + rightLeg.set_parent(body); + leftLeg.set_parent(body); - // Get the Metadata and Transform components of each GameObject - ComponentManager & mgr = ComponentManager::get_instance(); - vector> metadata = mgr.get_components_by_type(); - vector> transform = mgr.get_components_by_type(); + // Get the Metadata and Transform components of each GameObject + ComponentManager & mgr = ComponentManager::get_instance(); + vector> metadata + = mgr.get_components_by_type(); + vector> transform + = mgr.get_components_by_type(); - // Print the Metadata and Transform components - for(auto & m : metadata) { - cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name << " Tag: " << m.get().tag << " Parent: " << m.get().parent << " Children: "; - for(auto & c : m.get().children) { - cout << c << " "; + // Print the Metadata and Transform components + for (auto & m : metadata) { + cout << "Id: " << m.get().game_object_id + << " Name: " << m.get().name << " Tag: " << m.get().tag + << " Parent: " << m.get().parent << " Children: "; + for (auto & c : m.get().children) { + cout << c << " "; + } + cout << endl; } - cout << endl; - } - for(auto & t : transform) { - cout << "Id: " << t.get().game_object_id << " Position: [" << t.get().position.x << ", " << t.get().position.y << "]" << endl; + for (auto & t : transform) { + cout << "Id: " << t.get().game_object_id << " Position: [" + << t.get().position.x << ", " << t.get().position.y << "]" + << endl; + } + } catch (const exception & e) { + cerr << e.what() << endl; } return 0; diff --git a/src/example/physics.cpp b/src/example/physics.cpp index 54d1807..1bafdf3 100644 --- a/src/example/physics.cpp +++ b/src/example/physics.cpp @@ -16,7 +16,8 @@ using namespace std; int main(int argc, char * argv[]) { PhysicsSystem physics_system; GameObject * game_object[2]; - game_object[1] = new GameObject(2, "Name", "Tag", Point{0, 0}, 0, 0); // not found not used + // not found not used + game_object[1] = new GameObject(2, "Name", "Tag", Point{0, 0}, 0, 0); game_object[0] = new GameObject(5, "Name", "Tag", Point{0, 0}, 0, 0); game_object[0]->add_component(1, 1, BodyType::DYNAMIC); game_object[0]->add_component(1, 0); -- cgit v1.2.3 From 4460bd204d32f4f3dd00661b1b3e42c89588233f Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 14:17:22 +0100 Subject: Improved test by adding one Transform component too much --- src/example/ecs.cpp | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) (limited to 'src/example') diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index 2eb09b2..5646edd 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -24,31 +24,33 @@ int main() { rightLeg.set_parent(body); leftLeg.set_parent(body); - // Get the Metadata and Transform components of each GameObject - ComponentManager & mgr = ComponentManager::get_instance(); - vector> metadata - = mgr.get_components_by_type(); - vector> transform - = mgr.get_components_by_type(); - - // Print the Metadata and Transform components - for (auto & m : metadata) { - cout << "Id: " << m.get().game_object_id - << " Name: " << m.get().name << " Tag: " << m.get().tag - << " Parent: " << m.get().parent << " Children: "; - for (auto & c : m.get().children) { - cout << c << " "; - } - cout << endl; - } - for (auto & t : transform) { - cout << "Id: " << t.get().game_object_id << " Position: [" - << t.get().position.x << ", " << t.get().position.y << "]" - << endl; - } + // Adding a second Transform component is not allowed and will invoke an exception + body.add_component(Point{10, 10}, 0, 1); } catch (const exception & e) { cerr << e.what() << endl; } + // Get the Metadata and Transform components of each GameObject + ComponentManager & mgr = ComponentManager::get_instance(); + vector> metadata + = mgr.get_components_by_type(); + vector> transform + = mgr.get_components_by_type(); + + // Print the Metadata and Transform components + for (auto & m : metadata) { + cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name + << " Tag: " << m.get().tag << " Parent: " << m.get().parent + << " Children: "; + for (auto & c : m.get().children) { + cout << c << " "; + } + cout << endl; + } + for (auto & t : transform) { + cout << "Id: " << t.get().game_object_id << " Position: [" + << t.get().position.x << ", " << t.get().position.y << "]" << endl; + } + return 0; } -- 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/example') 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 5b248d068a94902be9ca4d00fe07d551f64c49b9 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 5 Nov 2024 16:14:32 +0100 Subject: `make format` --- src/crepe/system/RenderSystem.cpp | 2 +- src/example/asset_manager.cpp | 2 +- src/example/audio_internal.cpp | 2 +- src/example/particle.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/example') diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 96c94e9..5a07cc2 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -2,9 +2,9 @@ #include #include "../ComponentManager.h" -#include "../facade/SDLContext.h" #include "../api/Sprite.h" #include "../api/Transform.h" +#include "../facade/SDLContext.h" #include "../util/log.h" #include "RenderSystem.h" diff --git a/src/example/asset_manager.cpp b/src/example/asset_manager.cpp index ba18b62..cf64f89 100644 --- a/src/example/asset_manager.cpp +++ b/src/example/asset_manager.cpp @@ -1,6 +1,6 @@ -#include #include #include +#include using namespace crepe; diff --git a/src/example/audio_internal.cpp b/src/example/audio_internal.cpp index 0b36daa..1ea839d 100644 --- a/src/example/audio_internal.cpp +++ b/src/example/audio_internal.cpp @@ -3,8 +3,8 @@ * Standalone example for usage of the internal \c Sound class. */ -#include #include +#include #include #include diff --git a/src/example/particle.cpp b/src/example/particle.cpp index 83b1e8a..47ad2e2 100644 --- a/src/example/particle.cpp +++ b/src/example/particle.cpp @@ -5,9 +5,9 @@ #include #include #include -#include #include #include +#include #include using namespace crepe; -- 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') 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') 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 3fe7400de095756362b999908fd2a2ba3b71a848 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 5 Nov 2024 18:48:46 +0100 Subject: merge #18 --- contributing.md | 31 +++++++++++++++++++++++++++++++ src/crepe/Component.cpp | 1 - src/crepe/Component.h | 2 +- src/crepe/ComponentManager.hpp | 3 ++- src/crepe/Metadata.cpp | 5 ++--- src/crepe/Metadata.h | 6 +++--- src/crepe/api/GameObject.cpp | 21 +++++++++++---------- src/crepe/api/GameObject.h | 5 ++--- src/crepe/api/Transform.cpp | 3 +-- src/crepe/api/Transform.h | 4 ++-- src/example/ecs.cpp | 16 ++++++++-------- src/example/rendering.cpp | 1 - 12 files changed, 63 insertions(+), 35 deletions(-) (limited to 'src/example') diff --git a/contributing.md b/contributing.md index 2fe46f7..e910dba 100644 --- a/contributing.md +++ b/contributing.md @@ -386,6 +386,37 @@ that you can click on to open them. #endif ``` +-
+ Variables that are being moved always use the fully qualified std::move +
GoodBad
+ + ```cpp + using namespace std; + string foo = "bar"; + ref_fn(std::move(foo)); + ``` + + + ```cpp + using namespace std; + string foo = "bar"; + ref_fn(move(foo)); + ``` +
+-
+ If possible, classes and structs are passed to functions by (const) reference +
GoodBad
+ + ```cpp + void foo(const Point & p); + ``` + + + ```cpp + void foo(Point & p); + void bar(Point p); + ``` +
## CMakeLists-specific diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 78b47fa..41e7273 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -4,4 +4,3 @@ using namespace crepe; Component::Component(uint32_t id) : game_object_id(id), active(true) {} -int Component::get_instances_max() const { return -1; } diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 039836e..8db9b2a 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -13,7 +13,7 @@ protected: public: virtual ~Component() = default; - virtual int get_instances_max() const; + virtual int get_instances_max() const { return -1; } public: uint32_t game_object_id; diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index f469d12..e74f2e9 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -36,12 +36,13 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { // Check if the vector size is not greater than get_instances_max if (instance->get_instances_max() != -1 && components[type][id].size() >= instance->get_instances_max()) { + // TODO: Exception throw std::runtime_error( "Exceeded maximum number of instances for this component type"); } // store its unique_ptr in the vector<> - components[type][id].push_back(move(instance)); + components[type][id].push_back(std::move(instance)); return *instance; } diff --git a/src/crepe/Metadata.cpp b/src/crepe/Metadata.cpp index d362e0a..1ba150a 100644 --- a/src/crepe/Metadata.cpp +++ b/src/crepe/Metadata.cpp @@ -3,7 +3,6 @@ using namespace crepe; using namespace std; -Metadata::Metadata(uint32_t gameObjectId, string name, string tag) - : Component(gameObjectId), name(name), tag(tag) {} +Metadata::Metadata(uint32_t game_object_id, const string & name, const string & tag) + : Component(game_object_id), name(name), tag(tag) {} -int Metadata::get_instances_max() const { return 1; } diff --git a/src/crepe/Metadata.h b/src/crepe/Metadata.h index 2f08476..b946fd0 100644 --- a/src/crepe/Metadata.h +++ b/src/crepe/Metadata.h @@ -9,13 +9,13 @@ namespace crepe { class Metadata : public Component { public: - Metadata(uint32_t game_object_id, std::string name, std::string tag); - int get_instances_max() const; + Metadata(uint32_t game_object_id, const std::string & name, const std::string & tag); + virtual int get_instances_max() const { return 1; } public: std::string name; std::string tag; - uint32_t parent = UINT32_MAX; + uint32_t parent = -1; std::vector children; }; diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 2592d2d..8a1a235 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -6,20 +6,21 @@ using namespace crepe; using namespace std; -GameObject::GameObject(uint32_t id, std::string name, std::string tag, - 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); } -void GameObject::set_parent(GameObject & parent) { +void GameObject::set_parent(const GameObject & parent) { auto & mgr = ComponentManager::get_instance(); - vector> thisMetadata - = mgr.get_components_by_id(this->id); - vector> parentMetadata - = mgr.get_components_by_id(parent.id); - thisMetadata.at(0).get().parent = parent.id; - parentMetadata.at(0).get().children.push_back(this->id); + + // set parent on own Metadata component + 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); + 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 dcd33ad..4c87639 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -9,9 +9,8 @@ namespace crepe { class GameObject { public: - GameObject(uint32_t id, std::string name, std::string tag, Point position, - double rotation, double scale); - void set_parent(GameObject & parent); + GameObject(uint32_t id, std::string name, std::string tag, const Point & position, double rotation, double scale); + void set_parent(const GameObject & parent); template T & add_component(Args &&... args); diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index a80aff3..9c9bb06 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -8,11 +8,10 @@ using namespace crepe; -Transform::Transform(uint32_t game_id, 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(); } -int Transform::get_instances_max() const { return 1; } diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index f918115..557061b 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -14,9 +14,9 @@ class Transform : public Component { // works similar (or the same) as those found in GLSL? public: - Transform(uint32_t id, Point, double, double); + Transform(uint32_t id, const Point &, double, double); ~Transform(); - int get_instances_max() const; + virtual int get_instances_max() const { return 1; } //! Translation (shift) Point position; //! Rotation, in radians diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index eb5eeba..6f9752e 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -12,16 +12,16 @@ int main() { // Create a few GameObjects try { GameObject body(0, "body", "person", Point{0, 0}, 0, 1); - GameObject rightLeg(1, "rightLeg", "person", Point{1, 1}, 0, 1); - GameObject leftLeg(2, "leftLeg", "person", Point{1, 1}, 0, 1); - GameObject rightFoot(3, "rightFoot", "person", Point{2, 2}, 0, 1); - GameObject leftFoot(4, "leftFoot", "person", Point{2, 2}, 0, 1); + GameObject right_leg(1, "rightLeg", "person", Point{1, 1}, 0, 1); + GameObject left_leg(2, "leftLeg", "person", Point{1, 1}, 0, 1); + GameObject right_foot(3, "rightFoot", "person", Point{2, 2}, 0, 1); + GameObject left_foot(4, "leftFoot", "person", Point{2, 2}, 0, 1); // Set the parent of each GameObject - rightFoot.set_parent(rightLeg); - leftFoot.set_parent(leftLeg); - rightLeg.set_parent(body); - leftLeg.set_parent(body); + right_foot.set_parent(right_leg); + left_foot.set_parent(left_leg); + right_leg.set_parent(body); + left_leg.set_parent(body); // Adding a second Transform component is not allowed and will invoke an exception body.add_component(Point{10, 10}, 0, 1); diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index f3d8a7d..3fe43d6 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -17,7 +17,6 @@ using namespace std; using namespace crepe; int main() { - dbg_trace(); auto obj = GameObject(0, "name", "tag", Point{0, 0}, 1, 1); -- 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') 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') 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 84685b757f9b11653760bbb0f8b3d22ade8c523d Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 14:32:47 +0100 Subject: Added a missing include --- src/example/particle.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/example') diff --git a/src/example/particle.cpp b/src/example/particle.cpp index 69da015..733de4e 100644 --- a/src/example/particle.cpp +++ b/src/example/particle.cpp @@ -9,6 +9,7 @@ #include #include #include +#include using namespace crepe; using namespace std; -- cgit v1.2.3 From 36230ae0dc7a8784ef34123508efdbe571fe4869 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 14:44:45 +0100 Subject: Make format --- src/example/particle.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/example') diff --git a/src/example/particle.cpp b/src/example/particle.cpp index 733de4e..607530d 100644 --- a/src/example/particle.cpp +++ b/src/example/particle.cpp @@ -7,9 +7,9 @@ #include #include #include +#include #include #include -#include using namespace crepe; using namespace std; -- cgit v1.2.3 From 6296b85846b21083e4f545b209f1d9edce2b06f9 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 15:20:25 +0100 Subject: Moved Matadata to api folder (because it may be used by the game programmer) --- src/crepe/CMakeLists.txt | 2 -- src/crepe/Metadata.cpp | 8 -------- src/crepe/Metadata.h | 43 ------------------------------------------- src/crepe/api/CMakeLists.txt | 2 ++ src/crepe/api/Metadata.cpp | 8 ++++++++ src/crepe/api/Metadata.h | 43 +++++++++++++++++++++++++++++++++++++++++++ src/example/ecs.cpp | 2 +- src/makefile | 4 ++-- 8 files changed, 56 insertions(+), 56 deletions(-) delete mode 100644 src/crepe/Metadata.cpp delete mode 100644 src/crepe/Metadata.h create mode 100644 src/crepe/api/Metadata.cpp create mode 100644 src/crepe/api/Metadata.h (limited to 'src/example') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 867329f..8830e05 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -4,7 +4,6 @@ target_sources(crepe PUBLIC ComponentManager.cpp Component.cpp Collider.cpp - Metadata.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -13,7 +12,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES ComponentManager.hpp Component.h Collider.h - Metadata.h ) add_subdirectory(api) diff --git a/src/crepe/Metadata.cpp b/src/crepe/Metadata.cpp deleted file mode 100644 index 53d93da..0000000 --- a/src/crepe/Metadata.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "Metadata.h" - -using namespace crepe; -using namespace std; - -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 deleted file mode 100644 index d52ab67..0000000 --- a/src/crepe/Metadata.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include -#include - -#include "Component.h" - -namespace crepe { - -/** - * \brief Metadata component - * - * This class represents the Metadata component. It stores the name, tag, parent - * and children of a GameObject. - */ -class Metadata : public Component { -public: - /** - * \param game_object_id The id of the GameObject this component belongs to - * \param name The name of the GameObject - * \param tag The tag of the GameObject - */ - Metadata(uint32_t game_object_id, const std::string & name, - const std::string & tag); - /** - * \brief Get the maximum number of instances for this component - * - * \return The maximum number of instances for this component - */ - virtual int get_instances_max() const { return 1; } - -public: - //! The name of the GameObject - std::string name; - //! The tag of the GameObject - std::string tag; - //! The id of the parent GameObject (-1 if no parent) - uint32_t parent = -1; - //! The ids of the children GameObjects - std::vector children; -}; - -} // namespace crepe diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 0bb1263..3e0c044 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -11,6 +11,7 @@ target_sources(crepe PUBLIC Texture.cpp AssetManager.cpp Sprite.cpp + Metadata.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -28,4 +29,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Texture.h AssetManager.h AssetManager.hpp + Metadata.h ) diff --git a/src/crepe/api/Metadata.cpp b/src/crepe/api/Metadata.cpp new file mode 100644 index 0000000..53d93da --- /dev/null +++ b/src/crepe/api/Metadata.cpp @@ -0,0 +1,8 @@ +#include "Metadata.h" + +using namespace crepe; +using namespace std; + +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/api/Metadata.h b/src/crepe/api/Metadata.h new file mode 100644 index 0000000..4d37108 --- /dev/null +++ b/src/crepe/api/Metadata.h @@ -0,0 +1,43 @@ +#pragma once + +#include +#include + +#include "../Component.h" + +namespace crepe { + +/** + * \brief Metadata component + * + * This class represents the Metadata component. It stores the name, tag, parent + * and children of a GameObject. + */ +class Metadata : public Component { +public: + /** + * \param game_object_id The id of the GameObject this component belongs to + * \param name The name of the GameObject + * \param tag The tag of the GameObject + */ + Metadata(uint32_t game_object_id, const std::string & name, + const std::string & tag); + /** + * \brief Get the maximum number of instances for this component + * + * \return The maximum number of instances for this component + */ + virtual int get_instances_max() const { return 1; } + +public: + //! The name of the GameObject + std::string name; + //! The tag of the GameObject + std::string tag; + //! The id of the parent GameObject (-1 if no parent) + uint32_t parent = -1; + //! The ids of the children GameObjects + std::vector children; +}; + +} // namespace crepe diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index 6f9752e..a8df7e7 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -1,8 +1,8 @@ #include #include "../crepe/ComponentManager.h" -#include "../crepe/Metadata.h" #include "../crepe/api/GameObject.h" +#include "../crepe/api/Metadata.h" #include "../crepe/api/Transform.h" using namespace crepe; diff --git a/src/makefile b/src/makefile index 9b2d826..be1548c 100644 --- a/src/makefile +++ b/src/makefile @@ -24,8 +24,8 @@ MAX += crepe/Component.h MAX += crepe/ComponentManager.cpp MAX += crepe/ComponentManager.h MAX += crepe/ComponentManager.hpp -MAX += crepe/Metadata.cpp -MAX += crepe/Metadata.h +MAX += crepe/api/Metadata.cpp +MAX += crepe/api/Metadata.h TODO += crepe/Particle.cpp TODO += crepe/Particle.h TODO += crepe/Position.h -- cgit v1.2.3 From bc8f9c684cddbae8f185687b0f716f70d517858e Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 09:31:41 +0100 Subject: Fixed includes in example folder --- src/example/ecs.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/example') diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index a8df7e7..0c64373 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -1,9 +1,9 @@ #include -#include "../crepe/ComponentManager.h" -#include "../crepe/api/GameObject.h" -#include "../crepe/api/Metadata.h" -#include "../crepe/api/Transform.h" +#include +#include +#include +#include using namespace crepe; using namespace std; -- cgit v1.2.3 From d2ba8b538b137d104fc4751142fe58888d59d541 Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 10:00:08 +0100 Subject: Code style --- src/example/scene_manager.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src/example') diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index bce42ca..efbf2c2 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -1,18 +1,18 @@ #include -#include "../crepe/ComponentManager.h" -#include "../crepe/api/GameObject.h" -#include "../crepe/api/Metadata.h" -#include "../crepe/api/Point.h" -#include "../crepe/api/Scene.h" -#include "../crepe/api/SceneManager.h" +#include +#include +#include +#include +#include +#include using namespace crepe; using namespace std; -class concreteScene1 : public Scene { +class ConcreteScene1 : public Scene { public: - concreteScene1(string name) : Scene(name) {} + ConcreteScene1(string name) : Scene(name) {} void load_scene() { GameObject object1(0, "scene_1", "tag_scene_1", Point{0, 0}, 0, 1); @@ -21,9 +21,9 @@ public: } }; -class concreteScene2 : public Scene { +class ConcreteScene2 : public Scene { public: - concreteScene2(string name) : Scene(name) {} + ConcreteScene2(string name) : Scene(name) {} void load_scene() { GameObject object1(0, "scene_2", "tag_scene_2", Point{0, 0}, 0, 1); @@ -37,8 +37,8 @@ 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"); + 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 // Load scene1 (the first scene added) -- cgit v1.2.3 From 96ef24654d2ee26184ebcb5b9092649e67afe5d9 Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 12:33:37 +0100 Subject: Made game_object_id const --- src/crepe/Component.cpp | 2 +- src/crepe/Component.h | 2 +- src/crepe/api/Script.hpp | 2 +- src/crepe/system/PhysicsSystem.cpp | 4 ++-- src/crepe/system/RenderSystem.cpp | 2 +- src/example/ecs.cpp | 4 ++-- src/example/scene_manager.cpp | 4 ++-- 7 files changed, 10 insertions(+), 10 deletions(-) (limited to 'src/example') diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 230bb70..cdbda67 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -2,4 +2,4 @@ using namespace crepe; -Component::Component(uint32_t id) : game_object_id(id) {} +Component::Component(uint32_t id) : GAME_OBJECT_ID(id) {} diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 02a4e7e..41badc3 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -36,7 +36,7 @@ public: public: //! The id of the GameObject this component belongs to - uint32_t game_object_id; + const uint32_t GAME_OBJECT_ID; //! Whether the component is active bool active = true; }; diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index d96c0e8..6d111af 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -19,7 +19,7 @@ T & Script::get_component() { template std::vector> Script::get_components() { ComponentManager & mgr = ComponentManager::get_instance(); - return mgr.get_components_by_id(this->parent->game_object_id); + return mgr.get_components_by_id(this->parent->GAME_OBJECT_ID); } } // namespace crepe diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index cea8062..dd80312 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -23,12 +23,12 @@ void PhysicsSystem::update() { switch (rigidbody.body_type) { case BodyType::DYNAMIC: for (Transform & transform : transforms) { - if (transform.game_object_id == rigidbody.game_object_id) { + if (transform.GAME_OBJECT_ID == rigidbody.GAME_OBJECT_ID) { rigidbody.velocity_x = 0; rigidbody.velocity_y = 0; std::vector> forces = mgr.get_components_by_id( - rigidbody.game_object_id); + rigidbody.GAME_OBJECT_ID); rigidbody.velocity_y += rigidbody.gravity_scale * 1 * rigidbody.mass; diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 5a07cc2..2003eaf 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -32,7 +32,7 @@ void RenderSystem::update() { for (const Sprite & sprite : sprites) { std::vector> transforms - = mgr.get_components_by_id(sprite.game_object_id); + = mgr.get_components_by_id(sprite.GAME_OBJECT_ID); for (const Transform & transform : transforms) { render.draw(sprite, transform); } diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index 0c64373..dfd3595 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -38,7 +38,7 @@ int main() { // Print the Metadata and Transform components for (auto & m : metadata) { - cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name + cout << "Id: " << m.get().GAME_OBJECT_ID << " Name: " << m.get().name << " Tag: " << m.get().tag << " Parent: " << m.get().parent << " Children: "; for (auto & c : m.get().children) { @@ -47,7 +47,7 @@ int main() { cout << endl; } for (auto & t : transform) { - cout << "Id: " << t.get().game_object_id << " Position: [" + cout << "Id: " << t.get().GAME_OBJECT_ID << " Position: [" << t.get().position.x << ", " << t.get().position.y << "]" << endl; } diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index efbf2c2..471c400 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -52,7 +52,7 @@ int main() { cout << "Metadata components of Scene1:" << endl; // Print the Metadata for (auto & m : metadata) { - cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name + cout << "Id: " << m.get().GAME_OBJECT_ID << " Name: " << m.get().name << " Tag: " << m.get().tag << endl; } @@ -67,7 +67,7 @@ int main() { cout << "Metadata components of Scene2:" << endl; // Print the Metadata for (auto & m : metadata) { - cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name + cout << "Id: " << m.get().GAME_OBJECT_ID << " Name: " << m.get().name << " Tag: " << m.get().tag << endl; } -- cgit v1.2.3 From acbe6b5d4256db950827b120fe7cd781e45715f6 Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 12:36:08 +0100 Subject: Made name and tag const --- src/crepe/api/Metadata.cpp | 2 +- src/crepe/api/Metadata.h | 4 ++-- src/example/ecs.cpp | 4 ++-- src/example/scene_manager.cpp | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) (limited to 'src/example') diff --git a/src/crepe/api/Metadata.cpp b/src/crepe/api/Metadata.cpp index 53d93da..55d9ae2 100644 --- a/src/crepe/api/Metadata.cpp +++ b/src/crepe/api/Metadata.cpp @@ -5,4 +5,4 @@ using namespace std; Metadata::Metadata(uint32_t game_object_id, const string & name, const string & tag) - : Component(game_object_id), name(name), tag(tag) {} + : Component(game_object_id), NAME(name), TAG(tag) {} diff --git a/src/crepe/api/Metadata.h b/src/crepe/api/Metadata.h index 4d37108..fdbed41 100644 --- a/src/crepe/api/Metadata.h +++ b/src/crepe/api/Metadata.h @@ -31,9 +31,9 @@ public: public: //! The name of the GameObject - std::string name; + const std::string NAME; //! The tag of the GameObject - std::string tag; + const std::string TAG; //! The id of the parent GameObject (-1 if no parent) uint32_t parent = -1; //! The ids of the children GameObjects diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index dfd3595..7593faf 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -38,8 +38,8 @@ int main() { // Print the Metadata and Transform components for (auto & m : metadata) { - cout << "Id: " << m.get().GAME_OBJECT_ID << " Name: " << m.get().name - << " Tag: " << m.get().tag << " Parent: " << m.get().parent + cout << "Id: " << m.get().GAME_OBJECT_ID << " Name: " << m.get().NAME + << " Tag: " << m.get().TAG << " Parent: " << m.get().parent << " Children: "; for (auto & c : m.get().children) { cout << c << " "; diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index 471c400..bfa9479 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -52,8 +52,8 @@ int main() { 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; + cout << "Id: " << m.get().GAME_OBJECT_ID << " Name: " << m.get().NAME + << " Tag: " << m.get().TAG << endl; } // Set scene2 as the next scene @@ -67,8 +67,8 @@ int main() { 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; + cout << "Id: " << m.get().GAME_OBJECT_ID << " Name: " << m.get().NAME + << " Tag: " << m.get().TAG << endl; } return 0; -- cgit v1.2.3 From 97515abfb2859e289df9d65d7106f35159749131 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 7 Nov 2024 15:27:21 +0100 Subject: no more singleton systems --- src/crepe/system/ScriptSystem.cpp | 8 -------- src/crepe/system/ScriptSystem.h | 5 ----- src/crepe/system/System.h | 14 +++----------- src/example/script.cpp | 2 +- 4 files changed, 4 insertions(+), 25 deletions(-) (limited to 'src/example') diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index f1fae4d..f2673e7 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -12,14 +12,6 @@ using namespace std; using namespace crepe; -ScriptSystem::ScriptSystem() { dbg_trace(); } -ScriptSystem::~ScriptSystem() { dbg_trace(); } - -ScriptSystem & ScriptSystem::get_instance() { - static ScriptSystem instance; - return instance; -} - void ScriptSystem::update() { using namespace std; dbg_trace(); diff --git a/src/crepe/system/ScriptSystem.h b/src/crepe/system/ScriptSystem.h index 32e793c..4fa6141 100644 --- a/src/crepe/system/ScriptSystem.h +++ b/src/crepe/system/ScriptSystem.h @@ -10,13 +10,8 @@ class Script; class ScriptSystem : public System { public: - static ScriptSystem & get_instance(); void update(); -private: - ScriptSystem(); - ~ScriptSystem(); - private: // TODO: to forward_list std::forward_list