diff --git a/code/source-c/chunk_loader.c b/code/source-c/chunk_loader.c index 152695b..01b24ba 100644 --- a/code/source-c/chunk_loader.c +++ b/code/source-c/chunk_loader.c @@ -95,10 +95,8 @@ void tc_reload_chunk(tc_world_s *world, tc_vec3i_s 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) { // 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->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)) { - tc_remove_loaders_chunk(loader, chunk_index); + tc_remove_chunk_from_loader(loader, chunk_index); } } } diff --git a/code/source-c/chunk_pool.c b/code/source-c/chunk_pool.c index f9c65c6..702f7ca 100644 --- a/code/source-c/chunk_pool.c +++ b/code/source-c/chunk_pool.c @@ -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]; ++entry_index; } - + chunk_pool->continuation = NULL; chunk_pool->entries[0].previous = 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); } +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) { if(pool->first_free == NULL) @@ -59,7 +71,7 @@ tc_chunk_s * tc_allocate_chunk() return &entry->chunk; } -void tc_free_chunk(tc_chunk_s *chunk) +void tc_deallocate_chunk(tc_chunk_s *chunk) { if(chunk->vao != 0) { @@ -73,24 +85,10 @@ void tc_free_chunk(tc_chunk_s *chunk) chunk->vertex_data = 0; } - if(chunk->vertex_positions != NULL) - { - 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); + tc_chunk_pool_entry_s *entry = chunk->pool_entry; 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; } diff --git a/code/source-c/entity.c b/code/source-c/entity.c index 2d9016c..ce60668 100644 --- a/code/source-c/entity.c +++ b/code/source-c/entity.c @@ -128,6 +128,7 @@ void tc_delete_entity(tc_entity_s *entity) type->instances[instance_index] = type->instances[type->num_instances]; } + free(entity->attributes); entity->type->functions.fn_delete(entity); tc_deallocate_entity(entity); } diff --git a/code/source-c/initialization.c b/code/source-c/initialization.c index f2c45d0..55a96df 100644 --- a/code/source-c/initialization.c +++ b/code/source-c/initialization.c @@ -92,6 +92,7 @@ void tc_init() void tc_cleanup() { + tc_cleanup_chunk_pool(); SDL_GL_DeleteContext(tc_game_state_g.renderer.gl_context); SDL_DestroyWindow(tc_game_state_g.renderer.window); } diff --git a/code/source-c/shaders.c b/code/source-c/shaders.c index 64cda36..4dc04a6 100644 --- a/code/source-c/shaders.c +++ b/code/source-c/shaders.c @@ -16,6 +16,7 @@ char * load_file(char *path, int *out_len) { *out_len = length; } + fclose(file); return content; } @@ -41,6 +42,9 @@ tc_shader_program_s tc_make_shader_program(char *vertex_path, char *fragment_pat glCompileShader(vertex_shader); glCompileShader(fragment_shader); + free(vertex_source); + free(fragment_source); + int vertex_compile_status = 0; 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); glLinkProgram(program); + glDeleteShader(vertex_shader); + glDeleteShader(fragment_shader); + tc_shader_program_s shader_program; shader_program.program_id = program; diff --git a/code/source-c/world.c b/code/source-c/world.c index 3d3d59f..5e0efb7 100644 --- a/code/source-c/world.c +++ b/code/source-c/world.c @@ -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) { + 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_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) diff --git a/code/source-c/world.h b/code/source-c/world.h index be82174..4dc55a3 100644 --- a/code/source-c/world.h +++ b/code/source-c/world.h @@ -61,6 +61,18 @@ struct tc_chunk_pool_entry 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 { tc_vec3i_s center; @@ -75,18 +87,6 @@ typedef struct tc_chunkloader } 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 { tc_chunk_pool_s *pool; @@ -138,9 +138,10 @@ void tc_on_each_loaded_chunk ( void *userdata ); +void tc_cleanup_chunk_pool (); void tc_init_chunk_pool (u32_t capacity); 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);