diff --git a/Makefile b/Makefile index 27e6eee..30b2061 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,8 @@ OS = $(shell uname) # sources D_SRCS = srcs \ srcs/init \ + srcs/gnl \ + srcs/parsing \ srcs/hook \ srcs/draw \ srcs/player \ @@ -23,8 +25,16 @@ SRCS = cube3d.c SRCS += memorybook.c \ memorybook_utils.c # init/ -SRCS += init_struct.c \ - init_parsing.c +SRCS += init_struct.c +# gnl/ +SRCS += get_next_line.c \ + get_next_line_utils.c +# parsing/ +SRCS += init_parsing.c \ + check_extension.c \ + check_path.c \ + check_rgb.c \ + check_map.c # hook/ SRCS += keyhook.c \ key_action_1.c \ diff --git a/headers/cube3d.h b/headers/cube3d.h index ea44b55..d436996 100644 --- a/headers/cube3d.h +++ b/headers/cube3d.h @@ -7,6 +7,9 @@ # include // for atoi() # include // for printf() # include // for M_PI, cos(), sin() +# include // for open() +# include // for open flags + # include "colors.h" # include "memorybook.h" diff --git a/headers/cube3d_macro.h b/headers/cube3d_macro.h index 9efe213..25c97c5 100644 --- a/headers/cube3d_macro.h +++ b/headers/cube3d_macro.h @@ -1,6 +1,8 @@ #ifndef CUBE3D_MACRO_H # define CUBE3D_MACRO_H +# define TOTAL_ELEMENTS 6 + # define MAX_NB_KEY 3 # define SCREEN_DIST 50 # define SCREEN_SIZE 100 diff --git a/headers/cube3d_proto.h b/headers/cube3d_proto.h index 2fcbe4a..50918bf 100644 --- a/headers/cube3d_proto.h +++ b/headers/cube3d_proto.h @@ -13,14 +13,31 @@ int shut_down(); // SRC/INIT // ------------------------------- // init_struct.c -t_game *init_game(void); -// init_parsing.c -void init_parsing(int ac, char **av, t_game *game); +t_game *init_struct(void); +void init_game(t_game *game); +void init_plr(t_plr *plr, t_map *map); + + +// ------------------------------- +// SRC/GNL +// ------------------------------- +// get_next_line.c +int get_next_line(int fd, char **line); // ------------------------------- // SRC/PARSING // ------------------------------- +// init_parsing.c +int init_parsing(t_game *game, char *file); +// check_extension.c +int check_extension(char *filename, char *ext); +// check_path.c +int check_elements(t_game *game, char *file); +// check_rgb.c +int check_rgb(t_txt *txt, char *element, char identifier); +// check_map.c +int check_map(t_map *map); // ------------------------------- diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index a0b8e49..e4ebee9 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -75,6 +75,20 @@ typedef struct s_img int endian; } t_img; +/* + * wall textures and floor/ceiling colors + */ + +typedef struct s_txt +{ + char *txt_north; + char *txt_south; + char *txt_east; + char *txt_west; + int rgb_floor[3]; + int rgb_ceiling[3]; +} t_txt; + /* * structs for the map */ @@ -82,8 +96,11 @@ typedef struct s_img typedef struct s_map { char **content; + char *tmp_str; int size_x; int size_y; + int plr_x; + int plr_y; } t_map; /* @@ -120,6 +137,8 @@ typedef struct s_game t_plr plr; // rays t_rcast rcast; + // textures and colors + t_txt txt; // map t_map map; // key hook diff --git a/maps/map1.cub b/maps/map1.cub new file mode 100644 index 0000000..556cf51 --- /dev/null +++ b/maps/map1.cub @@ -0,0 +1,18 @@ +NO textures/coin.xpm +SO textures/coin.xpm +EA textures/coin.xpm +WE textures/coin.xpm + +F 220 , 100, 30 + +C 225 , 30 , 0 + +11111111111111111111 +10000000011100000001 +11110000100000110001 +11110010000110000111 +10000000010101000001 +11111100000000001111 +10000000000N00000001 +10000000000000000001 +11111111111111111111 diff --git a/maps/map2.cub b/maps/map2.cub new file mode 100644 index 0000000..853ee2d --- /dev/null +++ b/maps/map2.cub @@ -0,0 +1,33 @@ +NO textures/coin.xpm +SO textures/coin.xpm +EA textures/coin.xpm +WE textures/coin.xpm + +F 220 , 100, 30 + +C 225 , 30 , 0 + + + + + + + + 11111111111111111111 + 10000110000001110011 + 11100000000000000001 + 10101111111111111111 +111111111110001 +100000000000001 1111111111111 +111100000000001 11 10000011111 +1000000N0001111 100001111111 +11100000010000111111100100000001 +100000000000000000000000011011011 +100111100111100111100111001011001 +1111 11111 11111 111111001111101 + 11100111101 + 110011101 + 11001101 + 1100101 + 110001 + 11111 diff --git a/srcs/cube3d.c b/srcs/cube3d.c index f343b41..5d4b24a 100644 --- a/srcs/cube3d.c +++ b/srcs/cube3d.c @@ -26,8 +26,18 @@ int main(int ac, char **av) { t_game *game; - game = init_game(); - init_parsing(ac, av, game); + if ((ac != 2 || check_extension(av[1], ".cub")) && \ + write(2, "Error\nPlease use a valid .cub file as single argument.\n", 53)) + return (EXIT_FAILURE); + game = init_struct(); + if (init_parsing(game, av[1]) && write(2, "The .cub file is invalid.\n", 26)) + return (EXIT_FAILURE); + if (check_map(&(game->map)) && write(2, "The map is invalid.\n", 20)) + return (EXIT_FAILURE); + + // init game variables + init_game(game); + mb_init(destroy_mlx, game); // draw game a first time before it start diff --git a/srcs/gnl/get_next_line.c b/srcs/gnl/get_next_line.c new file mode 100644 index 0000000..6ed2dcc --- /dev/null +++ b/srcs/gnl/get_next_line.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: pblagoje +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/01/27 12:42:20 by pblagoje #+# #+# */ +/* Updated: 2022/04/18 17:01:06 by pblagoje ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +void ft_free_gnl(char **s) +{ + if (*s != NULL) + { + free(*s); + *s = NULL; + } +} + +char *ft_strcpy(char *s1, char *s2) +{ + int i; + + i = 0; + while (s2[i]) + { + s1[i] = s2[i]; + i++; + } + s1[i] = '\0'; + return (s1); +} + +int get_next_line(int fd, char **line) +{ + int ret; + static char *mem; + char *buf; + + ret = 1; + buf = NULL; + if (BUFFER_SIZE <= 0 || !line || read(fd, buf, 0) == -1) + return (-1); + buf = malloc(sizeof(char) * (BUFFER_SIZE + 1)); + while (!ft_is_newline(mem, '\n') && ret != 0) + { + ret = read(fd, buf, BUFFER_SIZE); + buf[ret] = '\0'; + mem = ft_strjoin_gnl(mem, buf); + } + if (!ret) + { + ft_free_gnl(&mem); + ft_free_gnl(&buf); + return (0); + } + *line = ft_strtrim_gnl(mem); + mem = ft_strchr_gnl(mem, '\n'); + ft_free_gnl(&buf); + return (1); +} diff --git a/srcs/gnl/get_next_line.h b/srcs/gnl/get_next_line.h new file mode 100644 index 0000000..9ecc689 --- /dev/null +++ b/srcs/gnl/get_next_line.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: pblagoje +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/01/27 12:43:50 by pblagoje #+# #+# */ +/* Updated: 2022/04/18 17:01:22 by pblagoje ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H +# define BUFFER_SIZE 100 + +# include +# include +# include + +int get_next_line(int fd, char **line); +int ft_strlen_gnl(char *s); +char *ft_strchr_gnl(char *s, int c); +char *ft_strjoin_gnl(char *s1, char *s2); +char *ft_strtrim_gnl(char *s); +int ft_is_newline(char *s, int c); +char *ft_strcpy(char *s1, char *s2); +void ft_free_gnl(char **s); + +#endif diff --git a/srcs/gnl/get_next_line_utils.c b/srcs/gnl/get_next_line_utils.c new file mode 100644 index 0000000..918d555 --- /dev/null +++ b/srcs/gnl/get_next_line_utils.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: pblagoje +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/02/04 10:35:41 by pblagoje #+# #+# */ +/* Updated: 2022/04/18 17:00:41 by pblagoje ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +int ft_strlen_gnl(char *s) +{ + int i; + + if (!s) + return (0); + i = 0; + while (s[i]) + i++; + return (i); +} + +char *ft_strjoin_gnl(char *s1, char *s2) +{ + char *res; + int i; + int j; + + if (!s1 && !s2) + return (NULL); + i = 0; + j = 0; + res = malloc(sizeof(char) * (ft_strlen_gnl(s1) + ft_strlen_gnl(s2) + 1)); + if (!res) + return (NULL); + while (s1 != NULL && s1[i]) + { + res[i] = s1[i]; + i++; + } + while (s2[j]) + { + res[i + j] = s2[j]; + j++; + } + res[i + j] = '\0'; + ft_free_gnl(&s1); + return (res); +} + +int ft_is_newline(char *s, int c) +{ + int i; + + if (!s) + return (0); + i = 0; + while (s[i]) + { + if (s[i] == (unsigned char)c) + return (1); + i++; + } + return (0); +} + +char *ft_strchr_gnl(char *s, int c) +{ + int i; + int is_newline; + char *tmp; + + if (!s) + return (NULL); + i = 0; + is_newline = 0; + while (s[i] && is_newline == 0) + { + if (s[i] == (unsigned char)c) + is_newline = 1; + i++; + } + tmp = malloc(sizeof(char) * (ft_strlen_gnl(&s[i]) + 1)); + if (!tmp) + return (NULL); + tmp = ft_strcpy(tmp, &s[i]); + ft_free_gnl(&s); + return (tmp); +} + +char *ft_strtrim_gnl(char *s) +{ + char *res; + int size; + int i; + + size = 0; + i = 0; + while (s[size] != '\n') + size++; + res = malloc(sizeof(char) * (size + 1)); + if (!res) + return (NULL); + while (s[i] != '\n') + { + res[i] = s[i]; + i++; + } + res[i] = '\0'; + return (res); +} diff --git a/srcs/init/init_parsing.c b/srcs/init/init_parsing.c deleted file mode 100644 index b9d81d9..0000000 --- a/srcs/init/init_parsing.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "cube3d.h" - -void init_parsing(int ac, char **av, t_game *game) -{ - (void)*game; - (void)av; - (void)ac; -} diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index b5d0307..f9faf6d 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -10,7 +10,7 @@ static void init_img(t_img *img, t_game *game) static void init_map(t_map *map) { - map->size_x = 24; +/* map->size_x = 24; map->size_y = 24; // map int tmp[24][24] = { @@ -59,7 +59,21 @@ static void init_map(t_map *map) j++; } i++; - } + } */ + map->content = NULL; + map->tmp_str = NULL; + map->size_x = 0; + map->size_y = 0; + map->plr_x = 0; + map->plr_y = 0; +} + +static void init_txt(t_txt *txt) +{ + txt->txt_north = NULL; + txt->txt_south = NULL; + txt->txt_east = NULL; + txt->txt_west = NULL; } static void init_raycast(t_rcast *rcast) @@ -81,11 +95,11 @@ static void init_raycast(t_rcast *rcast) rcast->ray.end.y = -SCREEN_DIST; } -static void init_plr(t_plr *plr) +void init_plr(t_plr *plr, t_map *map) { // player first position - plr->exact.x = 2 * CELL; - plr->exact.y = 2 * CELL; + plr->exact.x = map->plr_x * CELL; + plr->exact.y = map->plr_y * CELL; plr->pos.x = plr->exact.x; plr->pos.y = plr->exact.y; // rotation @@ -96,14 +110,16 @@ static void init_plr(t_plr *plr) plr->sinj = 0; } -t_game *init_game(void) +t_game *init_struct(void) { t_game *game; game = mb_alloc(sizeof(t_game)); // map init_map(&(game->map)); - // plr + // init textures and floor/ceiling colors + init_txt(&(game->txt)); +/* // plr init_plr(&(game->plr)); // size window map game->map_win.size_x = game->map.size_x * CELL; @@ -119,6 +135,27 @@ t_game *init_game(void) // raycasting init_raycast(&(game->rcast)); // create image and get its data address - init_img(&(game->map_img), game); + init_img(&(game->map_img), game); */ return (game); } + +void init_game(t_game *game) +{ + // plr + init_plr(&(game->plr), &(game->map)); + // size window map + game->map_win.size_x = game->map.size_x * CELL; + game->map_win.size_y = game->map.size_y * CELL; + // init connexion to server + game->mlx_ptr = mlx_init(); + mb_add(game->mlx_ptr); + // create the window + game->map_win.ptr = mlx_new_window(game->mlx_ptr, game->map_win.size_x, + game->map_win.size_y, "test"); + // k(ey)_hook is the array containing the values of key press events + ft_bzero(&game->k_hook, sizeof(game->k_hook)); + // raycasting + init_raycast(&(game->rcast)); + // create image and get its data address + init_img(&(game->map_img), game); +} diff --git a/srcs/parsing/check_extension.c b/srcs/parsing/check_extension.c new file mode 100644 index 0000000..2182dbd --- /dev/null +++ b/srcs/parsing/check_extension.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_extension.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: pblagoje +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/17 00:24:35 by pblagoje #+# #+# */ +/* Updated: 2022/04/17 00:24:38 by pblagoje ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cube3d.h" + +int check_extension(char *filename, char *ext) +{ + char *str; + + if (!filename || !ext) + return (1); + str = ft_strrchr(filename, '.'); + if (!str) + return (2); + if (!ft_strcmp(str, ext)) + return (0); + return (3); +} diff --git a/srcs/parsing/check_map.c b/srcs/parsing/check_map.c new file mode 100644 index 0000000..d2e0dfa --- /dev/null +++ b/srcs/parsing/check_map.c @@ -0,0 +1,122 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: pblagoje +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/18 12:37:48 by pblagoje #+# #+# */ +/* Updated: 2022/04/23 19:18:16 by pblagoje ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cube3d.h" + +int set_player(t_map *map, int y, int x) +{ + map->plr_x = x; + map->plr_y = y; + map->content[y][x] = '0'; + return (1); +} + +void set_points(int *y, int *x, int row, int col) +{ + y[0] = row - 1; + y[1] = row; + y[2] = row + 1; + x[0] = col - 1; + x[1] = col; + x[2] = col + 1; +} + +int check_spaces(t_map *map, int row, int col) +{ + int y[3]; + int x[3]; + int i; + int j; + + set_points(y, x, row, col); + i = 0; + while (i < 3) + { + j = 0; + while (j < 3) + { + if (y[i] > 0 && y[i] < map->size_y - 1 \ + && x[j] > 0 && x[j] < map->size_x - 1) + { + if (map->content[y[i]][x[j]] != '1' \ + && map->content[y[i]][x[j]] != ' ') + return (EXIT_FAILURE); + } + j++; + } + i++; + } + return (EXIT_SUCCESS); +} + +int check_content(t_map *map) +{ + int x; + int y; + int count; + + count = 0; + y = 0; + while (map->content[y] != NULL) + { + x = 0; + while (map->content[y][x] != '\0' && map->content[y][x] != '\n') + { + if (map->content[y][x] == ' ' && check_spaces(map, y, x)) + return (EXIT_FAILURE); + else if (!ft_strchr(" 01SNWE", map->content[y][x])) + return (EXIT_FAILURE); + else if (ft_strchr("SNWE", map->content[y][x]) \ + && set_player(map, y, x)) + count++; + x++; + } + y++; + } + if (count != 1) + return (EXIT_FAILURE); + return (EXIT_SUCCESS); +} + +int check_borders(t_map *map) +{ + int x; + int y; + + y = 0; + while (map->content[y] != NULL) + { + x = 0; + while (map->content[y][x] != '\0' && map->content[y][x] != '\n') + { + if (y == 0 || y == map->size_y - 1) + if (map->content[y][x] != '1' && map->content[y][x] != ' ') + return (EXIT_FAILURE); + if (x == 0 || x == map->size_x - 1) + if (map->content[y][x] != '1' && map->content[y][x] != ' ') + return (EXIT_FAILURE); + x++; + } + y++; + } + return (EXIT_SUCCESS); +} + +int check_map(t_map *map) +{ + if (check_borders(map) || check_content(map)) + { + ft_putstr_fd("Error\nThe map characters are invalid.\n", 2); + return (EXIT_FAILURE); + } + return (EXIT_SUCCESS); +} diff --git a/srcs/parsing/check_path.c b/srcs/parsing/check_path.c new file mode 100644 index 0000000..5aae789 --- /dev/null +++ b/srcs/parsing/check_path.c @@ -0,0 +1,113 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_path.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: pblagoje +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/16 20:54:37 by pblagoje #+# #+# */ +/* Updated: 2022/04/23 18:38:15 by pblagoje ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cube3d.h" + +int set_path(t_txt *txt, char *path, char identifier) +{ + if (identifier == 'N' && txt->txt_north == NULL) + { + txt->txt_north = ft_strdup(path); + return (EXIT_SUCCESS); + } + else if (identifier == 'S' && txt->txt_south == NULL) + { + txt->txt_south = ft_strdup(path); + return (EXIT_SUCCESS); + } + else if (identifier == 'W' && txt->txt_west == NULL) + { + txt->txt_west = ft_strdup(path); + return (EXIT_SUCCESS); + } + else if (identifier == 'E' && txt->txt_east == NULL) + { + txt->txt_east = ft_strdup(path); + return (EXIT_SUCCESS); + } + return (EXIT_FAILURE); +} + +int check_path(t_txt *txt, char *element, char identifier) +{ + char *path; + int fd; + + while (element && *element && ft_strchr(" \t\r", *element)) + element++; + path = ft_substr(element, 0, ft_strlen(element)); + fd = open(path, O_RDONLY); + if (fd == -1 || check_extension(path, ".xpm")) + { + ft_putstr_fd("Error\nInvalid texture file.\n", 2); + free(path); + close(fd); + return (EXIT_FAILURE); + } + if (set_path(txt, path, identifier) == EXIT_FAILURE) + { + free(path); + close(fd); + return (EXIT_FAILURE); + } + free(path); + close(fd); + return (EXIT_SUCCESS); +} + +int check_element(t_game *game, char *element) +{ + char identifier[3]; + + while (element && *element && ft_strchr(" \t\r", *element)) + element++; + if (element == NULL || *element == '\0' || ft_strlen(element) < 2) + return (1); + identifier[0] = *element++; + identifier[1] = *element++; + identifier[2] = '\0'; + if ((*element == ' ') && ft_strnstr("NOSOWEEA", identifier, 9) \ + && !check_path(&(game->txt), element, identifier[0])) + return (EXIT_SUCCESS); + if (ft_strnstr("F C ", identifier, 5) && \ + !check_rgb(&(game->txt), element, identifier[0])) + return (EXIT_SUCCESS); + return (EXIT_FAILURE); +} + +int check_elements(t_game *game, char *file) +{ + int fd; + char *line; + int count; + + line = NULL; + count = 0; + fd = open(file, O_RDONLY); + if (fd == -1) + return (-1); + while (count < TOTAL_ELEMENTS) + { + get_next_line(fd, &line); + if (line && *line != '\n' && !check_element(game, line)) + count++; + else if (!line || (*line != '\n' && *line != '\0')) + { + free(line); + close(fd); + return (-1); + } + free(line); + line = NULL; + } + return (fd); +} diff --git a/srcs/parsing/check_rgb.c b/srcs/parsing/check_rgb.c new file mode 100644 index 0000000..3e85e4c --- /dev/null +++ b/srcs/parsing/check_rgb.c @@ -0,0 +1,103 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_rgb.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: pblagoje +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/15 16:11:24 by pblagoje #+# #+# */ +/* Updated: 2022/04/23 18:40:13 by pblagoje ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cube3d.h" + +int check_colors(char **str) +{ + int i; + int num; + + i = 0; + num = 0; + while (str && str[i]) + { + num = ft_atoi(str[i]); + if (num < 0 || num > 255) + return (EXIT_FAILURE); + i++; + } + return (EXIT_SUCCESS); +} + +int ft_strlen_2d(char **str) +{ + int i; + + i = 0; + while (str && str[i]) + i++; + return (i); +} + +void set_floor_rgb(t_txt *txt, char **rgb) +{ + int i; + + i = 0; + while (i < 3) + { + txt->rgb_floor[i] = ft_atoi(rgb[i]); + i++; + } +} + +void set_ceiling_rgb(t_txt *txt, char **rgb) +{ + int i; + + i = 0; + while (i < 3) + { + txt->rgb_ceiling[i] = ft_atoi(rgb[i]); + i++; + } +} + +void ft_free_2d(char **str) +{ + int i; + + i = 0; + while (str && str[i]) + { + free(str[i]); + i++; + } + free(str); +} + +int check_rgb(t_txt *txt, char *element, char identifier) +{ + char **rgb; + + rgb = ft_split(element, ','); + if (!rgb || ft_strlen_2d(rgb) != 3 || \ + element[ft_strlen(element) - 1] == ',') + { + ft_free_2d(rgb); + ft_putstr_fd("Error\nInvalid RGB code.\n", 2); + return (EXIT_FAILURE); + } + if ((identifier != 'F' && identifier != 'C') || check_colors(rgb)) + { + ft_free_2d(rgb); + ft_putstr_fd("Error\nInvalid RGB code.\n", 2); + return (EXIT_FAILURE); + } + if (identifier == 'F') + set_floor_rgb(txt, rgb); + else + set_ceiling_rgb(txt, rgb); + ft_free_2d(rgb); + return (EXIT_SUCCESS); +} diff --git a/srcs/parsing/init_parsing.c b/srcs/parsing/init_parsing.c new file mode 100644 index 0000000..6b34a03 --- /dev/null +++ b/srcs/parsing/init_parsing.c @@ -0,0 +1,136 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init_parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: pblagoje +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/15 16:03:50 by pblagoje #+# #+# */ +/* Updated: 2022/04/23 18:57:37 by pblagoje ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cube3d.h" + +int size_map(t_map *map, int fd) +{ + char *line; + + get_next_line(fd, &line); + while (line) + { + if ((*line == '\n' || *line == '\0') && map->size_x) + { + free(line); + return (EXIT_FAILURE); + } + else if (*line != '\n' && (int)ft_strlen(line) > map->size_x) + { + map->size_x = ft_strlen(line); + if (line[map->size_x - 1] != '\n') + map->size_x++; + } + if (*line != '\n' && *line != '\0') + map->size_y++; + free(line); + line = NULL; + get_next_line(fd, &line); + } + return (EXIT_SUCCESS); +} + +int find_map(t_map *map, char *file) +{ + int fd; + int count; + + fd = open(file, O_RDONLY); + count = 0; + if (fd == -1) + return (-1); + while (count < TOTAL_ELEMENTS + 1) + { + free(map->tmp_str); + get_next_line(fd, &map->tmp_str); + if (!map->tmp_str) + { + close(fd); + return (-1); + } + if (map->tmp_str[0] != '\n' && map->tmp_str[0] != '\0') + count++; + } + return (fd); +} + +void fill_row(char *row, char *line, int width) +{ + int i; + + i = 0; + while (line != NULL && line[i] != '\0' && line[i] != '\n') + { + row[i] = line[i]; + i++; + } + while (i < width - 1) + { + row[i] = ' '; + i++; + } + row[i] = '\0'; +} + +int fill_map(t_map *map, int fd) +{ + int i; + + map->content = (char **)mb_alloc((map->size_y + 1) * sizeof(char *)); + if (!map->content) + { + free(map->tmp_str); + return (EXIT_FAILURE); + } + i = -1; + while (++i < map->size_y) + { + if (i > 0) + get_next_line(fd, &map->tmp_str); + map->content[i] = (char *)mb_alloc((map->size_x + 1) * sizeof(char)); + if (!map->content[i]) + { + free(map->tmp_str); + return (EXIT_FAILURE); + } + fill_row(map->content[i], map->tmp_str, map->size_x); + free(map->tmp_str); + map->tmp_str = NULL; + } + map->content[i] = NULL; + return (EXIT_SUCCESS); +} + +int init_parsing(t_game *game, char *file) +{ + int fd; + + fd = check_elements(game, file); + if (fd == -1) + return (EXIT_FAILURE); + if (size_map(&(game->map), fd)) + { + close(fd); + return (EXIT_FAILURE); + } + close(fd); + fd = find_map(&(game->map), file); + if (fd == -1) + return (EXIT_FAILURE); + if (fill_map(&(game->map), fd) == EXIT_FAILURE) + { + close(fd); + return (EXIT_FAILURE); + } + close(fd); + return (EXIT_SUCCESS); +} diff --git a/textures/coin.xpm b/textures/coin.xpm new file mode 100644 index 0000000..df06ce6 --- /dev/null +++ b/textures/coin.xpm @@ -0,0 +1,135 @@ +/* XPM */ +static char *result[] = { +/* columns rows colors chars-per-pixel */ +"32 32 97 2 ", +" c #62493F", +". c #6D5144", +"X c #6E5244", +"o c #755847", +"O c #7D5D49", +"+ c #78655A", +"@ c #83624B", +"# c #89664D", +"$ c #836E62", +"% c #668346", +"& c #668446", +"* c #678446", +"= c #678546", +"- c #688546", +"; c #688547", +": c #698547", +"> c #688647", +", c #698647", +"< c #698747", +"1 c #688548", +"2 c #688648", +"3 c #698648", +"4 c #698748", +"5 c #6A8748", +"6 c #6A8749", +"7 c #698848", +"8 c #6A8848", +"9 c #6B8848", +"0 c #6A8948", +"q c #6B8948", +"w c #6A8849", +"e c #6B8849", +"r c #6A8949", +"t c #6B8949", +"y c #6B8A49", +"u c #6B8A4A", +"i c #6C8948", +"p c #6C8949", +"a c #6C8A49", +"s c #6D8A49", +"d c #6C8B49", +"f c #6D8B49", +"g c #6C8A4A", +"h c #6C8B4A", +"j c #6D8B4A", +"k c #6D8B4B", +"l c #6D8C4A", +"z c #6D8C4B", +"x c #6E8C4A", +"c c #6E8D4A", +"v c #6E8C4B", +"b c #6E8D4B", +"n c #6F8D4B", +"m c #6E8E4B", +"M c #6F8E4B", +"N c #6F8F4B", +"B c #6F8E4C", +"V c #6F8F4C", +"C c #708F4B", +"Z c #708E4C", +"A c #708F4C", +"S c #718F4C", +"D c #718F4D", +"F c #70904C", +"G c #71904C", +"H c #71904D", +"J c #71914D", +"K c #72904D", +"L c #72914D", +"P c #72924D", +"I c #72934D", +"U c #72924E", +"Y c #73924E", +"T c #72934E", +"R c #73934E", +"E c #74934E", +"W c #73944E", +"Q c #73944F", +"! c #74944E", +"~ c #74944F", +"^ c #75944F", +"/ c #74954F", +"( c #75954F", +") c #75964F", +"_ c #759550", +"` c #769550", +"' c #759650", +"] c #759750", +"[ c #769650", +"{ c #769750", +"} c #779851", +"| c #779951", +" . c #789951", +".. c #789952", +"X. c #799B52", +"o. c #A28776", +"O. c #B1937B", +/* pixels */ +"M H A G A A M M t > Z A [ N x ! b l L ' g B A [ N c ! b l J { A ", +"L H j j A L b b j 3 4 , H T A n b M L J 5 4 , H T A n b M K Y L ", +"A B u l c M U H j w j a M / R l F } z F b j a M / R l A } z F G ", +"p b W L j M L ( A w M B V ( A h n A b z e n B B ( A h n A b b a ", +"l Z L A G E H a * 5 , j R Y P H S M P M a ; j R U P H S M P M l ", +"L b w 9 a j t 4 2 ; * < A R x A L A L M t * < A R x A L A H C H ", +"l d q 8 w B A t 2 2 l , 4 d 5 t G A S l l x > 4 d 5 t G A S l l ", +"0 t a S A t t a w 3 b B a y l A A ) b 4 l M M a y l A A ) b 4 8 ", +"l M l j q % 4 l M S l j S l l 8 c W m a A l h S l l 8 c W M q j ", +"L g q j > % 5 t j x l M A f . . . . R G a l M A s l x v m T H L ", +"I A 4 2 , * * j H x 4 d x . O O O O . l > 3 j l G b M z a y b P ", +"r l 4 * 8 q a j l l v Z . O o # # o O . 8 j A L [ W P ! M , j t ", +"1 > h q 4 8 l b t v x . O o o # o o O O . l H S M b M n J d M w ", +"8 * j M a = 5 x b A , . O O o o o O O @ . & a A w ; j b & d ! 8 ", +"J l b A j r A x W W z . @ O O O O @ @ X q a M Z a g J ; 8 S k ", +"c ) A l z t l H | F q o @ @ @ O X X ] l > c M z w l H M J ..", +"v A j w h a 4 d y q b b X X X X 8 d w 8 d r h a 4 a I X.P ", +"R J w 8 t j , q > 8 ` ~ J o.+ a t p t 8 e t j < 8 t H M ", +"/ Q g n A A b P l e ( ( / M b + o.o.+ U a a h t M M A j 5 j a M ", +"P _ g B A ( A M E b v L ] F S $ O.O.$ A c a = w A A ( G l t h x ", +"L J 5 4 , L T A B b M H Y L $ O.O.o.$ b b j ; > w , J T j ; , 8 ", +"g J b j a M / R j F } z F $ O.o.O.O.$ U H j w j x t A P ; n M n ", +"b b e n B B ( A j n A b b $ O.O.O.$ M L ^ S 4 5 M M A ^ L P V ( ", +"P M g ; f R Y P H A M P M l Z L Z G ! H a * 8 g ; f R U L I c a ", +"L M t * < A E x A L A H C H b 8 9 a h t 4 ; > t - < F E a z G M ", +"G l l x > 4 d 4 t S A A l l d q 8 8 Z A t 3 > j c > 4 a 4 w A .", +"l 8 l M M a y l A A ) b < 8 t a G A w t a w 4 l M M a a b b b M ", +"b a A x h G l l 8 c W B q j M l j q % 4 l n D S l j S j x b n x ", +"R A a l M A a l l x m P L H h q j > % 5 t j l a l M A a q t w * ", +"a l > 4 l l G b M z d a c P S 4 4 , * * j H x ; 4 l l G c M l a ", +", j 4 g B L [ R P ! b , j t j 7 * 9 q t f b l , h M L ( f i Y .", +"q A H b L A M m b n P d n 6 ; j q 4 q b b w M H b K G M : t W L " +};