[ecs] Add a function to remove a component from all entities

While simple component pools can be cleared simply by zeroing their
counts, ones that have a delete function need that function to be called
for all the components in the pool otherwise leaks can happen.
This commit is contained in:
Bill Currie 2022-11-17 21:49:38 +09:00
parent de2cc21c7e
commit ac0079f872
2 changed files with 16 additions and 0 deletions

View File

@ -238,6 +238,7 @@ void ECS_SortComponentPool (ecs_registry_t *registry, uint32_t component,
uint32_t ECS_NewEntity (ecs_registry_t *registry);
void ECS_DelEntity (ecs_registry_t *registry, uint32_t ent);
void ECS_RemoveEntities (ecs_registry_t *registry, uint32_t component);
void *Ent_AddComponent (uint32_t ent, uint32_t comp, ecs_registry_t *registry);
void Ent_RemoveComponent (uint32_t ent, uint32_t comp,

View File

@ -216,3 +216,18 @@ ECS_DelEntity (ecs_registry_t *registry, uint32_t ent)
Ent_RemoveComponent (ent, i, registry);
}
}
VISIBLE void
ECS_RemoveEntities (ecs_registry_t *registry, uint32_t component)
{
ecs_pool_t *pool = &registry->comp_pools[component];
const component_t *comp = &registry->components[component];
__auto_type destroy = comp->destroy;
if (destroy) {
byte *data = registry->comp_pools[component].data;
for (uint32_t i = 0; i < pool->count; i++) {
destroy (data + i * comp->size);
}
}
pool->count = 0;
}