Techneck/code/source-c/entity_pool.c

53 lines
1.4 KiB
C

#include "entity.h"
#include "state.h"
#include <stdlib.h>
tc_entity_s * tc_allocate_entity()
{
tc_entity_registry_s registry = tc_game_state_g.entity_registry;
uint32_t entity_index = 0;
while(entity_index < registry.entities_capacity)
{
if(registry.entities[entity_index] == NULL)
{
registry.entities[entity_index] = malloc(sizeof(tc_entity_s));
++registry.num_entities;
return registry.entities[entity_index];
}
++entity_index;
}
registry.entities_capacity *= 2;
registry.entities =
realloc(registry.entities, sizeof(tc_entity_s *) * registry.entities_capacity);
registry.entities[registry.num_entities] = malloc(sizeof(tc_entity_s));
++registry.num_entities;
return registry.entities[registry.num_entities-1];
}
void tc_deallocate_entity(tc_entity_s *entity)
{
if(entity->specific != NULL) free(entity->specific);
if(entity->attributes != NULL) free(entity->attributes);
tc_entity_registry_s registry = tc_game_state_g.entity_registry;
uint32_t entity_index = 0;
while(entity_index < registry.entities_capacity)
{
if(registry.entities[entity_index] == entity)
{
registry.entities[entity_index] = NULL;
break;
}
++entity_index;
}
free(entity);
}