From 5401420015ddbe10ad777e71d17ee0160f4eacd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Husi=C4=8Dka?= Date: Tue, 14 Dec 2021 22:02:07 +0100 Subject: [PATCH] Fix double free --- src/2048.c | 6 +++--- src/gui.c | 25 ++++++++++++++++--------- src/main.c | 42 ++++++++++++++++++++++++++++++------------ 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/2048.c b/src/2048.c index f5bfa72..dc1bd49 100644 --- a/src/2048.c +++ b/src/2048.c @@ -63,7 +63,7 @@ void game_move(Game *game, Direction direction) { for (int i = 0; i < game->field_size_y; i++) { uint16_t **field = game->field; // move everything on one side - for (int j = 0; j < game->field_size_x - 1; j++) { + for (int j = 0; j < game->field_size_y - 1; j++) { // a, b is the same as i and j. It is just flipped when // it does not go horizontally so I can access stuff vertically int a = i; @@ -104,7 +104,7 @@ void game_move(Game *game, Direction direction) { } // combine same numbers - for (int j = 0; j < game->field_size_x - 1; j++) { + for (int j = 0; j < game->field_size_y - 1; j++) { int a = i; int b = j; if (direction == Up || direction == Down) { @@ -151,7 +151,7 @@ void game_move(Game *game, Direction direction) { } // move everything again - for (int j = 0; j < game->field_size_x - 1; j++) { + for (int j = 0; j < game->field_size_y - 1; j++) { int a = i; int b = j; if (direction == Up || direction == Down) { diff --git a/src/gui.c b/src/gui.c index 9038ece..e340da7 100644 --- a/src/gui.c +++ b/src/gui.c @@ -12,7 +12,6 @@ const SCREEN_HEIGHT = 920; SDL_Window *window = NULL; // SDL_Surface* screenSurface = NULL; SDL_Renderer *window_renderer = NULL; -SDL_Texture *tile_texture = NULL; SDL_Texture *tile_textures[13]; SDL_Rect tile_size; SDL_Texture *game_over_texture = NULL; @@ -51,9 +50,8 @@ void gui_init() { // SDL_UpdateWindowSurface(window); // load up textures - tile_texture = texture_from_png("res/tile.png"); - tile_textures[0] = tile_texture; - // there's no texture for 1 because it's unnecesary - sad pikachu face + tile_textures[0] = texture_from_png("res/tile.png"); + tile_textures[1] = texture_from_png("res/1.png"); tile_textures[2] = texture_from_png("res/2.png"); tile_textures[3] = texture_from_png("res/4.png"); tile_textures[4] = texture_from_png("res/8.png"); @@ -67,7 +65,7 @@ void gui_init() { tile_textures[12] = texture_from_png("res/2048.png"); tile_size.x = 0; tile_size.y = 0; - SDL_QueryTexture(tile_texture, NULL, NULL, &tile_size.w, &tile_size.h); + SDL_QueryTexture(tile_textures[0], NULL, NULL, &tile_size.w, &tile_size.h); game_over_texture = texture_from_png("res/game_over.png"); you_won_texture = texture_from_png("res/you_won.png"); @@ -78,12 +76,17 @@ void gui_init() { void gui_destroy() { // Destroy window SDL_DestroyWindow(window); + window = NULL; SDL_DestroyRenderer(window_renderer); + window_renderer = NULL; for (int i = 0; i < 13; i++) { SDL_DestroyTexture(tile_textures[i]); + tile_textures[i] = NULL; } SDL_DestroyTexture(game_over_texture); + game_over_texture = NULL; SDL_DestroyTexture(you_won_texture); + you_won_texture = NULL; TTF_Quit(); // Quit SDL subsystems @@ -131,16 +134,20 @@ void gui_loop(Game *game) { case SDL_QUIT: quit = 1; break; + case SDL_WINDOWEVENT_SHOWN: + case SDL_WINDOWEVENT_EXPOSED: + needs_redraw = true; + break; } } if (needs_redraw) { SDL_RenderClear(window_renderer); SDL_Rect tile_rect = tile_size; - for (int i = 0; i < game->field_size_x; i++) { - tile_rect.x = i * 175 + 50; - for (int j = 0; j < game->field_size_y; j++) { - tile_rect.y = j * 175 + 150; + for (int i = 0; i < game->field_size_y; i++) { + tile_rect.x = i * (700 / game->field_size_y) + 50; + for (int j = 0; j < game->field_size_x; j++) { + tile_rect.y = j * (700 / game->field_size_x) + 150; // get texture index from value for (int k = 0; k < 13; k++) { diff --git a/src/main.c b/src/main.c index 61ab949..78a8acd 100644 --- a/src/main.c +++ b/src/main.c @@ -3,18 +3,36 @@ #include "tui.h" int main(int argc, char *argv[]) { + bool tui = false; + int size_x = 4; + int size_y = 4; + for (int i = 0; i < argc; i++) { + if (!strcmp(argv[i], "--tui")) { + tui = true; + } + if (!strcmp(argv[i], "--x")) { + if (argc >= i) { + size_x = atoi(argv[i + 1]); + } + } + if (!strcmp(argv[i], "--y")) { + if (argc >= i) { + size_y = atoi(argv[i + 1]); + } + } + } Game game; - game = game_init(4, 4); - /*tui_init(); - - tui_loop(&game); - - tui_destroy(); - */ - - gui_init(); - gui_loop(&game); - gui_destroy(); - game_destroy(game); + game = game_init(size_x, size_y); + if (tui) { + tui_init(); + tui_loop(&game); + tui_destroy(); + } + else { + gui_init(); + gui_loop(&game); + gui_destroy(); + game_destroy(game); + } return 0; }