From aa8755148cbca29b584f7a146636b506070717f9 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 6 Oct 2024 15:28:12 +0200 Subject: move components to separate files --- src/crepe/Component.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/crepe/Component.h (limited to 'src/crepe/Component.h') diff --git a/src/crepe/Component.h b/src/crepe/Component.h new file mode 100644 index 0000000..6e23d93 --- /dev/null +++ b/src/crepe/Component.h @@ -0,0 +1,15 @@ +#pragma once + +namespace crepe { + +class Component { +public: + Component(); + // TODO: shouldn't this constructor be deleted because this class will never + // directly be instantiated? + + bool active; +}; + +} // namespace crepe + -- cgit v1.2.3 From 8a509ad4c5e15fbf7eaade1c8bf4834f0d0069c5 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 6 Oct 2024 15:29:39 +0200 Subject: `make format` --- src/crepe/Collider.cpp | 1 - src/crepe/Collider.h | 3 +-- src/crepe/Component.cpp | 1 - src/crepe/Component.h | 1 - src/crepe/ComponentManager.h | 1 - src/crepe/GameObject.h | 1 - src/crepe/Rigidbody.cpp | 1 - src/crepe/Rigidbody.h | 2 +- src/crepe/Sprite.cpp | 1 - src/crepe/Sprite.h | 3 +-- src/example/components_internal.cpp | 47 +++++++++++++++++++------------------ 11 files changed, 27 insertions(+), 35 deletions(-) (limited to 'src/crepe/Component.h') diff --git a/src/crepe/Collider.cpp b/src/crepe/Collider.cpp index b7040eb..3f12afd 100644 --- a/src/crepe/Collider.cpp +++ b/src/crepe/Collider.cpp @@ -3,4 +3,3 @@ using namespace crepe; Collider::Collider(int size) : size(size) {} - diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h index 666386d..120da05 100644 --- a/src/crepe/Collider.h +++ b/src/crepe/Collider.h @@ -11,5 +11,4 @@ public: int size; }; -} - +} // namespace crepe diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 9a7731a..d14159c 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -3,4 +3,3 @@ using namespace crepe; Component::Component() : active(true) {} - diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 6e23d93..16a4ce5 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -12,4 +12,3 @@ public: }; } // namespace crepe - diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 9463558..2ab9dc8 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -60,4 +60,3 @@ private: } // namespace crepe #include "ComponentManager.hpp" - diff --git a/src/crepe/GameObject.h b/src/crepe/GameObject.h index d5a7c36..3588d9a 100644 --- a/src/crepe/GameObject.h +++ b/src/crepe/GameObject.h @@ -22,4 +22,3 @@ public: } // namespace crepe #include "GameObject.hpp" - diff --git a/src/crepe/Rigidbody.cpp b/src/crepe/Rigidbody.cpp index 1518d28..495d908 100644 --- a/src/crepe/Rigidbody.cpp +++ b/src/crepe/Rigidbody.cpp @@ -4,4 +4,3 @@ using namespace crepe; Rigidbody::Rigidbody(int mass, int gravityScale, int bodyType) : mass(mass), gravity_scale(gravityScale), body_type(bodyType) {} - diff --git a/src/crepe/Rigidbody.h b/src/crepe/Rigidbody.h index 67edb03..63a8877 100644 --- a/src/crepe/Rigidbody.h +++ b/src/crepe/Rigidbody.h @@ -13,4 +13,4 @@ public: int body_type; }; -} +} // namespace crepe diff --git a/src/crepe/Sprite.cpp b/src/crepe/Sprite.cpp index c835d83..a5a5e68 100644 --- a/src/crepe/Sprite.cpp +++ b/src/crepe/Sprite.cpp @@ -6,4 +6,3 @@ using namespace crepe; using namespace std; Sprite::Sprite(string path) : path(path) {} - diff --git a/src/crepe/Sprite.h b/src/crepe/Sprite.h index 029064b..143e702 100644 --- a/src/crepe/Sprite.h +++ b/src/crepe/Sprite.h @@ -13,5 +13,4 @@ public: std::string path; }; -} - +} // namespace crepe diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp index 4246dd7..54ce295 100644 --- a/src/example/components_internal.cpp +++ b/src/example/components_internal.cpp @@ -6,13 +6,13 @@ #include #include -#include +#include +#include #include #include -#include -#include #include -#include +#include +#include using namespace crepe; using namespace std; @@ -24,36 +24,37 @@ int main() { auto & mgr = ComponentManager::get_instance(); - auto start_adding = chrono::high_resolution_clock::now(); + auto start_adding = chrono::high_resolution_clock::now(); - GameObject * game_object[OBJ_COUNT]; + GameObject * game_object[OBJ_COUNT]; - for (int i = 0; i < OBJ_COUNT; ++i) { - game_object[i] = new GameObject(i, "Name", "Tag", 0); + for (int i = 0; i < OBJ_COUNT; ++i) { + game_object[i] = new GameObject(i, "Name", "Tag", 0); - game_object[i]->add_component("test"); - game_object[i]->add_component(0, 0, i); - game_object[i]->add_component(i); - } + game_object[i]->add_component("test"); + game_object[i]->add_component(0, 0, i); + game_object[i]->add_component(i); + } - auto stop_adding = chrono::high_resolution_clock::now(); + auto stop_adding = chrono::high_resolution_clock::now(); - auto sprites = mgr.get_components_by_type(); - for (auto sprite : sprites) { + auto sprites = mgr.get_components_by_type(); + for (auto sprite : sprites) { assert(sprite.get().path == "test"); - } + } - auto stop_looping = chrono::high_resolution_clock::now(); + auto stop_looping = chrono::high_resolution_clock::now(); - for (int i = 0; i < OBJ_COUNT; ++i) { - delete game_object[i]; - } + for (int i = 0; i < OBJ_COUNT; ++i) { + delete game_object[i]; + } - auto add_time = chrono::duration_cast(stop_adding - start_adding); - auto loop_time = chrono::duration_cast(stop_looping - stop_adding); + auto add_time = chrono::duration_cast(stop_adding + - start_adding); + auto loop_time = chrono::duration_cast(stop_looping + - stop_adding); printf("add time: %ldus\n", add_time.count()); printf("loop time: %ldus\n", loop_time.count()); return 0; } - -- cgit v1.2.3 From 5d041cce13da66aa3457d236579a9ac90ebf7618 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 12 Oct 2024 20:54:07 +0200 Subject: fix constructor/destructor member visibility for component manager --- src/crepe/Component.cpp | 1 - src/crepe/Component.h | 6 ++- src/crepe/ComponentManager.cpp | 12 +++++- src/crepe/ComponentManager.h | 3 +- src/crepe/ComponentManager.hpp | 86 +++++++++++++++++++----------------------- src/crepe/api/BehaviorScript.h | 7 +++- src/crepe/util/log.h | 2 +- 7 files changed, 61 insertions(+), 56 deletions(-) (limited to 'src/crepe/Component.h') diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index d14159c..737f30a 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -2,4 +2,3 @@ using namespace crepe; -Component::Component() : active(true) {} diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 16a4ce5..00b2164 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -3,12 +3,14 @@ namespace crepe { class Component { +protected: + Component() = default; public: - Component(); + virtual ~Component() = default; // TODO: shouldn't this constructor be deleted because this class will never // directly be instantiated? - bool active; + bool active = true; }; } // namespace crepe diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 9a3fec7..8aa9d4e 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -1,3 +1,4 @@ +#include "util/log.h" #include "ComponentManager.h" using namespace crepe; @@ -20,5 +21,14 @@ void ComponentManager::delete_all_components_of_id(uint32_t id) { void ComponentManager::delete_all_components() { // Clear the whole unordered_map<> - components.clear(); + this->components.clear(); } + +ComponentManager::ComponentManager() { + dbg_trace(); +} + +ComponentManager::~ComponentManager() { + dbg_trace(); +} + diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 2ab9dc8..eab9b45 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -44,7 +44,8 @@ public: std::vector> get_components_by_type() const; private: - ComponentManager() = default; + ComponentManager(); + virtual ~ComponentManager(); /* * The std::unordered_map>>> below might seem a bit strange, let me explain this structure: diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 084cd33..8fc1cba 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -60,11 +60,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); - // Find the type (in the unordered_map<>) - if (components.find(type) != components.end()) { - // Clear the whole vector<> of this specific type - components[type].clear(); - } + if (components.find(type) == components.end()) return; + + components[type].clear(); } template @@ -78,30 +76,25 @@ ComponentManager::get_components_by_id(uint32_t id) const { // Create an empty vector<> vector> component_vector; - // Find the type (in the unordered_map<>) - if (components.find(type) != components.end()) { - // Get the correct vector<> - const vector>> & component_array - = components.at(type); + if (components.find(type) == components.end()) return component_vector; - // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id < component_array.size()) { - // Loop trough the whole vector<> - for (const unique_ptr & component_ptr : - component_array[id]) { - // Cast the unique_ptr to a raw pointer - T * casted_component = static_cast(component_ptr.get()); - - // Ensure that the cast was successful - if (casted_component) { - // Add the dereferenced raw pointer to the vector<> - component_vector.push_back(*casted_component); - } - } - } + // Get the correct vector<> + const vector>> & component_array = 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; + + // Loop trough the whole vector<> + for (const unique_ptr & component_ptr : component_array[id]) { + // Cast the unique_ptr to a raw pointer + T * casted_component = static_cast(component_ptr.get()); + + if (casted_component == nullptr) continue; + + // Add the dereferenced raw pointer to the vector<> + component_vector.push_back(*casted_component); } - // Return the vector<> return component_vector; } @@ -119,30 +112,27 @@ ComponentManager::get_components_by_type() const { // uint32_t id = 0; // Find the type (in the unordered_map<>) - if (components.find(type) != components.end()) { + if (components.find(type) == components.end()) return component_vector; - // Get the correct vector<> - const vector>> & component_array - = components.at(type); - - // Loop through the whole vector<> - for (const vector> & component : - component_array) { - // Loop trough the whole vector<> - for (const unique_ptr & component_ptr : component) { - // Cast the unique_ptr to a raw pointer - T * casted_component = static_cast(component_ptr.get()); - - // Ensure that the cast was successful - if (casted_component) { - // Pair the dereferenced raw pointer and the id and add it to the vector<> - component_vector.emplace_back(ref(*casted_component)); - } - } - - // Increase the id (the id will also be stored in the returned vector<>) - //++id; + // Get the correct vector<> + const vector>> & component_array = components.at(type); + + // Loop through the whole vector<> + for (const vector> & component : component_array) { + // Loop trough the whole vector<> + for (const unique_ptr & component_ptr : component) { + // Cast the unique_ptr to a raw pointer + T * casted_component = static_cast(component_ptr.get()); + + // 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<> + 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/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index e9542c1..ba60a8c 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -6,11 +6,14 @@ namespace crepe::api { class BehaviorScript : public Script, public Component { +protected: // only allow ComponentManager to instantiate scripts friend class ComponentManager; - -protected: BehaviorScript(); +public: + // but allow uniqe_ptr to call the destructor (THIS IS VERY IMPORTANT) + virtual ~BehaviorScript() = default; + }; } diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h index 2b0fbe1..fa5f633 100644 --- a/src/crepe/util/log.h +++ b/src/crepe/util/log.h @@ -15,7 +15,7 @@ // very illegal global function-style macros // NOLINTBEGIN #define dbg_logf(fmt, ...) _crepe_logf_here(": " fmt, __VA_ARGS__) -#define dbg_log(str) _crepe_logf_here(": %s", str) +#define dbg_log(str) _crepe_logf_here("%s: " str, "") #define dbg_trace() _crepe_logf_here("%s", "") // NOLINTEND -- cgit v1.2.3 From a2c719948ff8af9abe267c4d1c9b16cd1e2fb317 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 17 Oct 2024 17:15:53 +0200 Subject: `make format` --- src/crepe/Component.cpp | 1 - src/crepe/Component.h | 1 + src/crepe/ComponentManager.cpp | 11 +++-------- src/crepe/ComponentManager.hpp | 9 ++++++--- src/crepe/ScriptSystem.cpp | 17 ++++++----------- src/crepe/ScriptSystem.h | 4 +--- src/crepe/System.h | 7 +++---- src/crepe/api/BehaviorScript.cpp | 7 ++----- src/crepe/api/BehaviorScript.h | 8 ++++---- src/crepe/api/BehaviorScript.hpp | 4 ++-- src/crepe/api/Script.cpp | 2 -- src/crepe/api/Script.h | 4 ++-- src/crepe/util/fmt.cpp | 5 ++--- src/crepe/util/fmt.h | 2 +- src/crepe/util/log.cpp | 3 +-- src/example/script.cpp | 11 ++++------- 16 files changed, 38 insertions(+), 58 deletions(-) (limited to 'src/crepe/Component.h') diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 737f30a..bce90f1 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -1,4 +1,3 @@ #include "Component.h" using namespace crepe; - diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 00b2164..d9a01ac 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -5,6 +5,7 @@ namespace crepe { class Component { protected: Component() = default; + public: virtual ~Component() = default; // TODO: shouldn't this constructor be deleted because this class will never diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 8aa9d4e..8bde33a 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -1,5 +1,5 @@ -#include "util/log.h" #include "ComponentManager.h" +#include "util/log.h" using namespace crepe; @@ -24,11 +24,6 @@ void ComponentManager::delete_all_components() { this->components.clear(); } -ComponentManager::ComponentManager() { - dbg_trace(); -} - -ComponentManager::~ComponentManager() { - dbg_trace(); -} +ComponentManager::ComponentManager() { dbg_trace(); } +ComponentManager::~ComponentManager() { dbg_trace(); } diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index e0242a2..2377a94 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -10,7 +10,8 @@ template T & ComponentManager::add_component(uint32_t id, Args &&... args) { using namespace std; - static_assert(is_base_of::value, "add_component must recieve a derivative class of Component"); + static_assert(is_base_of::value, + "add_component must recieve a derivative class of Component"); // Determine the type of T (this is used as the key of the unordered_map<>) type_index type = typeid(T); @@ -81,7 +82,8 @@ ComponentManager::get_components_by_id(uint32_t id) const { if (components.find(type) == components.end()) return component_vector; // Get the correct vector<> - const vector>> & component_array = components.at(type); + const vector>> & component_array + = 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; @@ -117,7 +119,8 @@ ComponentManager::get_components_by_type() const { if (components.find(type) == components.end()) return component_vector; // Get the correct vector<> - const vector>> & component_array = components.at(type); + const vector>> & component_array + = components.at(type); // Loop through the whole vector<> for (const vector> & component : component_array) { diff --git a/src/crepe/ScriptSystem.cpp b/src/crepe/ScriptSystem.cpp index 1a7bff4..5d882be 100644 --- a/src/crepe/ScriptSystem.cpp +++ b/src/crepe/ScriptSystem.cpp @@ -2,8 +2,8 @@ #include #include -#include "ScriptSystem.h" #include "ComponentManager.h" +#include "ScriptSystem.h" #include "api/BehaviorScript.h" #include "api/Script.h" #include "util/log.h" @@ -12,12 +12,8 @@ using namespace std; using namespace crepe; using namespace crepe::api; -ScriptSystem::ScriptSystem() { - dbg_trace(); -} -ScriptSystem::~ScriptSystem() { - dbg_trace(); -} +ScriptSystem::ScriptSystem() { dbg_trace(); } +ScriptSystem::~ScriptSystem() { dbg_trace(); } ScriptSystem & ScriptSystem::get_instance() { static ScriptSystem instance; @@ -29,14 +25,14 @@ void ScriptSystem::update() { dbg_trace(); forward_list