Bladeren bron

Imports for Components

Kajetan Johannes Hammerle 9 maanden geleden
bovenliggende
commit
982036461c
3 gewijzigde bestanden met toevoegingen van 133 en 117 verwijderingen
  1. 25 8
      include/core/Components.h
  2. 36 37
      src/Components.c
  3. 72 72
      test/modules/ComponentsTests.c

+ 25 - 8
include/core/Components.h

@@ -28,13 +28,30 @@ typedef struct {
 
 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);
-void coreInitComponentsIterator(CoreComponentIterator* ci, CoreComponents* c);
-bool coreComponentsHasNext(CoreComponentIterator* ci);
-CoreComponentNode* coreComponentsNext(CoreComponentIterator* ci);
-void* coreComponentsBegin(CoreComponents* c);
-void* coreComponentsEnd(CoreComponents* c);
+void* coreGetOrAddComponent(CoreComponents* c, CoreEntity e);
+void* coreSearchComponent(CoreComponents* c, CoreEntity e);
+bool coreRemoveComponent(CoreComponents* c, CoreEntity e);
+void coreInitComponentIterator(CoreComponentIterator* ci, CoreComponents* c);
+bool coreHasNextComponentNode(CoreComponentIterator* ci);
+CoreComponentNode* coreNextComponentNode(CoreComponentIterator* ci);
+void* coreGetComponentsStart(CoreComponents* c);
+void* coreGetComponentsEnd(CoreComponents* c);
+
+#ifdef IMPORT_CORE
+#define Entity CoreEntity
+#define Components CoreComponents
+#define ComponentNode CoreComponentNode
+#define ComponentIterator CoreComponentIterator
+#define initComponents coreInitComponents
+#define destroyComponents coreDestroyComponents
+#define getOrAddComponent coreGetOrAddComponent
+#define searchComponent coreSearchComponent
+#define removeComponent coreRemoveComponent
+#define initComponentIterator coreInitComponentIterator
+#define hasNextComponentNode coreHasNextComponentNode
+#define nextComponentNode coreNextComponentNode
+#define getComponentsStart coreGetComponentsStart
+#define getComponentsEnd coreGetComponentsEnd
+#endif
 
 #endif

+ 36 - 37
src/Components.c

@@ -1,70 +1,69 @@
 #include "core/Components.h"
 
