Fixed some memory leaks while creating others
This commit is contained in:
parent
f96a688c55
commit
163457c5b8
|
@ -95,10 +95,8 @@ void tc_reload_chunk(tc_world_s *world, tc_vec3i_s coord)
|
||||||
tc_reload_chunk_of_loader(loader, coord);
|
tc_reload_chunk_of_loader(loader, coord);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tc_remove_loaders_chunk(tc_chunkloader_s *loader, uint32_t index)
|
void tc_remove_chunk_from_loader(tc_chunkloader_s *loader, uint32_t index)
|
||||||
{
|
{
|
||||||
tc_delete_entity(loader->chunks[index]);
|
|
||||||
|
|
||||||
if(loader->num_chunks != 0)
|
if(loader->num_chunks != 0)
|
||||||
{
|
{
|
||||||
// This is is NOT last element
|
// This is is NOT last element
|
||||||
|
@ -106,7 +104,6 @@ void tc_remove_loaders_chunk(tc_chunkloader_s *loader, uint32_t index)
|
||||||
{
|
{
|
||||||
loader->chunks[index] = loader->chunks[loader->num_chunks-1]; // move last chunk into gap
|
loader->chunks[index] = loader->chunks[loader->num_chunks-1]; // move last chunk into gap
|
||||||
}
|
}
|
||||||
|
|
||||||
--loader->num_chunks;
|
--loader->num_chunks;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -120,7 +117,7 @@ void tc_remove_unnecessary_chunks_of_loader(tc_chunkloader_s *loader)
|
||||||
|
|
||||||
if(!tc_coord_is_within_loaders_range(loader, chunk->position))
|
if(!tc_coord_is_within_loaders_range(loader, chunk->position))
|
||||||
{
|
{
|
||||||
tc_remove_loaders_chunk(loader, chunk_index);
|
tc_remove_chunk_from_loader(loader, chunk_index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ void tc_reset_chunk_pool(tc_chunk_pool_s *chunk_pool)
|
||||||
chunk_pool->entries[entry_index].previous = &chunk_pool->entries[entry_index-1];
|
chunk_pool->entries[entry_index].previous = &chunk_pool->entries[entry_index-1];
|
||||||
++entry_index;
|
++entry_index;
|
||||||
}
|
}
|
||||||
|
chunk_pool->continuation = NULL;
|
||||||
chunk_pool->entries[0].previous = NULL;
|
chunk_pool->entries[0].previous = NULL;
|
||||||
chunk_pool->entries[chunk_pool->capacity-1].next = NULL;
|
chunk_pool->entries[chunk_pool->capacity-1].next = NULL;
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,18 @@ void tc_init_chunk_pool(uint32_t capacity)
|
||||||
tc_chunk_pool_g = tc_internal_new_chunk_pool(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);
|
||||||
|
}
|
||||||
|
|
||||||
tc_chunk_pool_entry_s * tc_allocate_chunk_pool_entry(tc_chunk_pool_s *pool)
|
tc_chunk_pool_entry_s * tc_allocate_chunk_pool_entry(tc_chunk_pool_s *pool)
|
||||||
{
|
{
|
||||||
if(pool->first_free == NULL)
|
if(pool->first_free == NULL)
|
||||||
|
@ -59,7 +71,7 @@ tc_chunk_s * tc_allocate_chunk()
|
||||||
return &entry->chunk;
|
return &entry->chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tc_free_chunk(tc_chunk_s *chunk)
|
void tc_deallocate_chunk(tc_chunk_s *chunk)
|
||||||
{
|
{
|
||||||
if(chunk->vao != 0)
|
if(chunk->vao != 0)
|
||||||
{
|
{
|
||||||
|
@ -73,24 +85,10 @@ void tc_free_chunk(tc_chunk_s *chunk)
|
||||||
chunk->vertex_data = 0;
|
chunk->vertex_data = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(chunk->vertex_positions != NULL)
|
tc_chunk_pool_entry_s *entry = chunk->pool_entry;
|
||||||
{
|
|
||||||
free(chunk->vertex_positions);
|
|
||||||
chunk->vertex_positions = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(chunk->vertex_uvs != NULL)
|
|
||||||
{
|
|
||||||
free(chunk->vertex_uvs);
|
|
||||||
chunk->vertex_uvs = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
chunk->num_vertices = 0;
|
|
||||||
chunk->position.x = 0;
|
|
||||||
chunk->position.y = 0;
|
|
||||||
chunk->position.z = 0;
|
|
||||||
memset(&chunk->blocks, 0x00, sizeof(uint32_t) * 32 * 32 * 32);
|
|
||||||
chunk->pool_entry->next = tc_chunk_pool_g->first_free;
|
chunk->pool_entry->next = tc_chunk_pool_g->first_free;
|
||||||
tc_chunk_pool_g->first_free = chunk->pool_entry;
|
tc_chunk_pool_g->first_free = entry;
|
||||||
|
memset(&chunk, 0x00, sizeof(tc_chunk_s));
|
||||||
|
chunk->pool_entry = entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -128,6 +128,7 @@ void tc_delete_entity(tc_entity_s *entity)
|
||||||
type->instances[instance_index] = type->instances[type->num_instances];
|
type->instances[instance_index] = type->instances[type->num_instances];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(entity->attributes);
|
||||||
entity->type->functions.fn_delete(entity);
|
entity->type->functions.fn_delete(entity);
|
||||||
tc_deallocate_entity(entity);
|
tc_deallocate_entity(entity);
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,6 +92,7 @@ void tc_init()
|
||||||
|
|
||||||
void tc_cleanup()
|
void tc_cleanup()
|
||||||
{
|
{
|
||||||
|
tc_cleanup_chunk_pool();
|
||||||
SDL_GL_DeleteContext(tc_game_state_g.renderer.gl_context);
|
SDL_GL_DeleteContext(tc_game_state_g.renderer.gl_context);
|
||||||
SDL_DestroyWindow(tc_game_state_g.renderer.window);
|
SDL_DestroyWindow(tc_game_state_g.renderer.window);
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ char * load_file(char *path, int *out_len)
|
||||||
{
|
{
|
||||||
*out_len = length;
|
*out_len = length;
|
||||||
}
|
}
|
||||||
|
fclose(file);
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,6 +42,9 @@ tc_shader_program_s tc_make_shader_program(char *vertex_path, char *fragment_pat
|
||||||
glCompileShader(vertex_shader);
|
glCompileShader(vertex_shader);
|
||||||
glCompileShader(fragment_shader);
|
glCompileShader(fragment_shader);
|
||||||
|
|
||||||
|
free(vertex_source);
|
||||||
|
free(fragment_source);
|
||||||
|
|
||||||
int vertex_compile_status = 0;
|
int vertex_compile_status = 0;
|
||||||
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &vertex_compile_status);
|
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &vertex_compile_status);
|
||||||
|
|
||||||
|
@ -86,6 +90,9 @@ tc_shader_program_s tc_make_shader_program(char *vertex_path, char *fragment_pat
|
||||||
glAttachShader(program, fragment_shader);
|
glAttachShader(program, fragment_shader);
|
||||||
glLinkProgram(program);
|
glLinkProgram(program);
|
||||||
|
|
||||||
|
glDeleteShader(vertex_shader);
|
||||||
|
glDeleteShader(fragment_shader);
|
||||||
|
|
||||||
tc_shader_program_s shader_program;
|
tc_shader_program_s shader_program;
|
||||||
shader_program.program_id = program;
|
shader_program.program_id = program;
|
||||||
|
|
||||||
|
|
|
@ -116,9 +116,17 @@ void tc_teleport_chunk_entity(tc_entity_s *entity, tc_location_s location)
|
||||||
|
|
||||||
void tc_delete_chunk_entity(tc_entity_s *entity)
|
void tc_delete_chunk_entity(tc_entity_s *entity)
|
||||||
{
|
{
|
||||||
|
tc_chunk_s *chunk = entity->specific;
|
||||||
|
glDeleteVertexArrays(1, &chunk->vao);
|
||||||
|
glDeleteBuffers(1, &chunk->vertex_data);
|
||||||
|
|
||||||
|
if(chunk->vertex_positions != NULL) free(chunk->vertex_positions);
|
||||||
|
if(chunk->vertex_uvs != NULL) free(chunk->vertex_uvs);
|
||||||
|
|
||||||
tc_physics_entity_s *physics_body = tc_get_pointer_from_entity(entity, "physics_body");
|
tc_physics_entity_s *physics_body = tc_get_pointer_from_entity(entity, "physics_body");
|
||||||
tc_remove_physics_entity(physics_body);
|
tc_remove_physics_entity(physics_body);
|
||||||
tc_free_chunk(entity->specific);
|
|
||||||
|
tc_deallocate_chunk(entity->specific);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tc_receive_chunk_entity_event(tc_entity_s *entity, tc_entity_event_s event)
|
void tc_receive_chunk_entity_event(tc_entity_s *entity, tc_entity_event_s event)
|
||||||
|
|
|
@ -61,6 +61,18 @@ struct tc_chunk_pool_entry
|
||||||
tc_chunk_s chunk;
|
tc_chunk_s chunk;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct tc_chunk_pool
|
||||||
|
{
|
||||||
|
tc_world_s *world;
|
||||||
|
|
||||||
|
u32_t capacity;
|
||||||
|
u32_t used_entries;
|
||||||
|
tc_chunk_pool_entry_s *entries;
|
||||||
|
tc_chunk_pool_entry_s *first_free;
|
||||||
|
|
||||||
|
tc_chunk_pool_s *continuation;
|
||||||
|
};
|
||||||
|
|
||||||
typedef struct tc_chunkloader
|
typedef struct tc_chunkloader
|
||||||
{
|
{
|
||||||
tc_vec3i_s center;
|
tc_vec3i_s center;
|
||||||
|
@ -75,18 +87,6 @@ typedef struct tc_chunkloader
|
||||||
|
|
||||||
} tc_chunkloader_s;
|
} tc_chunkloader_s;
|
||||||
|
|
||||||
struct tc_chunk_pool
|
|
||||||
{
|
|
||||||
tc_world_s *world;
|
|
||||||
|
|
||||||
u32_t capacity;
|
|
||||||
u32_t used_entries;
|
|
||||||
tc_chunk_pool_entry_s *entries;
|
|
||||||
tc_chunk_pool_entry_s *first_free;
|
|
||||||
|
|
||||||
tc_chunk_pool_s *continuation;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct tc_world
|
struct tc_world
|
||||||
{
|
{
|
||||||
tc_chunk_pool_s *pool;
|
tc_chunk_pool_s *pool;
|
||||||
|
@ -138,9 +138,10 @@ void tc_on_each_loaded_chunk (
|
||||||
void *userdata
|
void *userdata
|
||||||
);
|
);
|
||||||
|
|
||||||
|
void tc_cleanup_chunk_pool ();
|
||||||
void tc_init_chunk_pool (u32_t capacity);
|
void tc_init_chunk_pool (u32_t capacity);
|
||||||
tc_chunk_s * tc_allocate_chunk ();
|
tc_chunk_s * tc_allocate_chunk ();
|
||||||
void tc_free_chunk (tc_chunk_s *chunk);
|
void tc_deallocate_chunk (tc_chunk_s *chunk);
|
||||||
|
|
||||||
void tc_create_world_physics (tc_world_s *world, void *userdata);
|
void tc_create_world_physics (tc_world_s *world, void *userdata);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue