Fix: Bug with spawning where it would get stuck after reaching max entities

Update: increase max entity count and increase spawn rate
This commit is contained in:
Rodolfo Barcelli Jo 2026-01-22 22:35:20 +08:00
parent 26127dc35b
commit f4a74ffbd7
2 changed files with 17 additions and 16 deletions

20
becs.h
View file

@ -315,11 +315,6 @@ BECS_Entity BECS_EntityCreate(BECS_ECS *ecs)
BECS_Entity entity = BECS_NULL_ENTITY; BECS_Entity entity = BECS_NULL_ENTITY;
if (ecs->nextEntity + 1 > BECS_MAX_ENTITIES)
{
return entity;
}
if (ecs->freeEntityCount) if (ecs->freeEntityCount)
{ {
/* Pop entity off the back of the free list */ /* 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->entitySignatures[entity] = 0;
ecs->aliveEntities[entity] = true; ecs->aliveEntities[entity] = true;
ecs->entityCount++; ecs->entityCount++;
return entity;
} }
else if (ecs->nextEntity < BECS_MAX_ENTITIES)
entity = ecs->nextEntity; {
ecs->aliveEntities[entity] = true; entity = ecs->nextEntity;
ecs->entitySignatures[entity] = 0; ecs->aliveEntities[entity] = true;
ecs->nextEntity++; ecs->entitySignatures[entity] = 0;
ecs->entityCount++; ecs->nextEntity++;
ecs->entityCount++;
}
return entity; return entity;
} }

View file

@ -1,8 +1,9 @@
#define BECS_MAX_ENTITIES 20 #define BECS_MAX_ENTITIES 100000
#define BECS_MAX_COMPONENTS 4 #define BECS_MAX_COMPONENTS 4
#define BECS_IMPLEMENTATION #define BECS_IMPLEMENTATION
#include "../../becs.h" #include "../../becs.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include "external/raylib/src/raylib.h" #include "external/raylib/src/raylib.h"
// Component structs // Component structs
@ -49,6 +50,7 @@ int main(void) {
// Initialize ECS // Initialize ECS
size_t componentSizes[4] = {sizeof(Position), sizeof(Velocity), sizeof(Acceleration), sizeof(ParticleData)}; size_t componentSizes[4] = {sizeof(Position), sizeof(Velocity), sizeof(Acceleration), sizeof(ParticleData)};
size_t memSize = BECS_GetMinMemoryArenaSize(componentSizes, 4); size_t memSize = BECS_GetMinMemoryArenaSize(componentSizes, 4);
printf("%lu\n", memSize);
char *memory = (char *)malloc(memSize); char *memory = (char *)malloc(memSize);
BECS_ECS ecs = BECS_InitECS(memory, 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 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);
InitWindow(800, 400, "2D Particle System with BECS"); InitWindow(1600, 800, "2D Particle System with BECS");
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
float dt = GetFrameTime(); float dt = GetFrameTime();
@ -76,9 +78,12 @@ int main(void) {
} }
// Spawn particle on mouse click // Spawn particle on mouse click
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
Vector2 mousePos = GetMousePosition(); 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 // Physics system: update position and velocity