From da302520b780965a2f1b1a0c8555a51f8d3f6edc Mon Sep 17 00:00:00 2001 From: shariq Date: Wed, 31 May 2017 23:21:51 +0500 Subject: [PATCH] Fixed crash on exit --- orgfile.org | 3 ++- src/entity.c | 7 +++++-- src/file_io.c | 2 ++ src/framebuffer.c | 2 ++ src/game.c | 7 +++---- src/game.h | 6 +++--- src/geometry.c | 12 ++++++------ src/input.c | 3 +-- src/light.c | 1 + src/model.c | 6 ++---- src/model.h | 1 - src/platform.c | 6 +----- src/platform.h | 1 - src/shader.c | 2 ++ src/sound.c | 4 ++++ src/texture.c | 2 ++ 16 files changed, 36 insertions(+), 29 deletions(-) diff --git a/orgfile.org b/orgfile.org index 8f6b64f..b693486 100644 --- a/orgfile.org +++ b/orgfile.org @@ -263,7 +263,8 @@ x Log output on every run. - State "DONE" from "TODO" [2017-05-24 Wed 17:12] ** TODO Add default keybindings ** TODO Write default config/keybindings etc to file if none are found in preferences dir -** TODO Fix input map bugs +** DONE Fix input map bugs +- State "DONE" from "TODO" [2017-05-31 Wed 23:19] ** TODO Wrap malloc and free calls in custom functions to track usage ** TODO Flatpak packaging for linux releases ** TODO Use hashmap for debugvar slots in editor diff --git a/src/entity.c b/src/entity.c index 3c712ef..a5c22ab 100644 --- a/src/entity.c +++ b/src/entity.c @@ -24,8 +24,11 @@ void entity_init(void) void entity_cleanup(void) { - for(int i = 0; i < array_len(entity_list); i++) - entity_remove(i); + if(array_len(empty_indices) < array_len(entity_list)) + { + for(int i = 0; i < array_len(entity_list); i++) + entity_remove(i); + } array_free(entity_list); array_free(empty_indices); diff --git a/src/file_io.c b/src/file_io.c index 5c4e707..f7385f5 100644 --- a/src/file_io.c +++ b/src/file_io.c @@ -18,6 +18,8 @@ void io_file_cleanup(void) { if(install_directory) free(install_directory); if(user_directory) free(user_directory); + install_directory = NULL; + user_directory = NULL; } char* io_file_read(const int directory_type, const char* path, const char* mode, long* file_size) diff --git a/src/framebuffer.c b/src/framebuffer.c index 68648bd..28aaa56 100644 --- a/src/framebuffer.c +++ b/src/framebuffer.c @@ -34,6 +34,8 @@ void framebuffer_cleanup(void) array_free(fbo_list); array_free(empty_indices); + fbo_list = NULL; + empty_indices = NULL; } int framebuffer_create(int width, int height, int has_depth, int has_color, int resizeable) diff --git a/src/game.c b/src/game.c index d8488d1..71de0b6 100644 --- a/src/game.c +++ b/src/game.c @@ -60,7 +60,7 @@ int game_init(struct Window* window) game_state->window = window; game_state->player_node = -1; game_state->player_pitch_node = -1; - game_state->is_initialized = 0; + game_state->is_initialized = false; } /* TODO: Decouple systems' init/cleanup from game, they should exist and run even if there's no "game" */ @@ -81,7 +81,7 @@ int game_init(struct Window* window) /* Debug scene setup */ scene_setup(); - game_state->is_initialized = 1; + game_state->is_initialized = true; return run(); } @@ -1558,7 +1558,6 @@ void game_cleanup(void) editor_cleanup(); scene_cleanup(); entity_cleanup(); - model_cleanup(); material_cleanup(); geom_cleanup(); light_cleanup(); @@ -1571,9 +1570,9 @@ void game_cleanup(void) sound_cleanup(); window_destroy(game_state->window); gl_cleanup(); - window_cleanup(); } free(game_state); + game_state = NULL; } } diff --git a/src/game.h b/src/game.h index c309433..5474e56 100644 --- a/src/game.h +++ b/src/game.h @@ -1,5 +1,5 @@ -#ifndef game_H -#define game_H +#ifndef GAME_H +#define GAME_H #include "platform.h" @@ -8,7 +8,7 @@ struct Game_State struct Window* window; int player_node; int player_pitch_node; - int is_initialized; + bool is_initialized; }; diff --git a/src/geometry.c b/src/geometry.c index 988f474..cd1109a 100644 --- a/src/geometry.c +++ b/src/geometry.c @@ -381,12 +381,12 @@ int geom_render_in_frustum(int index, { geom_render(index, draw_mode); rendered = array_len(geometry->indices); - /* intersection = bv_intersect_frustum_box(frustum, &geometry->bounding_box, transform); */ - /* if(intersection == IT_INTERSECT || intersection == IT_INSIDE) */ - /* { */ - /* geom_render(index, draw_mode); */ - /* rendered = array_len(geometry->indices); */ - /* } */ + intersection = bv_intersect_frustum_box(frustum, &geometry->bounding_box, transform); + if(intersection == IT_INTERSECT || intersection == IT_INSIDE) + { + geom_render(index, draw_mode); + rendered = array_len(geometry->indices); + } } return rendered; } diff --git a/src/input.c b/src/input.c index 9a7d86c..6b429be 100644 --- a/src/input.c +++ b/src/input.c @@ -80,15 +80,14 @@ void input_init(void) void input_cleanup(void) { - //input_keybinds_save("keybindings.cfg"); for(int i = 0; i < array_len(input_map_list); i++) { struct Input_Map* map = &input_map_list[i]; - //log_message("Map : %s, Num keys : %d", map->name, array_len(map->keys)); if(map->name) free(map->name); array_free(map->keys); } array_free(input_map_list); + input_map_list = NULL; } bool input_keybinds_load(const char* filename, int directory_type) diff --git a/src/light.c b/src/light.c index 4dac20e..58a98e6 100644 --- a/src/light.c +++ b/src/light.c @@ -14,6 +14,7 @@ void light_init(void) void light_cleanup(void) { array_free(light_list); + light_list = NULL; } diff --git a/src/model.c b/src/model.c index ce5c48f..c2aead1 100644 --- a/src/model.c +++ b/src/model.c @@ -57,12 +57,10 @@ void model_destroy(struct Entity* entity) free(model->material_params[i].value); array_free(model->material_params); + model->material_params = NULL; + model->material = -1; } -void model_cleanup(void) -{ - -} void model_render_all(struct Entity* camera_entity, int draw_mode) { diff --git a/src/model.h b/src/model.h index d82e5a9..6e5afce 100644 --- a/src/model.h +++ b/src/model.h @@ -9,7 +9,6 @@ struct Entity; void model_init(void); void model_create(struct Entity* entity, const char* geo_name, const char* material_name); void model_destroy(struct Entity* entity); -void model_cleanup(void); void model_render_all(struct Entity* camera_entity, int draw_mode); int model_set_material_param(struct Entity* entity, const char* name, void* value); int model_get_material_param(struct Entity* entity, const char* name, void* value_out); diff --git a/src/platform.c b/src/platform.c index 7f90d23..991f4fc 100644 --- a/src/platform.c +++ b/src/platform.c @@ -149,11 +149,6 @@ void window_destroy(struct Window* window) SDL_DestroyWindow((SDL_Window*)window->sdl_window); } -void window_cleanup(void) -{ - -} - void window_set_size(struct Window* window, int width, int height) { SDL_SetWindowSize((SDL_Window*)window->sdl_window, width, height); @@ -208,6 +203,7 @@ int platform_init(void) void platform_cleanup(void) { if(platform_state) free(platform_state); + platform_state = NULL; SDL_Quit(); } diff --git a/src/platform.h b/src/platform.h index 16fcdf8..1d0a4fc 100644 --- a/src/platform.h +++ b/src/platform.h @@ -20,7 +20,6 @@ void window_show(struct Window* window); void window_hide(struct Window* window); void window_raise(struct Window* window); void window_make_context_current(struct Window* window); -void window_cleanup(void); void window_set_size(struct Window* window, int width, int height); void window_get_size(struct Window* window, int* out_width, int* out_height); void window_get_drawable_size(struct Window* window, int* out_width, int* out_height); diff --git a/src/shader.c b/src/shader.c index 4a9d5b0..77c9a94 100644 --- a/src/shader.c +++ b/src/shader.c @@ -299,6 +299,8 @@ void shader_cleanup(void) array_free(shader_list); array_free(empty_indices); + shader_list = NULL; + empty_indices = NULL; } void shader_set_uniform(const int uniform_type, const int uniform_loc, void* value) diff --git a/src/sound.c b/src/sound.c index 4627257..8323508 100644 --- a/src/sound.c +++ b/src/sound.c @@ -135,6 +135,10 @@ void sound_cleanup(void) alcMakeContextCurrent(NULL); alcDestroyContext(sound_state.context); alcCloseDevice(sound_state.device); + sound_state.context = NULL; + sound_state.device = NULL; + sound_state.volume = 0.f; + sound_state.listener_entity = -1; } void sound_error_check(const char* file, unsigned int line, const char* expression) diff --git a/src/texture.c b/src/texture.c index 3af6e48..16a0538 100644 --- a/src/texture.c +++ b/src/texture.c @@ -152,6 +152,8 @@ void texture_cleanup(void) array_free(texture_list); array_free(empty_indices); + texture_list = NULL; + empty_indices = NULL; } void texture_bind(int index)