diff --git a/Makefile b/Makefile index 39d0358..06197ef 100644 --- a/Makefile +++ b/Makefile @@ -81,6 +81,7 @@ RM_OBJS = rm -rf $(D_OBJS) # flags CFLAGS = -Wall -Wextra -Werror $(INCLUDES) CFLAGS += -g3 +#CFLAGS += -fsanitize=address LFLAGS = -L$(D_LFT) -lft LFLAGS += -L$(D_LMLX) -lm -lmlx -lXext -lX11 diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index 0cfdbc2..83b60f5 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -6,7 +6,7 @@ /* By: pblagoje +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/04 13:31:38 by pblagoje #+# #+# */ -/* Updated: 2022/05/04 13:34:07 by pblagoje ### ########.fr */ +/* Updated: 2022/05/04 21:36:13 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -169,7 +169,7 @@ typedef struct s_game t_rcast rcast; t_txt txt; t_map map; - + int fd; int k_hook[MAX_NB_KEY]; } t_game; diff --git a/maps/map_invalid_03.cub b/maps/map_invalid_03.cub index 11a1c5e..b5e0d8f 100644 --- a/maps/map_invalid_03.cub +++ b/maps/map_invalid_03.cub @@ -1,10 +1,8 @@ -R 801 597 -NO ./texture/grey.xpm -SO ./texture/brick.xpm -WE ./texture/wood.xpm -EA ./texture/blue.xpm +NO ./textures/grey.xpm +SO ./textures/brick.xpm +WE ./textures/dickbutt.xpm +EA ./textures/blue.xpm -S ./texture/dickbutt.xpm F 66,66,66 C 33,33,33 @@ -13,7 +11,7 @@ C 33,33,33 1111111111 1111111111 100000000111111000001 100000000111111000001 - 100000000111111000001111 + 1000E0000111111000001111 100000000111111000001001 1111111110000000011111100000111111 1000011000100000000111111111111 diff --git a/srcs/cube3d.c b/srcs/cube3d.c index 6e0df90..bafba9e 100644 --- a/srcs/cube3d.c +++ b/srcs/cube3d.c @@ -6,7 +6,7 @@ /* By: pblagoje +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/04 13:42:00 by pblagoje #+# #+# */ -/* Updated: 2022/05/04 13:43:02 by pblagoje ### ########.fr */ +/* Updated: 2022/05/04 21:58:13 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ void destroy_mlx(void *param) t_game *game; game = param; + if (game->fd != -1) + close(game->fd); if (game->mlx_ptr == NULL) return ; if (game->img.ptr) diff --git a/srcs/gnl/get_next_line.c b/srcs/gnl/get_next_line.c index 6ed2dcc..e657652 100644 --- a/srcs/gnl/get_next_line.c +++ b/srcs/gnl/get_next_line.c @@ -6,7 +6,7 @@ /* By: pblagoje +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/01/27 12:42:20 by pblagoje #+# #+# */ -/* Updated: 2022/04/18 17:01:06 by pblagoje ### ########.fr */ +/* Updated: 2022/05/04 21:23:28 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/gnl/get_next_line_utils.c b/srcs/gnl/get_next_line_utils.c index 918d555..981c2b9 100644 --- a/srcs/gnl/get_next_line_utils.c +++ b/srcs/gnl/get_next_line_utils.c @@ -6,7 +6,7 @@ /* By: pblagoje +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/02/04 10:35:41 by pblagoje #+# #+# */ -/* Updated: 2022/04/18 17:00:41 by pblagoje ### ########.fr */ +/* Updated: 2022/05/04 21:07:31 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index 4ae4b82..73ab37d 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -6,7 +6,7 @@ /* By: pblagoje +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/04 13:57:22 by pblagoje #+# #+# */ -/* Updated: 2022/05/04 13:57:28 by pblagoje ### ########.fr */ +/* Updated: 2022/05/04 21:57:53 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,5 +50,6 @@ t_game *init_struct(void) init_map(&(game->map)); init_txt_null(&(game->txt)); init_null_mlx_ptr(game); + game->fd = -1; return (game); } diff --git a/srcs/mem/memorybook.c b/srcs/mem/memorybook.c index b59c53e..4fe31a8 100644 --- a/srcs/mem/memorybook.c +++ b/srcs/mem/memorybook.c @@ -6,7 +6,7 @@ /* By: pblagoje +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/04 13:56:30 by pblagoje #+# #+# */ -/* Updated: 2022/05/04 13:58:58 by pblagoje ### ########.fr */ +/* Updated: 2022/05/04 19:47:54 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,9 +30,9 @@ void *mb_alloc(size_t size) lst = mb_get_lst(); tmp = ft_memalloc(size); if (!tmp) - mb_exit(B_RED"failed create new allocation"RESET"\n", EXIT_FAILURE); + mb_exit(B_RED"mb: failed create new allocation"RESET"\n", EXIT_FAILURE); if (!ft_lstpush_back(lst, ft_lstcreate(tmp))) - mb_exit(B_RED"failed add new element to list"RESET"\n", EXIT_FAILURE); + mb_exit(B_RED"mb: failed add new elmnt to list"RESET"\n", EXIT_FAILURE); return (tmp); } @@ -42,7 +42,7 @@ void mb_add(void *addr) lst = mb_get_lst(); if (!ft_lstpush_back(lst, ft_lstcreate(addr))) - mb_exit(B_RED"failed add new element to list"RESET"\n", EXIT_FAILURE); + mb_exit(B_RED"mb: failed add new elmnt to list"RESET"\n", EXIT_FAILURE); } void mb_free(void *addr) @@ -53,7 +53,7 @@ void mb_free(void *addr) lst = mb_get_lst(); tmp = ft_lstfind((*lst), addr, mb_comp_addr); if (!tmp) - ft_putstr_fd(B_RED"you try to free not allocated address"RESET"\n", 2); + ft_putstr_fd(B_RED"mb: try to free not allocated address"RESET"\n", 2); ft_lsterase(tmp, free); } diff --git a/srcs/parsing/init_parsing.c b/srcs/parsing/init_parsing.c index 35c68d2..c06859d 100644 --- a/srcs/parsing/init_parsing.c +++ b/srcs/parsing/init_parsing.c @@ -6,13 +6,13 @@ /* By: pblagoje +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/04/15 16:03:50 by pblagoje #+# #+# */ -/* Updated: 2022/05/04 15:32:54 by pblagoje ### ########.fr */ +/* Updated: 2022/05/04 21:57:24 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "cube3d.h" -int size_map(t_map *map, int fd) +static int size_map(t_map *map, int fd) { char *line; @@ -41,7 +41,7 @@ int size_map(t_map *map, int fd) return (EXIT_SUCCESS); } -int find_map(t_map *map, char *file) +static int find_map(t_map *map, char *file) { int fd; int count; @@ -61,11 +61,13 @@ int find_map(t_map *map, char *file) } if (map->tmp_str[0] != '\n' && map->tmp_str[0] != '\0') count++; + if (count < TOTAL_ELEMENTS + 1) + mb_free(map->tmp_str); } return (fd); } -void fill_row(char *row, char *line, int width) +static void fill_row(char *row, char *line, int width) { int i; @@ -83,53 +85,48 @@ void fill_row(char *row, char *line, int width) row[i] = '\0'; } -int fill_map(t_map *map, int fd) +static void fill_map(t_map *map, int fd) { - int i; + int i; + int ret; + ret = 1; map->content = (char **)mb_alloc((map->size_y + 1) * sizeof(char *)); - if (!map->content) - mb_exit("Error\nCouldn't allocate memory for map.\n", EXIT_FAILURE); i = -1; while (++i < map->size_y) { if (i > 0) { - get_next_line(fd, &map->tmp_str); + ret = get_next_line(fd, &map->tmp_str); mb_add(map->tmp_str); } map->content[i] = (char *)mb_alloc((map->size_x + 1) * sizeof(char)); - if (!map->content[i]) - mb_exit("Error\nCouldn't allocate memory for row.\n", EXIT_FAILURE); fill_row(map->content[i], map->tmp_str, map->size_x); mb_free(map->tmp_str); - map->tmp_str = NULL; } map->content[i] = NULL; - return (EXIT_SUCCESS); + while (ret) + { + ret = get_next_line(fd, &map->tmp_str); + if (ret) + mb_add(map->tmp_str); + } } int init_parsing(t_game *game, char *file) { - int fd; - - fd = check_elements(game, file); - if (fd == -1) + game->fd = check_elements(game, file); + if (game->fd == -1) mb_exit("Error\nInvalid .cub file.\n", EXIT_FAILURE); - if (size_map(&(game->map), fd)) - { - close(fd); + if (size_map(&(game->map), game->fd)) mb_exit("Error\nInvalid map format.\n", EXIT_FAILURE); - } - close(fd); - fd = find_map(&(game->map), file); - if (fd == -1) + close(game->fd); + game->fd = -1; + game->fd = find_map(&(game->map), file); + if (game->fd == -1) mb_exit("Error\nCouldn't locate the map.\n", EXIT_FAILURE); - if (fill_map(&(game->map), fd) == EXIT_FAILURE) - { - close(fd); - mb_exit("Error\nCouldn't save the map.\n", EXIT_FAILURE); - } - close(fd); + fill_map(&(game->map), game->fd); + close(game->fd); + game->fd = -1; return (EXIT_SUCCESS); }