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
#endif /* BECS_MAX_COMPONENTS */
#define BECS_NULL_ENTITY
#define BECS_NULL_ENTITY BECS_MAX_COMPONENTS + 1
#define BECS_INVALID_COMPONENT_ID UINT32_MAX
typedef uint64_t BECS_Signature;
@ -73,10 +73,10 @@ struct BECS_ECS {
/* Entity Management */
uint32_t entityCount;
BECS_Signature entitySignatures[BECS_MAX_COMPONENTS];
bool aliveEntites[BECS_MAX_COMPONENTS];
BECS_Signature entitySignatures[BECS_MAX_ENTITIES];
bool aliveEntites[BECS_MAX_ENTITIES];
BECS_Entity freeEntities[BECS_MAX_COMPONENTS];
BECS_Entity freeEntities[BECS_MAX_ENTITIES];
uint32_t freeEntityCount;
/* Component Management */
@ -85,29 +85,29 @@ struct BECS_ECS {
};
/* 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);
}
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;
}
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;
}
/* 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);
BECS_Arena arena = {
baseOffset,
size,
capacity,
memory
};
return arena;
@ -140,7 +140,8 @@ static void BECS_ComponentPoolInit
pool->denseArray = BECS_AllocateMemory(arena, componentSize * 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;
}
@ -198,19 +199,44 @@ static bool BECS_ComponentPoolHas(BECS_ComponentPool *pool, BECS_Entity entity)
/* World API Functions */
/* TODO: Implement Initialization Functions
BECS_ECS BECS_InitECS(BECS_Arena *arena)
BECS_ECS BECS_InitECS(void *memory, size_t size)
{
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)
{
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 */