Add: Implemented ECS initialization and reset functions

This commit is contained in:
Rodolfo Barcelli Jo 2026-01-21 19:06:41 +08:00
parent 01edc3f866
commit 0b34262db6

60
becs3.h
View file

@ -48,7 +48,7 @@ bool BECS_ComponentHas(BECS_ECS *ecs, BECS_Entity entity, BECS_ComponentID compo
#define BECS_MAX_COMPONENTS 64 #define BECS_MAX_COMPONENTS 64
#endif /* BECS_MAX_COMPONENTS */ #endif /* BECS_MAX_COMPONENTS */
#define BECS_NULL_ENTITY #define BECS_NULL_ENTITY BECS_MAX_COMPONENTS + 1
#define BECS_INVALID_COMPONENT_ID UINT32_MAX #define BECS_INVALID_COMPONENT_ID UINT32_MAX
typedef uint64_t BECS_Signature; typedef uint64_t BECS_Signature;
@ -73,10 +73,10 @@ struct BECS_ECS {
/* Entity Management */ /* Entity Management */
uint32_t entityCount; uint32_t entityCount;
BECS_Signature entitySignatures[BECS_MAX_COMPONENTS]; BECS_Signature entitySignatures[BECS_MAX_ENTITIES];
bool aliveEntites[BECS_MAX_COMPONENTS]; bool aliveEntites[BECS_MAX_ENTITIES];
BECS_Entity freeEntities[BECS_MAX_COMPONENTS]; BECS_Entity freeEntities[BECS_MAX_ENTITIES];
uint32_t freeEntityCount; uint32_t freeEntityCount;
/* Component Management */ /* Component Management */
@ -85,29 +85,29 @@ struct BECS_ECS {
}; };
/* Signature Helpers */ /* Signature Helpers */
static inline BECS_Signature BECS_SignatureSet(BECS_Signature sig, BECS_ComponentID componentId) static BECS_Signature BECS_SignatureSet(BECS_Signature sig, BECS_ComponentID componentId)
{ {
return sig | (1ULL << componentId); return sig | (1ULL << componentId);
} }
static inline bool ecs_signature_has(BECS_Signature sig, BECS_ComponentID component_id) static bool ecs_signature_has(BECS_Signature sig, BECS_ComponentID component_id)
{ {
return (sig & (1ULL << component_id)) != 0; return (sig & (1ULL << component_id)) != 0;
} }
static inline bool ecs_signature_matches(BECS_Signature entitySig, BECS_Signature querySig) static bool ecs_signature_matches(BECS_Signature entitySig, BECS_Signature querySig)
{ {
return (entitySig & querySig) == querySig; return (entitySig & querySig) == querySig;
} }
/* Memory Arena */ /* Memory Arena */
BECS_Arena BECS_CreateArena(void *memory, size_t size) BECS_Arena BECS_CreateArena(void *memory, size_t capacity)
{ {
uintptr_t baseOffset = 64 - ((uintptr_t)memory % 64); uintptr_t baseOffset = 64 - ((uintptr_t)memory % 64);
BECS_Arena arena = { BECS_Arena arena = {
baseOffset, baseOffset,
size, capacity,
memory memory
}; };
return arena; return arena;
@ -140,7 +140,8 @@ static void BECS_ComponentPoolInit
pool->denseArray = BECS_AllocateMemory(arena, componentSize * capacity); pool->denseArray = BECS_AllocateMemory(arena, componentSize * capacity);
pool->denseEntityMapping = (BECS_Entity *)BECS_AllocateMemory(arena, sizeof(BECS_Entity) * capacity); pool->denseEntityMapping = (BECS_Entity *)BECS_AllocateMemory(arena, sizeof(BECS_Entity) * capacity);
for (uint32_t i = 0; i < BECS_MAX_ENTITIES; i++) uint32_t i;
for (i = 0; i < BECS_MAX_ENTITIES; i++)
{ {
pool->sparseArray[i] = UINT32_MAX; pool->sparseArray[i] = UINT32_MAX;
} }
@ -198,19 +199,44 @@ static bool BECS_ComponentPoolHas(BECS_ComponentPool *pool, BECS_Entity entity)
/* World API Functions */ /* World API Functions */
/* TODO: Implement Initialization Functions BECS_ECS BECS_InitECS(void *memory, size_t size)
BECS_ECS BECS_InitECS(BECS_Arena *arena)
{ {
NOTE: Make use of memset here BECS_ECS ecs;
ecs.arena = BECS_CreateArena(memory, size);
ecs.entityCount = 0;
memset(ecs.entitySignatures, 0, sizeof(BECS_Signature) * BECS_MAX_ENTITIES);
memset(ecs.aliveEntites, 0, sizeof(bool) * BECS_MAX_ENTITIES);
memset(ecs.freeEntities, BECS_NULL_ENTITY, sizeof(BECS_Entity) * BECS_MAX_ENTITIES);
ecs.freeEntityCount = 0;
ecs.componentTypeCount = 0;
uint32_t i;
for (i = 0; i < BECS_MAX_COMPONENTS; i++)
{
ecs.componentPools[i].denseArray = NULL;
ecs.componentPools[i].denseEntityMapping = NULL;
}
return ecs;
} }
void BECS_ResetECS(BECS_ECS *ecs) void BECS_ResetECS(BECS_ECS *ecs)
{ {
NOTE: Make use of memset here ecs->arena = BECS_CreateArena(ecs->arena.memory, ecs->arena.capacity);
} ecs->entityCount = 0;
memset(ecs->entitySignatures, 0, sizeof(BECS_Signature) * BECS_MAX_ENTITIES);
memset(ecs->aliveEntites, 0, sizeof(bool) * BECS_MAX_ENTITIES);
memset(ecs->freeEntities, BECS_NULL_ENTITY, sizeof(BECS_Entity) * BECS_MAX_ENTITIES);
ecs->freeEntityCount = 0;
*/ ecs->componentTypeCount = 0;
uint32_t i;
for (i = 0; i < BECS_MAX_COMPONENTS; i++)
{
ecs->componentPools[i].denseArray = NULL;
ecs->componentPools[i].denseEntityMapping = NULL;
}
}
/* Entity API Functions */ /* Entity API Functions */