From 006d9fc95e34a120c73272d98e042bf2963e0064 Mon Sep 17 00:00:00 2001 From: NukeBird Date: Tue, 18 Feb 2025 22:27:54 +0300 Subject: [PATCH] Custom concept for components --- tests/zecsy.cpp | 23 +++++++------- zecsy.hpp | 82 ++++++++++++++++++++++++++++++------------------- 2 files changed, 61 insertions(+), 44 deletions(-) diff --git a/tests/zecsy.cpp b/tests/zecsy.cpp index 2eddd4c..497b145 100644 --- a/tests/zecsy.cpp +++ b/tests/zecsy.cpp @@ -1,5 +1,4 @@ #include -#include #define CATCH_CONFIG_MAIN #include @@ -74,9 +73,9 @@ TEST_CASE("Attach a simple component to an entity and verify it is correctly " REQUIRE(w.has(e2)); } -struct Name +struct Comp { - std::string value; + int v = 0; }; TEST_CASE("Retrieve a component from an entity and verify its data matches " @@ -85,13 +84,13 @@ TEST_CASE("Retrieve a component from an entity and verify its data matches " world w; auto e = w.make_entity(); - w.set(e, Name{"zecsy!"}); + w.set(e, Comp()); - REQUIRE(w.get(e).value == "zecsy!"); + REQUIRE(w.get(e).v == 0); - w.get(e).value = "super-zecsy!"; + w.get(e).v = 77; - REQUIRE(w.get(e).value == "super-zecsy!"); + REQUIRE(w.get(e).v == 77); } TEST_CASE( @@ -161,15 +160,15 @@ TEST_CASE("Attach multiple components to an entity and verify all are " world w; auto e = w.make_entity(); - w.set(e, ChoosenOne{}, Name{"zecsy"}); + w.set(e, ChoosenOne{}, Comp{}); - REQUIRE(w.has(e)); + REQUIRE(w.has(e)); - w.remove(e); + w.remove(e); - REQUIRE_FALSE(w.has(e)); + REQUIRE_FALSE(w.has(e)); REQUIRE_FALSE(w.has(e)); - REQUIRE_FALSE(w.has(e)); + REQUIRE_FALSE(w.has(e)); } TEST_CASE("Create a simple system that processes entities with a specific " diff --git a/zecsy.hpp b/zecsy.hpp index 34f5216..4d4a467 100644 --- a/zecsy.hpp +++ b/zecsy.hpp @@ -6,13 +6,33 @@ #include #include #include +#include #include +#include #include +#include namespace zecsy { using entity_id = uint64_t; + /* + * Actually, not a pure POD. Gotta figure out a better name + */ + template + concept Component = [] + { + static_assert((std::is_default_constructible_v && ...), + "Should have a default constructor"); + static_assert((std::is_trivially_copyable_v && ...), + "Should be trivially copyable"); + static_assert((std::is_trivially_destructible_v && ...), + "Should be trivially destructible"); + static_assert((std::is_standard_layout_v && ...), + "Should have standard layout"); + return true; + }(); + /* * Using std::set for entities_set to maintain sorted order, which can * improve cache locality during queries and iterations. @@ -22,32 +42,32 @@ namespace zecsy class component_storage final { public: - template + template bool has(entity_id e) const; - template + template bool has(entity_id e) const; - template + template T& get(entity_id e); - template + template void set(entity_id e); - template + template void set(entity_id e, const T& comp); - template + template void set(entity_id e); - template + template void set(entity_id e, const First& comp0, const Second& comp1, const Rest&... rest_comps); - template + template void remove(entity_id e); - template + template void remove(entity_id e); private: @@ -75,7 +95,7 @@ namespace zecsy inline component_storage::comp_id component_storage::next_component_id = 0; - template + template inline bool component_storage::has(entity_id e) const { auto id = get_component_id(); @@ -86,7 +106,7 @@ namespace zecsy return false; } - template + template inline T& component_storage::get(entity_id e) { auto id = get_component_id(); @@ -101,7 +121,7 @@ namespace zecsy return *reinterpret_cast(&pool.data[index * sizeof(T)]); } - template + template inline void component_storage::set(entity_id e, const T& comp) { auto id = get_component_id(); @@ -124,13 +144,13 @@ namespace zecsy pool.index_to_entity[index] = e; } - template + template inline void component_storage::set(entity_id e) { set(e, T{}); } - template + template inline void component_storage::remove(entity_id e) { auto id = get_component_id(); @@ -153,13 +173,13 @@ namespace zecsy pool.index_to_entity.erase(index); } - template + template inline bool component_storage::has(entity_id e) const { return has(e) && has(e) && (has(e) && ...); } - template + template inline void component_storage::set(entity_id e) { set(e, First{}); @@ -167,7 +187,7 @@ namespace zecsy (set(e, Rest{}), ...); } - template + template inline void component_storage::set(entity_id e, const First& comp0, const Second& comp1, const Rest&... rest_comps) @@ -177,7 +197,7 @@ namespace zecsy (set(e, rest_comps), ...); } - template + template inline void component_storage::remove(entity_id e) { remove(e); @@ -193,7 +213,6 @@ namespace zecsy void add_system(int freq, auto&& func); void update(float dt); - private: struct system_handler { @@ -238,24 +257,23 @@ namespace zecsy void destroy_entity(entity_id e); bool is_alive(entity_id e) const; - template + template bool has(entity_id e) const; - template + template T& get(entity_id e); - template + template void set(entity_id e); - template + template void set(entity_id e, const T&... comps); - template + template void remove(entity_id e); - template + template void query(auto&& system); - private: entities_set alive_entities; entity_id entity_counter = 0; @@ -279,37 +297,37 @@ namespace zecsy return alive_entities.contains(e); } - template + template inline bool world::has(entity_id e) const { return storage.has(e); } - template + template inline T& world::get(entity_id e) { return storage.get(e); } - template + template inline void world::set(entity_id e) { storage.set(e); } - template + template inline void world::set(entity_id e, const T&... comps) { storage.set(e, comps...); } - template + template inline void world::remove(entity_id e) { storage.remove(e); } - template + template inline void world::query(auto&& system) { for(auto e: alive_entities)