From 218546d26f02a87372f83f8775dbe9e5007521de Mon Sep 17 00:00:00 2001 From: Rodolfo Barcelli Jo Date: Fri, 16 Jan 2026 17:36:44 +0800 Subject: [PATCH] initial commit --- .clangd | 14 +++++++ .gitignore | 1 + becs.h | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++ build.bat | 1 + build.sh | 1 + test/main.c | 34 +++++++++++++++++ 6 files changed, 155 insertions(+) create mode 100644 .clangd create mode 100644 .gitignore create mode 100644 becs.h create mode 100644 build.bat create mode 100644 build.sh create mode 100644 test/main.c diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..fa3677e --- /dev/null +++ b/.clangd @@ -0,0 +1,14 @@ +CompileFlags: + Add: [ + "-Wall", + "-Wextra", + "-Werror", + "-DBECS_IMPLEMENTATION=1"] +Diagnostics: + Suppress: [ + "-Wunused-function", + "-Wunused-but-set-parameter", + "-Wunused-parameter", + "-Wunused-variable", + "-Wuninitialized", + "-Wunused-but-set-variable"] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a007fea --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/* diff --git a/becs.h b/becs.h new file mode 100644 index 0000000..503c355 --- /dev/null +++ b/becs.h @@ -0,0 +1,104 @@ +#ifndef BECS_H +#define BECS_H + + +#ifdef BECS_IMPLEMENTATION +#include + +typedef struct BECS_ComponentArray { + uint32_t lastEntityAdded; + size_t unitSize; + uint32_t length; + uint32_t capacity; + void *array; +} BECS_ComponentArray; + +typedef struct BECS_Properties { + uint32_t maxEntities; + uint32_t numComponents; + size_t *componentSizes; +} BECS_Properties; + +typedef struct BECS_ECS { + uint64_t *bitMasks; + BECS_Properties props; + BECS_ComponentArray *componentSets; +} BECS_ECS; + +size_t BECS_GetMinMemorySize +(BECS_Properties props) +{ + size_t totalSize = 0; + totalSize += sizeof(BECS_ECS); + totalSize += sizeof(BECS_ComponentArray) * props.numComponents; + for (uint32_t i = 0; i < props.numComponents; i++) + { + totalSize += props.componentSizes[i] * props.maxEntities; + totalSize += sizeof(uint64_t); + } + return totalSize; +} + +BECS_ECS *BECS_Init +(void *memory, size_t size, BECS_Properties *props) +{ + BECS_ECS *ecs = (BECS_ECS *)memory; + uint64_t *bitMasks = (uint64_t *)(ecs + 1); + BECS_ComponentArray *componentSets = (BECS_ComponentArray *)(bitMasks + props->maxEntities); + ecs->bitMasks = bitMasks; + ecs->componentSets = componentSets; + ecs->props = *props; + + ecs->bitMasks = 0; + for (uint32_t i = 0; i < props->numComponents; i++) + { + ecs->componentSets[i].length = 0; + } + return ecs; +} + +int32_t +BECS_AddComponent(BECS_ECS *ecs, uint32_t entityID, uint64_t componentBitMask, void *component) +{ + int32_t result = 0; + if (componentBitMask == 0) + { + return result; + } + + uint32_t componentType = 0; + while (!(componentBitMask & 1)) + { + componentBitMask >>= 1; + componentType++; + } + + ecs->bitMasks[entityID] = ecs->bitMasks[entityID] | componentBitMask; + uint32_t insertIndex = ecs->componentSets[componentType].length; + if (insertIndex > ecs->props.maxEntities) + { + return result; + } + + size_t componentSize = ecs->props.componentSizes[componentType]; + uint8_t *componentArray = (uint8_t *)ecs->componentSets[componentType].array; + uint8_t *source = (uint8_t *)component; + componentArray += componentSize; + while (componentSize > 0) + { + *componentArray = *source; + componentSize--; + } + ecs->componentSets[componentType].length++; + result = 1; + + return result; +} + +// int32_t +// BECS_RemoveComponent() +// { +// } +#endif + +#endif diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..825ed05 --- /dev/null +++ b/build.bat @@ -0,0 +1 @@ +REM Build Script for BECS diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..72cbea7 --- /dev/null +++ b/build.sh @@ -0,0 +1 @@ +# Build script for BECS diff --git a/test/main.c b/test/main.c new file mode 100644 index 0000000..d562a71 --- /dev/null +++ b/test/main.c @@ -0,0 +1,34 @@ +#include +#include + +#define BECS_IMPLEMENTATION +#include "becs.h" + +typedef struct compA { + uint32_t thing; +} compA; + +typedef struct compB { + uint32_t thing; +} compB; + +int main(void) { + BECS_Properties EcsProps = {}; + + size_t componentSizes[2] = { + sizeof(compA), + sizeof(compB), + }; + + EcsProps.componentSizes = componentSizes; + EcsProps.numComponents = 2; + EcsProps.maxEntities = 100; + + size_t MinMemorySize = BECS_GetMinMemorySize(EcsProps); + printf("%llu\n", MinMemorySize); + + BECS_ECS *ecs = malloc(MinMemorySize); + BECS_Init(ecs, MinMemorySize, &EcsProps); + + printf("Initialized\n"); +}