Techneck/code/source-c/world/chunk_pool.c

95 lines
2.6 KiB
C
Raw Normal View History

2023-10-12 16:52:05 +00:00
#include "world.h"
#include <stdlib.h>
2023-10-14 13:04:52 +00:00
#include <string.h>
2023-10-12 16:52:05 +00:00
2023-10-14 19:17:30 +00:00
tc_chunk_pool_s *tc_chunk_pool_g = NULL;
2023-10-12 16:52:05 +00:00
void tc_reset_chunk_pool(tc_chunk_pool_s *chunk_pool)
{
chunk_pool->used_entries = 0;
chunk_pool->first_free = chunk_pool->entries;
uint32_t entry_index = 0;
while(entry_index < chunk_pool->capacity)
{
chunk_pool->entries[entry_index].next = &chunk_pool->entries[entry_index+1];
chunk_pool->entries[entry_index].previous = &chunk_pool->entries[entry_index-1];
++entry_index;
}
chunk_pool->continuation = NULL;
2023-10-12 16:52:05 +00:00
chunk_pool->entries[0].previous = NULL;
chunk_pool->entries[chunk_pool->capacity-1].next = NULL;
}
2023-10-14 19:17:30 +00:00
tc_chunk_pool_s * tc_internal_new_chunk_pool(uint32_t capacity)
2023-10-12 16:52:05 +00:00
{
tc_chunk_pool_s *pool = malloc(sizeof(tc_chunk_pool_s));
pool->capacity = capacity;
pool->entries = calloc(sizeof(tc_chunk_pool_entry_s), pool->capacity);
tc_reset_chunk_pool(pool);
return pool;
}
2023-10-14 19:17:30 +00:00
void tc_init_chunk_pool(uint32_t capacity)
{
tc_chunk_pool_g = tc_internal_new_chunk_pool(capacity);
}
void tc_internal_free_chunk_pool(tc_chunk_pool_s *pool)
{
if(pool->continuation != NULL) tc_internal_free_chunk_pool(pool->continuation);
free(pool->entries);
free(pool);
}
void tc_cleanup_chunk_pool()
{
tc_internal_free_chunk_pool(tc_chunk_pool_g);
}
2023-10-12 16:52:05 +00:00
tc_chunk_pool_entry_s * tc_allocate_chunk_pool_entry(tc_chunk_pool_s *pool)
{
if(pool->first_free == NULL)
{
2023-10-14 19:17:30 +00:00
if(pool->continuation == NULL)
pool->continuation = tc_internal_new_chunk_pool(pool->capacity * 2);
2023-10-12 16:52:05 +00:00
return tc_allocate_chunk_pool_entry(pool->continuation);
}
tc_chunk_pool_entry_s *allocated = pool->first_free;
pool->first_free = allocated->next;
return allocated;
}
2023-10-14 19:17:30 +00:00
tc_chunk_s * tc_allocate_chunk()
2023-10-12 16:52:05 +00:00
{
2023-10-14 19:17:30 +00:00
tc_chunk_pool_entry_s *entry = tc_allocate_chunk_pool_entry(tc_chunk_pool_g);
2023-10-14 08:47:21 +00:00
entry->chunk.pool_entry = entry;
2023-10-12 16:52:05 +00:00
return &entry->chunk;
}
void tc_deallocate_chunk(tc_chunk_s *chunk)
2023-10-12 16:52:05 +00:00
{
2023-10-14 08:47:21 +00:00
if(chunk->vao != 0)
{
2023-10-17 11:25:02 +00:00
glDeleteVertexArrays(1, &chunk->vbo);
2023-10-14 08:47:21 +00:00
chunk->vao = 0;
}
2023-10-13 06:00:53 +00:00
2023-10-17 11:25:02 +00:00
if(chunk->vbo != 0)
2023-10-14 08:47:21 +00:00
{
2023-10-17 11:25:02 +00:00
glDeleteBuffers(1, &chunk->vbo);
chunk->vbo = 0;
2023-10-14 08:47:21 +00:00
}
2023-10-13 06:00:53 +00:00
2023-10-17 11:25:02 +00:00
tc_chunk_pool_entry_s *entry = chunk->pool_entry;
entry->next = tc_chunk_pool_g->first_free;
tc_chunk_pool_g->first_free = entry;
memset(chunk, 0x00, sizeof(tc_chunk_s));
chunk->pool_entry = entry;
2023-10-12 16:52:05 +00:00
}