Bläddra i källkod

Replace Components makros with functions

Kajetan Johannes Hammerle 11 månader sedan
förälder
incheckning
074f8d2282
3 ändrade filer med 36 tillägg och 21 borttagningar
  1. 3 13
      include/core/Components.h
  2. 20 0
      src/Components.c
  3. 13 8
      test/modules/ComponentsTests.c

+ 3 - 13
include/core/Components.h

@@ -26,23 +26,13 @@ typedef struct {
     CoreComponentNode node;
 } CoreComponentIterator;
 
-#define CORE_COMPONENTS(componentSize)                                         \
-    ((CoreComponents){CORE_HASH_MAP(sizeof(CoreEntity), sizeof(size_t),        \
-                                    coreHash, coreEqual),                      \
-                      CORE_LIST(sizeof(CoreEntity)),                           \
-                      CORE_LIST(componentSize)})
+void coreInitComponents(CoreComponents* c, size_t componentSize);
 void coreDestroyComponents(CoreComponents* c);
 void* coreComponentsGetOrAdd(CoreComponents* c, CoreEntity e);
 void* coreComponentsSearch(CoreComponents* c, CoreEntity e);
 bool coreComponentsRemove(CoreComponents* c, CoreEntity e);
-#define CORE_COMPONENTS_ITERATOR(c)                                            \
-    ((CoreComponentIterator){coreListBegin(&(c)->indexToEntity),               \
-                             coreListEnd(&(c)->indexToEntity),                 \
-                             coreListBegin(&(c)->components),                  \
-                             coreListEnd(&(c)->components),                    \
-                             (c)->components.dataSize,                         \
-                             {0}})
-#define coreComponentsHasNext(ci) (ci.indexToEntity != ci.indexToEntityEnd)
+void coreInitComponentsIterator(CoreComponentIterator* ci, CoreComponents* c);
+bool coreComponentsHasNext(CoreComponentIterator* ci);
 CoreComponentNode* coreComponentsNext(CoreComponentIterator* ci);
 void* coreComponentsBegin(CoreComponents* c);
 void* coreComponentsEnd(CoreComponents* c);

+ 20 - 0
src/Components.c

@@ -1,5 +1,12 @@
 #include "core/Components.h"
 
+void coreInitComponents(CoreComponents* c, size_t componentSize) {
+    c->entityToIndex =
+        CORE_HASH_MAP(sizeof(CoreEntity), sizeof(size_t), coreHash, coreEqual);
+    c->indexToEntity = CORE_LIST(sizeof(CoreEntity));
+    c->components = CORE_LIST(componentSize);
+}
+
 void coreDestroyComponents(CoreComponents* c) {
     coreDestroyHashMap(&c->entityToIndex);
     coreDestroyList(&c->indexToEntity);
@@ -44,6 +51,19 @@ bool coreComponentsRemove(CoreComponents* c, CoreEntity e) {
     return true;
 }
 
+void coreInitComponentsIterator(CoreComponentIterator* ci, CoreComponents* c) {
+    ci->indexToEntity = coreListBegin(&c->indexToEntity);
+    ci->indexToEntityEnd = coreListEnd(&c->indexToEntity);
+    ci->component = coreListBegin(&c->components);
+    ci->componentEnd = coreListEnd(&c->components);
+    ci->componentSize = c->components.dataSize;
+    ci->node = (CoreComponentNode){0};
+}
+
+bool coreComponentsHasNext(CoreComponentIterator* ci) {
+    return ci->indexToEntity != ci->indexToEntityEnd;
+}
+
 CoreComponentNode* coreComponentsNext(CoreComponentIterator* ci) {
     ci->node.component = ci->component;
     ci->node.entity = *ci->indexToEntity;

+ 13 - 8
test/modules/ComponentsTests.c

@@ -2,7 +2,9 @@
 #include "core/Components.h"
 
 static void testAddForEach() {
-    CoreComponents c = CORE_COMPONENTS(sizeof(int));
+    CoreComponents c;
+    coreInitComponents(&c, sizeof(int));
+
     int* i1 = coreComponentsGetOrAdd(&c, 1);
     if(CORE_TEST_NOT_NULL(i1)) {
         *i1 = 10;
@@ -21,28 +23,30 @@ static void testAddForEach() {
     }
     CORE_TEST_TRUE(i1 == i2);
 
-    CoreComponentIterator iter = CORE_COMPONENTS_ITERATOR(&c);
-    if(CORE_TEST_TRUE(coreComponentsHasNext(iter))) {
+    CoreComponentIterator iter;
+    coreInitComponentsIterator(&iter, &c);
+    if(CORE_TEST_TRUE(coreComponentsHasNext(&iter))) {
         CoreComponentNode* n = coreComponentsNext(&iter);
         CORE_TEST_SIZE(1, n->entity);
         CORE_TEST_INT(15, *(int*)n->component);
     }
-    if(CORE_TEST_TRUE(coreComponentsHasNext(iter))) {
+    if(CORE_TEST_TRUE(coreComponentsHasNext(&iter))) {
         CoreComponentNode* n = coreComponentsNext(&iter);
         CORE_TEST_SIZE(5, n->entity);
         CORE_TEST_INT(20, *(int*)n->component);
     }
-    if(CORE_TEST_TRUE(coreComponentsHasNext(iter))) {
+    if(CORE_TEST_TRUE(coreComponentsHasNext(&iter))) {
         CoreComponentNode* n = coreComponentsNext(&iter);
         CORE_TEST_SIZE(10, n->entity);
         CORE_TEST_INT(30, *(int*)n->component);
     }
-    CORE_TEST_FALSE(coreComponentsHasNext(iter));
+    CORE_TEST_FALSE(coreComponentsHasNext(&iter));
     coreDestroyComponents(&c);
 }
 
 static void testAddComponentForEach() {
-    CoreComponents c = CORE_COMPONENTS(sizeof(int));
+    CoreComponents c;
+    coreInitComponents(&c, sizeof(int));
     int* i1 = coreComponentsGetOrAdd(&c, 1);
     if(CORE_TEST_NOT_NULL(i1)) {
         *i1 = 10;
@@ -72,7 +76,8 @@ static void testAddComponentForEach() {
 }
 
 static void testRemove() {
-    CoreComponents c = CORE_COMPONENTS(sizeof(int));
+    CoreComponents c;
+    coreInitComponents(&c, sizeof(int));
     *(int*)coreComponentsGetOrAdd(&c, 1) = 10;
     *(int*)coreComponentsGetOrAdd(&c, 5) = 20;
     *(int*)coreComponentsGetOrAdd(&c, 10) = 30;