diff --git a/Makefile b/Makefile index c5d4a08..e602647 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ NAME = cub3d LIBFT = libft.a -SRCS = sources/main.c sources/utils/getline.c +SRCS = sources/main.c sources/parsing.c sources/utils/getline.c OBJS = ${SRCS:.c=.o} CC = gcc @@ -14,7 +14,7 @@ endif ifeq ($(UNAME_S), Darwin) LFLAGS = -lmlx -framework OpenGL -framework AppKit -L. -lft endif -CFLAGS = -Werror -Wall -Wextra -g -O3 -ffast-math -funsafe-math-optimizations +CFLAGS = -Werror -Wall -Wextra -O3 -ffast-math -funsafe-math-optimizations $(LIBFT): #@git clone @@ -30,7 +30,12 @@ all: $(NAME) $(NAME): $(LIBFT) $(OBJS) @echo "Making Cub3D." - @${CC} -g ${OBJS} -o ${NAME} ${LFLAGS} + @${CC} ${OBJS} -o ${NAME} ${LFLAGS} + @echo "Done." + +debug: $(LIBFT) + @echo "Making Cub3D with debug on." + @${CC} ${SRCS} -g -D DEBUG=1 ${CFLAGS} ${LFLAGS} -o ${NAME} @echo "Done." clean: @@ -49,4 +54,4 @@ fclean: clean re: fclean all -.PHONY: all clean fclean re +.PHONY: all clean fclean re debug diff --git a/includes/cub3d.h b/includes/cub3d.h index 14fc32a..bbb3669 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,13 +6,17 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/21 19:24:12 by narnaud #+# #+# */ -/* Updated: 2022/05/23 14:44:49 by narnaud ### ########.fr */ +/* Updated: 2022/05/23 17:31:16 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef CUB3D_H # define CUB3D_H +# ifndef DEBUG +# define DEBUG 0 +# endif + # define BUFFER_SIZE 4096 # define UNIT 200 # define WALL_HEIGHT 200 @@ -57,6 +61,7 @@ typedef struct s_env t_map *map; } t_env; -char *get_next_line(int fd); +t_env *parse_envFile(char *filename); +char *get_next_line(int fd); #endif diff --git a/sources/main.c b/sources/main.c index ac19092..572fb4b 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,140 +6,12 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/21 19:22:50 by narnaud #+# #+# */ -/* Updated: 2022/05/23 16:54:20 by narnaud ### ########.fr */ +/* Updated: 2022/05/23 17:23:39 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/cub3d.h" -void get_map_datas(t_env *env, char *line) -{ - t_slist *lst_e; - char *map_y; - int i; - - i = 0; - map_y = malloc((ft_strlen(line)) * sizeof(char)); - while (*line) - { - if (*line == '\n') - break ; - else if (!ft_isspace(*line)) - map_y[i] = *line; - else - map_y[i] = '0'; - line++; - i++; - } - map_y[i] = '\0'; - env->deep++; - lst_e = malloc(sizeof(*lst_e)); - lst_e->next = NULL; - lst_e->content = map_y; - ft_slst_add_back(&(env->raw_map), lst_e); -} - -int rgb_to_int(char **rgb) -{ - int ret; - int i; - int c; - char **colors; - - ret = 0; - rgb++; - colors = ft_split(*rgb, ','); - c = 0; - i = 0; - while (c < 3) - { - if (colors[i]) - { - ret |= ft_atoi(colors[i]) << (8 * (2 - c)); - c++; - i++; - } - else - { - ft_free_split(colors); - rgb++; - i = 0; - colors = ft_split(*rgb, ','); - } - } - ft_free_split(colors); - return (ret); -} - -void register_data(t_env *env, char *line) -{ - char **elem; - - if (*line && *line != '\n' && env->step > 5) - { - get_map_datas(env, line); - return ; - } - elem = ft_split(line, ' '); - if (!elem[0] || !elem[1]) - return ; - if (!ft_strncmp(elem[0],"NO", 3)) - env->wallTexture[0] = ft_strtrim(elem[1], "\n"); - else if (!ft_strncmp(elem[0],"SO", 3)) - env->wallTexture[1] = ft_strtrim(elem[1], "\n"); - else if (!ft_strncmp(elem[0],"WE", 3)) - env->wallTexture[2] = ft_strtrim(elem[1], "\n"); - else if (!ft_strncmp(elem[0],"EA", 3)) - env->wallTexture[3] = ft_strtrim(elem[1], "\n"); - else if (!ft_strncmp(elem[0],"F", 2)) - env->floorColor = rgb_to_int(elem); - else if (!ft_strncmp(elem[0],"C", 2)) - env->ceilColor = rgb_to_int(elem); - else - env->step--; - env->step++; - ft_free_split(elem); -} - -t_env *parse_envFile(char *filename) -{ - int fd; - char *line; - t_env *ret; - int y; - t_slist *raw_map; - - ret = malloc(sizeof(t_env)); - ret->deep = 0; - fd = open(filename, O_RDONLY); - line = get_next_line(fd); - while (line) - { - printf("%s", line); - register_data(ret, line); - free(line); - line = get_next_line(fd); - } - if (ret->step < 6) - return (NULL); - printf("\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n"); - printf("%s\n", ret->wallTexture[0]); - printf("%s\n", ret->wallTexture[1]); - printf("%s\n", ret->wallTexture[2]); - printf("%s\n", ret->wallTexture[3]); - printf("%d\n", ret->floorColor); - printf("%d\n", ret->ceilColor); - y = 0; - raw_map = ret->raw_map; - while (y < ret->deep) - { - printf("%s\n", (char *)raw_map->content); - y++; - raw_map = raw_map->next; - } - return (ret); -} - int main(int argc, char **argv) { t_env *env; diff --git a/sources/parsing.c b/sources/parsing.c new file mode 100644 index 0000000..17f7592 --- /dev/null +++ b/sources/parsing.c @@ -0,0 +1,144 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/05/23 17:20:29 by narnaud #+# #+# */ +/* Updated: 2022/05/23 17:47:07 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/cub3d.h" + +void get_map_datas(t_env *env, char *line) +{ + t_slist *lst_e; + char *map_y; + int i; + + i = 0; + map_y = malloc((ft_strlen(line)) * sizeof(char)); + while (*line) + { + if (*line == '\n') + break ; + else if (!ft_isspace(*line)) + map_y[i] = *line; + else + map_y[i] = '0'; + line++; + i++; + } + map_y[i] = '\0'; + env->deep++; + lst_e = malloc(sizeof(*lst_e)); + lst_e->next = NULL; + lst_e->content = map_y; + ft_slst_add_back(&(env->raw_map), lst_e); +} + +int rgb_to_int(char **rgb) +{ + int ret; + int i; + int c; + char **colors; + + ret = 0; + rgb++; + colors = ft_split(*rgb, ','); + c = 0; + i = 0; + while (c < 3) + { + if (colors[i]) + { + ret |= ft_atoi(colors[i]) << (8 * (2 - c)); + c++; + i++; + } + else + { + ft_free_split(colors); + rgb++; + i = 0; + colors = ft_split(*rgb, ','); + } + } + ft_free_split(colors); + return (ret); +} + +void register_data(t_env *env, char *line) +{ + char **elem; + + if (*line && *line != '\n' && env->step > 5) + { + get_map_datas(env, line); + return ; + } + elem = ft_split(line, ' '); + if (!elem[0] || !elem[1]) + return ; + if (!ft_strncmp(elem[0],"NO", 3)) + env->wallTexture[0] = ft_strtrim(elem[1], "\n"); + else if (!ft_strncmp(elem[0],"SO", 3)) + env->wallTexture[1] = ft_strtrim(elem[1], "\n"); + else if (!ft_strncmp(elem[0],"WE", 3)) + env->wallTexture[2] = ft_strtrim(elem[1], "\n"); + else if (!ft_strncmp(elem[0],"EA", 3)) + env->wallTexture[3] = ft_strtrim(elem[1], "\n"); + else if (!ft_strncmp(elem[0],"F", 2)) + env->floorColor = rgb_to_int(elem); + else if (!ft_strncmp(elem[0],"C", 2)) + env->ceilColor = rgb_to_int(elem); + else + env->step--; + env->step++; + ft_free_split(elem); +} + +t_env *parse_envFile(char *filename) +{ + int fd; + char *line; + t_env *ret; + int y; + t_slist *raw_map; + + ret = malloc(sizeof(t_env)); + ret->deep = 0; + fd = open(filename, O_RDONLY); + line = get_next_line(fd); + while (line) + { + register_data(ret, line); + free(line); + line = get_next_line(fd); + } + if (ret->step < 6) + return (NULL); + if (DEBUG) + { + printf("========> WALLS <========\nNorth: %s\nSouth: %s\nWest: %s\nEast: %s\n",\ + ret->wallTexture[0], ret->wallTexture[1],\ + ret->wallTexture[2], ret->wallTexture[3]); + printf("========================\n"); + printf("=> Floor color: %d\n=> Ceil color: %d\n", ret->floorColor, ret->ceilColor); + printf("========> MAP <========\n"); + y = 0; + raw_map = ret->raw_map; + while (y < ret->deep) + { + printf("%s\n", (char *)raw_map->content); + y++; + raw_map = raw_map->next; + } + } + return (ret); +} + +