From f7d913f4789c09d6132f6a0f881afb68f11d9d1a Mon Sep 17 00:00:00 2001 From: vSKAH Date: Sat, 14 Oct 2023 16:03:34 +0200 Subject: [PATCH] fix:(vSKAH): Fix norm errors add:(vSKAH): "esc" key now exit game add:(vSKAH): Nb of movements are now print in shell add:(vSKAH): ft_printf is now in libft --- errors_utils.c | 25 +++--- frees.c | 2 +- libft/Makefile | 126 ++++++++++++++++-------------- libft/ft_base.c | 32 ++++++++ libft/ft_printf/ft_args_handler.c | 37 +++++++++ libft/ft_printf/ft_get_address.c | 27 +++++++ libft/ft_printf/ft_get_base16.c | 21 +++++ libft/ft_printf/ft_get_char.c | 21 +++++ libft/ft_printf/ft_get_integer.c | 32 ++++++++ libft/ft_printf/ft_get_string.c | 23 ++++++ libft/ft_printf/ft_printf.c | 41 ++++++++++ libft/ft_putchar_fd.c | 6 +- libft/ft_putnbr_fd.c | 43 +++++++--- libft/ft_putstr_fd.c | 13 +-- libft/libft.h | 22 +++++- main.c | 7 +- maps_utils.c | 57 +++++++------- player.c | 4 +- so_long.h | 4 +- 19 files changed, 414 insertions(+), 129 deletions(-) create mode 100644 libft/ft_base.c create mode 100644 libft/ft_printf/ft_args_handler.c create mode 100644 libft/ft_printf/ft_get_address.c create mode 100644 libft/ft_printf/ft_get_base16.c create mode 100644 libft/ft_printf/ft_get_char.c create mode 100644 libft/ft_printf/ft_get_integer.c create mode 100644 libft/ft_printf/ft_get_string.c create mode 100644 libft/ft_printf/ft_printf.c diff --git a/errors_utils.c b/errors_utils.c index ac41c78..5cc86aa 100644 --- a/errors_utils.c +++ b/errors_utils.c @@ -18,10 +18,10 @@ int handle_file_error(char *argv[]) int fd; if (!argv[1]) - return (ft_putstr_fd("Error\n -> Invalid file path.\n", 1), -2); + return (ft_printf("Error\n -> Invalid file path.\n"), -2); fd = open(argv[1], O_RDONLY); if (fd == -1) - return (ft_putstr_fd("Error\n -> Invalid file path.\n", 1), -2); + return (ft_printf("Error\n -> Invalid file path.\n"), -2); return (fd); } @@ -30,34 +30,33 @@ int handle_map_error(char *path, t_game game) int elements_code; t_world cloned; - if (has_illegal_character(game.world)){ + if (has_illegal_character(game.world)) + { free_map(&game.world); free_collectibles(game.world.player.collectibles); - return (ft_putstr_fd("Error\n -> Invalid Map / illegal character", 1), -1); - + return (ft_printf("Error\n -> Invalid Map / illegal character"), -1); } if (!is_valid_shape(game.world)) { free_map(&game.world); free_collectibles(game.world.player.collectibles); - return (ft_putstr_fd("Error\n -> Invalid shape / not closed", 1), -1); + return (ft_printf("Error\n -> Invalid shape / not closed"), -1); } elements_code = valid_elements(game.world); if (elements_code < 0) { - ft_putstr_fd("Error\n -> Please, check your map !", 1); - ft_putstr_fd("\nThe map must contains 1 E, 1 P, minimum 1 C", 1); + ft_printf("Error\n -> Map must contains 1 E, 1 P, minimum 1 C"); return (-1); } load_map(open(path, O_RDONLY), path, &cloned); - is_solvable(cloned.map, game.world.player.location.x, game.world.player.location.y, game.world.length_y); + is_solvable(cloned.map, game.world.player.location.x, \ + game.world.player.location.y, game.world.length_y); if (count_element(cloned, 'C') > 0 || count_element(cloned, 'E') > 0) { - ft_putstr_fd("Error\n -> Sorry, your map is not resolvable", 1); - free_map(&game.world); + ft_printf("Error\n -> Sorry, your map is not resolvable"); free_collectibles(game.world.player.collectibles); - return (free_map(&cloned), -2); + return (free_map(&game.world), free_map(&cloned), -2); } free_map(&cloned); return (0); -} \ No newline at end of file +} diff --git a/frees.c b/frees.c index 9bf4b03..91f60a2 100644 --- a/frees.c +++ b/frees.c @@ -58,7 +58,7 @@ int free_unavailable_texture(t_game game) { if (has_unavailable_texture(game.textures)) { - ft_putstr_fd("Error\n -> Textures can't be loaded.", 1); + ft_printf("Error\n -> Textures can't be loaded."); free_map(&game.world); free_textures(&game); free_collectibles(game.world.player.collectibles); diff --git a/libft/Makefile b/libft/Makefile index c0445c5..1928645 100755 --- a/libft/Makefile +++ b/libft/Makefile @@ -10,80 +10,88 @@ # # # **************************************************************************** # -NAME = libft.a -RM = rm -rf -CC = cc -FLAGS = -Wall -Wextra -Werror -g -OBJ_DIRECTORY = ./.obj/ -INCLUDE = libft.h +NAME = libft.a +RM = rm -rf +CC = gcc +FLAGS = -Wall -Wextra -Werror -g3 +OBJ_DIRECTORY = ./.obj/ +INCLUDE = libft.h -SRCS = \ - ft_bzero.c \ - ft_isalnum.c \ - ft_isalpha.c \ - ft_isascii.c \ - ft_isdigit.c \ - ft_isprint.c \ - ft_memset.c \ - ft_strlen.c \ - ft_strlcat.c \ - ft_tolower.c \ - ft_toupper.c \ - ft_strrchr.c \ - ft_strchr.c \ - ft_strncmp.c \ - ft_memchr.c \ - ft_memcmp.c \ - ft_strnstr.c \ - ft_atoi.c \ - ft_calloc.c \ - ft_strdup.c \ - ft_memcpy.c \ - ft_memmove.c \ - ft_strlcpy.c \ - ft_substr.c \ - ft_strjoin.c \ - ft_strtrim.c \ - ft_split.c \ - ft_itoa.c \ - ft_putchar_fd.c\ - ft_putstr_fd.c \ - ft_putendl_fd.c\ - ft_striteri.c\ - ft_strmapi.c\ - ft_putnbr_fd.c \ - ft_lstnew.c \ - ft_lstadd_front.c\ - ft_lstsize.c \ - ft_lstlast.c \ - ft_lstadd_back.c \ - ft_lstdelone.c \ - ft_lstclear.c \ - ft_lstiter.c \ - get_next_line.c \ - get_next_line_utils.c \ +SRCS = \ + ft_bzero.c \ + ft_isalnum.c \ + ft_isalpha.c \ + ft_isascii.c \ + ft_isdigit.c \ + ft_isprint.c \ + ft_memset.c \ + ft_strlen.c \ + ft_strlcat.c \ + ft_tolower.c \ + ft_toupper.c \ + ft_strrchr.c \ + ft_strchr.c \ + ft_strncmp.c \ + ft_memchr.c \ + ft_memcmp.c \ + ft_strnstr.c \ + ft_atoi.c \ + ft_calloc.c \ + ft_strdup.c \ + ft_memcpy.c \ + ft_memmove.c \ + ft_strlcpy.c \ + ft_substr.c \ + ft_strjoin.c \ + ft_strtrim.c \ + ft_split.c \ + ft_itoa.c \ + ft_putchar_fd.c \ + ft_putstr_fd.c \ + ft_putendl_fd.c \ + ft_striteri.c \ + ft_strmapi.c \ + ft_putnbr_fd.c \ + ft_lstnew.c \ + ft_lstadd_front.c \ + ft_lstsize.c \ + ft_lstlast.c \ + ft_lstadd_back.c \ + ft_lstdelone.c \ + ft_lstclear.c \ + ft_lstiter.c \ + ft_base.c \ + get_next_line.c \ + get_next_line_utils.c \ + ft_printf/ft_printf.c \ + ft_printf/ft_args_handler.c \ + ft_printf/ft_get_address.c \ + ft_printf/ft_get_base16.c \ + ft_printf/ft_get_char.c \ + ft_printf/ft_get_integer.c \ + ft_printf/ft_get_string.c \ -all: $(NAME) +OBJS = $(addprefix $(OBJ_DIRECTORY), $(SRCS:.c=.o)) -so: $(all) +all: $(NAME) -OBJS = $(addprefix $(OBJ_DIRECTORY), $(SRCS:.c=.o)) +so: $(all) $(NAME): $(OBJ_DIRECTORY) $(OBJS) $(INCLUDE) ar -rcs $(NAME) $(OBJS) -$(OBJ_DIRECTORY)%.o:%.c $(INCLUDE) +$(OBJ_DIRECTORY)%.o: %.c $(INCLUDE) Makefile $(CC) $(FLAGS) -I. -c $< -o $@ $(OBJ_DIRECTORY): - mkdir -p $(OBJ_DIRECTORY) + mkdir -p $(OBJ_DIRECTORY) $(OBJ_DIRECTORY)ft_printf clean: $(RM) $(OBJ_DIRECTORY) -fclean: clean +fclean: clean $(RM) $(NAME) *.out *.gch *.o .so -re: fclean $(NAME) +re: fclean $(NAME) -.PHONY: fclean clean all re +.PHONY: fclean clean all re diff --git a/libft/ft_base.c b/libft/ft_base.c new file mode 100644 index 0000000..1863ad7 --- /dev/null +++ b/libft/ft_base.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jbadaire +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/14 15:22:00 by jbadaire #+# #+# */ +/* Updated: 2023/10/14 15:22:00 by jbadaire ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_base(unsigned long number, int base_number, char base_array[], int value) +{ + char character; + + value = 0; + if (number >= (unsigned long)base_number) + { + value += ft_base(number / base_number, base_number, base_array, value); + value += ft_base(number % base_number, base_number, base_array, value); + } + else + { + character = base_array[number]; + value += ft_putchar_fd(character, 1); + return (value); + } + return (value); +} diff --git a/libft/ft_printf/ft_args_handler.c b/libft/ft_printf/ft_args_handler.c new file mode 100644 index 0000000..a148f19 --- /dev/null +++ b/libft/ft_printf/ft_args_handler.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* args_handler.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jbadaire +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/06 18:39:32 by jbadaire #+# #+# */ +/* Updated: 2023/09/18 08:23:56 by jbadaire ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_args_handler(char c, va_list params) +{ + int value; + + value = 0; + if (c == 'c') + value = ft_get_char(params); + else if (c == 's') + value = ft_get_string(params); + else if (c == 'u') + value = ft_get_unsigned_integer(params); + else if (c == 'd' || c == 'i') + value = ft_get_integer(params); + else if (c == 'x') + value = ft_get_base16(params, "0123456789abcdef"); + else if (c == 'X') + value = ft_get_base16(params, "0123456789ABCDEF"); + else if (c == 'p') + value = ft_get_address(params, "0123456789abcdef"); + else if (c == '%') + value = ft_putchar_fd('%', 1); + return (value); +} diff --git a/libft/ft_printf/ft_get_address.c b/libft/ft_printf/ft_get_address.c new file mode 100644 index 0000000..3fbffa7 --- /dev/null +++ b/libft/ft_printf/ft_get_address.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_address.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jbadaire +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/07 18:21:53 by jbadaire #+# #+# */ +/* Updated: 2023/09/18 08:54:49 by jbadaire ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_get_address(va_list param, char *base_array) +{ + size_t number; + int value; + + number = va_arg(param, size_t); + value = 0; + if (number == 0) + return (ft_putstr_fd("(nil)", 1)); + value += ft_putstr_fd("0x", 1); + value += ft_base(number, 16, base_array, 0); + return (value); +} diff --git a/libft/ft_printf/ft_get_base16.c b/libft/ft_printf/ft_get_base16.c new file mode 100644 index 0000000..6812140 --- /dev/null +++ b/libft/ft_printf/ft_get_base16.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_base16.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jbadaire +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/07 18:10:35 by jbadaire #+# #+# */ +/* Updated: 2023/09/12 14:58:19 by jbadaire ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_get_base16(va_list param, char *base) +{ + unsigned int number; + + number = va_arg(param, unsigned int); + return (ft_base(number, 16, base, 0)); +} diff --git a/libft/ft_printf/ft_get_char.c b/libft/ft_printf/ft_get_char.c new file mode 100644 index 0000000..ee3e3c0 --- /dev/null +++ b/libft/ft_printf/ft_get_char.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_char.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jbadaire +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/06 18:47:16 by jbadaire #+# #+# */ +/* Updated: 2023/09/12 13:56:35 by jbadaire ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_get_char(va_list param) +{ + char c; + + c = va_arg(param, int); + return (ft_putchar_fd(c, 1)); +} diff --git a/libft/ft_printf/ft_get_integer.c b/libft/ft_printf/ft_get_integer.c new file mode 100644 index 0000000..e3fa749 --- /dev/null +++ b/libft/ft_printf/ft_get_integer.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_integer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jbadaire +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/07 18:18:42 by jbadaire #+# #+# */ +/* Updated: 2023/09/12 13:57:52 by jbadaire ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_get_integer(va_list param) +{ + int value; + + value = va_arg(param, int); + return (ft_putnbr_fd(value, 1)); +} + +int ft_get_unsigned_integer(va_list param) +{ + unsigned int value; + int return_value; + + value = va_arg(param, unsigned int); + return_value = 0; + return_value += ft_put_unsigned_nbr_fd(value, 1); + return (return_value); +} diff --git a/libft/ft_printf/ft_get_string.c b/libft/ft_printf/ft_get_string.c new file mode 100644 index 0000000..227ff91 --- /dev/null +++ b/libft/ft_printf/ft_get_string.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_string.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jbadaire +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/06 18:47:16 by jbadaire #+# #+# */ +/* Updated: 2023/09/12 13:56:35 by jbadaire ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_get_string(va_list param) +{ + char *string; + + string = va_arg(param, char *); + if (string == NULL) + return (ft_putstr_fd("(null)", 1)); + return (ft_putstr_fd(string, 1)); +} diff --git a/libft/ft_printf/ft_printf.c b/libft/ft_printf/ft_printf.c new file mode 100644 index 0000000..d02839a --- /dev/null +++ b/libft/ft_printf/ft_printf.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jbadaire +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/06 18:43:52 by jbadaire #+# #+# */ +/* Updated: 2023/09/12 14:57:14 by jbadaire ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_printf(const char *str, ...) +{ + va_list params; + int index; + int return_value; + int tmp_value; + + index = 0; + return_value = 0; + va_start(params, str); + while (str[index]) + { + if (str[index] == '%') + tmp_value = ft_args_handler(str[++index], params); + else + tmp_value = ft_putchar_fd(str[index], 1); + if (tmp_value == -1) + { + va_end(params); + return (0); + } + return_value += tmp_value; + index++; + } + va_end(params); + return (return_value); +} diff --git a/libft/ft_putchar_fd.c b/libft/ft_putchar_fd.c index 6377159..46cc549 100644 --- a/libft/ft_putchar_fd.c +++ b/libft/ft_putchar_fd.c @@ -10,9 +10,9 @@ /* */ /* ************************************************************************** */ -#include +#include "libft.h" -void ft_putchar_fd(char c, int fd) +int ft_putchar_fd(char c, int fd) { - write(fd, &c, 1); + return ((int)write(fd, &c, 1)); } diff --git a/libft/ft_putnbr_fd.c b/libft/ft_putnbr_fd.c index a252fff..33c5425 100644 --- a/libft/ft_putnbr_fd.c +++ b/libft/ft_putnbr_fd.c @@ -3,35 +3,60 @@ /* ::: :::::::: */ /* ft_putnbr_fd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jbadaire +#+ +:+ +#+ */ +/* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2022/11/24 14:30:42 by jbadaire #+# #+# */ -/* Updated: 2022/11/24 17:05:37 by jbadaire ### ########.fr */ +/* Created: 2023/10/14 15:18:30 by jbadaire #+# #+# */ +/* Updated: 2023/10/14 15:18:30 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -#include "unistd.h" -void ft_putnbr_fd(int n, int fd) +int ft_putnbr_fd(int n, int fd) { unsigned int nb; char ch; + int value; + value = 0; nb = (unsigned int)n; if (n < 0) { nb = nb * -1; - write (fd, "-", 1); + value += write (fd, "-", 1); } if (nb <= 9) { ch = nb + '0'; - write(fd, &ch, 1); + value += write(fd, &ch, 1); } if (nb > 9) { - ft_putnbr_fd(nb / 10, fd); - ft_putnbr_fd(nb % 10, fd); + value += ft_putnbr_fd(nb / 10, fd); + value += ft_putnbr_fd(nb % 10, fd); } + return (value); +} + +int ft_put_unsigned_nbr_fd(unsigned int n, int fd) +{ + unsigned int nb; + char ch; + int value; + + nb = n; + ch = 0; + value = 0; + if (nb <= 9) + { + ch = nb + '0'; + value = write(fd, &ch, 1); + } + if (nb > 9) + { + value = 0; + value += ft_put_unsigned_nbr_fd(nb / 10, fd); + value += ft_put_unsigned_nbr_fd(nb % 10, fd); + } + return (value); } diff --git a/libft/ft_putstr_fd.c b/libft/ft_putstr_fd.c index fa218c1..1677313 100644 --- a/libft/ft_putstr_fd.c +++ b/libft/ft_putstr_fd.c @@ -3,25 +3,26 @@ /* ::: :::::::: */ /* ft_putstr_fd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jbadaire +#+ +:+ +#+ */ +/* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2022/11/24 14:30:32 by jbadaire #+# #+# */ -/* Updated: 2022/11/24 14:33:24 by jbadaire ### ########.fr */ +/* Created: 2023/10/14 15:22:26 by jbadaire #+# #+# */ +/* Updated: 2023/10/14 15:22:26 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_putstr_fd(char *s, int fd) +int ft_putstr_fd(char *s, int fd) { int index; if (!s) - return ; + return (0); index = 0; - while (s[index] != '\0') + while (s[index] && s[index] != '\0') { ft_putchar_fd(s[index], fd); index++; } + return (index); } diff --git a/libft/libft.h b/libft/libft.h index 0e09188..68aabab 100644 --- a/libft/libft.h +++ b/libft/libft.h @@ -15,7 +15,8 @@ # include # include "stdlib.h" -# include "unistd.h" +# include +# include # ifndef BUFFER_SIZE # define BUFFER_SIZE 50 @@ -31,6 +32,11 @@ int ft_tolower(int c); int ft_strncmp(const char *s1, const char *s2, size_t n); int ft_atoi(const char *nptr); int ft_memcmp(const void *s1, const void *s2, size_t n); +int ft_base(unsigned long n, int b_n, char array[], int v); +int ft_putchar_fd(char c, int fd); +int ft_putnbr_fd(int n, int fd); +int ft_put_unsigned_nbr_fd(unsigned int n, int fd); +int ft_putstr_fd(char *s, int fd); size_t ft_strlen(const char *s); size_t ft_strlcat(char *dst, const char *src, size_t size); @@ -41,10 +47,7 @@ void *ft_memmove(void *dest, const void *src, size_t n); void ft_bzero(void *s, size_t n); void *ft_memcpy(void *dest, const void *src, size_t n); void *ft_calloc(size_t count, size_t size); -void ft_putchar_fd(char c, int fd); -void ft_putstr_fd(char *s, int fd); void ft_putendl_fd(char *s, int fd); -void ft_putnbr_fd(int n, int fd); void *ft_memchr(const void *s, int c, size_t n); void ft_striteri(char *s, void (*f)(unsigned int, char*)); @@ -90,4 +93,15 @@ void ft_fill_of_zero(char *buffer, int index); char *ft_str_join(char *r_line, char *buf, int ch_read, int l_line); char *ft_growth_line(char *r_line, char *buf, int ch_readed); char *get_next_line(int file_descriptor); + +/* PRINTF PART */ +int ft_printf(const char *str, ...); +int ft_args_handler(char c, va_list params); +int ft_get_char(va_list param); +int ft_get_string(va_list param); +int ft_get_integer(va_list param); +int ft_get_unsigned_integer(va_list param); +int ft_get_address(va_list param, char *base_array); +int ft_get_base16(va_list param, char *base); + #endif diff --git a/main.c b/main.c index f467ce6..2459c7f 100644 --- a/main.c +++ b/main.c @@ -26,15 +26,16 @@ int main(int argc, char *argv[]) game.world.player = init_player(find_element(game.world, 'P'), \ load_collectibles(&game.world)); value = handle_map_error(argv[1], game); - if(value < 0) - return(0); + if (value < 0) + return (0); game.mlx = mlx_init(); if (!game.mlx) return (0); game.textures = load_textures(game.mlx); if (free_unavailable_texture(game)) return (0); - game.window = mlx_new_window(game.mlx, 128 * ft_strlen(game.world.map[0]), 128 * game.world.length_y, "Xe'Burger"); + game.window = mlx_new_window(game.mlx, 128 * ft_strlen(game.world.map[0]), \ + 128 * game.world.length_y, "Xe'Burger"); draws(game); mlx_hook(game.window, 2, (1L << 0), on_player_move, &game); mlx_loop(game.mlx); diff --git a/maps_utils.c b/maps_utils.c index 961c23c..1c181d8 100644 --- a/maps_utils.c +++ b/maps_utils.c @@ -6,17 +6,17 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/02 18:09:47 by jbadaire #+# #+# */ -/* Updated: 2023/10/12 20:13:08 by jbadaire ### ########.fr */ +/* Updated: 2023/10/14 16:01:26 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" #include -t_world *load_map(int fd, char *path, t_world *world) +t_world *load_map(int fd, char *path, t_world *world) { - int index; - int y; - char *s; + int index; + int y; + char *s; char *tmp; index = 0; @@ -34,46 +34,47 @@ t_world *load_map(int fd, char *path, t_world *world) world->map[index] = NULL; close(fd); fd = open(path, O_RDONLY); - while (y <= index) { + while (y <= index) + { s = get_next_line(fd); - if(s != NULL) { + if (s != NULL) + { world->map[y] = ft_strtrim(s, "\n"); free(s); } y++; - } return (world->length_y = index, world); } -int valid_elements(t_world world) +int valid_elements(t_world world) { - int players; - int exits; - int collectibles; + int players; + int exits; + int collectibles; players = count_element(world, 'P'); - if(players > 1) + if (players > 1) return (-1); - if(players < 1) + if (players < 1) return (-2); exits = count_element(world, 'E'); - if(exits > 1) + if (exits > 1) return (-3); - if(exits < 1) + if (exits < 1) return (-4); collectibles = count_element(world, 'C'); - if(collectibles < 1) + if (collectibles < 1) return (-5); return (1); } -void is_solvable(char **map, int x, int y, int length_y) +void is_solvable(char **map, int x, int y, int length_y) { + char character; - char character; - if(map[y] == NULL || y > length_y) - return; + if (map[y] == NULL || y > length_y) + return ; character = map[y][x]; if (character == 'O') return ; @@ -83,23 +84,23 @@ void is_solvable(char **map, int x, int y, int length_y) is_solvable(map, x - 1, y, length_y); is_solvable(map, x + 1, y, length_y); is_solvable(map, x, y + 1, length_y); - is_solvable(map,x, y - 1, length_y); + is_solvable(map, x, y - 1, length_y); } - if(character == 'E') + if (character == 'E') { map[y][x] = 'L'; is_solvable(map, x - 1, y, length_y); is_solvable(map, x + 1, y, length_y); is_solvable(map, x, y + 1, length_y); - is_solvable(map,x, y - 1, length_y); + is_solvable(map, x, y - 1, length_y); } } -t_boolean is_inside_world(int y, int x, t_world world) +t_boolean is_inside_world(int y, int x, t_world world) { - if(y < 0 || y > world.length_y) + if (y < 0 || y > world.length_y) return (_false); - if(x < 0 || x > (int) ft_strlen(world.map[0])) + if (x < 0 || x > (int) ft_strlen(world.map[0])) return (_false); return (_true); -} \ No newline at end of file +} diff --git a/player.c b/player.c index 8f9b896..011beb9 100644 --- a/player.c +++ b/player.c @@ -41,6 +41,8 @@ int on_player_move(int keycode, t_game *game) { t_collectible *collectible; + if (keycode == 65307) + mlx_loop_end(game->mlx); if (!can_move(keycode, *game)) return (-1); if (keycode != 119 && keycode != 115 && keycode != 97 && keycode != 100) @@ -60,7 +62,7 @@ int on_player_move(int keycode, t_game *game) collectible = get_collectible_at(game->world, game->world.player.location); if (collectible != NULL && !collectible->collected) update_col(collectible, game->world.player.location, _true); - ft_putstr_fd("INFOS: \nMovements ", 1); + ft_printf("-> INFOS: \n| Movements: %d\n", game->world.player.movements); return (0); } diff --git a/so_long.h b/so_long.h index 746cb1c..2965826 100644 --- a/so_long.h +++ b/so_long.h @@ -75,8 +75,8 @@ t_boolean is_inside_world(int y, int x, t_world world); int handle_launch_error(char *argv[], t_world *world); // ** ERRORS **// -int handle_file_error(char *argv[]); -int handle_map_error(char *path, t_game game); +int handle_file_error(char *argv[]); +int handle_map_error(char *path, t_game game); // ** MAP ** // t_world *load_map(int fd, char *path, t_world *world);