diff --git a/Makefile b/Makefile index ac51997..14eca9b 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: fgras-ca +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/01/14 16:54:48 by fgras-ca #+# #+# # -# Updated: 2024/01/30 12:40:15 by fgras-ca ### ########.fr # +# Updated: 2024/01/30 16:18:26 by fgras-ca ### ########.fr # # # # **************************************************************************** # @@ -59,6 +59,8 @@ SRC = main.c \ ./parsing/ft_map_check_utils.c \ ./parsing/ft_text_and_col_utils.c \ ./utils/ft_utils_mem.c \ + ./exit/ft_exit.c \ + ./exit/ft_kill_prog.c \ OBJ = $(SRC:.c=.o) diff --git a/cub3d.h b/cub3d.h index 3a926a4..1fcf0e5 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/14 16:56:52 by fgras-ca #+# #+# */ -/* Updated: 2024/01/30 15:14:01 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/30 15:47:04 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -156,6 +156,7 @@ typedef struct s_structure_main t_texture *t; char current_wall_type; t_sprite sprite; + int error; } t_structure_main; typedef struct s_res_params { diff --git a/draw/ft_load_textures.c b/draw/ft_load_textures.c index 746169f..131319e 100644 --- a/draw/ft_load_textures.c +++ b/draw/ft_load_textures.c @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/14 20:30:59 by fgras-ca #+# #+# */ -/* Updated: 2024/01/30 15:05:11 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/30 16:07:36 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,8 +38,3 @@ void load_wall_textures(t_structure_main *w) load_texture(w, w->t->east, (void **)&w->s_img.east_texture); load_texture(w, "textures/door_close.xpm", (void **)&w->s_img.door_texture); } - -void exit_error(t_structure_main *w) -{ - exit(1); -} diff --git a/exit/ft_exit.c b/exit/ft_exit.c new file mode 100644 index 0000000..4340833 --- /dev/null +++ b/exit/ft_exit.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_exit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/30 16:06:24 by fgras-ca #+# #+# */ +/* Updated: 2024/01/30 16:07:27 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../cub3d.h" + +static void free_texture_images(t_structure_main *w) +{ + int i; + + i = 0; + while (++i <= 10) + { + if (w->s_img.roomadslam[i]) + mlx_destroy_image(w->s_win.mlx, w->s_img.roomadslam[i]); + } +} + +static void free_textures(t_texture *t) +{ + if (t->north) + free(t->north); + if (t->south) + free(t->south); + if (t->west) + free(t->west); + if (t->east) + free(t->east); +} + +static void destroy_images(t_structure_main *w) +{ + if (w->s_win.mlx) + { + mlx_destroy_image(w->s_win.mlx, w->s_img.buffer); + mlx_destroy_image(w->s_win.mlx, w->s_img.img_player); + mlx_destroy_image(w->s_win.mlx, w->s_img.img_wall); + mlx_destroy_image(w->s_win.mlx, w->s_img.pedro_wall); + mlx_destroy_image(w->s_win.mlx, w->s_img.north_texture); + mlx_destroy_image(w->s_win.mlx, w->s_img.south_texture); + mlx_destroy_image(w->s_win.mlx, w->s_img.west_texture); + mlx_destroy_image(w->s_win.mlx, w->s_img.east_texture); + mlx_destroy_image(w->s_win.mlx, w->s_img.door_texture); + free_texture_images(w); + } +} + +void exit_error(t_structure_main *w) +{ + if (w->s_win.mlx) + { + mlx_destroy_window(w->s_win.mlx, w->s_win.win); + destroy_images(w); + mlx_destroy_display(w->s_win.mlx); + free(w->s_win.mlx); + } + if (w->s_map.map && (w->error != 1)) + free(w->s_map.map); + if (w->t) + { + free_textures(w->t); + free(w->t); + } + exit(1); +} diff --git a/exit/ft_kill_prog.c b/exit/ft_kill_prog.c new file mode 100644 index 0000000..ab8a2df --- /dev/null +++ b/exit/ft_kill_prog.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_kill_prog.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/30 16:12:26 by fgras-ca #+# #+# */ +/* Updated: 2024/01/30 16:17:45 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../cub3d.h" + +static void destroy_images_and_window(t_structure_main *w) +{ + int i; + + i = -1; + if (w->s_win.mlx) + { + mlx_destroy_window(w->s_win.mlx, w->s_win.win); + mlx_destroy_image(w->s_win.mlx, w->s_img.buffer); + mlx_destroy_image(w->s_win.mlx, w->s_img.img_player); + mlx_destroy_image(w->s_win.mlx, w->s_img.img_wall); + mlx_destroy_image(w->s_win.mlx, w->s_img.pedro_wall); + mlx_destroy_image(w->s_win.mlx, w->s_img.north_texture); + mlx_destroy_image(w->s_win.mlx, w->s_img.south_texture); + mlx_destroy_image(w->s_win.mlx, w->s_img.west_texture); + mlx_destroy_image(w->s_win.mlx, w->s_img.east_texture); + mlx_destroy_image(w->s_win.mlx, w->s_img.door_texture); + while (++i <= 10) + mlx_destroy_image(w->s_win.mlx, w->s_img.roomadslam[i]); + } +} + +static void cleanup_and_exit(t_structure_main *w) +{ + if (w->s_map.map) + { + free(w->s_map.map); + w->s_map.map = NULL; + } + if (w->t) + { + free(w->t->north); + free(w->t->south); + free(w->t->west); + free(w->t->east); + free(w->t); + } + if (w->s_win.mlx) + { + mlx_destroy_display(w->s_win.mlx); + free(w->s_win.mlx); + } + exit(0); +} + +int *kill_prog(t_structure_main *w) +{ + destroy_images_and_window(w); + cleanup_and_exit(w); + return (0); +} diff --git a/main.c b/main.c index 809d02c..feb5af1 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/15 18:45:52 by fgras-ca #+# #+# */ -/* Updated: 2024/01/30 14:59:40 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/30 15:47:44 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,8 @@ int setup_and_load_map(int argc, char **argv, if (!load_cub_file(argv[1], textures, &w->s_map)) { printf("Failed to load the map or map is not closed. Exiting...\n"); + w->error = 1; + exit_error(w); return (0); } if (w->s_map.map == NULL) diff --git a/move/ft_collision.c b/move/ft_collision.c index 9b600c3..036e702 100644 --- a/move/ft_collision.c +++ b/move/ft_collision.c @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 14:09:52 by fgras-ca #+# #+# */ -/* Updated: 2024/01/29 17:43:10 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/30 16:21:09 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ static int check_collision(t_structure_main *w, double future_x, map_x = (int)((future_x + coll_params->dx[i]) / w->s_map.map_s); map_y = (int)((future_y + coll_params->dy[i]) / w->s_map.map_s); map_position = w->s_map.map[map_y * w->s_map.map_x + map_x]; - if (map_position != '0' && map_position != '3') + if (map_position == '1' || map_position == '2') return (0); i++; } diff --git a/move/ft_key.c b/move/ft_key.c index 569edce..1e9e6a5 100644 --- a/move/ft_key.c +++ b/move/ft_key.c @@ -6,35 +6,12 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 14:17:56 by fgras-ca #+# #+# */ -/* Updated: 2024/01/29 17:53:45 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/30 16:15:56 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ #include "../cub3d.h" -int *kill_prog(t_structure_main *w) -{ - int i; - - mlx_destroy_window(w->s_win.mlx, w->s_win.win); - mlx_destroy_image(w->s_win.mlx, w->s_img.buffer); - mlx_destroy_image(w->s_win.mlx, w->s_img.img_player); - mlx_destroy_image(w->s_win.mlx, w->s_img.img_wall); - mlx_destroy_image(w->s_win.mlx, w->s_img.pedro_wall); - i = -1; - while (++i <= 10) - mlx_destroy_image(w->s_win.mlx, w->s_img.roomadslam[i]); - if (w->s_map.map) - { - free(w->s_map.map); - w->s_map.map = NULL; - } - mlx_destroy_display(w->s_win.mlx); - free(w->s_win.mlx); - exit(0); - return (0); -} - void adjust_player_angle(t_structure_main *w, int key) { double angle_adjustment;