-void coreInitComponents(CoreComponents* c, size_t componentSize) {
-    coreInitHashMap(&c->entityToIndex, sizeof(CoreEntity), sizeof(size_t));
-    coreInitList(&c->indexToEntity, sizeof(CoreEntity));
-    coreInitList(&c->components, componentSize);
+void initComponents(Components* c, size_t componentSize) {
+    initHashMap(&c->entityToIndex, sizeof(Entity), sizeof(size_t));
+    initList(&c->indexToEntity, sizeof(Entity));
+    initList(&c->components, componentSize);
 }
 
-void coreDestroyComponents(CoreComponents* c) {
-    coreDestroyHashMap(&c->entityToIndex);
-    coreDestroyList(&c->indexToEntity);
-    coreDestroyList(&c->components);
+void destroyComponents(Components* c) {
+    destroyHashMap(&c->entityToIndex);
+    destroyList(&c->indexToEntity);
+    destroyList(&c->components);
 }
 
-void* coreComponentsGetOrAdd(CoreComponents* c, CoreEntity e) {
-    void* component = coreComponentsSearch(c, e);
+void* getOrAddComponent(Components* c, Entity e) {
+    void* component = searchComponent(c, e);
     if(component != nullptr) {
         return component;
     }
     size_t index = c->components.length;
-    corePutHashMapPair(&c->entityToIndex, &e, &index);
-    coreAddListData(&c->indexToEntity, &e);
-    return coreAddEmptyListData(&c->components);
+    putHashMapPair(&c->entityToIndex, &e, &index);
+    addListData(&c->indexToEntity, &e);
+    return addEmptyListData(&c->components);
 }
 
-void* coreComponentsSearch(CoreComponents* c, CoreEntity e) {
-    size_t* index = coreSearchHashMapKey(&c->entityToIndex, &e);
+void* searchComponent(Components* c, Entity e) {
+    size_t* index = searchHashMapKey(&c->entityToIndex, &e);
     if(index == nullptr) {
         return nullptr;
     }
-    return coreGetListIndex(&c->components, *index);
+    return getListIndex(&c->components, *index);
 }
 
-bool coreComponentsRemove(CoreComponents* c, CoreEntity e) {
-    size_t* indexP = coreSearchHashMapKey(&c->entityToIndex, &e);
+bool removeComponent(Components* c, Entity e) {
+    size_t* indexP = searchHashMapKey(&c->entityToIndex, &e);
     if(indexP == nullptr) {
         return false;
     }
     size_t lastIndex = c->components.length - 1;
     size_t index = *indexP;
-    coreRemoveHashMapKey(&c->entityToIndex, &e);
-    coreRemoveListIndexBySwap(&c->components, index);
+    removeHashMapKey(&c->entityToIndex, &e);
+    removeListIndexBySwap(&c->components, index);
     if(index == lastIndex) {
-        coreRemoveListIndexBySwap(&c->indexToEntity, index);
+        removeListIndexBySwap(&c->indexToEntity, index);
         return true;
     }
-    CoreEntity other =
-        coreGetTypedListIndex(&c->indexToEntity, lastIndex, CoreEntity);
-    coreRemoveListIndexBySwap(&c->indexToEntity, index);
-    corePutHashMapPair(&c->entityToIndex, &other, &index);
+    Entity other = getTypedListIndex(&c->indexToEntity, lastIndex, Entity);
+    removeListIndexBySwap(&c->indexToEntity, index);
+    putHashMapPair(&c->entityToIndex, &other, &index);
     return true;
 }
 
-void coreInitComponentsIterator(CoreComponentIterator* ci, CoreComponents* c) {
-    ci->indexToEntity = coreGetListStart(&c->indexToEntity);
-    ci->indexToEntityEnd = coreGetListEnd(&c->indexToEntity);
-    ci->component = coreGetListStart(&c->components);
-    ci->componentEnd = coreGetListEnd(&c->components);
+void initComponentIterator(ComponentIterator* ci, Components* c) {
+    ci->indexToEntity = getListStart(&c->indexToEntity);
+    ci->indexToEntityEnd = getListEnd(&c->indexToEntity);
+    ci->component = getListStart(&c->components);
+    ci->componentEnd = getListEnd(&c->components);
     ci->componentSize = c->components.dataSize;
-    ci->node = (CoreComponentNode){0};
+    ci->node = (ComponentNode){0};
 }
 
-bool coreComponentsHasNext(CoreComponentIterator* ci) {
+bool hasNextComponentNode(ComponentIterator* ci) {
     return ci->indexToEntity != ci->indexToEntityEnd;
 }
 
-CoreComponentNode* coreComponentsNext(CoreComponentIterator* ci) {
+ComponentNode* nextComponentNode(ComponentIterator* ci) {
     ci->node.component = ci->component;
     ci->node.entity = *ci->indexToEntity;
     ci->indexToEntity++;
@@ -72,10 +71,10 @@ CoreComponentNode* coreComponentsNext(CoreComponentIterator* ci) {
     return &ci->node;
 }
 
-void* coreComponentsBegin(CoreComponents* c) {
-    return coreGetListStart(&c->components);
+void* getComponentsStart(Components* c) {
+    return getListStart(&c->components);
 }
 
-void* coreComponentsEnd(CoreComponents* c) {
-    return coreGetListEnd(&c->components);
+void* getComponentsEnd(Components* c) {
+    return getListEnd(&c->components);
 }

+ 72 - 72
test/modules/ComponentsTests.c

@@ -2,115 +2,115 @@
 #include "core/Components.h"
 
 static void testAddForEach() {
-    CoreComponents c;
-    coreInitComponents(&c, sizeof(int));
+    Components c;
+    initComponents(&c, sizeof(int));
 
-    int* i1 = coreComponentsGetOrAdd(&c, 1);
-    if(CORE_TEST_NOT_NULL(i1)) {
+    int* i1 = getOrAddComponent(&c, 1);
+    if(TEST_NOT_NULL(i1)) {
         *i1 = 10;
     }
-    int* i2 = coreComponentsGetOrAdd(&c, 1);
-    if(CORE_TEST_NOT_NULL(i2)) {
+    int* i2 = getOrAddComponent(&c, 1);
+    if(TEST_NOT_NULL(i2)) {
         *i2 = 15;
     }
-    int* i3 = coreComponentsGetOrAdd(&c, 5);
-    if(CORE_TEST_NOT_NULL(i3)) {
+    int* i3 = getOrAddComponent(&c, 5);
+    if(TEST_NOT_NULL(i3)) {
         *i3 = 20;
     }
-    int* i4 = coreComponentsGetOrAdd(&c, 10);
-    if(CORE_TEST_NOT_NULL(i4)) {
+    int* i4 = getOrAddComponent(&c, 10);
+    if(TEST_NOT_NULL(i4)) {
         *i4 = 30;
     }
-    CORE_TEST_TRUE(i1 == i2);
+    TEST_TRUE(i1 == i2);
 
-    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);
+    ComponentIterator iter;
+    initComponentIterator(&iter, &c);
+    if(TEST_TRUE(hasNextComponentNode(&iter))) {
+        ComponentNode* n = nextComponentNode(&iter);
+        TEST_SIZE(1, n->entity);
+        TEST_INT(15, *(int*)n->component);
     }
-    if(CORE_TEST_TRUE(coreComponentsHasNext(&iter))) {
-        CoreComponentNode* n = coreComponentsNext(&iter);
-        CORE_TEST_SIZE(5, n->entity);
-        CORE_TEST_INT(20, *(int*)n->component);
+    if(TEST_TRUE(hasNextComponentNode(&iter))) {
+        ComponentNode* n = nextComponentNode(&iter);
+        TEST_SIZE(5, n->entity);
+        TEST_INT(20, *(int*)n->component);
     }
-    if(CORE_TEST_TRUE(coreComponentsHasNext(&iter))) {
-        CoreComponentNode* n = coreComponentsNext(&iter);
-        CORE_TEST_SIZE(10, n->entity);
-        CORE_TEST_INT(30, *(int*)n->component);
+    if(TEST_TRUE(hasNextComponentNode(&iter))) {
+        ComponentNode* n = nextComponentNode(&iter);
+        TEST_SIZE(10, n->entity);
+        TEST_INT(30, *(int*)n->component);
     }
-    CORE_TEST_FALSE(coreComponentsHasNext(&iter));
-    coreDestroyComponents(&c);
+    TEST_FALSE(hasNextComponentNode(&iter));
+    destroyComponents(&c);
 }
 
 static void testAddComponentForEach() {
-    CoreComponents c;
-    coreInitComponents(&c, sizeof(int));
-    int* i1 = coreComponentsGetOrAdd(&c, 1);
-    if(CORE_TEST_NOT_NULL(i1)) {
+    Components c;
+    initComponents(&c, sizeof(int));
+    int* i1 = getOrAddComponent(&c, 1);
+    if(TEST_NOT_NULL(i1)) {
         *i1 = 10;
     }
-    int* i2 = coreComponentsGetOrAdd(&c, 5);
-    if(CORE_TEST_NOT_NULL(i2)) {
+    int* i2 = getOrAddComponent(&c, 5);
+    if(TEST_NOT_NULL(i2)) {
         *i2 = 20;
     }
-    int* i3 = coreComponentsGetOrAdd(&c, 10);
-    if(CORE_TEST_NOT_NULL(i3)) {
+    int* i3 = getOrAddComponent(&c, 10);
+    if(TEST_NOT_NULL(i3)) {
         *i3 = 30;
     }
 
-    int* iter = coreComponentsBegin(&c);
-    int* end = coreComponentsEnd(&c);
-    if(CORE_TEST_TRUE(iter != end)) {
-        CORE_TEST_INT(10, *(iter++));
+    int* iter = getComponentsStart(&c);
+    int* end = getComponentsEnd(&c);
+    if(TEST_TRUE(iter != end)) {
+        TEST_INT(10, *(iter++));
     }
-    if(CORE_TEST_TRUE(iter != end)) {
-        CORE_TEST_INT(20, *(iter++));
+    if(TEST_TRUE(iter != end)) {
+        TEST_INT(20, *(iter++));
     }
-    if(CORE_TEST_TRUE(iter != end)) {
-        CORE_TEST_INT(30, *(iter++));
+    if(TEST_TRUE(iter != end)) {
+        TEST_INT(30, *(iter++));
     }
-    CORE_TEST_TRUE(iter == end);
-    coreDestroyComponents(&c);
+    TEST_TRUE(iter == end);
+    destroyComponents(&c);
 }
 
 static void testRemove() {
-    CoreComponents c;
-    coreInitComponents(&c, sizeof(int));
-    *(int*)coreComponentsGetOrAdd(&c, 1) = 10;
-    *(int*)coreComponentsGetOrAdd(&c, 5) = 20;
-    *(int*)coreComponentsGetOrAdd(&c, 10) = 30;
+    Components c;
+    initComponents(&c, sizeof(int));
+    *(int*)getOrAddComponent(&c, 1) = 10;
+    *(int*)getOrAddComponent(&c, 5) = 20;
+    *(int*)getOrAddComponent(&c, 10) = 30;
 
-    CORE_TEST_FALSE(coreComponentsRemove(&c, 20));
-    CORE_TEST_TRUE(coreComponentsRemove(&c, 5));
-    CORE_TEST_FALSE(coreComponentsRemove(&c, 30));
+    TEST_FALSE(removeComponent(&c, 20));
+    TEST_TRUE(removeComponent(&c, 5));
+    TEST_FALSE(removeComponent(&c, 30));
 
-    *(int*)coreComponentsGetOrAdd(&c, 20) = 40;
-    CORE_TEST_TRUE(coreComponentsRemove(&c, 20));
+    *(int*)getOrAddComponent(&c, 20) = 40;
+    TEST_TRUE(removeComponent(&c, 20));
 
-    int* i1 = coreComponentsSearch(&c, 1);
-    int* i3 = coreComponentsSearch(&c, 10);
-    CORE_TEST_NULL(coreComponentsSearch(&c, 5));
-    if(CORE_TEST_NOT_NULL(i1) && CORE_TEST_NOT_NULL(i3)) {
-        CORE_TEST_INT(10, *i1);
-        CORE_TEST_INT(30, *i3);
+    int* i1 = searchComponent(&c, 1);
+    int* i3 = searchComponent(&c, 10);
+    TEST_NULL(searchComponent(&c, 5));
+    if(TEST_NOT_NULL(i1) && TEST_NOT_NULL(i3)) {
+        TEST_INT(10, *i1);
+        TEST_INT(30, *i3);
     }
 
-    CORE_TEST_TRUE(coreComponentsRemove(&c, 10));
-    i1 = coreComponentsSearch(&c, 1);
-    CORE_TEST_NULL(coreComponentsSearch(&c, 5));
-    CORE_TEST_NULL(coreComponentsSearch(&c, 10));
-    if(CORE_TEST_NOT_NULL(i1)) {
-        CORE_TEST_INT(10, *i1);
+    TEST_TRUE(removeComponent(&c, 10));
+    i1 = searchComponent(&c, 1);
+    TEST_NULL(searchComponent(&c, 5));
+    TEST_NULL(searchComponent(&c, 10));
+    if(TEST_NOT_NULL(i1)) {
+        TEST_INT(10, *i1);
     }
 
-    CORE_TEST_TRUE(coreComponentsRemove(&c, 1));
-    CORE_TEST_NULL(coreComponentsSearch(&c, 1));
-    CORE_TEST_NULL(coreComponentsSearch(&c, 5));
-    CORE_TEST_NULL(coreComponentsSearch(&c, 10));
+    TEST_TRUE(removeComponent(&c, 1));
+    TEST_NULL(searchComponent(&c, 1));
+    TEST_NULL(searchComponent(&c, 5));
+    TEST_NULL(searchComponent(&c, 10));
 
-    coreDestroyComponents(&c);
+    destroyComponents(&c);
 }
 
 void testComponents() {