aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-06 15:12:01 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-06 15:12:01 +0200
commit39815f58e3842bb28e644e83111a619bd1374855 (patch)
tree39dd4631505fc063bb4336c25b4469cb2190fda8
parentf8bc2a06a90c0ee172054db7ae2e1fdae09d14a3 (diff)
add components_internal example
-rw-r--r--src/crepe/ComponentManager.cpp2
-rw-r--r--src/crepe/ComponentManager.h11
-rw-r--r--src/crepe/GameObject.cpp2
-rw-r--r--src/crepe/GameObject.h3
-rw-r--r--src/example/CMakeLists.txt18
-rw-r--r--src/example/components_internal.cpp56
6 files changed, 84 insertions, 8 deletions
diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp
index b080c9d..9a3fec7 100644
--- a/src/crepe/ComponentManager.cpp
+++ b/src/crepe/ComponentManager.cpp
@@ -7,7 +7,7 @@ ComponentManager & ComponentManager::get_instance() {
return instance;
}
-void ComponentManager::delete_all_components_of_id(std::uint32_t id) {
+void ComponentManager::delete_all_components_of_id(uint32_t id) {
// Loop through all the types (in the unordered_map<>)
for (auto & [type, componentArray] : components) {
// Make sure that the id (that we are looking for) is within the boundaries of the vector<>
diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h
index cf6edad..9e559dd 100644
--- a/src/crepe/ComponentManager.h
+++ b/src/crepe/ComponentManager.h
@@ -23,22 +23,22 @@ public:
public:
//! Add a component of a specific type
template <typename T, typename... Args>
- void add_component(std::uint32_t id, Args &&... args);
+ void add_component(uint32_t id, Args &&... args);
//! Deletes all components of a specific type and id
template <typename T>
- void delete_components_by_id(std::uint32_t id);
+ void delete_components_by_id(uint32_t id);
//! Deletes all components of a specific type
template <typename T>
void delete_components();
//! Deletes all components of a specific id
- void delete_all_components_of_id(std::uint32_t id);
+ void delete_all_components_of_id(uint32_t id);
//! Deletes all components
void delete_all_components();
//! Get a vector<> of all components at specific type and id
template <typename T>
std::vector<std::reference_wrapper<T>>
- get_components_by_id(std::uint32_t id) const;
+ get_components_by_id(uint32_t id) const;
//! Get a vector<> of all components of a specific type
template <typename T>
std::vector<std::reference_wrapper<T>> get_components_by_type() const;
@@ -58,3 +58,6 @@ private:
};
} // namespace crepe
+
+#include "ComponentManager.hpp"
+
diff --git a/src/crepe/GameObject.cpp b/src/crepe/GameObject.cpp
index 304f75a..de3beb6 100644
--- a/src/crepe/GameObject.cpp
+++ b/src/crepe/GameObject.cpp
@@ -2,6 +2,6 @@
using namespace crepe;
-GameObject::GameObject(std::uint32_t id, std::string name, std::string tag,
+GameObject::GameObject(uint32_t id, std::string name, std::string tag,
int layer)
: id(id), name(name), tag(tag), active(true), layer(layer) {}
diff --git a/src/crepe/GameObject.h b/src/crepe/GameObject.h
index 114990c..d5a7c36 100644
--- a/src/crepe/GameObject.h
+++ b/src/crepe/GameObject.h
@@ -20,3 +20,6 @@ public:
};
} // namespace crepe
+
+#include "GameObject.hpp"
+
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
index bcc9271..eef38fd 100644
--- a/src/example/CMakeLists.txt
+++ b/src/example/CMakeLists.txt
@@ -1,3 +1,17 @@
-add_executable(audio_internal EXCLUDE_FROM_ALL audio_internal.cpp)
-target_link_libraries(audio_internal PUBLIC crepe)
+# add_example(target_name [SOURCES...])
+function(add_example target_name)
+ # if SOURCES is not specified
+ if(NOT ARGV1)
+ # A .cpp file with target_name exists, and should be used
+ set(sources ${target_name}.cpp)
+ else()
+ set(sources ${ARGV})
+ endif()
+
+ add_executable(${target_name} EXCLUDE_FROM_ALL ${sources})
+ target_link_libraries(${target_name} PUBLIC crepe)
+endfunction()
+
+add_example(audio_internal)
+add_example(components_internal)
diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp
new file mode 100644
index 0000000..821a7de
--- /dev/null
+++ b/src/example/components_internal.cpp
@@ -0,0 +1,56 @@
+/** \file
+ *
+ * Standalone example for usage of the internal ECS
+ */
+
+#include <cassert>
+#include <chrono>
+
+#include <crepe/util/log.h>
+#include <crepe/ComponentManager.h>
+#include <crepe/GameObject.h>
+#include <crepe/Components.h>
+
+using namespace crepe;
+using namespace std;
+
+#define OBJ_COUNT 100000
+
+int main() {
+ dbg_trace();
+
+ auto & mgr = ComponentManager::get_instance();
+
+ auto start_adding = chrono::high_resolution_clock::now();
+
+ GameObject * game_object[OBJ_COUNT];
+
+ for (int i = 0; i < OBJ_COUNT; ++i) {
+ game_object[i] = new GameObject(i, "Name", "Tag", 0);
+
+ game_object[i]->add_component<Sprite>("test");
+ game_object[i]->add_component<Rigidbody>(0, 0, i);
+ game_object[i]->add_component<Collider>(i);
+ }
+
+ auto stop_adding = chrono::high_resolution_clock::now();
+
+ auto sprites = mgr.get_components_by_type<Sprite>();
+ for (auto sprite : sprites) {
+ assert(sprite.get().path == "test");
+ }
+
+ auto stop_looping = chrono::high_resolution_clock::now();
+
+ for (int i = 0; i < OBJ_COUNT; ++i) {
+ delete game_object[i];
+ }
+
+ auto add_time = chrono::duration_cast<chrono::microseconds>(stop_adding - start_adding);
+ auto loop_time = chrono::duration_cast<chrono::microseconds>(stop_looping - stop_adding);
+ printf("add time: %ldus\n", add_time.count());
+ printf("loop time: %ldus\n", loop_time.count());
+
+ return 0;
+}
+