diff --git a/becs.h b/becs.h index 0fd2011..7c1463f 100644 --- a/becs.h +++ b/becs.h @@ -26,30 +26,7 @@ #include #else #define assert(expression) -#endif /* BECS_SLOW */ - -typedef struct BECS_Arena BECS_Arena; -typedef struct BECS_ComponentPool BECS_ComponentPool; -typedef struct BECS_ECS BECS_ECS; - -typedef uint32_t BECS_Entity; -typedef uint32_t BECS_ComponentID; - -size_t BECS_GetMinMemoryArenaSize(size_t *componentSizes, size_t arrayLength); -BECS_ECS BECS_InitECS(void *memory, size_t size); -void BECS_ResetECS(BECS_ECS *ecs); - -BECS_Entity BECS_EntityCreate(BECS_ECS *ecs); -void BECS_EntityDestroy(BECS_ECS *ecs, BECS_Entity entity); -bool BECS_IsEntityAlive(BECS_ECS *ecs, BECS_Entity entity); - -BECS_ComponentID BECS_ComponentRegister(BECS_ECS *ecs, size_t componentSize, size_t maximumComponentCount); -void *BECS_ComponentAdd(BECS_ECS *ecs, BECS_Entity entity, BECS_ComponentID componentId); -void BECS_ComponentRemove(BECS_ECS *ecs, BECS_Entity entity, BECS_ComponentID componentId); -void *BECS_ComponentGet(BECS_ECS *ecs, BECS_Entity entity, BECS_ComponentID componentId); -bool BECS_ComponentHas(BECS_ECS *ecs, BECS_Entity entity, BECS_ComponentID componentId); - -#ifdef BECS_IMPLEMENTATION +#endif /* BECS_NO_ASSERT */ #ifndef BECS_MAX_ENTITIES #define BECS_MAX_ENTITIES 4096 @@ -62,41 +39,30 @@ bool BECS_ComponentHas(BECS_ECS *ecs, BECS_Entity entity, BECS_ComponentID compo #define BECS_NULL_ENTITY BECS_MAX_ENTITIES + 1 #define BECS_INVALID_COMPONENT_ID UINT32_MAX -typedef uint64_t BECS_Signature; +/*============================================================================= + BECS Public API +=============================================================================*/ -struct BECS_Arena { +typedef uint32_t BECS_Entity; +typedef uint64_t BECS_Signature; +typedef uint32_t BECS_ComponentID; + +typedef struct BECS_Arena { uintptr_t nextAllocation; size_t capacity; void *memory; -}; +} BECS_Arena; -struct BECS_ComponentPool { +typedef struct BECS_ComponentPool { size_t unitSize; size_t capacity; size_t count; uint32_t *sparseArray; void *denseArray; BECS_Entity *denseEntityMapping; -}; +} BECS_ComponentPool; -/* -struct BECS_ECS { - BECS_Arena arena; - - BECS_Entity nextEntity; - uint32_t entityCount; - BECS_Signature entitySignatures[BECS_MAX_ENTITIES]; - bool aliveEntities[BECS_MAX_ENTITIES]; - - BECS_Entity freeEntities[BECS_MAX_ENTITIES]; - uint32_t freeEntityCount; - - uint32_t componentTypeCount; - BECS_ComponentPool componentPools[BECS_MAX_COMPONENTS]; -}; -*/ - -struct BECS_ECS { +typedef struct BECS_ECS { BECS_Arena arena; /* Entity Management */ @@ -111,7 +77,31 @@ struct BECS_ECS { /* Component Management */ uint32_t componentTypeCount; BECS_ComponentPool componentPools[BECS_MAX_COMPONENTS]; -}; +} BECS_ECS; + +size_t BECS_GetMinMemoryArenaSize(size_t *componentSizes, size_t arrayLength); +BECS_ECS BECS_InitECS(void *memory, size_t size); +void BECS_ResetECS(BECS_ECS *ecs); + +BECS_Entity BECS_EntityCreate(BECS_ECS *ecs); +void BECS_EntityDestroy(BECS_ECS *ecs, BECS_Entity entity); +bool BECS_IsEntityAlive(BECS_ECS *ecs, BECS_Entity entity); + +BECS_ComponentID BECS_ComponentRegister(BECS_ECS *ecs, size_t componentSize, size_t maximumComponentCount); +void *BECS_ComponentAdd(BECS_ECS *ecs, BECS_Entity entity, BECS_ComponentID componentId); +void BECS_ComponentRemove(BECS_ECS *ecs, BECS_Entity entity, BECS_ComponentID componentId); +void *BECS_ComponentGet(BECS_ECS *ecs, BECS_Entity entity, BECS_ComponentID componentId); +bool BECS_ComponentHas(BECS_ECS *ecs, BECS_Entity entity, BECS_ComponentID componentId); +void *BECS_ComponentGetPool(BECS_ECS *ecs, BECS_ComponentID componentId); + +#endif /* BECS_H */ + +/*============================================================================= + BECS IMPLEMENTATION +=============================================================================*/ + +#ifdef BECS_IMPLEMENTATION +#undef BECS_IMPLEMENTATION /* Signature Helpers */ static BECS_Signature BECS_SignatureSet(BECS_Signature sig, BECS_ComponentID componentId) @@ -482,5 +472,11 @@ bool BECS_ComponentHas(BECS_ECS *ecs, BECS_Entity entity, BECS_ComponentID compo return BECS_SignatureHas(ecs->entitySignatures[entity], componentId); } +void *BECS_ComponentGetPool(BECS_ECS *ecs, BECS_ComponentID componentId) +{ + assert(componentId < BECS_MAX_COMPONENTS); + void *result = ecs->componentPools[componentId].denseArray; + return result; +} + #endif /* BECS_IMPLEMENTATION */ -#endif /* BECS_H */ diff --git a/examples/example1/example1.c b/examples/example1/example1.c index 138392a..7edd883 100644 --- a/examples/example1/example1.c +++ b/examples/example1/example1.c @@ -143,10 +143,12 @@ int main(void) { BECS_ComponentID accID = BECS_ComponentRegister(&ecs, sizeof(Acceleration), BECS_MAX_ENTITIES); BECS_ComponentID dataID = BECS_ComponentRegister(&ecs, sizeof(ParticleData), BECS_MAX_ENTITIES); - Position *posData = (Position *)ecs.componentPools[posID].denseArray; - Velocity *velData = (Velocity *)ecs.componentPools[velID].denseArray; - Acceleration *accData = (Acceleration *)ecs.componentPools[accID].denseArray; - ParticleData *dataData = (ParticleData *)ecs.componentPools[dataID].denseArray; + Position *posData = (Position *)BECS_ComponentGetPool(&ecs, posID); + Velocity *velData = (Velocity *)BECS_ComponentGetPool(&ecs, velID); + Acceleration *accData = (Acceleration *)BECS_ComponentGetPool(&ecs, accID); + ParticleData *dataData = (ParticleData *)BECS_ComponentGetPool(&ecs, dataID); + + // TODO: This should probably be done in a query system BECS_Entity *entityData = (BECS_Entity *)ecs.componentPools[dataID].denseEntityMapping; InitWindow(1600, 800, "2D Particle System with BECS");