Эх сурвалжийг харах

Correct components instancing

Kajetan Johannes Hammerle 9 сар өмнө
parent
commit
ba70e48657

+ 27 - 26
include/core/Components.h

@@ -13,47 +13,47 @@ typedef size_t Entity;
         CoreHashMap entityToIndex;                                             \
         ListSize indexToEntity;                                                \
         List##N components;                                                    \
-    } Components;                                                              \
+    } Components##N;                                                           \
                                                                                \
     typedef struct {                                                           \
         Entity entity;                                                         \
         T* component;                                                          \
-    } ComponentNode;                                                           \
+    } ComponentNode##N;                                                        \
                                                                                \
     typedef struct {                                                           \
         const Entity* indexToEntity;                                           \
         const Entity* indexToEntityEnd;                                        \
         T* component;                                                          \
         T* componentEnd;                                                       \
-        ComponentNode node;                                                    \
-    } ComponentIterator;                                                       \
+        ComponentNode##N node;                                                 \
+    } ComponentIterator##N;                                                    \
                                                                                \
-    void initComponents(Components* c);                                        \
-    void destroyComponents(Components* c);                                     \
-    T* getOrAddComponent(Components* c, Entity e);                             \
-    T* searchComponent(Components* c, Entity e);                               \
-    bool removeComponent(Components* c, Entity e);                             \
-    void initComponentIterator(ComponentIterator* ci, Components* c);          \
-    bool hasNextComponentNode(ComponentIterator* ci);                          \
-    ComponentNode* nextComponentNode(ComponentIterator* ci);                   \
-    T* getComponentsStart(Components* c);                                      \
-    T* getComponentsEnd(Components* c);
+    void initComponents##N(Components##N* c);                                  \
+    void destroyComponents##N(Components##N* c);                               \
+    T* getOrAddComponent##N(Components##N* c, Entity e);                       \
+    T* searchComponent##N(Components##N* c, Entity e);                         \
+    bool removeComponent##N(Components##N* c, Entity e);                       \
+    void initComponentIterator##N(ComponentIterator##N* ci, Components##N* c); \
+    bool hasNextComponentNode##N(ComponentIterator##N* ci);                    \
+    ComponentNode##N* nextComponentNode##N(ComponentIterator##N* ci);          \
+    T* getComponentsStart##N(Components##N* c);                                \
+    T* getComponentsEnd##N(Components##N* c);
 
 #define COMPONENTS_SOURCE(T, N)                                                \
-    void initComponents(Components* c) {                                       \
+    void initComponents##N(Components##N* c) {                                 \
         coreInitHashMap(&c->entityToIndex, sizeof(Entity), sizeof(size_t));    \
         initListSize(&c->indexToEntity);                                       \
         initList##N(&c->components);                                           \
     }                                                                          \
                                                                                \
-    void destroyComponents(Components* c) {                                    \
+    void destroyComponents##N(Components##N* c) {                              \
         coreDestroyHashMap(&c->entityToIndex);                                 \
         destroyListSize(&c->indexToEntity);                                    \
         destroyList##N(&c->components);                                        \
     }                                                                          \
                                                                                \
-    T* getOrAddComponent(Components* c, Entity e) {                            \
-        void* component = searchComponent(c, e);                               \
+    T* getOrAddComponent##N(Components##N* c, Entity e) {                      \
+        void* component = searchComponent##N(c, e);                            \
         if(component != nullptr) {                                             \
             return component;                                                  \
         }                                                                      \
@@ -63,7 +63,7 @@ typedef size_t Entity;
         return addEmptyListData##N(&c->components);                            \
     }                                                                          \
                                                                                \
