From 7831fc143ea6e5aaff2889d3451efa22442096eb Mon Sep 17 00:00:00 2001 From: Rodolfo Barcelli Jo Date: Mon, 19 Jan 2026 15:36:30 +0800 Subject: [PATCH] Update to more simplified becs2.h --- becs2.h | 54 +++++++++++++++++++++++++++-------------------------- test/main.c | 26 ++++++++++++++++++-------- 2 files changed, 46 insertions(+), 34 deletions(-) diff --git a/becs2.h b/becs2.h index cf142b4..36c0094 100644 --- a/becs2.h +++ b/becs2.h @@ -4,9 +4,20 @@ #include #include #include +#include -#define MAX_ENTITIES 100 -#define NUM_COMPONENTS 2 +typedef struct Entity Entity; +typedef struct BECS_Properties BECS_Properties; +typedef struct BECS_ComponentArray BECS_ComponentArray; +typedef struct BECS_ECS BECS_ECS; + +size_t BECS_GetMinMemorySize(BECS_Properties props); +BECS_ECS *BECS_Init(void *memory, size_t size, BECS_Properties props); +void *BECS_AddComponent(BECS_ECS *ecs, uint32_t entityID, uint32_t componentIndex); +void *BECS_GetComponent(BECS_ECS *ecs, uint32_t entityID, uint32_t componentIndex); + +//#define MAX_ENTITIES 100: +//#define NUM_COMPONENTS 2 #ifdef MAX_ENTITIES #ifdef NUM_COMPONENTS @@ -19,7 +30,7 @@ struct Entity { }; struct BECS_Properties { - uint32_t componentSizes[NUM_COMPONENTS]; + size_t componentSizes[NUM_COMPONENTS]; }; struct BECS_ComponentArray { @@ -50,29 +61,18 @@ size_t BECS_GetMinMemorySize(BECS_Properties props) return totalSize; } -void BECS_InitComponentArray(BECS_ECS *ecs, size_t size, uint32_t componentIndex) -{ - ecs->componentArrays[componentIndex] = {}; - ecs->componentArrays[componentIndex].lastEntityAdded = BECS_NULL_INDEX; - ecs->componentArrays[componentIndex].unitSize = size; - ecs->componentArrays[componentIndex].denseArraylength= 0; - ecs->componentArrays[componentIndex].denseArray = NULL; -} - -BECS_ECS *BECS_Init -(void *memory, size_t size, BECS_Properties props) +BECS_ECS *BECS_Init(void *memory, size_t size, BECS_Properties props) { + assert(memory); BECS_ECS *ecs = (BECS_ECS *)memory; - *ecs = {}; ecs->props = props; for (uint32_t i = 0; i < NUM_COMPONENTS; i++) { - ecs->componentArrays[i] = {}; ecs->componentArrays[i].lastEntityAdded = BECS_NULL_INDEX; ecs->componentArrays[i].unitSize = props.componentSizes[i]; ecs->componentArrays[i].denseArraylength = 0; - for (uint32_t j = 0; i < MAX_ENTITIES; i++) + for (uint32_t j = 0; j < MAX_ENTITIES; j++) { ecs->componentArrays[i].sparseArray[j] = BECS_NULL_INDEX; } @@ -115,10 +115,7 @@ void *BECS_AddComponent(BECS_ECS *ecs, uint32_t entityID, uint32_t componentInde int32_t BECS_RemoveComponent(BECS_ECS *ecs, uint32_t entityID, uint32_t componentIndex) { - if (entityID > MAX_ENTITIES) - { - return 1; - } + assert(entityID < MAX_ENTITIES); uint64_t componentBitMask = 0 | (1 << (componentIndex + 1)); if (!(ecs->entities[entityID].componentBitMask & componentBitMask)) @@ -140,6 +137,8 @@ int32_t BECS_RemoveComponent(BECS_ECS *ecs, uint32_t entityID, uint32_t componen break; } } + + ecs->entities[entityID].componentBitMask &= ~(1 << (componentIndex + 1)); return 0; } @@ -152,18 +151,20 @@ int32_t BECS_RemoveComponent(BECS_ECS *ecs, uint32_t entityID, uint32_t componen uintptr_t deletedComp = denseArray + (lastEntityAddedIndex * componentArray->unitSize); memcpy((void *)deletedComp, (void*)lastEntityAdded, componentArray->unitSize); - - componentArray->denseArraylength--; componentArray->sparseArray[entityID] = lastEntityAddedIndex; + componentArray->denseArraylength--; + + ecs->entities[entityID].componentBitMask &= ~(1 << (componentIndex + 1)); return 0; } void *BECS_GetComponent(BECS_ECS *ecs, uint32_t entityID, uint32_t componentIndex) { + assert(entityID < MAX_ENTITIES); + assert(componentIndex < NUM_COMPONENTS); + void *component = NULL; - if (componentIndex > NUM_COMPONENTS) - return component; BECS_ComponentArray *componentArray = &ecs->componentArrays[componentIndex]; @@ -173,7 +174,8 @@ void *BECS_GetComponent(BECS_ECS *ecs, uint32_t entityID, uint32_t componentInde { return component; } else { - component = componentArray->denseArray; + size_t componentSize = componentArray->unitSize; + component = (void *)((uintptr_t)componentArray->denseArray + (componentSize * componentDenseIndex)); } return component; diff --git a/test/main.c b/test/main.c index d297d9e..f978452 100644 --- a/test/main.c +++ b/test/main.c @@ -1,8 +1,11 @@ #include #include +#define MAX_ENTITIES 100 +#define NUM_COMPONENTS 2 + #define BECS_IMPLEMENTATION -#include "../becs.h" +#include "../becs2.h" typedef struct compA { uint32_t thing; @@ -20,18 +23,25 @@ int main(void) { sizeof(compB), }; - EcsProps.componentSizes = componentSizes; - EcsProps.numComponents = 2; - EcsProps.maxEntities = 100; + for (uint32_t i = 0; i < NUM_COMPONENTS; i++) + { + EcsProps.componentSizes[i] = componentSizes[i]; + } size_t MinMemorySize = BECS_GetMinMemorySize(EcsProps); printf("%llu\n", MinMemorySize); BECS_ECS *ecs = malloc(MinMemorySize); - BECS_Init(ecs, MinMemorySize, &EcsProps); - compA A = {}; - A.thing = 1; - BECS_AddComponent(ecs, 0, 1, &A); + BECS_Init(ecs, MinMemorySize, EcsProps); + + uint32_t entityID = 0; + compA *component = (compA *)BECS_AddComponent(ecs, entityID, 0); + + component->thing = 1; + + compA *component2 = (compA *)BECS_GetComponent(ecs, entityID, 0); + + BECS_RemoveComponent(ecs, entityID, 0); printf("Initialized\n"); }