From f4a74ffbd75ddc5fcb7abdf54c35b9d5d6bbeadb Mon Sep 17 00:00:00 2001 From: Rodolfo Barcelli Jo Date: Thu, 22 Jan 2026 22:35:20 +0800 Subject: [PATCH] Fix: Bug with spawning where it would get stuck after reaching max entities Update: increase max entity count and increase spawn rate --- becs.h | 20 ++++++++------------ examples/example1/example1.c | 13 +++++++++---- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/becs.h b/becs.h index 8df4f4b..56da0d0 100644 --- a/becs.h +++ b/becs.h @@ -315,11 +315,6 @@ BECS_Entity BECS_EntityCreate(BECS_ECS *ecs) BECS_Entity entity = BECS_NULL_ENTITY; - if (ecs->nextEntity + 1 > BECS_MAX_ENTITIES) - { - return entity; - } - if (ecs->freeEntityCount) { /* Pop entity off the back of the free list */ @@ -327,14 +322,15 @@ BECS_Entity BECS_EntityCreate(BECS_ECS *ecs) ecs->entitySignatures[entity] = 0; ecs->aliveEntities[entity] = true; ecs->entityCount++; - return entity; } - - entity = ecs->nextEntity; - ecs->aliveEntities[entity] = true; - ecs->entitySignatures[entity] = 0; - ecs->nextEntity++; - ecs->entityCount++; + else if (ecs->nextEntity < BECS_MAX_ENTITIES) + { + entity = ecs->nextEntity; + ecs->aliveEntities[entity] = true; + ecs->entitySignatures[entity] = 0; + ecs->nextEntity++; + ecs->entityCount++; + } return entity; } diff --git a/examples/example1/example1.c b/examples/example1/example1.c index a45af71..50c8835 100644 --- a/examples/example1/example1.c +++ b/examples/example1/example1.c @@ -1,8 +1,9 @@ -#define BECS_MAX_ENTITIES 20 +#define BECS_MAX_ENTITIES 100000 #define BECS_MAX_COMPONENTS 4 #define BECS_IMPLEMENTATION #include "../../becs.h" #include +#include #include "external/raylib/src/raylib.h" // Component structs @@ -49,6 +50,7 @@ int main(void) { // Initialize ECS size_t componentSizes[4] = {sizeof(Position), sizeof(Velocity), sizeof(Acceleration), sizeof(ParticleData)}; size_t memSize = BECS_GetMinMemoryArenaSize(componentSizes, 4); + printf("%lu\n", memSize); char *memory = (char *)malloc(memSize); BECS_ECS ecs = BECS_InitECS(memory, memSize); @@ -58,7 +60,7 @@ int main(void) { BECS_ComponentID accID = BECS_ComponentRegister(&ecs, sizeof(Acceleration), BECS_MAX_ENTITIES); BECS_ComponentID dataID = BECS_ComponentRegister(&ecs, sizeof(ParticleData), BECS_MAX_ENTITIES); - InitWindow(800, 400, "2D Particle System with BECS"); + InitWindow(1600, 800, "2D Particle System with BECS"); while (!WindowShouldClose()) { float dt = GetFrameTime(); @@ -76,9 +78,12 @@ int main(void) { } // Spawn particle on mouse click - if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { + if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) { Vector2 mousePos = GetMousePosition(); - SpawnParticle(&ecs, posID, velID, accID, dataID, mousePos.x, mousePos.y); + for (uint32_t i = 0; i < 10; i++) + { + SpawnParticle(&ecs, posID, velID, accID, dataID, mousePos.x, mousePos.y); + } } // Physics system: update position and velocity