initial commit

This commit is contained in:
Rodolfo Barcelli Jo 2026-01-16 17:36:44 +08:00
commit 218546d26f
6 changed files with 155 additions and 0 deletions

14
.clangd Normal file
View file

@ -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"]

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
build/*

104
becs.h Normal file
View file

@ -0,0 +1,104 @@
#ifndef BECS_H
#define BECS_H
#ifdef BECS_IMPLEMENTATION
#include <stdint.h>
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

1
build.bat Normal file
View file

@ -0,0 +1 @@
REM Build Script for BECS

1
build.sh Normal file
View file

@ -0,0 +1 @@
# Build script for BECS

34
test/main.c Normal file
View file

@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#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");
}