Update: Properly define Public API and Implementation
This commit is contained in:
parent
983cba4c98
commit
f2dae37c02
94
becs.h
94
becs.h
|
|
@ -26,30 +26,7 @@
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#else
|
#else
|
||||||
#define assert(expression)
|
#define assert(expression)
|
||||||
#endif /* BECS_SLOW */
|
#endif /* BECS_NO_ASSERT */
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
#ifndef BECS_MAX_ENTITIES
|
#ifndef BECS_MAX_ENTITIES
|
||||||
#define BECS_MAX_ENTITIES 4096
|
#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_NULL_ENTITY BECS_MAX_ENTITIES + 1
|
||||||
#define BECS_INVALID_COMPONENT_ID UINT32_MAX
|
#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;
|
uintptr_t nextAllocation;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
void *memory;
|
void *memory;
|
||||||
};
|
} BECS_Arena;
|
||||||
|
|
||||||
struct BECS_ComponentPool {
|
typedef struct BECS_ComponentPool {
|
||||||
size_t unitSize;
|
size_t unitSize;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
size_t count;
|
size_t count;
|
||||||
uint32_t *sparseArray;
|
uint32_t *sparseArray;
|
||||||
void *denseArray;
|
void *denseArray;
|
||||||
BECS_Entity *denseEntityMapping;
|
BECS_Entity *denseEntityMapping;
|
||||||
};
|
} BECS_ComponentPool;
|
||||||
|
|
||||||
/*
|
typedef struct BECS_ECS {
|
||||||
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 {
|
|
||||||
BECS_Arena arena;
|
BECS_Arena arena;
|
||||||
|
|
||||||
/* Entity Management */
|
/* Entity Management */
|
||||||
|
|
@ -111,7 +77,31 @@ struct BECS_ECS {
|
||||||
/* Component Management */
|
/* Component Management */
|
||||||
uint32_t componentTypeCount;
|
uint32_t componentTypeCount;
|
||||||
BECS_ComponentPool componentPools[BECS_MAX_COMPONENTS];
|
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 */
|
/* Signature Helpers */
|
||||||
static BECS_Signature BECS_SignatureSet(BECS_Signature sig, BECS_ComponentID componentId)
|
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);
|
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_IMPLEMENTATION */
|
||||||
#endif /* BECS_H */
|
|
||||||
|
|
|
||||||
|
|
@ -143,10 +143,12 @@ int main(void) {
|
||||||
BECS_ComponentID accID = BECS_ComponentRegister(&ecs, sizeof(Acceleration), BECS_MAX_ENTITIES);
|
BECS_ComponentID accID = BECS_ComponentRegister(&ecs, sizeof(Acceleration), BECS_MAX_ENTITIES);
|
||||||
BECS_ComponentID dataID = BECS_ComponentRegister(&ecs, sizeof(ParticleData), BECS_MAX_ENTITIES);
|
BECS_ComponentID dataID = BECS_ComponentRegister(&ecs, sizeof(ParticleData), BECS_MAX_ENTITIES);
|
||||||
|
|
||||||
Position *posData = (Position *)ecs.componentPools[posID].denseArray;
|
Position *posData = (Position *)BECS_ComponentGetPool(&ecs, posID);
|
||||||
Velocity *velData = (Velocity *)ecs.componentPools[velID].denseArray;
|
Velocity *velData = (Velocity *)BECS_ComponentGetPool(&ecs, velID);
|
||||||
Acceleration *accData = (Acceleration *)ecs.componentPools[accID].denseArray;
|
Acceleration *accData = (Acceleration *)BECS_ComponentGetPool(&ecs, accID);
|
||||||
ParticleData *dataData = (ParticleData *)ecs.componentPools[dataID].denseArray;
|
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;
|
BECS_Entity *entityData = (BECS_Entity *)ecs.componentPools[dataID].denseEntityMapping;
|
||||||
|
|
||||||
InitWindow(1600, 800, "2D Particle System with BECS");
|
InitWindow(1600, 800, "2D Particle System with BECS");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue