2023-10-14 13:04:52 +00:00
|
|
|
#include "entity.h"
|
|
|
|
#include "state.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2023-10-14 19:17:30 +00:00
|
|
|
// TODO: Write a *real* pool allocator
|
|
|
|
|
2023-10-15 09:51:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:17:30 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-15 09:51:45 +00:00
|
|
|
tc_entity_s * tc_allocate_entity_from_pool(tc_entity_pool_s *pool)
|
2023-10-14 13:04:52 +00:00
|
|
|
{
|
2023-10-15 09:51:45 +00:00
|
|
|
if(pool->first_free == NULL)
|
2023-10-14 13:04:52 +00:00
|
|
|
{
|
2023-10-15 09:51:45 +00:00
|
|
|
if(pool->continuation == NULL)
|
|
|
|
pool->continuation = tc_create_entity_pool(pool->entities_capacity * 2);
|
|
|
|
|
|
|
|
return tc_allocate_entity_from_pool(pool->continuation);
|
2023-10-14 13:04:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 09:51:45 +00:00
|
|
|
tc_entity_s *entity = &pool->first_free->entity;
|
|
|
|
entity->pool_entry = pool->first_free;
|
|
|
|
pool->first_free = pool->first_free->next;
|
2023-10-14 13:04:52 +00:00
|
|
|
|
2023-10-15 09:51:45 +00:00
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
tc_entity_s * tc_allocate_entity()
|
|
|
|
{
|
|
|
|
return tc_allocate_entity_from_pool(tc_game_state_g.entity_registry.pool);
|
2023-10-14 13:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tc_deallocate_entity(tc_entity_s *entity)
|
|
|
|
{
|
2023-10-15 09:51:45 +00:00
|
|
|
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;
|
2023-10-14 13:04:52 +00:00
|
|
|
}
|
|
|
|
|