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(-) 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