-    T* searchComponent(Components* c, Entity e) {                              \
+    T* searchComponent##N(Components##N* c, Entity e) {                        \
         size_t* index = coreSearchHashMapKey(&c->entityToIndex, &e);           \
         if(index == nullptr) {                                                 \
             return nullptr;                                                    \
@@ -71,7 +71,7 @@ typedef size_t Entity;
         return getListIndex##N(&c->components, *index);                        \
     }                                                                          \
                                                                                \
-    bool removeComponent(Components* c, Entity e) {                            \
+    bool removeComponent##N(Components##N* c, Entity e) {                      \
         size_t* indexP = coreSearchHashMapKey(&c->entityToIndex, &e);          \
         if(indexP == nullptr) {                                                \
             return false;                                                      \
@@ -90,19 +90,20 @@ typedef size_t Entity;
         return true;                                                           \
     }                                                                          \
                                                                                \
-    void initComponentIterator(ComponentIterator* ci, Components* c) {         \
+    void initComponentIterator##N(ComponentIterator##N* ci,                    \
+                                  Components##N* c) {                          \
         ci->indexToEntity = getListStartSize(&c->indexToEntity);               \
         ci->indexToEntityEnd = getListEndSize(&c->indexToEntity);              \
         ci->component = getListStart##N(&c->components);                       \
         ci->componentEnd = getListEnd##N(&c->components);                      \
-        ci->node = (ComponentNode){0};                                         \
+        ci->node = (ComponentNode##N){0};                                      \
     }                                                                          \
                                                                                \
-    bool hasNextComponentNode(ComponentIterator* ci) {                         \
+    bool hasNextComponentNode##N(ComponentIterator##N* ci) {                   \
         return ci->indexToEntity != ci->indexToEntityEnd;                      \
     }                                                                          \
                                                                                \
-    ComponentNode* nextComponentNode(ComponentIterator* ci) {                  \
+    ComponentNode##N* nextComponentNode##N(ComponentIterator##N* ci) {         \
         ci->node.component = ci->component;                                    \
         ci->node.entity = *ci->indexToEntity;                                  \
         ci->indexToEntity++;                                                   \
@@ -110,11 +111,11 @@ typedef size_t Entity;
         return &ci->node;                                                      \
     }                                                                          \
                                                                                \
-    T* getComponentsStart(Components* c) {                                     \
+    T* getComponentsStart##N(Components##N* c) {                               \
         return getListStart##N(&c->components);                                \
     }                                                                          \
                                                                                \
-    T* getComponentsEnd(Components* c) {                                       \
+    T* getComponentsEnd##N(Components##N* c) {                                 \
         return getListEnd##N(&c->components);                                  \
     }
 

+ 46 - 46
test/modules/ComponentsTests.c

@@ -8,66 +8,66 @@ COMPONENTS(int, Int)
 COMPONENTS_SOURCE(int, Int)
 
 static void testAddForEach() {
-    Components c;
-    initComponents(&c);
+    ComponentsInt c;
+    initComponentsInt(&c);
 
-    int* i1 = getOrAddComponent(&c, 1);
+    int* i1 = getOrAddComponentInt(&c, 1);
     if(TEST_NOT_NULL(i1)) {
         *i1 = 10;
     }
-    int* i2 = getOrAddComponent(&c, 1);
+    int* i2 = getOrAddComponentInt(&c, 1);
     if(TEST_NOT_NULL(i2)) {
         *i2 = 15;
     }
-    int* i3 = getOrAddComponent(&c, 5);
+    int* i3 = getOrAddComponentInt(&c, 5);
     if(TEST_NOT_NULL(i3)) {
         *i3 = 20;
     }
-    int* i4 = getOrAddComponent(&c, 10);
+    int* i4 = getOrAddComponentInt(&c, 10);
     if(TEST_NOT_NULL(i4)) {
         *i4 = 30;
     }
     TEST_TRUE(i1 == i2);
 
-    ComponentIterator iter;
-    initComponentIterator(&iter, &c);
-    if(TEST_TRUE(hasNextComponentNode(&iter))) {
-        ComponentNode* n = nextComponentNode(&iter);
+    ComponentIteratorInt iter;
+    initComponentIteratorInt(&iter, &c);
+    if(TEST_TRUE(hasNextComponentNodeInt(&iter))) {
+        ComponentNodeInt* n = nextComponentNodeInt(&iter);
         TEST_SIZE(1, n->entity);
         TEST_INT(15, *n->component);
     }
-    if(TEST_TRUE(hasNextComponentNode(&iter))) {
-        ComponentNode* n = nextComponentNode(&iter);
+    if(TEST_TRUE(hasNextComponentNodeInt(&iter))) {
+        ComponentNodeInt* n = nextComponentNodeInt(&iter);
         TEST_SIZE(5, n->entity);
         TEST_INT(20, *n->component);
     }
-    if(TEST_TRUE(hasNextComponentNode(&iter))) {
-        ComponentNode* n = nextComponentNode(&iter);
+    if(TEST_TRUE(hasNextComponentNodeInt(&iter))) {
+        ComponentNodeInt* n = nextComponentNodeInt(&iter);
         TEST_SIZE(10, n->entity);
         TEST_INT(30, *n->component);
     }
-    TEST_FALSE(hasNextComponentNode(&iter));
-    destroyComponents(&c);
+    TEST_FALSE(hasNextComponentNodeInt(&iter));
+    destroyComponentsInt(&c);
 }
 
 static void testAddComponentForEach() {
-    Components c;
-    initComponents(&c);
-    int* i1 = getOrAddComponent(&c, 1);
+    ComponentsInt c;
+    initComponentsInt(&c);
+    int* i1 = getOrAddComponentInt(&c, 1);
     if(TEST_NOT_NULL(i1)) {
         *i1 = 10;
     }
-    int* i2 = getOrAddComponent(&c, 5);
+    int* i2 = getOrAddComponentInt(&c, 5);
     if(TEST_NOT_NULL(i2)) {
         *i2 = 20;
     }
-    int* i3 = getOrAddComponent(&c, 10);
+    int* i3 = getOrAddComponentInt(&c, 10);
     if(TEST_NOT_NULL(i3)) {
         *i3 = 30;
     }
 
-    int* iter = getComponentsStart(&c);
-    int* end = getComponentsEnd(&c);
+    int* iter = getComponentsStartInt(&c);
+    int* end = getComponentsEndInt(&c);
     if(TEST_TRUE(iter != end)) {
         TEST_INT(10, *(iter++));
     }
@@ -78,45 +78,45 @@ static void testAddComponentForEach() {
         TEST_INT(30, *(iter++));
     }
     TEST_TRUE(iter == end);
-    destroyComponents(&c);
+    destroyComponentsInt(&c);
 }
 
 static void testRemove() {
-    Components c;
-    initComponents(&c);
-    *getOrAddComponent(&c, 1) = 10;
-    *getOrAddComponent(&c, 5) = 20;
-    *getOrAddComponent(&c, 10) = 30;
+    ComponentsInt c;
+    initComponentsInt(&c);
+    *getOrAddComponentInt(&c, 1) = 10;
+    *getOrAddComponentInt(&c, 5) = 20;
+    *getOrAddComponentInt(&c, 10) = 30;
 
-    TEST_FALSE(removeComponent(&c, 20));
-    TEST_TRUE(removeComponent(&c, 5));
-    TEST_FALSE(removeComponent(&c, 30));
+    TEST_FALSE(removeComponentInt(&c, 20));
+    TEST_TRUE(removeComponentInt(&c, 5));
+    TEST_FALSE(removeComponentInt(&c, 30));
 
-    *getOrAddComponent(&c, 20) = 40;
-    TEST_TRUE(removeComponent(&c, 20));
+    *getOrAddComponentInt(&c, 20) = 40;
+    TEST_TRUE(removeComponentInt(&c, 20));
 
-    int* i1 = searchComponent(&c, 1);
-    int* i3 = searchComponent(&c, 10);
-    TEST_NULL(searchComponent(&c, 5));
+    int* i1 = searchComponentInt(&c, 1);
+    int* i3 = searchComponentInt(&c, 10);
+    TEST_NULL(searchComponentInt(&c, 5));
     if(TEST_NOT_NULL(i1) && TEST_NOT_NULL(i3)) {
         TEST_INT(10, *i1);
         TEST_INT(30, *i3);
     }
 
-    TEST_TRUE(removeComponent(&c, 10));
-    i1 = searchComponent(&c, 1);
-    TEST_NULL(searchComponent(&c, 5));
-    TEST_NULL(searchComponent(&c, 10));
+    TEST_TRUE(removeComponentInt(&c, 10));
+    i1 = searchComponentInt(&c, 1);
+    TEST_NULL(searchComponentInt(&c, 5));
+    TEST_NULL(searchComponentInt(&c, 10));
     if(TEST_NOT_NULL(i1)) {
         TEST_INT(10, *i1);
     }
 
-    TEST_TRUE(removeComponent(&c, 1));
-    TEST_NULL(searchComponent(&c, 1));
-    TEST_NULL(searchComponent(&c, 5));
-    TEST_NULL(searchComponent(&c, 10));
+    TEST_TRUE(removeComponentInt(&c, 1));
+    TEST_NULL(searchComponentInt(&c, 1));
+    TEST_NULL(searchComponentInt(&c, 5));
+    TEST_NULL(searchComponentInt(&c, 10));
 
-    destroyComponents(&c);
+    destroyComponentsInt(&c);
 }
 
 void testComponents() {