diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 12 | ||||
| -rw-r--r-- | cub3d.h | 116 | ||||
| m--------- | cub3d_generator | 0 | ||||
| -rw-r--r-- | event.c | 23 | ||||
| m--------- | generator | 0 | ||||
| m--------- | libft | 0 | ||||
| -rw-r--r-- | main.c | 38 | ||||
| -rw-r--r-- | parse/parse.c | 126 | ||||
| -rw-r--r-- | parse/parse_ceilling_color.c | 18 | ||||
| -rw-r--r-- | parse/parse_east_texture.c | 15 | ||||
| -rw-r--r-- | parse/parse_floor_color.c | 18 | ||||
| -rw-r--r-- | parse/parse_north_texture.c | 15 | ||||
| -rw-r--r-- | parse/parse_resolution.c | 16 | ||||
| -rw-r--r-- | parse/parse_south_texture.c | 15 | ||||
| -rw-r--r-- | parse/parse_sprite_texture.c | 15 | ||||
| -rw-r--r-- | parse/parse_west_texture.c | 15 |
17 files changed, 358 insertions, 85 deletions
@@ -2,3 +2,4 @@ *.ghc *.a a.out +cub3D @@ -2,11 +2,17 @@ LIBFT_PATH = ./libft MINILIBX_PATH = ./miniLibX CC = gcc -CCFLAGS = -I$(LIBFT_PATH) -I$(MINILIBX_PATH) -Wall -Wextra #-Werror -LDFLAGS = -L$(LIBFT_PATH) -lft -L$(MINILIBX_PATH) -lmlx +CCFLAGS = -I$(LIBFT_PATH) -I$(MINILIBX_PATH) -I. -Wall -Wextra #-Werror +LDFLAGS = -L$(LIBFT_PATH) -lft \ + -L$(MINILIBX_PATH) -lmlx \ + -framework OpenGL -framework AppKit NAME = cub3D -SRC = main.c +SRC = main.c event.c parse/parse.c parse/parse_east_texture.c \ + parse/parse_north_texture.c parse/parse_south_texture.c \ + parse/parse_west_texture.c parse/parse_ceilling_color.c \ + parse/parse_floor_color.c parse/parse_resolution.c \ + parse/parse_sprite_texture.c OBJ = $(SRC:.c=.o) INCLUDE = cub3d.h @@ -1,19 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cub3d.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 06:40:37 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:28:43 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include <stdio.h> + #ifndef CUB3D_H # define CUB3D_H +# define MLXK_W 13 +# define MLXK_A 0 +# define MLXK_S 1 +# define MLXK_D 2 +# define MLXK_ESC 53 + +# include <unistd.h> +# include <fcntl.h> # include <stdlib.h> +# include "mlx.h" # include "libft.h" # define TRUE 1 # define FALSE 0 -typedef int t_bool; +typedef int t_bool; typedef struct { - int x; - int y; -} t_point; + int x; + int y; +} t_vector; typedef struct { @@ -37,55 +59,63 @@ typedef t_cell** t_map; typedef struct { - int resolution_height; - int resolution_width; - char *north_texture_path; - char *south_texture_path; - char *west_texture_path; - char *east_texture_path; - char *sprite_texture_path; - t_color floor_color; - t_color ceilling_color; - t_map map; -} t_parsing; - -typedef struct + int resolution_height; + int resolution_width; + char *north_texture_path; + char *south_texture_path; + char *west_texture_path; + char *east_texture_path; + char *sprite_texture_path; + t_color floor_color; + t_color ceilling_color; + t_map map; + int map_width; + int map_height; +} t_parsing; + +typedef struct s_state { - t_point origin; - int angle; -} t_ray; + t_bool running; + t_vector pos; + t_vector dir; + t_vector plane; +} t_state; -typedef struct -{ - t_point a; - t_point b; -} t_wall; +typedef t_bool (*t_option_parser_func)(t_parsing *parsing, char *line); -typedef struct +typedef struct s_option_parser { - t_point pos; - int view_angle; -} t_player; + char *id; + t_option_parser_func func; +} t_option_parser; -typedef struct -{ - t_ray *rays; - t_player player; -} t_state; +/* +** parse.c +*/ -typedef t_bool (*func)(t_parsing *parsing, char *line) t_line_parser_func; +t_parsing *parse(char *filename); +char **get_file_lines(char *filename); +t_bool parse_line(t_parsing *parsing, char *line); +t_parsing *parse_map(t_parsing *parsing, char **lines); +t_cell *create_map_row(char *line); -typedef struct -{ - char *id; - t_line_parser_func func; -} t_line_parser; +/* +** parse_*.c +*/ + +t_bool parse_resolution(t_parsing *parsing, char *line); +t_bool parse_north_texture(t_parsing *parsing, char *line); +t_bool parse_south_texture(t_parsing *parsing, char *line); +t_bool parse_west_texture(t_parsing *parsing, char *line); +t_bool parse_east_texture(t_parsing *parsing, char *line); +t_bool parse_sprite_texture(t_parsing *parsing, char *line); +t_bool parse_floor_color(t_parsing *parsing, char *line); +t_bool parse_ceilling_color(t_parsing *parsing, char *line); /* -** parse.c +** event.c */ -t_parsing *parse(char *filename); -t_bool parse_line(t_parsing *parsing, char *line); +int handle_key(int key, void *param); #endif diff --git a/cub3d_generator b/cub3d_generator deleted file mode 160000 -Subproject 8b3b331455518b328fc2eb58223c6bb002f43cc @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* event.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 06:39:37 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:27:55 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +int handle_key(int key, void *param) +{ + t_state *state; + + state = (t_state*)param; + if (key == MLXK_ESC) + state->running = FALSE; + return (0); +} diff --git a/generator b/generator -Subproject 8b3b331455518b328fc2eb58223c6bb002f43cc +Subproject fa840dd345f1f23cc419afdbfe67ace4eb018cb diff --git a/libft b/libft -Subproject d8e5d376244c6cf92e52666634cddaaf4e492aa +Subproject e0f11e486518930e82e67f2dc305595671c074b @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 06:39:39 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:26:16 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "cub3d.h" int main(int argc, char **argv) @@ -7,8 +19,32 @@ int main(int argc, char **argv) ft_putendl("Error"); return (1); } - t_parsing *p = parse(argv[1]); + printf("R %d %d\n", p->resolution_width, p->resolution_height); + printf("NO %s\n", p->north_texture_path); + printf("SO %s\n", p->south_texture_path); + printf("WE %s\n", p->west_texture_path); + printf("EA %s\n\n", p->east_texture_path); + printf("S %s\n", p->sprite_texture_path); + printf("F %d,%d,%d\n", p->floor_color.r, p->floor_color.g, p->floor_color.b); + printf("C %d,%d,%d\n\n", p->ceilling_color.r, p->ceilling_color.g, p->ceilling_color.b); + + /* printf("%dx%d\n", p->map_height, p->map_width); */ + for (int i = 0; i < p->map_height; i++) + { + for (int j = 0; j < p->map_width; j++) + { + printf("%d", p->map[i][j]); + if (j != p->map_width - 1) + printf(" "); + } + printf("\n"); + } + /* void *mlx_ptr = mlx_init(); */ + /* void *mlx_window = mlx_new_window(mlx_ptr, 640, 480, "bonjour"); */ + /* mlx_key_hook(mlx_window, handle_key, NULL); */ + /* mlx_loop(mlx_ptr); */ + /* mlx_destroy_window(mlx_ptr, mlx_window); */ return (0); diff --git a/parse/parse.c b/parse/parse.c index c882424..f543ca5 100644 --- a/parse/parse.c +++ b/parse/parse.c @@ -1,24 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 09:29:21 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:32:54 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "cub3d.h" -t_parsing *parse(char *filename) +t_parsing *parse(char *filename) { - int fd; - int ret; - char *line; - t_parsing *parsing; + int i; + char **lines; + t_parsing *parsing; + if ((lines = get_file_lines(filename)) == NULL) + return (NULL); if ((parsing = (t_parsing*)malloc(sizeof(t_parsing))) == NULL) return (NULL); + parsing->map = NULL; + i = -1; + while (lines[++i] != NULL) + { + if (*lines[i] == '1') + break ; + if (!parse_line(parsing, lines[i])) + return (NULL); + } + if ((parsing = parse_map(parsing, lines + i)) == NULL) + return (NULL); + free(lines); + return (parsing); +} + +char **get_file_lines(char *filename) +{ + int fd; + int ret; + char *line; + char *file; + fd = open(filename, O_RDONLY); + if ((file = ft_strdup("")) == NULL) + return (NULL); while ((ret = get_next_line(fd, &line)) == 1) - if (!parse_line(parsing, line)) + if ((file = ft_strjoin_free(file, ft_strjoin_free(line, ft_strdup("\n"), 2), 2)) == NULL) return (NULL); if (ret == -1) return (NULL); - return (parsing); + free(line); + close(fd); + return (ft_split(file, '\n')); } -static t_option_parser option_parsers[] = { +static t_option_parser option_parsers[] = +{ {"R", parse_resolution}, {"NO", parse_north_texture}, {"SO", parse_south_texture}, @@ -26,29 +66,75 @@ static t_option_parser option_parsers[] = { {"EA", parse_east_texture}, {"S", parse_sprite_texture}, {"F", parse_floor_color}, - {"C", parse_ceilling_color}, + {"C", parse_ceilling_color} }; -#define OPTIONS_PARSER_SIZE (sizeof(line_parsers) / sizeof(t_line_parser)) +#define OPTIONS_PARSERS_SIZE (sizeof(option_parsers) / sizeof(t_option_parser)) -t_bool parse_option(t_parsing *parsing, char *line) +t_bool parse_line(t_parsing *parsing, char *line) { int i; if (!*line) return (TRUE); i = -1; - while (++i < LINE_PARSER_SIZE) - if (ft_strchr("012", *line) != NULL) - return (parse_map(parsing, line)); - else if (ft_strncmp(option_parsers[i].id, line, - ft_strlen(option_parsers[i].id)) == 0) - return (option_parsers[i] - .func(parsing, line + ft_strlen(option_parsers[i].id)); + while (++i < (int)OPTIONS_PARSERS_SIZE) + if (ft_strncmp(option_parsers[i].id, line, ft_strlen(option_parsers[i].id)) == 0) + return (option_parsers[i].func(parsing, line + ft_strlen(option_parsers[i].id))); return (FALSE); } -t_bool parse_map(t_parsing *parsing, char *line) +t_parsing *parse_map(t_parsing *parsing, char **lines) { - return (FALSE); + int i; + + i = -1; + while (lines[++i] != NULL) + if (*lines[i] != '1') + return (NULL); + parsing->map_height = i; + if ((parsing->map = (t_map)malloc(sizeof(t_cell*) * i)) == NULL) + return (NULL); + parsing->map_width = ft_strcount(*lines, '1'); + i = -1; + while (lines[++i] != NULL) + if ((parsing->map[i] = create_map_row(lines[i])) == NULL) + return (NULL); + return (parsing); +} + +t_cell *create_map_row(char *line) +{ + int i; + t_cell *row; + + if ((row = (t_cell*)malloc(sizeof(t_cell) * ft_strlen(line))) == NULL) + return (NULL); + i = 0; + while (*line) + { + if (*line == '0') + row[i++] = CELL_EMPTY; + else if (*line == '1') + row[i++] = CELL_WALL; + else if (*line == '2') + row[i++] = CELL_ITEM; + else if (*line == 'N') + row[i++] = CELL_LOOK_NORTH; + else if (*line == 'S') + row[i++] = CELL_LOOK_SOUTH; + else if (*line == 'W') + row[i++] = CELL_LOOK_WEST; + else if (*line == 'E') + row[i++] = CELL_LOOK_EAST; + else + { + free(row); + return (NULL); + } + line++; + while (*line == ' ') + line++; + } + return (row); } diff --git a/parse/parse_ceilling_color.c b/parse/parse_ceilling_color.c index 0f93ed1..67291d0 100644 --- a/parse/parse_ceilling_color.c +++ b/parse/parse_ceilling_color.c @@ -1,12 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_ceilling_color.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 09:31:32 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:31:37 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "cub3d.h" -t_bool parse_ceilling_color(t_parsing *parsing, char *line) +t_bool parse_ceilling_color(t_parsing *parsing, char *line) { line++; parsing->ceilling_color.r = ft_atoi(line); - line = ft_strchr(line, ','); + line = ft_strchr(line, ',') + 1; parsing->ceilling_color.g = ft_atoi(line); - line = ft_strchr(line, ','); + line = ft_strchr(line, ',') + 1; parsing->ceilling_color.b = ft_atoi(line); return (TRUE); } diff --git a/parse/parse_east_texture.c b/parse/parse_east_texture.c index e8d356a..846b36d 100644 --- a/parse/parse_east_texture.c +++ b/parse/parse_east_texture.c @@ -1,8 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_east_texture.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 09:29:37 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:31:02 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "cub3d.h" -t_bool parse_east_texture(t_parsing *parsing, char *line) +t_bool parse_east_texture(t_parsing *parsing, char *line) { parsing->east_texture_path = ft_strdup(line + 1); return (TRUE); } - diff --git a/parse/parse_floor_color.c b/parse/parse_floor_color.c index 62d0a06..ef2204a 100644 --- a/parse/parse_floor_color.c +++ b/parse/parse_floor_color.c @@ -1,12 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_floor_color.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 09:29:32 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:31:16 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "cub3d.h" -t_bool parse_floor_color(t_parsing *parsing, char *line) +t_bool parse_floor_color(t_parsing *parsing, char *line) { line++; parsing->floor_color.r = ft_atoi(line); - line = ft_strchr(line, ','); + line = ft_strchr(line, ',') + 1; parsing->floor_color.g = ft_atoi(line); - line = ft_strchr(line, ','); + line = ft_strchr(line, ',') + 1; parsing->floor_color.b = ft_atoi(line); return (TRUE); } diff --git a/parse/parse_north_texture.c b/parse/parse_north_texture.c index 0d1af87..eb8cb3c 100644 --- a/parse/parse_north_texture.c +++ b/parse/parse_north_texture.c @@ -1,8 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_north_texture.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 09:29:47 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:30:52 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "cub3d.h" -t_bool parse_north_texture(t_parsing *parsing, char *line) +t_bool parse_north_texture(t_parsing *parsing, char *line) { parsing->north_texture_path = ft_strdup(line + 1); return (TRUE); } - diff --git a/parse/parse_resolution.c b/parse/parse_resolution.c index c8e0c7f..e63317e 100644 --- a/parse/parse_resolution.c +++ b/parse/parse_resolution.c @@ -1,9 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_resolution.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 09:29:27 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:31:47 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "cub3d.h" -t_bool parse_resolution(t_parsing *parsing, char *line) +t_bool parse_resolution(t_parsing *parsing, char *line) { parsing->resolution_width = ft_atoi(line); - line = ft_strchr(line, ' '); + line = ft_strrchr(line, ' ') + 1; parsing->resolution_height = ft_atoi(line); return (TRUE); } diff --git a/parse/parse_south_texture.c b/parse/parse_south_texture.c index 9ec89d0..10ee403 100644 --- a/parse/parse_south_texture.c +++ b/parse/parse_south_texture.c @@ -1,8 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_south_texture.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 09:29:55 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:30:46 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "cub3d.h" -t_bool parse_south_texture(t_parsing *parsing, char *line) +t_bool parse_south_texture(t_parsing *parsing, char *line) { parsing->south_texture_path = ft_strdup(line + 1); return (TRUE); } - diff --git a/parse/parse_sprite_texture.c b/parse/parse_sprite_texture.c index 039a827..8e3f40b 100644 --- a/parse/parse_sprite_texture.c +++ b/parse/parse_sprite_texture.c @@ -1,8 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_sprite_texture.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 09:30:05 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:31:56 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "cub3d.h" -t_bool parse_sprite_texture(t_parsing *parsing, char *line) +t_bool parse_sprite_texture(t_parsing *parsing, char *line) { parsing->sprite_texture_path = ft_strdup(line + 1); return (TRUE); } - diff --git a/parse/parse_west_texture.c b/parse/parse_west_texture.c index 5252ab4..dca1629 100644 --- a/parse/parse_west_texture.c +++ b/parse/parse_west_texture.c @@ -1,8 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_west_texture.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 09:29:42 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:30:40 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "cub3d.h" -t_bool parse_west_texture(t_parsing *parsing, char *line) +t_bool parse_west_texture(t_parsing *parsing, char *line) { parsing->west_texture_path = ft_strdup(line + 1); return (TRUE); } - |
