diff --git a/MacroLibX b/MacroLibX index 5a09ebb..4d640ed 160000 --- a/MacroLibX +++ b/MacroLibX @@ -1 +1 @@ -Subproject commit 5a09ebb179f8f20afa8405a6d1e29845f0952c46 +Subproject commit 4d640ed70cd392f3e3311fe2779905876b7f1b6f diff --git a/Makefile b/Makefile index 5676f07..49bbe3e 100644 --- a/Makefile +++ b/Makefile @@ -18,9 +18,19 @@ INCLUDE = -I $(I_DIR) -I $(LIBFT_DIR)/$(I_DIR) -I $(MACRO_DIR)/$(I_DIR) SRCS = src/utils/mess_error.c \ src/utils/parse_utils.c \ + src/utils/parse_utils_two.c \ + src/utils/parse_utils_three.c \ + src/utils/utils.c \ + src/utils/clear_free.c \ + src/parsing/check_image.c \ src/parsing/check_map.c \ src/parsing/check_arg.c \ + src/parsing/check_id_text.c \ + src/parsing/check_rgb.c \ + src/parsing/color_utils.c \ + src/parsing/find_player.c \ src/parsing/principal_pars.c \ + src/parsing/start.c \ src/cub3d.c OBJS = $(addprefix $(OBJSDIR), $(SRCS:.c=.o)) @@ -33,9 +43,9 @@ LIB = libft/libft.a \ $(NAME): $(OBJS) @make -sj $(nproc) -C $(LIBFT_DIR) @echo "✅ Libft compiled" - @make -sj $(nproc) -C $(MACRO_DIR) > /dev/null + @make -sj $(nproc) -C $(MACRO_DIR) @echo "✅ MacroLibX compiled" - @$(CC) $(FLAGS) $(OBJS) $(LIB) -o $(NAME) + @$(CC) $(OBJS) $(LIB) -o $(NAME) $(FLAGS) @echo "✅ Compiled" $(OBJSDIR)%.o: %.c diff --git a/ea_texture.png b/ea_texture.png new file mode 100755 index 0000000..e69de29 diff --git a/includes/cub3d.h b/includes/cub3d.h index 5b688b1..474f42c 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,21 +6,13 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/04 17:01:05 by madumerg #+# #+# */ -/* Updated: 2024/09/12 12:54:48 by madumerg ### ########.fr */ +/* Updated: 2024/10/05 14:30:04 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef CUB3D_H # define CUB3D_H -# include "../libft/includes/libft.h" -# include "mess_err.h" -# include -# include -# include -# include -# include # include "parsing.h" - #endif diff --git a/includes/mess_err.h b/includes/mess_err.h index 0ffd9a1..967530b 100644 --- a/includes/mess_err.h +++ b/includes/mess_err.h @@ -6,10 +6,13 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/04 21:59:56 by madumerg #+# #+# */ -/* Updated: 2024/09/11 14:18:39 by madumerg ### ########.fr */ +/* Updated: 2024/10/05 18:34:04 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ +//general +#define CRASH "Memory problems or open failed" + //for argurments #define ERR_ARGS "Wrong number of arguments" @@ -21,5 +24,14 @@ #define NOT_CLOSE "The map isn't closed" #define ERR_PLAYER "Incorrect number of players" #define WRONG_CHAR "Invalid character in the map" +#define MISS_MAP "Missing map" -//for permission +//for information map + +#define ERR_IMG "Problem with image" +#define WRONG_F_IMG "Wrong type of image" +#define DUP_ACR "There is a duplicate acronym" +#define NOT_FOUND "Information searched is not complete" +#define ERR_COLOR "The number is not between 0 and 255" +#define WRONG_F_RGB "Wrong format RGB" +#define INVALID_CHAR "Invalid character in format color" diff --git a/includes/parsing.h b/includes/parsing.h index 490f354..409583c 100644 --- a/includes/parsing.h +++ b/includes/parsing.h @@ -6,13 +6,20 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/12 12:49:16 by madumerg #+# #+# */ -/* Updated: 2024/09/12 16:58:29 by madumerg ### ########.fr */ +/* Updated: 2024/10/05 18:43:16 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PARSING_H # define PARSING_H +# include "libft.h" +# include "ft_printf.h" +# include "mess_err.h" +# include +# include +# include + typedef struct s_coord { int x; @@ -21,32 +28,82 @@ typedef struct s_coord typedef struct s_pars { - int f_color; - int c_color; + int f_color; + int c_color; + int color; + char l_player; t_coord coor; + char *no_png; + char *so_png; + char *we_png; + char *ea_png; + char **map; } t_pars; -//check_arg.c -int check_err_arg(int ac, char **av); -int check_format_file(char *file); +//start.c +t_pars init_pars(void); +int basics_check(int ac, char **av, t_pars *pars); + + //check_arg.c +int check_err_arg(int ac, char **av); +int check_format_file(char *file); + +//check_id_text.c +int detect_info(char **tab, t_pars *pars); + +//check_image.c +int assign_img(char *sign, char *path, t_pars *pars); +int check_texture(char *sign, char *path, t_pars *pars); //check_map.c -int err_not_close(char after, char before, char down, char up); -int actual_char(char actual); -int check_map_close(char **map); -int count_player(char **map); -int check_char_map(char **map); +int err_not_close(char after, char before, char down, char up); +int actual_char(char actual); +int check_map_close(char **map); +int check_char_map(char **map); + +//check_rgb.c +int check_format_rgb(char *sign, char **tab, t_pars *pars); + +//color_utils.c +int create_argb(int r, int g, int b); +char *ft_strndup_color(char *src, int start, int end); +int check_char_color(char *tab); + +//find_player.c +int search_letter(char **map, int i, char l); +int count_player(char **map, t_pars *pars, int ct); //principal_pars.c -int verif_info_map(char *av); -int verif_all_map(char **map); -char **parse_map(char *map); +int verif_info_file(char *av, t_pars *pars); +int verif_all_map(char **map, t_pars *pars); +char **parse_file(char *map, int ct, int i); //mess_error.c in utils -int err_mess(char *str); +int err_mess(char *str); //parse_utils.c -char **info_map(char **file); +void info_map(char **file, char ***f_part); +int verif_char(char c); +int space_line(char *line); + +//parse_utils_two.c +int ft_count_line_map(char **map, int y); +void replace_nl(char *l); +int recup_map(char **old_map, int i, t_pars *pars); +int all_skip(char **map, t_pars *pars); + +//parse_utils_three.c +char **alloc_map(char **old_map, int size); +char *fill_line(char *l, int size); +int last_first_line(char **map); + +//utils.c +int count_tab(char **tab); void convert_white_s(char *str); -int verif_char(char c); +int longest_line(char **line); +int count_line_file(int fd); + +//clear_free.c +void free_tab(char **tab); +void free_pars(t_pars *pars); #endif diff --git a/libft/gnl/get_next_line.c b/libft/gnl/get_next_line.c index 8827b3c..932c292 100644 --- a/libft/gnl/get_next_line.c +++ b/libft/gnl/get_next_line.c @@ -12,6 +12,33 @@ #include "../includes/libft.h" +char *ft_strjoin_gnl(char *s1, char *s2) +{ + size_t len; + size_t i; + size_t j; + char *str; + + if (!s2) + return (NULL); + len = ft_strlen(s1) + ft_strlen(s2); + str = malloc(sizeof(char) * (len + 1)); + if (!str) + return (NULL); + i = 0; + while (s1[i] != '\0') + { + str[i] = s1[i]; + i++; + } + j = 0; + while (s2[j] != '\0') + str[i++] = s2[j++]; + free(s1); + str[i] = '\0'; + return (str); +} + char ft_end_of_line(char *line, char *buffer) { int i; @@ -60,7 +87,7 @@ char *get_next_line(int fd) buffer = ft_calloc(sizeof(char), (BUFFER_SIZE + 1)); while (buffer && line) { - line = ft_strjoin(line, buffer); + line = ft_strjoin_gnl(line, buffer); if (!line) return (NULL); if (ft_end_of_line(line, buffer) == 1) diff --git a/map.cub b/map.cub old mode 100644 new mode 100755 index ecf31f3..2510b1f --- a/map.cub +++ b/map.cub @@ -1,22 +1,30 @@ -NO ./path_to_the_north_texture +NO ./no_texture.png + + +SO ./so_texture.png + +WE ./we_texture.png +EA ./ea_texture.png +C 255,100,200 +F 50,0,250 + + +111111 +1000W1 +1111111 + +11111111111111 + 11 + 11101 +1111111111111111 + + + + 111111111111111 +111111111111111111111111 + -SO ./path_to_the_south_texture -WE ./path_to_the_west_texture - -EA ./path_to_the_east_texture -F 220,100,0 -C 225,30,0 -11111 -1001S1 -11111 -11111 -11111 -11101 -11111 - 111 - 101 - 111 diff --git a/no_texture.png b/no_texture.png new file mode 100644 index 0000000..e69de29 diff --git a/so_texture.png b/so_texture.png new file mode 100755 index 0000000..e69de29 diff --git a/src/cub3d.c b/src/cub3d.c new file mode 100644 index 0000000..11442ac --- /dev/null +++ b/src/cub3d.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cub3d.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/04 16:58:27 by madumerg #+# #+# */ +/* Updated: 2024/10/05 18:45:09 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +int main(int ac, char **av) +{ + t_pars pars; + + pars = init_pars(); + if (basics_check(ac, av, &pars) == 1) + { + free_pars(&pars); + return (1); + } + free_pars(&pars); + return (0); +} diff --git a/src/other.c b/src/other.c new file mode 100644 index 0000000..0172cfe --- /dev/null +++ b/src/other.c @@ -0,0 +1,21 @@ + +void f_c_color(void *mlx, void *win, t_pars *pars) +{ + int x; + int y; + + x = 0; + y = 0; + + while (y <= (400 / 2)) //changer 400 par le define + { + x = 0; + while (x <= 800) //changer 800 par le define + { + mlx_pixel_put(mlx, win, x, y, pars->c_color); + mlx_pixel_put(mlx, win, x, 400 - y, pars->f_color); // changer 400 par le define + x++; + } + y++; + } +} diff --git a/src/parsing/check_arg.c b/src/parsing/check_arg.c index 7d9eeb7..fcf460e 100644 --- a/src/parsing/check_arg.c +++ b/src/parsing/check_arg.c @@ -6,11 +6,11 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/04 21:33:35 by madumerg #+# #+# */ -/* Updated: 2024/09/12 12:57:41 by madumerg ### ########.fr */ +/* Updated: 2024/10/05 15:39:53 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ -#include "cub3d.h" +#include "parsing.h" int check_err_arg(int ac, char **av) { @@ -24,10 +24,7 @@ int check_err_arg(int ac, char **av) return (err_mess(ERR_TYPE)); fd = open(av[1], O_RDONLY); if (fd == -1) - { - close(fd); return (err_mess(EMPTY)); - } rd = read(fd, buf, 10); close(fd); return (rd); diff --git a/src/parsing/check_id_text.c b/src/parsing/check_id_text.c index e69de29..2def421 100644 --- a/src/parsing/check_id_text.c +++ b/src/parsing/check_id_text.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_id_text.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/16 09:38:32 by madumerg #+# #+# */ +/* Updated: 2024/10/05 15:51:44 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" + +int detect_info(char **tab, t_pars *pars) +{ + if (ft_strncmp("NO", tab[0], 2) == 0 && count_tab(tab) == 2) + return (check_texture(tab[0], tab[1], pars)); + else if (ft_strncmp("SO", tab[0], 2) == 0 && count_tab(tab) == 2) + return (check_texture(tab[0], tab[1], pars)); + else if (ft_strncmp("WE", tab[0], 2) == 0 && count_tab(tab) == 2) + return (check_texture(tab[0], tab[1], pars)); + else if (ft_strncmp("EA", tab[0], 2) == 0 && count_tab(tab) == 2) + return (check_texture(tab[0], tab[1], pars)); + else if (ft_strncmp("F", tab[0], 1) == 0 && count_tab(tab) == 2) + return (check_format_rgb("F", tab, pars)); + else if (ft_strncmp("C", tab[0], 1) == 0 && count_tab(tab) == 2) + return (check_format_rgb("C", tab, pars)); + else + return (err_mess(NOT_FOUND)); + return (0); +} diff --git a/src/parsing/check_image.c b/src/parsing/check_image.c new file mode 100644 index 0000000..b4cdc8a --- /dev/null +++ b/src/parsing/check_image.c @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_image.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/19 10:44:07 by madumerg #+# #+# */ +/* Updated: 2024/10/05 13:39:06 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" + +int assign_img_two(char *sign, char *path, t_pars *pars) +{ + if (ft_strcmp("WE", sign) == 0) + { + if (pars->we_png == NULL) + pars->we_png = ft_strdup(path); + else + return (err_mess(DUP_ACR)); + } + else if (ft_strcmp("EA", sign) == 0) + { + if (pars->ea_png == NULL) + pars->ea_png = ft_strdup(path); + else + return (err_mess(DUP_ACR)); + } + return (0); +} + +int assign_img(char *sign, char *path, t_pars *pars) +{ + if (ft_strcmp("NO", sign) == 0) + { + if (pars->no_png == NULL) + pars->no_png = ft_strdup(path); + else + return (err_mess(DUP_ACR)); + } + else if (ft_strcmp("SO", sign) == 0) + { + if (pars->so_png == NULL) + pars->so_png = ft_strdup(path); + else + return (err_mess(DUP_ACR)); + } + if (assign_img_two(sign, path, pars) == 1) + return (1); + return (0); +} + +int format_img(char *img) +{ + int len; + + len = ft_strlen(img) - 1; + if (img[len] == 'g' && img[len - 1] == 'n' && \ + img[len - 2] == 'p' && img[len - 3] == '.') + return (0); + return (1); +} + +int check_texture(char *sign, char *path, t_pars *pars) +{ + int fd; + + fd = -1; + fd = open(path, O_RDONLY, 0644); + if (fd < 0) + return (err_mess(ERR_IMG)); + if (format_img(path) == 1) + { + close(fd); + return (err_mess(WRONG_F_IMG)); + } + if (assign_img(sign, path, pars) == 1) + { + close(fd); + return (1); + } + close(fd); + return (0); +} diff --git a/src/parsing/check_map.c b/src/parsing/check_map.c index c7dce2f..6740038 100644 --- a/src/parsing/check_map.c +++ b/src/parsing/check_map.c @@ -6,21 +6,21 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/11 11:24:13 by madumerg #+# #+# */ -/* Updated: 2024/09/12 12:57:24 by madumerg ### ########.fr */ +/* Updated: 2024/09/29 20:37:16 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ -#include "cub3d.h" +#include "parsing.h" int err_not_close(char after, char before, char down, char up) { - if (after == ' ' || after == '\0') + if (after == ' ' || after == '\0' || after == '\n') return (1); - else if (before == ' ' || before == '\0') + else if (before == ' ' || before == '\0' || before == '\n') return (1); - else if (down == ' ' || down == '\0') + else if (down == ' ' || down == '\0' || down == '\n') return (1); - else if (up == ' ' || up == '\0') + else if (up == ' ' || up == '\0' || up == '\n') return (1); return (0); } @@ -39,6 +39,8 @@ int check_map_close(char **map) int x; y = 0; + if (last_first_line(map) == 1) + return (1); while (map[y]) { x = 0; @@ -56,31 +58,6 @@ int check_map_close(char **map) return (0); } -int count_player(char **map) -{ - int y; - int x; - int cpt; - - cpt = 0; - y = 0; - while (map[y]) - { - x = 0; - while (map[y][x]) - { - if (map[y][x] == 'N' || map[y][x] == 'S' || \ - map[y][x] == 'E' || map[y][x] == 'W') - cpt++; - x++; - } - y++; - } - if (cpt != 1) - return (1); - return (0); -} - int check_char_map(char **map) { int y; diff --git a/src/parsing/check_rgb.c b/src/parsing/check_rgb.c index e69de29..6a01a95 100644 --- a/src/parsing/check_rgb.c +++ b/src/parsing/check_rgb.c @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_rgb.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/16 09:36:07 by madumerg #+# #+# */ +/* Updated: 2024/10/05 18:10:09 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" + +int check_nb_color(char *r, char *g, char *b) +{ + if (ft_strlen(r) > 3 || ft_strlen(g) > 3 || ft_strlen(b) > 3 || \ + ft_atoi(r) > 255 || ft_atoi(g) > 255 || ft_atoi(b) > 255) + return (-1); + return (0); +} + +int save_color(char *tab, t_pars *pars, int j, int i) +{ + char *r; + char *g; + char *b; + + while (tab[i] != ',') + i++; + r = ft_strndup_color(tab, 0, i); + i += 1; + j = i; + while (tab[i] != ',') + i++; + g = ft_strndup_color(tab, j, i); + i += 1; + j = i; + while (tab[i]) + i++; + b = ft_strndup_color(tab, j, i); + if (check_nb_color(r, g, b) == -1) + return (-1); + pars->color = create_argb(ft_atoi(r), ft_atoi(g), ft_atoi(b)); + free(r); + free(g); + free(b); + return (0); +} + +int count_precise_char(char *str, char c) +{ + int ct; + int i; + + i = 0; + ct = 0; + while (str[i]) + { + if (str[i] == c) + ct++; + i++; + } + if (ct != 2) + return (1); + return (0); +} + +int check_format_rgb(char *sign, char **tab, t_pars *pars) +{ + static int ct_f; + static int ct_c; + + if (ft_strcmp(sign, "F") == 0) + ct_f++; + if (ft_strcmp(sign, "C") == 0) + ct_c++; + if (count_tab(tab) > 2) + return (err_mess(NOT_FOUND)); + if (check_char_color(tab[1]) == 1) + return (err_mess(INVALID_CHAR)); + if (count_precise_char(tab[1], ',') == 1) + return (err_mess(WRONG_F_RGB)); + if (save_color(tab[1], pars, 0, 0) == -1) + return (err_mess(ERR_COLOR)); + if (ct_f > 1 || ct_c > 1) + return (err_mess(DUP_ACR)); + if (ft_strcmp(sign, "F") == 0) + pars->f_color = pars->color; + if (ft_strcmp(sign, "C") == 0) + pars->c_color = pars->color; + return (0); +} diff --git a/src/parsing/color_utils.c b/src/parsing/color_utils.c new file mode 100644 index 0000000..f4522ea --- /dev/null +++ b/src/parsing/color_utils.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* color_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/18 19:44:37 by madumerg #+# #+# */ +/* Updated: 2024/10/05 17:52:43 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" + +int create_argb(int r, int g, int b) +{ + return (255 << 24 | r << 16 | g << 8 | b); +} + +char *ft_strndup_color(char *src, int start, int end) +{ + int size; + char *dest; + int i; + + size = start; + i = 0; + dest = ft_calloc(sizeof(char), ft_strlen(src) + 1); + if (dest == NULL) + return (NULL); + while (size < end) + { + dest[i] = src[size]; + size++; + i++; + } + dest[i] = '\0'; + return (dest); +} + +int valid_char_color(char c) +{ + if (c == ',' || (c >= '0' && c <= '9')) + return (0); + return (1); +} + +int check_char_color(char *tab) +{ + int i; + + i = 0; + while (tab[i]) + { + if (valid_char_color(tab[i]) == 1) + return (1); + i++; + } + return (0); +} diff --git a/src/parsing/find_player.c b/src/parsing/find_player.c new file mode 100644 index 0000000..612c773 --- /dev/null +++ b/src/parsing/find_player.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_player.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/17 14:57:11 by madumerg #+# #+# */ +/* Updated: 2024/09/29 20:37:45 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" + +int search_letter(char **map, int i, char l) +{ + int x; + int y; + + y = 0; + while (map[y]) + { + x = 0; + while (map[y][x]) + { + if (map[y][x] == l) + { + if (i == 0) + return (x); + else if (i == 1) + return (y); + } + x++; + } + y++; + } + return (0); +} + +int count_player(char **map, t_pars *pars, int ct) +{ + int y; + int x; + + y = 0; + while (map[y]) + { + x = 0; + while (map[y][x]) + { + if (map[y][x] == 'N' || map[y][x] == 'S' || \ + map[y][x] == 'E' || map[y][x] == 'W') + { + pars->l_player = map[y][x]; + ct++; + } + x++; + } + y++; + } + if (ct != 1) + return (1); + return (0); +} diff --git a/src/parsing/memo_parsing b/src/parsing/memo_parsing deleted file mode 100644 index b240277..0000000 --- a/src/parsing/memo_parsing +++ /dev/null @@ -1,6 +0,0 @@ - // convertir tt les whites spaces en simple espace sur les 6 1ere lignes - - WARNIIIIIINNNNNNNNNNNNNGGG TESTER LES MALLOC A NULL - - //faire struct de coor - utiliser la ft pour couleur diff --git a/src/parsing/principal_pars.c b/src/parsing/principal_pars.c index 52a38eb..b1915ce 100644 --- a/src/parsing/principal_pars.c +++ b/src/parsing/principal_pars.c @@ -6,61 +6,108 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/12 12:44:52 by madumerg #+# #+# */ -/* Updated: 2024/09/12 16:49:20 by madumerg ### ########.fr */ +/* Updated: 2024/10/05 18:35:52 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ -#include "cub3d.h" +#include "parsing.h" -int check_info(char **l) +int all_data_verif(char **tab, t_pars *pars) { - -} - -int verif_info_map(char *av) -{ - char **map; - char **f_part; - - map = parse_map(av); - f_part = info_map(map); - if (check_info(f_part) == 1) + if (detect_info(tab, pars) == 1) + { + free_tab(tab); return (1); - if (verif_all_map(map) == 1) - return (1); -} - -int verif_all_map(char **map) -{ - if (check_char_map(map) == 1) - return (err_mess(WRONG_CHAR)); - if (count_player(map) == 1) - return (err_mess(ERR_PLAYER)); - if (check_map_close(map) == 1) - return (err_mess(NOT_CLOSE)); + } return (0); } -char **parse_map(char *map) +int check_info(char **file, t_pars *pars) +{ + char **tab; + int i; + int ct; + + ct = 0; + i = 0; + while (file[i]) + { + if (ct == 6) + break ; + if (space_line(file[i]) == 1) + { + ct++; + tab = ft_split(file[i], ' '); + if (tab == NULL || all_data_verif(tab, pars) == 1) + return (1); + free_tab(tab); + } + i++; + } + if (pars->f_color == 0 || pars->c_color == 0 || pars->ea_png == NULL || \ + pars->no_png == NULL || pars->so_png == NULL || pars->we_png == NULL) + return (err_mess(NOT_FOUND)); + return (0); +} + +int verif_info_file(char *av, t_pars *pars) +{ + char **map; + + map = parse_file(av, 0, 1); + if (map == NULL) + return (err_mess(CRASH)); + if (check_info(map, pars) == 1 || all_skip(map, pars) == 1) + { + free_tab(map); + return (1); + } + free_tab(map); + return (0); +} + +int verif_all_map(char **map, t_pars *pars) +{ + if (longest_line(map) == 1) + return (1); + pars->map = alloc_map(map, longest_line(map)); + if (pars->map == NULL) + return (err_mess(CRASH)); + if (check_char_map(pars->map) == 1) + return (err_mess(WRONG_CHAR)); + if (count_player(pars->map, pars, 0) == 1) + return (err_mess(ERR_PLAYER)); + if (check_map_close(pars->map) == 1) + return (err_mess(NOT_CLOSE)); + pars->coor.x = search_letter(pars->map, 0, pars->l_player); + pars->coor.y = search_letter(pars->map, 1, pars->l_player); + return (0); +} + +char **parse_file(char *map, int ct, int i) { int fd; char **parse_map; - char *save; - char *join; fd = open(map, O_RDONLY); - save = get_next_line(fd); - join = ft_calloc(1, 1); - if (!join) + if (fd < 0) return (NULL); - while (save != NULL) + ct = count_line_file(fd); + fd = open(map, O_RDONLY); + if (fd < 0) + return (NULL); + parse_map = ft_calloc(sizeof(char *), ct + 1); + if (!parse_map) { - join = ft_strjoin(join, save); - free(save); - save = get_next_line(fd); + close(fd); + return (NULL); } - parse_map = ft_split(join, '\n'); - free(join); - close(fd); + parse_map[0] = get_next_line(fd); + while (i <= ct) + { + parse_map[i] = get_next_line(fd); + i++; + } + close (fd); return (parse_map); } diff --git a/src/parsing/start.c b/src/parsing/start.c new file mode 100644 index 0000000..2e767a8 --- /dev/null +++ b/src/parsing/start.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* start.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/05 18:42:05 by madumerg #+# #+# */ +/* Updated: 2024/10/05 18:44:40 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" + +t_pars init_pars(void) +{ + t_pars new; + + new.f_color = -1; + new.c_color = -1; + new.color = 0; + new.l_player = 0; + new.coor.x = 0; + new.coor.y = 0; + new.no_png = NULL; + new.so_png = NULL; + new.we_png = NULL; + new.ea_png = NULL; + new.map = NULL; + return (new); +} + +int basics_check(int ac, char **av, t_pars *pars) +{ + if (check_err_arg(ac, av) == 1) + return (1); + if (verif_info_file(av[1], pars) == 1) + return (1); + return (0); +} diff --git a/src/utils/clear_free.c b/src/utils/clear_free.c new file mode 100644 index 0000000..fcd55fe --- /dev/null +++ b/src/utils/clear_free.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* clear_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/28 17:37:10 by madumerg #+# #+# */ +/* Updated: 2024/09/29 20:34:58 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" + +void free_tab(char **tab) +{ + char **tmp; + + tmp = tab; + while (tmp && *tmp) + { + free(*tmp); + tmp++; + } + free(tab); + tab = NULL; +} + +void free_pars(t_pars *pars) +{ + if (pars->no_png) + free(pars->no_png); + if (pars->so_png) + free(pars->so_png); + if (pars->we_png) + free(pars->we_png); + if (pars->ea_png) + free(pars->ea_png); + if (pars->map) + free_tab(pars->map); +} diff --git a/src/utils/mess_error.c b/src/utils/mess_error.c index c199dc9..ef7dbb2 100644 --- a/src/utils/mess_error.c +++ b/src/utils/mess_error.c @@ -6,11 +6,11 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/07 17:35:14 by madumerg #+# #+# */ -/* Updated: 2024/09/11 11:10:48 by adjoly ### ########.fr */ +/* Updated: 2024/09/29 20:35:10 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ -#include "cub3d.h" +#include "parsing.h" int err_mess(char *str) { diff --git a/src/utils/parse_utils.c b/src/utils/parse_utils.c index 6890f1c..9df2e87 100644 --- a/src/utils/parse_utils.c +++ b/src/utils/parse_utils.c @@ -6,43 +6,28 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/11 14:20:54 by madumerg #+# #+# */ -/* Updated: 2024/09/12 16:20:31 by madumerg ### ########.fr */ +/* Updated: 2024/09/29 20:35:22 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ -#include "cub3d.h" +#include "parsing.h" -char **info_map(char **file) -{ - char **f_part; - int i; - int j; - - i = 0; - j = 0; - f_part = ft_calloc(6, sizeof(char *)); - if (!f_part) - return (NULL); - while (i < 6) - { - f_part[j] = file[i]; - j++; - i++; - } - return (f_part); -} - -void convert_white_s(char *str) +int space_line(char *line) { int i; i = 0; - while (str[i]) + convert_white_s(line); + while (line[i]) { - if (str[i] >= 9 && str[i] <= 13) - str[i] = 32; - i++; + if (line[i] == ' ' || (line[i] >= 9 && line[i] <= 13)) + i++; + else + break ; } + if (line[i] != '\0') + return (1); + return (0); } int verif_char(char c) diff --git a/src/utils/parse_utils_three.c b/src/utils/parse_utils_three.c new file mode 100644 index 0000000..8f56744 --- /dev/null +++ b/src/utils/parse_utils_three.c @@ -0,0 +1,101 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_utils_three.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/28 15:40:12 by madumerg #+# #+# */ +/* Updated: 2024/10/05 18:49:41 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" + +char *fill_line(char *l, int size) +{ + char *tmp; + int i; + int j; + + i = 0; + j = 0; + tmp = ft_calloc(sizeof(char), size + 1); + if (!tmp) + return (NULL); + while (l[j]) + { + tmp[i] = l[j]; + i++; + j++; + } + if (i < size) + { + while (i <= size - 1) + { + tmp[i] = ' '; + i++; + } + } + tmp[i] = '\0'; + return (tmp); +} + +char **alloc_map(char **old_map, int size) +{ + int i; + int j; + char **tmp; + + i = 0; + j = 0; + tmp = ft_calloc(sizeof(char *), ft_count_line_map(old_map, 0) + 1); + if (!tmp) + return (NULL); + while (old_map[j]) + { + tmp[i] = fill_line(old_map[j], size); + if (tmp[i] == NULL) + { + free_tab(tmp); + return (NULL); + } + i++; + j++; + } + tmp[i] = NULL; + return (tmp); +} + +int line_char(char *l) +{ + int i; + + i = 0; + while (l[i]) + { + if (actual_char(l[i]) == 0) + return (1); + else + i++; + } + return (0); +} + +int last_first_line(char **map) +{ + int y; + int tmp; + + tmp = ft_count_line_map(map, 0); + y = 0; + while (map[y]) + { + if ((y == 0 && line_char(map[y]) == 1) || \ + (y == (tmp - 1) && line_char(map[y]) == 1)) + return (1); + else + y++; + } + return (0); +} diff --git a/src/utils/parse_utils_two.c b/src/utils/parse_utils_two.c new file mode 100644 index 0000000..aeceab6 --- /dev/null +++ b/src/utils/parse_utils_two.c @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_utils_two.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/17 15:12:28 by madumerg #+# #+# */ +/* Updated: 2024/09/29 20:35:50 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" + +int ft_count_line_map(char **file, int y) +{ + while (file[y]) + y++; + return (y); +} + +void replace_nl(char *l) +{ + int i; + + i = 0; + while (l[i]) + { + if (l[i] == '\n') + l[i] = '\0'; + i++; + } +} + +int recup_map(char **old_map, int i, t_pars *pars) +{ + int k; + + k = i; + while (old_map[i]) + { + replace_nl(old_map[i]); + i++; + } + if (verif_all_map(&old_map[k], pars) == 1) + return (1); + return (0); +} + +int space_l_two(char *line) +{ + int i; + + i = 0; + while (line[i]) + { + if (line[i] == ' ' || (line[i] >= 9 && line[i] <= 13)) + i++; + else + break ; + } + if (line[i] != '\0') + return (1); + return (0); +} + +int all_skip(char **map, t_pars *pars) +{ + char **tmp; + int ct; + int i; + + ct = 0; + i = 0; + tmp = map; + while (tmp[i]) + { + if (space_line(tmp[i]) == 1) + ct++; + if (ct == 6) + break ; + i++; + } + i += 1; + while (tmp[i] && space_l_two(tmp[i]) != 1) + i++; + if (recup_map(map, i, pars) == 1) + return (1); + return (0); +} diff --git a/src/utils/utils.c b/src/utils/utils.c new file mode 100644 index 0000000..c19b374 --- /dev/null +++ b/src/utils/utils.c @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/20 09:57:13 by madumerg #+# #+# */ +/* Updated: 2024/10/05 18:33:01 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" + +int count_tab(char **tab) +{ + int i; + int ct; + + i = 0; + ct = 0; + while (tab[i]) + { + i++; + ct++; + } + return (ct); +} + +void convert_white_s(char *str) +{ + int i; + + i = 0; + while (str[i]) + { + if (str[i] >= 9 && str[i] <= 13) + str[i] = 32; + i++; + } +} + +int longest_line(char **line) +{ + int i; + int stock; + int actual; + + stock = ft_strlen(line[0]); + if (stock == 0) + return (err_mess(MISS_MAP)); + i = 1; + actual = 0; + while (line[i]) + { + actual = ft_strlen(line[i]); + if (actual > stock) + stock = actual; + i++; + } + return (stock); +} + +int count_line_file(int fd) +{ + int i; + char *gnl; + + i = 0; + gnl = get_next_line(fd); + while (gnl) + { + free(gnl); + gnl = get_next_line(fd); + i++; + } + close (fd); + return (i); +} diff --git a/tamere.jpeg b/tamere.jpeg new file mode 100644 index 0000000..e69de29 diff --git a/we_texture.png b/we_texture.png new file mode 100644 index 0000000..e69de29