From 8eac9296a487f5b77fc1cba269c70ee23f7603dc Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 11:56:39 +0100 Subject: Added get_instances_max() method --- src/crepe/Component.cpp | 4 ++++ src/crepe/Component.h | 1 + 2 files changed, 5 insertions(+) diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 358ce31..25c6947 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -3,3 +3,7 @@ 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 bc44865..039836e 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -13,6 +13,7 @@ protected: public: virtual ~Component() = default; + virtual int get_instances_max() const; public: uint32_t game_object_id; -- cgit v1.2.3 From 9adde5495c0c30d214c30e593cc289470448494c Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 12:02:04 +0100 Subject: Added Matadata component --- src/crepe/CMakeLists.txt | 3 ++- src/crepe/Metadata.cpp | 10 ++++++++++ src/crepe/Metadata.h | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/crepe/Metadata.cpp create mode 100644 src/crepe/Metadata.h diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index d938eb8..9ad1ff9 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -12,8 +12,8 @@ target_sources(crepe PUBLIC CollisionSystem.cpp Collider.cpp SDLContext.cpp - RenderSystem.cpp + Metadata.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -31,6 +31,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Collider.h SDLContext.h RenderSystem.h + Metadata.h ) add_subdirectory(api) diff --git a/src/crepe/Metadata.cpp b/src/crepe/Metadata.cpp new file mode 100644 index 0000000..3d1d910 --- /dev/null +++ b/src/crepe/Metadata.cpp @@ -0,0 +1,10 @@ +#include "Metadata.h" + +using namespace crepe; +using namespace std; + +Metadata::Metadata(uint32_t gameObjectId, string name, string tag) : Component(gameObjectId), name(name), tag(tag) {} + +int Metadata::get_instances_max() const { + return 1; +} diff --git a/src/crepe/Metadata.h b/src/crepe/Metadata.h new file mode 100644 index 0000000..2f08476 --- /dev/null +++ b/src/crepe/Metadata.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +#include "Component.h" + +namespace crepe { + +class Metadata : public Component { +public: + Metadata(uint32_t game_object_id, std::string name, std::string tag); + int get_instances_max() const; + +public: + std::string name; + std::string tag; + uint32_t parent = UINT32_MAX; + std::vector children; +}; + +} // namespace crepe -- cgit v1.2.3 From db280c2787cfc77641082f94d40d9231fbc62d56 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 12:07:10 +0100 Subject: Replaced Point references with Point copy by value --- src/crepe/api/Transform.cpp | 2 +- src/crepe/api/Transform.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index c76bc72..2a36e67 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -8,7 +8,7 @@ using namespace crepe::api; -Transform::Transform(uint32_t game_id, const Point & point, double rot, +Transform::Transform(uint32_t game_id, Point point, double rot, double scale) : Component(game_id), position(point), rotation(rot), scale(scale) { dbg_trace(); diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index c451c16..5d45060 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -14,7 +14,7 @@ class Transform : public Component { // works similar (or the same) as those found in GLSL? public: - Transform(uint32_t id, const Point &, double, double); + Transform(uint32_t id, Point, double, double); ~Transform(); //! Translation (shift) Point position; -- cgit v1.2.3 From 6aa2084b52e632447267bdff5342aa7af224f1d4 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 13:01:23 +0100 Subject: Modified GameObject according to class diagram (design document) --- src/crepe/api/GameObject.cpp | 18 ++++++++++++++++-- src/crepe/api/GameObject.h | 9 ++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index b167187..708ff71 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -1,7 +1,21 @@ +#include "api/Transform.h" + +#include "Metadata.h" #include "GameObject.h" using namespace crepe::api; using namespace std; -GameObject::GameObject(uint32_t id, string name, string tag, int layer) - : id(id), name(name), tag(tag), active(true), layer(layer) {} +GameObject::GameObject(uint32_t id, std::string name, std::string tag, Point position, double rotation, double scale) : id(id) { + auto & 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) { + 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); +} diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index 57508c5..fd513ec 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -3,20 +3,19 @@ #include #include +#include "api/Point.h" + namespace crepe::api { class GameObject { public: - GameObject(uint32_t id, std::string name, std::string tag, int layer); + GameObject(uint32_t id, std::string name, std::string tag, Point position, double rotation, double scale); + void set_parent(GameObject & parent); template T & add_component(Args &&... args); uint32_t id; - std::string name; - std::string tag; - bool active; - int layer; }; } // namespace crepe::api -- cgit v1.2.3 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(-) 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 efc5795029a6da3db116ef96676596832c6b8754 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 13:27:12 +0100 Subject: Replaced auto with concrete type --- src/crepe/api/GameObject.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 708ff71..388f1c4 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -7,7 +7,7 @@ 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) { - auto & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = ComponentManager::get_instance(); mgr.add_component(this->id, position, rotation, scale); mgr.add_component(this->id, name, tag); } -- 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 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(-) 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(-) 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 154e974a0d553ea8209c3be9b1bd5820fef5e0a3 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 13:49:20 +0100 Subject: Implemented get_instances_max() at Transform component --- src/crepe/api/Transform.cpp | 5 +++++ src/crepe/api/Transform.h | 1 + 2 files changed, 6 insertions(+) diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index 2a36e67..64ad81c 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -13,4 +13,9 @@ Transform::Transform(uint32_t game_id, Point point, double rot, : 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 5d45060..85e16b4 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -16,6 +16,7 @@ class Transform : public Component { public: Transform(uint32_t id, Point, double, double); ~Transform(); + int get_instances_max() const; //! Translation (shift) Point position; //! Rotation, in radians -- 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(-) 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 99f71fea64ada18def09e9c32633cf7c8e7ce5d7 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 14:17:05 +0100 Subject: Implemented get_instances_max() at add_component() --- src/crepe/ComponentManager.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 9b07f13..828ca8e 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -31,6 +31,13 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { // Create a new component of type T (arguments directly forwarded). The // constructor must be called by ComponentManager. T * instance = new T(id, forward(args)...); + + // Check if the vector size is not greater than get_instances_max + if (components[type][id].size() >= instance->get_instances_max()) { + 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(unique_ptr(instance)); -- 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(-) 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 3d6fbf38dba40c97d2a06cfe2cf344190d05cfe4 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 14:26:41 +0100 Subject: Implemented -1 case --- src/crepe/ComponentManager.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 828ca8e..66e027f 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -33,7 +33,8 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { T * instance = new T(id, forward(args)...); // Check if the vector size is not greater than get_instances_max - if (components[type][id].size() >= instance->get_instances_max()) { + if (instance->get_instances_max() != -1 + && components[type][id].size() >= instance->get_instances_max()) { throw std::runtime_error( "Exceeded maximum number of instances for this component type"); } -- cgit v1.2.3 From 20bb6820d1f5f5c0260bbe86c938ca4e4d91a1ae Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 14:39:05 +0100 Subject: Made add_component() exception save --- src/crepe/ComponentManager.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 66e027f..f469d12 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -30,7 +30,8 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { // Create a new component of type T (arguments directly forwarded). The // constructor must be called by ComponentManager. - T * instance = new T(id, forward(args)...); + T * instance_pointer = new T(id, forward(args)...); + unique_ptr instance = unique_ptr(instance_pointer); // Check if the vector size is not greater than get_instances_max if (instance->get_instances_max() != -1 @@ -40,7 +41,7 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { } // store its unique_ptr in the vector<> - components[type][id].push_back(unique_ptr(instance)); + components[type][id].push_back(move(instance)); return *instance; } -- cgit v1.2.3 From 02c4c796888397496452aaa46c52691eef8707eb Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 16:11:54 +0100 Subject: Added abstract Scene class --- src/crepe/api/Scene.cpp | 5 +++++ src/crepe/api/Scene.h | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/crepe/api/Scene.cpp create mode 100644 src/crepe/api/Scene.h diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp new file mode 100644 index 0000000..189650f --- /dev/null +++ b/src/crepe/api/Scene.cpp @@ -0,0 +1,5 @@ +#include "Scene.h" + +using namespace crepe::api; + +Scene::Scene(std::string name) : name(name) {} diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h new file mode 100644 index 0000000..35fe9db --- /dev/null +++ b/src/crepe/api/Scene.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +namespace crepe::api { + +class Scene { +public: + Scene(std::string name); + virtual ~Scene() = default; + virtual void load_scene() = 0; + +public: + std::string name; +}; + +} // namespace crepe::api -- cgit v1.2.3 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/CMakeLists.txt | 5 +++++ src/crepe/api/SceneManager.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/crepe/api/SceneManager.h | 36 ++++++++++++++++++++++++++++++++++++ src/crepe/api/SceneManager.hpp | 11 +++++++++++ 4 files changed, 88 insertions(+) create mode 100644 src/crepe/api/SceneManager.cpp create mode 100644 src/crepe/api/SceneManager.h create mode 100644 src/crepe/api/SceneManager.hpp diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 0bb1263..7fd8ffd 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -11,6 +11,8 @@ target_sources(crepe PUBLIC Texture.cpp AssetManager.cpp Sprite.cpp + Scene.cpp + SceneManager.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -28,4 +30,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Texture.h AssetManager.h AssetManager.hpp + Scene.h + SceneManager.h + SceneManager.hpp ) diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/api/SceneManager.cpp new file mode 100644 index 0000000..773303b --- /dev/null +++ b/src/crepe/api/SceneManager.cpp @@ -0,0 +1,36 @@ +#include "../ComponentManager.h" + +#include "SceneManager.h" + +using namespace crepe::api; + +SceneManager::SceneManager() {} + +SceneManager & SceneManager::get_instance() { + static SceneManager instance; + return instance; +} + +// Push the next scene onto the queue +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() { + while (!next_scene.empty()) { + string name = next_scene.front(); + next_scene.pop(); + + for (auto & scene : scenes) { + if (scene->name == name) { + // Delete all components of the current scene + ComponentManager & mgr = ComponentManager::get_instance(); + mgr.delete_all_components(); + // Load the new scene + scene->load_scene(); + break; + } + } + } +} 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" diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/api/SceneManager.hpp new file mode 100644 index 0000000..120640f --- /dev/null +++ b/src/crepe/api/SceneManager.hpp @@ -0,0 +1,11 @@ +#include "SceneManager.h" + +using namespace crepe::api; +using namespace std; + +// 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"); + scenes.emplace_back(make_unique(name)); +} -- 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 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(-) 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(-) 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 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(-) 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(-) 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(-) 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 6ce2c14077e3e6dd01398d582b42dc50e9141f54 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 5 Nov 2024 19:33:31 +0100 Subject: start cleanup --- src/makefile | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/src/makefile b/src/makefile index 3f74a2a..8506a43 100644 --- a/src/makefile +++ b/src/makefile @@ -1,8 +1,106 @@ .PHONY: FORCE -FMT += $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') +# STEPS FOR BIG CLEANUP +# +# 1. Change TODO to your name (in capitals) for each file in the list below +# that is yours (or you are going to fix) +# 2. Update the name between parentheses below this list (see comment) to your +# name +# 3. Create a git commit at this point (ensure `git status` reports "working +# tree clean") +# 4. Run `make format` in the REPOSITORY ROOT DIRECTORY (NOT HERE), and start +# fixing reported errors or miscorrections manually until everything works +# again. +# 5. Once everything is working again, create another git commit, and create a +# pull request. Make sure to ask someone to review the code standards for +# each ENTIRE FILE in this pull request. + +LOEK += crepe/Asset.cpp +LOEK += crepe/Asset.h +TODO += crepe/Collider.cpp +TODO += crepe/Collider.h +TODO += crepe/Component.cpp +TODO += crepe/Component.h +TODO += crepe/ComponentManager.cpp +TODO += crepe/ComponentManager.h +TODO += crepe/ComponentManager.hpp +TODO += crepe/Metadata.cpp +TODO += crepe/Metadata.h +TODO += crepe/Particle.cpp +TODO += crepe/Particle.h +TODO += crepe/Position.h +TODO += crepe/api/AssetManager.cpp +TODO += crepe/api/AssetManager.h +TODO += crepe/api/AssetManager.hpp +LOEK += crepe/api/AudioSource.cpp +LOEK += crepe/api/AudioSource.h +LOEK += crepe/api/BehaviorScript.cpp +LOEK += crepe/api/BehaviorScript.h +LOEK += crepe/api/BehaviorScript.hpp +TODO += crepe/api/CircleCollider.h +TODO += crepe/api/Color.cpp +TODO += crepe/api/Color.h +LOEK += crepe/api/Config.h +TODO += crepe/api/Force.cpp +TODO += crepe/api/Force.h +TODO += crepe/api/GameObject.cpp +TODO += crepe/api/GameObject.h +TODO += crepe/api/GameObject.hpp +TODO += crepe/api/ParticleEmitter.cpp +TODO += crepe/api/ParticleEmitter.h +TODO += crepe/api/Point.h +TODO += crepe/api/Rigidbody.cpp +TODO += crepe/api/Rigidbody.h +LOEK += crepe/api/Script.cpp +LOEK += crepe/api/Script.h +LOEK += crepe/api/Script.hpp +TODO += crepe/api/Sprite.cpp +TODO += crepe/api/Sprite.h +TODO += crepe/api/Texture.cpp +TODO += crepe/api/Texture.h +TODO += crepe/api/Transform.cpp +TODO += crepe/api/Transform.h +TODO += crepe/facade/SDLApp.cpp +TODO += crepe/facade/SDLApp.h +TODO += crepe/facade/SDLContext.cpp +TODO += crepe/facade/SDLContext.h +LOEK += crepe/facade/Sound.cpp +LOEK += crepe/facade/Sound.h +LOEK += crepe/facade/SoundContext.cpp +LOEK += crepe/facade/SoundContext.h +TODO += crepe/system/CollisionSystem.cpp +TODO += crepe/system/CollisionSystem.h +TODO += crepe/system/ParticleSystem.cpp +TODO += crepe/system/ParticleSystem.h +TODO += crepe/system/PhysicsSystem.cpp +TODO += crepe/system/PhysicsSystem.h +TODO += crepe/system/RenderSystem.cpp +TODO += crepe/system/RenderSystem.h +LOEK += crepe/system/ScriptSystem.cpp +LOEK += crepe/system/ScriptSystem.h +LOEK += crepe/system/System.h +LOEK += crepe/util/LogColor.cpp +LOEK += crepe/util/LogColor.h +LOEK += crepe/util/fmt.cpp +LOEK += crepe/util/fmt.h +LOEK += crepe/util/log.cpp +LOEK += crepe/util/log.h +TODO += example/asset_manager.cpp +LOEK += example/audio_internal.cpp +TODO += example/components_internal.cpp +TODO += example/ecs.cpp +LOEK += example/log.cpp +TODO += example/particle.cpp +TODO += example/physics.cpp +TODO += example/rendering.cpp +LOEK += example/script.cpp +LOEK += test/audio.cpp +LOEK += test/dummy.cpp + +FMT := $(LOEK) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 format: FORCE - # clang-tidy -p build/compile_commands.json --fix-errors $(FMT) + clang-tidy -p build/compile_commands.json --fix-errors $(FMT) -# TODO: re-enable linter after 2024-11-10 +# FMT += $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') +# TODO: re-enable linter after all corrections -- cgit v1.2.3 From 452944cd6dc666a58f5f8441b6cde0aaf8cc48aa Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 09:56:59 +0100 Subject: Make format --- src/crepe/Component.cpp | 1 - src/crepe/Metadata.cpp | 4 ++-- src/crepe/Metadata.h | 3 ++- src/crepe/api/GameObject.cpp | 11 +++++++---- src/crepe/api/GameObject.h | 3 ++- src/crepe/api/Transform.cpp | 4 ++-- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 41e7273..358ce31 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -3,4 +3,3 @@ using namespace crepe; Component::Component(uint32_t id) : game_object_id(id), active(true) {} - diff --git a/src/crepe/Metadata.cpp b/src/crepe/Metadata.cpp index 1ba150a..53d93da 100644 --- a/src/crepe/Metadata.cpp +++ b/src/crepe/Metadata.cpp @@ -3,6 +3,6 @@ using namespace crepe; using namespace std; -Metadata::Metadata(uint32_t game_object_id, const string & name, const string & tag) +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 index b946fd0..1577987 100644 --- a/src/crepe/Metadata.h +++ b/src/crepe/Metadata.h @@ -9,7 +9,8 @@ namespace crepe { class Metadata : public Component { public: - Metadata(uint32_t game_object_id, const std::string & name, const std::string & tag); + Metadata(uint32_t game_object_id, const std::string & name, + const std::string & tag); virtual int get_instances_max() const { return 1; } public: diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 8a1a235..51cd08f 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -6,7 +6,9 @@ using namespace crepe; using namespace std; -GameObject::GameObject(uint32_t id, std::string name, std::string tag, const 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); @@ -16,11 +18,12 @@ void GameObject::set_parent(const GameObject & parent) { auto & mgr = ComponentManager::get_instance(); // set parent on own Metadata component - vector> this_metadata = mgr.get_components_by_id(this->id); + 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); + 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 4c87639..602f33c 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -9,7 +9,8 @@ namespace crepe { class GameObject { public: - GameObject(uint32_t id, std::string name, std::string tag, const Point & position, double rotation, double scale); + GameObject(uint32_t id, std::string name, std::string tag, + const Point & position, double rotation, double scale); void set_parent(const GameObject & parent); template diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index 9c9bb06..1d8d401 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -8,10 +8,10 @@ using namespace crepe; -Transform::Transform(uint32_t game_id, const 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(); } - -- 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(-) 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(-) 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(-) 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 9bc23a15143af0eed3222cd8d8bb23bd0a10ea46 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 10:35:54 +0100 Subject: Big cleanup setup --- src/makefile | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/makefile b/src/makefile index 8506a43..9b2d826 100644 --- a/src/makefile +++ b/src/makefile @@ -19,13 +19,13 @@ LOEK += crepe/Asset.cpp LOEK += crepe/Asset.h TODO += crepe/Collider.cpp TODO += crepe/Collider.h -TODO += crepe/Component.cpp -TODO += crepe/Component.h -TODO += crepe/ComponentManager.cpp -TODO += crepe/ComponentManager.h -TODO += crepe/ComponentManager.hpp -TODO += crepe/Metadata.cpp -TODO += crepe/Metadata.h +MAX += crepe/Component.cpp +MAX += crepe/Component.h +MAX += crepe/ComponentManager.cpp +MAX += crepe/ComponentManager.h +MAX += crepe/ComponentManager.hpp +MAX += crepe/Metadata.cpp +MAX += crepe/Metadata.h TODO += crepe/Particle.cpp TODO += crepe/Particle.h TODO += crepe/Position.h @@ -43,9 +43,9 @@ TODO += crepe/api/Color.h LOEK += crepe/api/Config.h TODO += crepe/api/Force.cpp TODO += crepe/api/Force.h -TODO += crepe/api/GameObject.cpp -TODO += crepe/api/GameObject.h -TODO += crepe/api/GameObject.hpp +MAX += crepe/api/GameObject.cpp +MAX += crepe/api/GameObject.h +MAX += crepe/api/GameObject.hpp TODO += crepe/api/ParticleEmitter.cpp TODO += crepe/api/ParticleEmitter.h TODO += crepe/api/Point.h @@ -58,8 +58,8 @@ TODO += crepe/api/Sprite.cpp TODO += crepe/api/Sprite.h TODO += crepe/api/Texture.cpp TODO += crepe/api/Texture.h -TODO += crepe/api/Transform.cpp -TODO += crepe/api/Transform.h +MAX += crepe/api/Transform.cpp +MAX += crepe/api/Transform.h TODO += crepe/facade/SDLApp.cpp TODO += crepe/facade/SDLApp.h TODO += crepe/facade/SDLContext.cpp @@ -88,7 +88,7 @@ LOEK += crepe/util/log.h TODO += example/asset_manager.cpp LOEK += example/audio_internal.cpp TODO += example/components_internal.cpp -TODO += example/ecs.cpp +MAX += example/ecs.cpp LOEK += example/log.cpp TODO += example/particle.cpp TODO += example/physics.cpp @@ -97,7 +97,7 @@ LOEK += example/script.cpp LOEK += test/audio.cpp LOEK += test/dummy.cpp -FMT := $(LOEK) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 +FMT := $(MAX) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 format: FORCE clang-tidy -p build/compile_commands.json --fix-errors $(FMT) -- cgit v1.2.3 From b2dea4e5f127e78fe9ff67e2504b8943085fe3b1 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 10:38:49 +0100 Subject: Make format --- src/crepe/Component.cpp | 1 - src/crepe/Metadata.cpp | 4 ++-- src/crepe/Metadata.h | 3 ++- src/crepe/api/GameObject.cpp | 11 +++++++---- src/crepe/api/GameObject.h | 3 ++- src/crepe/api/Transform.cpp | 4 ++-- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 41e7273..358ce31 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -3,4 +3,3 @@ using namespace crepe; Component::Component(uint32_t id) : game_object_id(id), active(true) {} - diff --git a/src/crepe/Metadata.cpp b/src/crepe/Metadata.cpp index 1ba150a..53d93da 100644 --- a/src/crepe/Metadata.cpp +++ b/src/crepe/Metadata.cpp @@ -3,6 +3,6 @@ using namespace crepe; using namespace std; -Metadata::Metadata(uint32_t game_object_id, const string & name, const string & tag) +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 index b946fd0..1577987 100644 --- a/src/crepe/Metadata.h +++ b/src/crepe/Metadata.h @@ -9,7 +9,8 @@ namespace crepe { class Metadata : public Component { public: - Metadata(uint32_t game_object_id, const std::string & name, const std::string & tag); + Metadata(uint32_t game_object_id, const std::string & name, + const std::string & tag); virtual int get_instances_max() const { return 1; } public: diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 8a1a235..51cd08f 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -6,7 +6,9 @@ using namespace crepe; using namespace std; -GameObject::GameObject(uint32_t id, std::string name, std::string tag, const 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); @@ -16,11 +18,12 @@ void GameObject::set_parent(const GameObject & parent) { auto & mgr = ComponentManager::get_instance(); // set parent on own Metadata component - vector> this_metadata = mgr.get_components_by_id(this->id); + 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); + 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 4c87639..602f33c 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -9,7 +9,8 @@ namespace crepe { class GameObject { public: - GameObject(uint32_t id, std::string name, std::string tag, const Point & position, double rotation, double scale); + GameObject(uint32_t id, std::string name, std::string tag, + const Point & position, double rotation, double scale); void set_parent(const GameObject & parent); template diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index 9c9bb06..1d8d401 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -8,10 +8,10 @@ using namespace crepe; -Transform::Transform(uint32_t game_id, const 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(); } - -- cgit v1.2.3 From db7fd6a24c4da44fed3cee97d24f1cbda1471137 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 6 Nov 2024 12:07:43 +0100 Subject: `make format` --- mwe/events/include/event.h | 2 +- src/crepe/Component.cpp | 1 - src/crepe/Metadata.cpp | 4 ++-- src/crepe/Metadata.h | 3 ++- src/crepe/api/GameObject.cpp | 11 +++++++---- src/crepe/api/GameObject.h | 3 ++- src/crepe/api/Transform.cpp | 4 ++-- src/crepe/system/System.h | 4 ++-- 8 files changed, 18 insertions(+), 14 deletions(-) diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index 3e70201..16c75bf 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 41e7273..358ce31 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -3,4 +3,3 @@ using namespace crepe; Component::Component(uint32_t id) : game_object_id(id), active(true) {} - diff --git a/src/crepe/Metadata.cpp b/src/crepe/Metadata.cpp index 1ba150a..53d93da 100644 --- a/src/crepe/Metadata.cpp +++ b/src/crepe/Metadata.cpp @@ -3,6 +3,6 @@ using namespace crepe; using namespace std; -Metadata::Metadata(uint32_t game_object_id, const string & name, const string & tag) +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 index b946fd0..1577987 100644 --- a/src/crepe/Metadata.h +++ b/src/crepe/Metadata.h @@ -9,7 +9,8 @@ namespace crepe { class Metadata : public Component { public: - Metadata(uint32_t game_object_id, const std::string & name, const std::string & tag); + Metadata(uint32_t game_object_id, const std::string & name, + const std::string & tag); virtual int get_instances_max() const { return 1; } public: diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 8a1a235..51cd08f 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -6,7 +6,9 @@ using namespace crepe; using namespace std; -GameObject::GameObject(uint32_t id, std::string name, std::string tag, const 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); @@ -16,11 +18,12 @@ void GameObject::set_parent(const GameObject & parent) { auto & mgr = ComponentManager::get_instance(); // set parent on own Metadata component - vector> this_metadata = mgr.get_components_by_id(this->id); + 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); + 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 4c87639..602f33c 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -9,7 +9,8 @@ namespace crepe { class GameObject { public: - GameObject(uint32_t id, std::string name, std::string tag, const Point & position, double rotation, double scale); + GameObject(uint32_t id, std::string name, std::string tag, + const Point & position, double rotation, double scale); void set_parent(const GameObject & parent); template diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index 9c9bb06..1d8d401 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -8,10 +8,10 @@ using namespace crepe; -Transform::Transform(uint32_t game_id, const 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(); } - diff --git a/src/crepe/system/System.h b/src/crepe/system/System.h index 8744920..ecbb7f5 100644 --- a/src/crepe/system/System.h +++ b/src/crepe/system/System.h @@ -8,8 +8,8 @@ public: virtual void update() = 0; protected: - System(){}; - virtual ~System(){}; + System() {}; + virtual ~System() {}; private: // singleton -- cgit v1.2.3 From 72095ca7d6e2b316202d963d07065993be0c5f45 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 6 Nov 2024 12:08:33 +0100 Subject: fix code style --- contributing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contributing.md b/contributing.md index e910dba..e88d0a9 100644 --- a/contributing.md +++ b/contributing.md @@ -277,7 +277,7 @@ that you can click on to open them. ```cpp struct Foo { - int bar; + int bar = 0; std::string baz; }; ``` @@ -285,7 +285,7 @@ that you can click on to open them. ```cpp struct Foo { - int bar = 0; + int bar; std::string baz; }; ``` -- cgit v1.2.3 From 8e7f77d7f7ada19b1b08731fc6741c0963aafc3d Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 13:08:51 +0100 Subject: Default value defined in class instead of constructor --- src/crepe/Component.cpp | 2 +- src/crepe/Component.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 358ce31..230bb70 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), active(true) {} +Component::Component(uint32_t id) : game_object_id(id) {} diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 8db9b2a..40e6f49 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -17,7 +17,7 @@ public: public: uint32_t game_object_id; - bool active; + bool active = true; }; } // namespace crepe -- cgit v1.2.3 From e4238e662b26cec586d601b778dec422683b498a Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 6 Nov 2024 13:22:17 +0100 Subject: update contributing.md --- contributing.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/contributing.md b/contributing.md index e88d0a9..8ed3b80 100644 --- a/contributing.md +++ b/contributing.md @@ -49,10 +49,10 @@ that you can click on to open them. class Cars {}; ``` -- Source files contain the following types of comments: +- Source files (.cpp, .hpp) contain the following types of comments: - What is the code supposed to do (optional) - Implementation details (if applicable) -- Header files contain the following types of comments: +- Header files (.h) contain the following types of comments: - Usage documentation (required) - Implementation details (if they affect the header) - Design/data structure decisions (if applicable) @@ -110,7 +110,8 @@ that you can click on to open them. ``` -
- using namespace may not be used in header files, only in source files. + using namespace may not be used in header files (.h, .hpp), only + in source files (.cpp).
GoodBad
example.h: -- cgit v1.2.3 From 4f953a35912c3d92c26bcb6c2cc77cc3ced176a3 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 6 Nov 2024 13:27:52 +0100 Subject: update contributing.md --- contributing.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contributing.md b/contributing.md index 8ed3b80..88cba32 100644 --- a/contributing.md +++ b/contributing.md @@ -54,6 +54,9 @@ that you can click on to open them. - Implementation details (if applicable) - Header files (.h) contain the following types of comments: - Usage documentation (required) + + > [!NOTE] + > Constructors/destructors aren't required to have a `\brief` description - Implementation details (if they affect the header) - Design/data structure decisions (if applicable) -
-- cgit v1.2.3 From 64ca0a7ae147c7d98ff092e04d86468ace6ea2a1 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 14:01:27 +0100 Subject: Added public --- src/crepe/api/AudioSource.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 42add50..1e24ae8 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -10,7 +10,7 @@ namespace crepe { class Sound; //! Audio source component -class AudioSource : Component { +class AudioSource : public Component { public: AudioSource(std::unique_ptr audio_clip); virtual ~AudioSource() = default; -- cgit v1.2.3 From db5a3668aa4b3919ee8dce7ed0872aa5707f80ff Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 14:03:34 +0100 Subject: Added Doxygen comments --- src/crepe/Component.h | 21 ++++++++++ src/crepe/ComponentManager.h | 93 +++++++++++++++++++++++++++++++++++++------- src/crepe/Metadata.h | 20 ++++++++++ src/crepe/api/GameObject.h | 41 ++++++++++++++++++- src/crepe/api/Transform.h | 25 +++++++++--- 5 files changed, 179 insertions(+), 21 deletions(-) diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 40e6f49..02a4e7e 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -6,17 +6,38 @@ namespace crepe { class ComponentManager; +/** + * \brief Base class for all components + * + * This class is the base class for all components. It provides a common + * interface for all components. + */ class Component { protected: + //! Only the ComponentManager can create components friend class crepe::ComponentManager; + /** + * \param id The id of the GameObject this component belongs to + */ Component(uint32_t id); public: virtual ~Component() = default; + /** + * \brief Get the maximum number of instances for this component + * + * This method returns -1 by default, which means that there is no limit + * for the number of instances. Concrete components can override this method + * to set a limit. + * + * \return The maximum number of instances for this component + */ virtual int get_instances_max() const { return -1; } public: + //! The id of the GameObject this component belongs to uint32_t game_object_id; + //! Whether the component is active bool active = true; }; diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 2b5e1df..850aadc 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -10,35 +10,92 @@ namespace crepe { +/** + * \brief Manages all components + * + * This class manages all components. It provides methods to add, delete and get + * components. + */ class ComponentManager { public: - // Singleton + /** + * \brief Get the instance of the ComponentManager + * + * \return The instance of the ComponentManager + */ static ComponentManager & get_instance(); ComponentManager(const ComponentManager &) = delete; ComponentManager(ComponentManager &&) = delete; ComponentManager & operator=(const ComponentManager &) = delete; ComponentManager & operator=(ComponentManager &&) = delete; -public: - //! Add a component of a specific type + /** + * \brief Add a component to the ComponentManager + * + * This method adds a component to the ComponentManager. The component is + * created with the given arguments and added to the ComponentManager. + * + * \tparam T The type of the component + * \tparam Args The types of the arguments + * \param id The id of the GameObject this component belongs to + * \param args The arguments to create the component + * \return The created component + */ template T & add_component(uint32_t id, Args &&... args); - //! Deletes all components of a specific type and id + /** + * \brief Delete all components of a specific type and id + * + * This method deletes all components of a specific type and id. + * + * \tparam T The type of the component + * \param id The id of the GameObject this component belongs to + */ template void delete_components_by_id(uint32_t id); - //! Deletes all components of a specific type + /** + * \brief Delete all components of a specific type + * + * This method deletes all components of a specific type. + * + * \tparam T The type of the component + */ template void delete_components(); - //! Deletes all components of a specific id + /** + * \brief Delete all components of a specific id + * + * This method deletes all components of a specific id. + * + * \param id The id of the GameObject this component belongs to + */ void delete_all_components_of_id(uint32_t id); - //! Deletes all components + /** + * \brief Delete all components + * + * This method deletes all components. + */ void delete_all_components(); - - //! Get a vector<> of all components at specific type and id + /** + * \brief Get all components of a specific type and id + * + * This method gets all components of a specific type and id. + * + * \tparam T The type of the component + * \param id The id of the GameObject this component belongs to + * \return A vector of all components of the specific type and id + */ template std::vector> get_components_by_id(uint32_t id) const; - //! Get a vector<> of all components of a specific type + /** + * \brief Get all components of a specific type + * + * This method gets all components of a specific type. + * + * \tparam T The type of the component + * \return A vector of all components of the specific type + */ template std::vector> get_components_by_type() const; @@ -46,11 +103,17 @@ private: ComponentManager(); virtual ~ComponentManager(); - /* - * The std::unordered_map>>> below might seem a bit strange, let me explain this structure: - * The std::unordered_map<> has a key and value. The key is a std::type_index and the value is a std::vector. So, a new std::vector will be created for each new std::type_index. - * The first std::vector<> stores another vector<>. This first vector<> is to bind the entity's id to a component. - * The second std::vector<> stores unique_ptrs. Each component can be gathered via an unique_ptr. This second vector<> allows multiple components of the same std::type_index for one entity (id). +private: + /** + * \brief The components + * + * This unordered_map stores all components. The key is the type of the + * component and the value is a vector of vectors of unique pointers to the + * components. + * Every component type has its own vector of vectors of unique pointers to + * the components. The first vector is for the ids of the GameObjects and the + * second vector is for the components (because a GameObject might have multiple + * components). */ std::unordered_map>>> diff --git a/src/crepe/Metadata.h b/src/crepe/Metadata.h index 1577987..d52ab67 100644 --- a/src/crepe/Metadata.h +++ b/src/crepe/Metadata.h @@ -7,16 +7,36 @@ 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; }; diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index 602f33c..246c4d4 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -7,15 +7,54 @@ namespace crepe { +/** + * \brief Represents a GameObject + * + * This class represents a GameObject. The GameObject class is only used + * as an interface for the game programmer. The actual implementation is + * done in the ComponentManager. + */ class GameObject { public: + /** + * This constructor creates a new GameObject. It creates a new + * Transform and Metadata component and adds them to the ComponentManager. + * + * \param id The id of the GameObject + * \param name The name of the GameObject + * \param tag The tag of the GameObject + * \param position The position of the GameObject + * \param rotation The rotation of the GameObject + * \param scale The scale of the GameObject + */ GameObject(uint32_t id, std::string name, std::string tag, const Point & position, double rotation, double scale); + /** + * \brief Set the parent of this GameObject + * + * This method sets the parent of this GameObject. It sets the parent + * in the Metadata component of this GameObject and adds this GameObject + * to the children list of the parent GameObject. + * + * \param parent The parent GameObject + */ void set_parent(const GameObject & parent); - + /** + * \brief Add a component to the GameObject + * + * This method adds a component to the GameObject. It forwards the + * arguments to the ComponentManager. + * + * \tparam T The type of the component + * \tparam Args The types of the arguments + * \param args The arguments to create the component + * \return The created component + */ template T & add_component(Args &&... args); +public: + //! The id of the GameObject uint32_t id; }; diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 557061b..02125ef 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -8,15 +8,30 @@ namespace crepe { +/** + * \brief Transform component + * + * This class represents the Transform component. It stores the position, + * rotation and scale of a GameObject. + */ class Transform : public Component { - // FIXME: What's the difference between the `Point` and `Position` - // classes/structs? How about we replace both with a universal `Vec2` that - // works similar (or the same) as those found in GLSL? - public: - Transform(uint32_t id, const Point &, double, double); + /** + * \param id The id of the GameObject this component belongs to + * \param point The position of the GameObject + * \param rot The rotation of the GameObject + * \param scale The scale of the GameObject + */ + Transform(uint32_t id, const Point & point, double rot, double scale); ~Transform(); + /** + * \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: //! Translation (shift) Point position; //! Rotation, in radians -- cgit v1.2.3 From c6ce7e13c5fe802a7eba35189916023925dbfdc1 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 14:16:55 +0100 Subject: Added comments and replaced some auto with concrete types --- src/crepe/ComponentManager.hpp | 7 +------ src/crepe/api/GameObject.cpp | 7 ++++--- src/crepe/api/GameObject.hpp | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index e74f2e9..11c4d15 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -122,8 +122,6 @@ ComponentManager::get_components_by_type() const { // Create an empty vector<> vector> component_vector; - // Set the id to 0 (the id will also be stored in the returned vector<>) - // uint32_t id = 0; // Find the type (in the unordered_map<>) if (components.find(type) == components.end()) return component_vector; @@ -142,12 +140,9 @@ ComponentManager::get_components_by_type() const { // Ensure that the cast was successful if (casted_component == nullptr) continue; - // Pair the dereferenced raw pointer and the id and add it to the vector<> + // Add the dereferenced raw pointer to the vector<> component_vector.emplace_back(ref(*casted_component)); } - - // Increase the id (the id will also be stored in the returned vector<>) - //++id; } // Return the vector<> diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 51cd08f..ffc9510 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -9,20 +9,21 @@ using namespace std; GameObject::GameObject(uint32_t id, std::string name, std::string tag, const Point & position, double rotation, double scale) : id(id) { + // Add Transform and Metadata components ComponentManager & mgr = ComponentManager::get_instance(); mgr.add_component(this->id, position, rotation, scale); mgr.add_component(this->id, name, tag); } void GameObject::set_parent(const GameObject & parent) { - auto & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = ComponentManager::get_instance(); - // set parent on own Metadata component + // 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 + // 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.hpp b/src/crepe/api/GameObject.hpp index 77cf40e..bfba7fe 100644 --- a/src/crepe/api/GameObject.hpp +++ b/src/crepe/api/GameObject.hpp @@ -8,7 +8,7 @@ namespace crepe { template T & GameObject::add_component(Args &&... args) { - auto & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = ComponentManager::get_instance(); return mgr.add_component(this->id, std::forward(args)...); } -- cgit v1.2.3 From bb0dad00c815c9d577214db2788990f560cb10f5 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 14:20:31 +0100 Subject: Include order --- src/crepe/ComponentManager.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 8bde33a..55bd82f 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -1,6 +1,7 @@ -#include "ComponentManager.h" #include "util/log.h" +#include "ComponentManager.h" + using namespace crepe; ComponentManager & ComponentManager::get_instance() { -- cgit v1.2.3 From 254fe777c9e2ed4a6b8e061c12ac350065d979fe Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 14:32:19 +0100 Subject: Changed include to incomplete decleration --- src/crepe/api/GameObject.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index 246c4d4..13aa681 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -3,10 +3,10 @@ #include #include -#include "api/Point.h" - namespace crepe { +class Point; + /** * \brief Represents a GameObject * -- cgit v1.2.3 From 438e87f038b52c3e985d9acc999c610ba5397e54 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 14:32:39 +0100 Subject: Removed two unnecessary includes --- src/crepe/api/Transform.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index 1d8d401..5274b01 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -1,9 +1,7 @@ #include -#include "api/Point.h" #include "util/log.h" -#include "Component.h" #include "Transform.h" using namespace crepe; -- 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(+) 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(-) 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 3617f5c22621be4780bb30021b920fd72189b72b Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 14:45:01 +0100 Subject: Made std::string const reference (instead of copy by value) --- src/crepe/api/GameObject.cpp | 5 +++-- src/crepe/api/GameObject.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index ffc9510..b24b980 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -6,8 +6,9 @@ using namespace crepe; using namespace std; -GameObject::GameObject(uint32_t id, std::string name, std::string tag, - const Point & position, double rotation, double scale) +GameObject::GameObject(uint32_t id, const std::string & name, + const std::string & tag, const Point & position, + double rotation, double scale) : id(id) { // Add Transform and Metadata components ComponentManager & mgr = ComponentManager::get_instance(); diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index 13aa681..2a82258 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -27,7 +27,7 @@ public: * \param rotation The rotation of the GameObject * \param scale The scale of the GameObject */ - GameObject(uint32_t id, std::string name, std::string tag, + GameObject(uint32_t id, const std::string & name, const std::string & tag, const Point & position, double rotation, double scale); /** * \brief Set the parent of this GameObject -- cgit v1.2.3 From 5af0b90f11929ca99a48389ef29d8a56f39b0efd Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 14:49:18 +0100 Subject: Made the destructor public and non-virtual (because virtual is not necesary) --- src/crepe/ComponentManager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 850aadc..f3b0ace 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -28,6 +28,7 @@ public: ComponentManager(ComponentManager &&) = delete; ComponentManager & operator=(const ComponentManager &) = delete; ComponentManager & operator=(ComponentManager &&) = delete; + ~ComponentManager(); /** * \brief Add a component to the ComponentManager @@ -101,7 +102,6 @@ public: private: ComponentManager(); - virtual ~ComponentManager(); private: /** -- 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 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 20d4d711f8d300682d560548ec1a5501ce7b9978 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 16:31:16 +0100 Subject: Added extra code-style guidelines --- contributing.md | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/contributing.md b/contributing.md index 88cba32..91cd12b 100644 --- a/contributing.md +++ b/contributing.md @@ -421,6 +421,118 @@ that you can click on to open them. void bar(Point p); ```
+-
+ Follow the rule-of-five +
GoodBad
+ + ```cpp + class Foo { + public: + Foo(); + ~Foo(); + Foo(const Foo &); + Foo(Foo &&) noexcept; + Foo & operator=(const Foo &); + Foo & operator=(Foo &&) noexcept; + }; + ``` + + + ```cpp + class Foo { + public: + Foo(); + ~Foo(); + Foo(const Foo &); + }; + ``` +
+-
+ Ensure const-correctness +
GoodBad
+ + ```cpp + class Foo { + public: + int get_value() const; + void set_value(int new_value); + const std::string & get_name() const; + void set_name(const std::string & new_name); + private: + int value; + std::string name; + }; + ``` + + + ```cpp + class Foo { + public: + int get_value(); + void set_value(int new_value); + std::string get_name(); + void set_name(std::string new_name); + private: + int value; + std::string name; + }; + ``` +
+-
+ File names (.h/.cpp/.hpp) should be written using CamelCase +
GoodBad
+ + ```cpp + MyClass.h + MyClass.cpp + MyClass.hpp + ``` + + + ```cpp + my_class.h + my_class.cpp + my_class.hpp + ``` +
+-
+ Implementation is not allowed in header files, except if the method only returns a constant value +
GoodBad
+ + ```cpp + class Foo { + public: + int get_value() const { return 42; } + }; + ``` + + + ```cpp + class Foo { + public: + int calculate_value() const { + int result = 0; + // complex calculation + return result; + } + }; + ``` +
+-
+ Use angle brackets (`<>`) for including libraries and double quotes (`""`) for including local files. +
GoodBad
+ + ```cpp + #include + #include "MyClass.h" + ``` + + + ```cpp + #include "iostream" + #include + ``` +
## CMakeLists-specific -- cgit v1.2.3 From 0438e0f4d129431f7ecef7996a11b301eda882fb Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 16:34:09 +0100 Subject: Clarified --- contributing.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contributing.md b/contributing.md index 91cd12b..014df51 100644 --- a/contributing.md +++ b/contributing.md @@ -479,7 +479,7 @@ that you can click on to open them. ``` -
- File names (.h/.cpp/.hpp) should be written using CamelCase + File names (.h, .cpp, .hpp) should be written using CamelCase
GoodBad
```cpp @@ -491,8 +491,8 @@ that you can click on to open them. ```cpp my_class.h - my_class.cpp - my_class.hpp + myClass.cpp + my-class.hpp ```
-
-- cgit v1.2.3 From 9e9549a8b70454610eec1b74c44eeaa78507b92c Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 16:39:05 +0100 Subject: Used --- contributing.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contributing.md b/contributing.md index 014df51..8da91ee 100644 --- a/contributing.md +++ b/contributing.md @@ -49,7 +49,7 @@ that you can click on to open them. class Cars {}; ```
-- Source files (.cpp, .hpp) contain the following types of comments: +- Source files (.cpp, .hpp) contain the following types of comments: - What is the code supposed to do (optional) - Implementation details (if applicable) - Header files (.h) contain the following types of comments: @@ -479,7 +479,7 @@ that you can click on to open them. ``` -
- File names (.h, .cpp, .hpp) should be written using CamelCase + File names (.h, .cpp, .hpp) should be written using CamelCase
GoodBad
```cpp @@ -519,7 +519,7 @@ that you can click on to open them. ```
-
- Use angle brackets (`<>`) for including libraries and double quotes (`""`) for including local files. + Use angle brackets (<>) for including libraries and double quotes ("") for including local files.
GoodBad
```cpp -- cgit v1.2.3 From b7696c14052e0583519ed2315b52d39f243ff916 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 16:39:54 +0100 Subject: Enter too much --- contributing.md | 1 - 1 file changed, 1 deletion(-) diff --git a/contributing.md b/contributing.md index 8da91ee..695b99b 100644 --- a/contributing.md +++ b/contributing.md @@ -54,7 +54,6 @@ that you can click on to open them. - Implementation details (if applicable) - Header files (.h) contain the following types of comments: - Usage documentation (required) - > [!NOTE] > Constructors/destructors aren't required to have a `\brief` description - Implementation details (if they affect the header) -- cgit v1.2.3 From e097ed023c3c0cf98b368f57941b16a05725f481 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 16:44:34 +0100 Subject: Added hyperlink to # documentation --- contributing.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/contributing.md b/contributing.md index 695b99b..f6805ac 100644 --- a/contributing.md +++ b/contributing.md @@ -53,12 +53,9 @@ that you can click on to open them. - What is the code supposed to do (optional) - Implementation details (if applicable) - Header files (.h) contain the following types of comments: - - Usage documentation (required) + -
Usage documentation (required) > [!NOTE] > Constructors/destructors aren't required to have a `\brief` description - - Implementation details (if they affect the header) - - Design/data structure decisions (if applicable) --
Comments are placed *above* the line(s) they are explaining
GoodBad
-- cgit v1.2.3 From 7887904a68d000fa602a2eff1aabdff6a6f74a44 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 16:46:01 +0100 Subject: Fix --- contributing.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contributing.md b/contributing.md index f6805ac..ea8d724 100644 --- a/contributing.md +++ b/contributing.md @@ -56,6 +56,9 @@ that you can click on to open them. -
Usage documentation (required) > [!NOTE] > Constructors/destructors aren't required to have a `\brief` description + - Implementation details (if they affect the header) + - Design/data structure decisions (if applicable) +-
Comments are placed *above* the line(s) they are explaining
GoodBad
-- cgit v1.2.3 From 0c44c5eb59cd95990f24f11c2ac1ce1fd49537ab Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 16:47:39 +0100 Subject: Fix --- contributing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contributing.md b/contributing.md index ea8d724..1e2cc87 100644 --- a/contributing.md +++ b/contributing.md @@ -53,7 +53,7 @@ that you can click on to open them. - What is the code supposed to do (optional) - Implementation details (if applicable) - Header files (.h) contain the following types of comments: - -
Usage documentation (required) + - Usage documentation (required) > [!NOTE] > Constructors/destructors aren't required to have a `\brief` description - Implementation details (if they affect the header) -- cgit v1.2.3 From b585bd7b981279b90ffd11e0feb51748b888833c Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 6 Nov 2024 16:48:29 +0100 Subject: Fix --- contributing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contributing.md b/contributing.md index 1e2cc87..cea6e37 100644 --- a/contributing.md +++ b/contributing.md @@ -53,7 +53,7 @@ that you can click on to open them. - What is the code supposed to do (optional) - Implementation details (if applicable) - Header files (.h) contain the following types of comments: - - Usage documentation (required) + - Usage documentation (required) > [!NOTE] > Constructors/destructors aren't required to have a `\brief` description - Implementation details (if they affect the header) -- cgit v1.2.3 From 32bc6d21987e9861d9089228388f9aab5f4f43d1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 6 Nov 2024 18:25:10 +0100 Subject: merge #21 --- contributing.md | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/contributing.md b/contributing.md index cea6e37..cd1b6a6 100644 --- a/contributing.md +++ b/contributing.md @@ -49,11 +49,11 @@ that you can click on to open them. class Cars {}; ```
-- Source files (.cpp, .hpp) contain the following types of comments: +- Source files (`.cpp`, `.hpp`) contain the following types of comments: - What is the code supposed to do (optional) - Implementation details (if applicable) -- Header files (.h) contain the following types of comments: - - Usage documentation (required) +- Header files (`.h`) contain the following types of comments: + - [Usage documentation](#documentation) (required) > [!NOTE] > Constructors/destructors aren't required to have a `\brief` description - Implementation details (if they affect the header) @@ -421,7 +421,7 @@ that you can click on to open them. ```
-
- Follow the rule-of-five + Follow the rule of five
GoodBad
```cpp @@ -429,10 +429,9 @@ that you can click on to open them. public: Foo(); ~Foo(); - Foo(const Foo &); Foo(Foo &&) noexcept; - Foo & operator=(const Foo &); - Foo & operator=(Foo &&) noexcept; + Foo & operator = (const Foo &); + Foo & operator = (Foo &&) noexcept; }; ``` @@ -442,7 +441,6 @@ that you can click on to open them. public: Foo(); ~Foo(); - Foo(const Foo &); }; ```
@@ -478,7 +476,7 @@ that you can click on to open them. ```
-
- File names (.h, .cpp, .hpp) should be written using CamelCase + Files should be named after the class/struct/interface they implement
GoodBad
```cpp @@ -495,7 +493,12 @@ that you can click on to open them. ```
-
- Implementation is not allowed in header files, except if the method only returns a constant value + Implementations are not allowed in header files, except if the implementation + + - is `= default` + - is `= delete` + - is `{}` (empty) + - only returns a constant literal
GoodBad
```cpp @@ -518,18 +521,24 @@ that you can click on to open them. ```
-
- Use angle brackets (<>) for including libraries and double quotes ("") for including local files. + Use angle brackets (<>) only for including system headers and + double quotes ("") for including other engine files. + + > [!NOTE] + > Only files in the examples folder should include engine headers with angle + > brackets
GoodBad
```cpp #include - #include "MyClass.h" + + #include "facade/Sound.h" ``` ```cpp - #include "iostream" - #include + #include + #include ```
-- 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(-) 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 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(-) 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 e0db2fa06232e9f081df6dac2e360195becae17c Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 09:43:17 +0100 Subject: Added this-> --- src/crepe/ComponentManager.cpp | 2 +- src/crepe/ComponentManager.hpp | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 55bd82f..01bc8d7 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -11,7 +11,7 @@ ComponentManager & ComponentManager::get_instance() { void ComponentManager::delete_all_components_of_id(uint32_t id) { // Loop through all the types (in the unordered_map<>) - for (auto & [type, componentArray] : components) { + for (auto & [type, componentArray] : this->components) { // Make sure that the id (that we are looking for) is within the boundaries of the vector<> if (id < componentArray.size()) { // Clear the components at this specific id diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 11c4d15..eac38fc 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -17,15 +17,15 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { type_index type = typeid(T); // Check if this component type is already in the unordered_map<> - if (components.find(type) == components.end()) { + if (this->components.find(type) == this->components.end()) { //If not, create a new (empty) vector<> of vector> - components[type] = vector>>(); + this->components[type] = vector>>(); } // Resize the vector<> if the id is greater than the current size - if (id >= components[type].size()) { + if (id >= this->components[type].size()) { // Initialize new slots to nullptr (resize does automatically init to nullptr) - components[type].resize(id + 1); + this->components[type].resize(id + 1); } // Create a new component of type T (arguments directly forwarded). The @@ -35,14 +35,14 @@ 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()) { + && this->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(std::move(instance)); + this->components[type][id].push_back(std::move(instance)); return *instance; } @@ -55,10 +55,10 @@ void ComponentManager::delete_components_by_id(uint32_t id) { type_index type = typeid(T); // Find the type (in the unordered_map<>) - if (components.find(type) != components.end()) { + if (this->components.find(type) != this->components.end()) { // Get the correct vector<> vector>> & component_array - = components[type]; + = this->components[type]; // Make sure that the id (that we are looking for) is within the boundaries of the vector<> if (id < component_array.size()) { @@ -73,9 +73,9 @@ void ComponentManager::delete_components() { // Determine the type of T (this is used as the key of the unordered_map<>) std::type_index type = typeid(T); - if (components.find(type) == components.end()) return; + if (this->components.find(type) == this->components.end()) return; - components[type].clear(); + this->components[type].clear(); } template @@ -89,11 +89,11 @@ ComponentManager::get_components_by_id(uint32_t id) const { // Create an empty vector<> vector> component_vector; - if (components.find(type) == components.end()) return component_vector; + if (this->components.find(type) == this->components.end()) return component_vector; // Get the correct vector<> const vector>> & component_array - = components.at(type); + = this->components.at(type); // Make sure that the id (that we are looking for) is within the boundaries of the vector<> if (id >= component_array.size()) return component_vector; @@ -124,11 +124,11 @@ ComponentManager::get_components_by_type() const { vector> component_vector; // Find the type (in the unordered_map<>) - if (components.find(type) == components.end()) return component_vector; + if (this->components.find(type) == this->components.end()) return component_vector; // Get the correct vector<> const vector>> & component_array - = components.at(type); + = this->components.at(type); // Loop through the whole vector<> for (const vector> & component : component_array) { -- cgit v1.2.3 From fd60c0280440da053948f7500657ae65b94036bf Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 09:45:08 +0100 Subject: Improved RAII --- src/crepe/ComponentManager.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index eac38fc..e52b679 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -30,8 +30,7 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { // Create a new component of type T (arguments directly forwarded). The // constructor must be called by ComponentManager. - T * instance_pointer = new T(id, forward(args)...); - unique_ptr instance = unique_ptr(instance_pointer); + unique_ptr instance = unique_ptr(new T(id, forward(args)...)); // Check if the vector size is not greater than get_instances_max if (instance->get_instances_max() != -1 -- 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(-) 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 098ae26d16188b276af77b8bfe9975fcabe8d7f4 Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 10:10:07 +0100 Subject: Added code styles --- contributing.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/contributing.md b/contributing.md index cd1b6a6..c7f836e 100644 --- a/contributing.md +++ b/contributing.md @@ -541,6 +541,53 @@ that you can click on to open them. #include ``` +-
+ Ensure exception safety by using RAII classes +
GoodBad
+ + ```cpp + std::unique_ptr foo = std::make_unique(); + ``` + + + ```cpp + Foo* foo = new Foo(); + // ... + delete foo; + ``` +
+-
+ Do not use malloc, calloc, or free (instead use new and delete) +
GoodBad
+ + ```cpp + Foo* foo = new Foo(); + delete foo; + ``` + + + ```cpp + Foo* foo = (Foo*)malloc(sizeof(Foo)); + free(foo); + ``` +
+-
+ Prefix member variables with this-> +
GoodBad
+ + ```cpp + void Foo::set_value(int value) { + this->value = value; + } + ``` + + + ```cpp + void Foo::set_value(int value) { + value = value; + } + ``` +
## CMakeLists-specific -- cgit v1.2.3 From 0756fe048916e696431e1ae99f95ada06d0de7a0 Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 10:10:44 +0100 Subject: Fix --- contributing.md | 1 - 1 file changed, 1 deletion(-) diff --git a/contributing.md b/contributing.md index c7f836e..9f39e41 100644 --- a/contributing.md +++ b/contributing.md @@ -523,7 +523,6 @@ that you can click on to open them. -
Use angle brackets (<>) only for including system headers and double quotes ("") for including other engine files. - > [!NOTE] > Only files in the examples folder should include engine headers with angle > brackets -- cgit v1.2.3 From dc093939558e2d71ad3dd322f1f75dfba893783f Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 10:11:09 +0100 Subject: Fix --- contributing.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contributing.md b/contributing.md index 9f39e41..c7f836e 100644 --- a/contributing.md +++ b/contributing.md @@ -523,6 +523,7 @@ that you can click on to open them. -
Use angle brackets (<>) only for including system headers and double quotes ("") for including other engine files. + > [!NOTE] > Only files in the examples folder should include engine headers with angle > brackets -- cgit v1.2.3 From 54f4a322e939dcdb62ab9fbf919bf8cb4b6f7b75 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 7 Nov 2024 10:46:46 +0100 Subject: fix script example segmentation fault --- src/crepe/ComponentManager.hpp | 14 +++++++++----- src/crepe/api/BehaviorScript.h | 5 +---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index e74f2e9..89ed111 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -30,12 +30,16 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { // Create a new component of type T (arguments directly forwarded). The // constructor must be called by ComponentManager. - T * instance_pointer = new T(id, forward(args)...); - unique_ptr instance = unique_ptr(instance_pointer); + T * instance_ptr = new T(id, forward(args)...); + if (instance_ptr == nullptr) + throw std::bad_alloc(); + + T & instance_ref = *instance_ptr; + unique_ptr instance = unique_ptr(instance_ptr); // 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()) { + int max_instances = instance->get_instances_max(); + if (max_instances != -1 && components[type][id].size() >= max_instances) { // TODO: Exception throw std::runtime_error( "Exceeded maximum number of instances for this component type"); @@ -44,7 +48,7 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { // store its unique_ptr in the vector<> components[type][id].push_back(std::move(instance)); - return *instance; + return instance_ref; } template diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 21638f4..6b1fec7 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -5,12 +5,9 @@ #include "../Component.h" namespace crepe { + class ScriptSystem; class ComponentManager; -} // namespace crepe - -namespace crepe { - class Script; class BehaviorScript : public Component { -- cgit v1.2.3 From cd0940f72e1e0d4abb0bc1ef5fb481b389a1f77a Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 12:25:17 +0100 Subject: Removed destructor --- src/crepe/api/Transform.cpp | 2 -- src/crepe/api/Transform.h | 1 - 2 files changed, 3 deletions(-) diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index 5274b01..be1769e 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -11,5 +11,3 @@ Transform::Transform(uint32_t game_id, const Point & point, double rot, : Component(game_id), position(point), rotation(rot), scale(scale) { dbg_trace(); } - -Transform::~Transform() { dbg_trace(); } diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 02125ef..69ea48f 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -23,7 +23,6 @@ public: * \param scale The scale of the GameObject */ Transform(uint32_t id, const Point & point, double rot, double scale); - ~Transform(); /** * \brief Get the maximum number of instances for this component * -- cgit v1.2.3 From ce8debc03d2ed291285535462cbf4411bf298278 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 7 Nov 2024 12:28:39 +0100 Subject: update contributing.md --- contributing.md | 65 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/contributing.md b/contributing.md index c7f836e..ba8ad68 100644 --- a/contributing.md +++ b/contributing.md @@ -78,8 +78,8 @@ that you can click on to open them. } ```
-- Header includes are split into paragraphs separated by a blank line. The - order is: +- Header includes (at the top of files) are split into paragraphs separated by + a blank line. The order is: 1. system headers (using `<`brackets`>`) 2. relative headers NOT in the same folder as the current file 3. relative headers in the same folder as the current file @@ -111,6 +111,52 @@ that you can click on to open them. #include "api/Sprite.h" ```
+-
+ If there is one, the matching template header (.hpp) is included + at the bottom of the regular header (.h) +
GoodBad
+ + Foo.h: + ```cpp + #pragma once + + template + void foo(); + + #include "Foo.hpp" + ``` + + Foo.hpp: + ```cpp + #pragma once + #include "Foo.h" + + template + void foo() { + // ... + } + ``` + + + Foo.h: + ```cpp + #pragma once + + template + void foo(); + ``` + + Foo.hpp: + ```cpp + #pragma once + #include "Foo.h" + + template + void foo() { + // ... + } + ``` +
-
using namespace may not be used in header files (.h, .hpp), only in source files (.cpp). @@ -546,7 +592,7 @@ that you can click on to open them.
GoodBad
```cpp - std::unique_ptr foo = std::make_unique(); + auto foo = std::make_unique(); ``` @@ -557,22 +603,23 @@ that you can click on to open them. ```
-
- Do not use malloc, calloc, or free (instead use new and delete) + Do not use C-style memory management APIs (malloc, + calloc, free)
GoodBad
```cpp - Foo* foo = new Foo(); + Foo * foo = new Foo(); delete foo; ``` ```cpp - Foo* foo = (Foo*)malloc(sizeof(Foo)); + Foo * foo = (Foo *) malloc(sizeof(Foo)); free(foo); ```
-
- Prefix member variables with this-> + Prefix all class members with this->
GoodBad
```cpp @@ -583,8 +630,8 @@ that you can click on to open them. ```cpp - void Foo::set_value(int value) { - value = value; + void Foo::set_value(int new_value) { + value = new_value; } ```
-- 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(-) 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(-) 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 d7b17796943e2cc09bf4de853a9c1a19d3cb6786 Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 12:37:28 +0100 Subject: Made id const --- src/crepe/api/GameObject.cpp | 14 +++++++------- src/crepe/api/GameObject.h | 2 +- src/crepe/api/GameObject.hpp | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index b24b980..b1b8f85 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -9,11 +9,11 @@ using namespace std; GameObject::GameObject(uint32_t id, const std::string & name, const std::string & tag, const Point & position, double rotation, double scale) - : id(id) { + : ID(id) { // Add Transform and Metadata components ComponentManager & mgr = ComponentManager::get_instance(); - mgr.add_component(this->id, position, rotation, scale); - mgr.add_component(this->id, name, tag); + mgr.add_component(this->ID, position, rotation, scale); + mgr.add_component(this->ID, name, tag); } void GameObject::set_parent(const GameObject & parent) { @@ -21,11 +21,11 @@ void GameObject::set_parent(const GameObject & parent) { // Set parent on own Metadata component vector> this_metadata - = mgr.get_components_by_id(this->id); - this_metadata.at(0).get().parent = parent.id; + = 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); + = 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 2a82258..2992787 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -55,7 +55,7 @@ public: public: //! The id of the GameObject - uint32_t id; + const uint32_t ID; }; } // namespace crepe diff --git a/src/crepe/api/GameObject.hpp b/src/crepe/api/GameObject.hpp index bfba7fe..7e6148c 100644 --- a/src/crepe/api/GameObject.hpp +++ b/src/crepe/api/GameObject.hpp @@ -9,7 +9,7 @@ namespace crepe { template T & GameObject::add_component(Args &&... args) { ComponentManager & mgr = ComponentManager::get_instance(); - return mgr.add_component(this->id, std::forward(args)...); + return mgr.add_component(this->ID, std::forward(args)...); } } // namespace crepe -- cgit v1.2.3 From b4834c99c1afced63ee0c266b38925b95782bdf6 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 7 Nov 2024 13:32:48 +0100 Subject: merge #20 --- src/crepe/ComponentManager.hpp | 3 +-- src/crepe/api/SceneManager.cpp | 21 ++++++++------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 489e188..cf9f65a 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -31,8 +31,7 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { // Create a new component of type T (arguments directly forwarded). The // constructor must be called by ComponentManager. T * instance_ptr = new T(id, forward(args)...); - if (instance_ptr == nullptr) - throw std::bad_alloc(); + if (instance_ptr == nullptr) throw std::bad_alloc(); T & instance_ref = *instance_ptr; unique_ptr instance = unique_ptr(instance_ptr); diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/api/SceneManager.cpp index 57ec302..dfed6ee 100644 --- a/src/crepe/api/SceneManager.cpp +++ b/src/crepe/api/SceneManager.cpp @@ -13,24 +13,20 @@ SceneManager & SceneManager::get_instance() { return instance; } -void SceneManager::set_next_scene(const string & name) { - next_scene = name; -} +void SceneManager::set_next_scene(const string & name) { next_scene = name; } void SceneManager::load_next_scene() { // next scene not set - if (this->next_scene.empty()) - return; + 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; - } - ); + 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; + if (it == this->scenes.end()) return; unique_ptr & scene = *it; // Delete all components of the current scene @@ -40,4 +36,3 @@ void SceneManager::load_next_scene() { // Load the new scene scene->load_scene(); } - -- 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(-) 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