From a2ed84c5e1081b12d2891a5b924cb7eccfee07d4 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Mon, 2 May 2022 23:36:27 +0200 Subject: [PATCH] add null initialisation of mlx pointers and move mb init closer to begining --- srcs/cube3d.c | 22 +++++++++++++++------- srcs/init/init_struct.c | 17 +++++++++++++++-- srcs/init/init_textures.c | 29 +++++------------------------ 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/srcs/cube3d.c b/srcs/cube3d.c index ca058fd..427b991 100644 --- a/srcs/cube3d.c +++ b/srcs/cube3d.c @@ -5,8 +5,20 @@ void destroy_mlx(void *param) t_game *game; game = param; - mlx_destroy_image(game->mlx_ptr, game->img.ptr); - mlx_destroy_window(game->mlx_ptr, game->win.ptr); + if (game->mlx_ptr == NULL) + return ; + if (game->img.ptr) + mlx_destroy_image(game->mlx_ptr, game->img.ptr); + if (game->txt.img_n.ptr) + mlx_destroy_image(game->mlx_ptr, game->txt.img_n.ptr); + if (game->txt.img_s.ptr) + mlx_destroy_image(game->mlx_ptr, game->txt.img_s.ptr); + if (game->txt.img_e.ptr) + mlx_destroy_image(game->mlx_ptr, game->txt.img_e.ptr); + if (game->txt.img_w.ptr) + mlx_destroy_image(game->mlx_ptr, game->txt.img_w.ptr); + if (game->win.ptr) + mlx_destroy_window(game->mlx_ptr, game->win.ptr); mlx_destroy_display(game->mlx_ptr); } @@ -24,19 +36,15 @@ int main(int ac, char **av) write(2, "Error\nPlease use a valid .cub file as single argument.\n", 53)) return (EXIT_FAILURE); game = init_struct(); + mb_init(destroy_mlx, game); if (init_parsing(game, av[1]) && write(2, "The .cub file is invalid.\n", 26)) return (EXIT_FAILURE); if (check_map(&(game->map)) && write(2, "The map is invalid.\n", 20)) return (EXIT_FAILURE); - - // init game variables init_game(game); - mb_init(destroy_mlx, game); - // draw game a first time before it start draw(game); - // receive a keypress event mlx_hook(game->win.ptr, 2, 1L << 0, keypress, game); // receive a keyprelease event diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index 9e172d7..b5846a2 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -1,5 +1,16 @@ #include "cube3d.h" +static void init_null_mlx_ptr(t_game *game) +{ + game->img.ptr = NULL; + game->txt.img_n.ptr = NULL; + game->txt.img_s.ptr = NULL; + game->txt.img_e.ptr = NULL; + game->txt.img_w.ptr = NULL; + game->win.ptr = NULL; + game->mlx_ptr = NULL; +} + static void init_map(t_map *map) { map->content = NULL; @@ -11,7 +22,7 @@ static void init_map(t_map *map) map->plr_y = 0; } -static void init_txt(t_txt *txt) +static void init_txt_null(t_txt *txt) { txt->txt_north = NULL; txt->txt_south = NULL; @@ -27,6 +38,8 @@ t_game *init_struct(void) // map init_map(&(game->map)); // init textures and floor/ceiling colors - init_txt(&(game->txt)); + init_txt_null(&(game->txt)); + // put all mlx allocated pointer to null + init_null_mlx_ptr(game); return (game); } diff --git a/srcs/init/init_textures.c b/srcs/init/init_textures.c index 15e226e..2e17423 100644 --- a/srcs/init/init_textures.c +++ b/srcs/init/init_textures.c @@ -1,34 +1,15 @@ #include "cube3d.h" -static int init_txtr_img(t_img *img, void *mlx_ptr, char *path) +static void init_txtr_img(t_img *img, void *mlx_ptr, char *path) { img->ptr = mlx_xpm_file_to_image(mlx_ptr, path, &img->width, &img->height); - if (img->ptr == NULL) - return (1); img->data = mlx_get_data_addr(img->ptr, &img->bpp, &img->szl, &img->ndn); - return (0); } void init_txtr(t_txt *txt, void *mlx_ptr) { - if (init_txtr_img(&txt->img_n, mlx_ptr, txt->txt_north) == 1) - mb_exit("failed to read image for texture nord", EXIT_FAILURE); - if (init_txtr_img(&txt->img_s, mlx_ptr, txt->txt_south) == 1) - { - mlx_destroy_image(mlx_ptr, txt->img_n.ptr); - mb_exit("failed to read image for texture south", EXIT_FAILURE); - } - if (init_txtr_img(&txt->img_e, mlx_ptr, txt->txt_east) == 1) - { - mlx_destroy_image(mlx_ptr, txt->img_n.ptr); - mlx_destroy_image(mlx_ptr, txt->img_s.ptr); - mb_exit("failed to read image for texture east", EXIT_FAILURE); - } - if (init_txtr_img(&txt->img_w, mlx_ptr, txt->txt_west) == 1) - { - mlx_destroy_image(mlx_ptr, txt->img_n.ptr); - mlx_destroy_image(mlx_ptr, txt->img_s.ptr); - mlx_destroy_image(mlx_ptr, txt->img_e.ptr); - mb_exit("failed to read image for texture west", EXIT_FAILURE); - } + init_txtr_img(&txt->img_n, mlx_ptr, txt->txt_north); + init_txtr_img(&txt->img_s, mlx_ptr, txt->txt_south); + init_txtr_img(&txt->img_e, mlx_ptr, txt->txt_east); + init_txtr_img(&txt->img_w, mlx_ptr, txt->txt_west); }