#include "entity.h" #include "state.h" #include // TODO: Write a *real* pool allocator void tc_reset_entity(tc_entity_s *entity) { if(entity->attributes != NULL) free(entity->attributes); entity->attributes_capacity = 8; entity->attributes = calloc(sizeof(tc_entity_attribute_s), entity->attributes_capacity); } 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] = calloc(sizeof(tc_entity_s), 1); ++registry.num_entities; registry.entities[entity_index]->attributes_capacity = 8; registry.entities[entity_index]->attributes = calloc(sizeof(tc_entity_attribute_s), registry.entities[entity_index]->attributes_capacity); tc_reset_entity(registry.entities[entity_index]); return registry.entities[entity_index]; } ++entity_index; } registry.entities = realloc(registry.entities, sizeof(tc_entity_s *) * registry.entities_capacity * 2); memset(®istry.entities[registry.entities_capacity], 0x00, sizeof(tc_entity_s *) * registry.entities_capacity); registry.entities_capacity *= 2; registry.entities[registry.num_entities] = malloc(sizeof(tc_entity_s)); tc_reset_entity(registry.entities[registry.num_entities]); ++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); }