#include "entity.h" #include "state.h" #include // TODO: Write a *real* pool allocator tc_entity_pool_s * tc_create_entity_pool(u32_t capacity) { tc_entity_pool_s *pool = malloc(sizeof(tc_entity_pool_s)); pool->entities_capacity = capacity; pool->entities = calloc(sizeof(tc_entity_pool_entry_s), capacity); pool->first_free = &pool->entities[0]; pool->continuation = NULL; u32_t entity_index = 0; while(entity_index < capacity) { pool->entities[entity_index].next = &pool->entities[entity_index+1]; pool->entities[entity_index].previous = &pool->entities[entity_index-1]; ++entity_index; } pool->entities[0].previous = NULL; pool->entities[capacity-1].next = NULL; return pool; } 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_from_pool(tc_entity_pool_s *pool) { if(pool->first_free == NULL) { if(pool->continuation == NULL) pool->continuation = tc_create_entity_pool(pool->entities_capacity * 2); return tc_allocate_entity_from_pool(pool->continuation); } tc_entity_s *entity = &pool->first_free->entity; entity->pool_entry = pool->first_free; pool->first_free = pool->first_free->next; return entity; } tc_entity_s * tc_allocate_entity() { return tc_allocate_entity_from_pool(tc_game_state_g.entity_registry.pool); } void tc_deallocate_entity(tc_entity_s *entity) { tc_entity_pool_s *pool = tc_game_state_g.entity_registry.pool; entity->pool_entry->next = pool->first_free; pool->first_free = entity->pool_entry; }