becs/test/main.c

102 lines
2.2 KiB
C

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define BECS_MAX_ENTITIES 100
#define BECS_MAX_COMPONENTS 2
#define BECS_IMPLEMENTATION
#include "../becs.h"
typedef struct compA {
uint32_t thing;
} compA;
typedef struct compB {
uint32_t thing;
} compB;
enum {
COMPA,
COMPB
};
int main(void) {
size_t componentSizes[2] = {
sizeof(compA),
sizeof(compB),
};
size_t MinMemorySize = BECS_GetMinMemoryArenaSize(componentSizes, 2);
#ifdef _WIN32
printf("%llu\n", MinMemorySize);
#endif
#ifdef UNIX
printf("%lu\n", MinMemorySize);
#endif
void *memory= malloc(MinMemorySize);
BECS_ECS ecs = BECS_InitECS(memory, MinMemorySize);
BECS_ComponentRegister(&ecs, sizeof(compA), BECS_MAX_ENTITIES);
BECS_ComponentRegister(&ecs, sizeof(compB), BECS_MAX_ENTITIES);
printf("Initialized\n");
BECS_Entity entity1 = BECS_EntityCreate(&ecs);
BECS_Entity entity2 = BECS_EntityCreate(&ecs);
uint32_t i;
for (i = 0; i < BECS_MAX_ENTITIES; i++)
{
BECS_Entity entity = BECS_EntityCreate(&ecs);
if (entity != BECS_NULL_ENTITY)
{
printf("%u\n", entity);
}
else
{
printf("Reached Max Entity Count\n");
}
}
BECS_ComponentAdd(&ecs, entity1, COMPA);
BECS_ComponentAdd(&ecs, entity2, COMPB);
bool testComponent1 = BECS_ComponentHas(&ecs, entity1, 0);
bool testComponent2 = BECS_ComponentHas(&ecs, entity1, 1);
bool testComponent3 = BECS_ComponentHas(&ecs, entity2, 0);
bool testComponent4 = BECS_ComponentHas(&ecs, entity2, 1);
if (testComponent1)
{
printf("Entity:%u has compA\n", entity1);
}
if (testComponent2)
{
printf("Entity:%u has compB\n", entity1);
}
if (testComponent3)
{
printf("Entity:%u has compA\n", entity2);
}
if (testComponent4)
{
printf("Entity:%u has compB\n", entity2);
}
compA *thingA = (compA *)BECS_ComponentGet(&ecs, entity1, COMPA);
compB *thingB = (compB *)BECS_ComponentGet(&ecs, entity1, COMPB);
BECS_ComponentRemove(&ecs, entity1, COMPA);
BECS_ComponentRemove(&ecs, entity2, COMPB);
BECS_ComponentRemove(&ecs, entity1, COMPB);
BECS_ComponentRemove(&ecs, entity2, COMPA);
return 0;
}