add null initialisation of mlx pointers and move mb init closer to begining

This commit is contained in:
hugogogo
2022-05-02 23:36:27 +02:00
parent 75692c09a6
commit a2ed84c5e1
3 changed files with 35 additions and 33 deletions

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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);
}