From 249035e50625c79913c575ff198352c247bb1740 Mon Sep 17 00:00:00 2001 From: narnaud Date: Mon, 23 May 2022 10:48:17 +0200 Subject: [PATCH] working gnl --- Makefile | 10 ++-- includes/cub3d.h | 13 +++-- includes/mlx_keycode.h | 116 ++++++++++++++++++++++++++++++++++++++++ maps/inv.cub | 11 ++++ maps/inv2.cub | 22 ++++++++ maps/map.cub | 22 ++++++++ maps/sqr.cub | 20 +++++++ sources/main.c | 22 +++++--- sources/utils/getline.c | 49 +++++++++++------ 9 files changed, 252 insertions(+), 33 deletions(-) create mode 100644 includes/mlx_keycode.h create mode 100644 maps/inv.cub create mode 100644 maps/inv2.cub create mode 100644 maps/map.cub create mode 100644 maps/sqr.cub diff --git a/Makefile b/Makefile index 3dc3e37..bbb9109 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ NAME = cub3d LIBFT = libft.a -SRCS = sources/cub3d.c -OBJS = $(SRCS:.c=.o) +SRCS = sources/main.c sources/utils/getline.c +OBJS = ${SRCS:.c=.o} CC = gcc RM = rm -rf @@ -29,9 +29,9 @@ $(LIBFT): all: $(NAME) $(NAME): $(LIBFT) $(OBJS) - @echo "Making Cub3D." - @${CC} -g ${OBJS} -o ${NAME} ${LFLAGS} - @echo "Done." + @echo "Making Cub3D." + @${CC} -g ${OBJS} -o ${NAME} ${LFLAGS} + @echo "Done." clean: @echo "Cleaning objects." diff --git a/includes/cub3d.h b/includes/cub3d.h index c5829cd..c09b95d 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,13 +6,14 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/21 19:24:12 by narnaud #+# #+# */ -/* Updated: 2022/05/22 14:10:24 by narnaud ### ########.fr */ +/* Updated: 2022/05/23 10:44:57 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef CUB3D_H # define CUB3D_H +# define BUFFER_SIZE 4096 # define UNIT 100 # define WALL_HEIGHT 200 # define PLAYER_HEIGHT 170 @@ -22,7 +23,7 @@ # include # include # include -# include +# include # include # include "../includes/libft.h" # ifdef __linux__ @@ -36,9 +37,15 @@ typedef struct s_vector { int x; int y; - int z; } t_vec; +typedef struct s_map +{ + size_t wide; + size_t deep; + char **map; +} t_map; + char *get_next_line(int fd); #endif diff --git a/includes/mlx_keycode.h b/includes/mlx_keycode.h new file mode 100644 index 0000000..400a5ef --- /dev/null +++ b/includes/mlx_keycode.h @@ -0,0 +1,116 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mlx_keycode.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bledda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/08/10 20:43:21 by bledda #+# #+# */ +/* Updated: 2022/05/23 09:42:56 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MLX_KEYCODE_H +# define MLX_KEYCODE_H + +# ifdef __linux__ +# define KEY_Q 113 +# define KEY_W 119 +# define KEY_E 101 +# define KEY_R 114 +# define KEY_T 116 +# define KEY_Y 121 +# define KEY_U 117 +# define KEY_I 105 +# define KEY_O 111 +# define KEY_P 112 +# define KEY_A 97 +# define KEY_S 115 +# define KEY_D 100 +# define KEY_F 102 +# define KEY_G 103 +# define KEY_H 104 +# define KEY_J 106 +# define KEY_K 107 +# define KEY_L 108 +# define KEY_Z 122 +# define KEY_X 120 +# define KEY_C 99 +# define KEY_V 118 +# define KEY_B 98 +# define KEY_N 110 +# define KEY_M 109 +# define KEY_ECHAP 65307 +# define KEY_ENTER 65293 +# define KEY_ARROW_LEFT 65361 +# define KEY_ARROW_RIGHT 65363 +# define KEY_ARROW_DOWN 65364 +# define KEY_ARROW_UP 65362 +# define KEY_DELETE 65288 +# define KEY_0 48 +# define KEY_1 49 +# define KEY_2 50 +# define KEY_3 51 +# define KEY_4 52 +# define KEY_5 53 +# define KEY_6 54 +# define KEY_7 55 +# define KEY_8 56 +# define KEY_9 57 +# define KEY_TAB 65289 +# define KEY_MAJ 65509 +# define KEY_SHFT 65505 +# define KEY_CTRL 65507 +# define KEY_SPACE 32 +# elif __APPLE__ +# define KEY_Q 12 +# define KEY_W 13 +# define KEY_E 14 +# define KEY_R 15 +# define KEY_T 17 +# define KEY_Y 16 +# define KEY_U 32 +# define KEY_I 34 +# define KEY_O 31 +# define KEY_P 35 +# define KEY_A 0 +# define KEY_S 1 +# define KEY_D 2 +# define KEY_F 3 +# define KEY_G 5 +# define KEY_H 4 +# define KEY_J 38 +# define KEY_K 40 +# define KEY_L 37 +# define KEY_Z 6 +# define KEY_X 7 +# define KEY_C 8 +# define KEY_V 9 +# define KEY_B 11 +# define KEY_N 45 +# define KEY_M 46 +# define KEY_ECHAP 53 +# define KEY_ENTER 36 +# define KEY_ARROW_LEFT 123 +# define KEY_ARROW_RIGHT 124 +# define KEY_ARROW_DOWN 125 +# define KEY_ARROW_UP 126 +# define KEY_DELETE 51 +# define KEY_0 29 +# define KEY_1 18 +# define KEY_2 19 +# define KEY_3 20 +# define KEY_4 21 +# define KEY_5 23 +# define KEY_6 22 +# define KEY_7 26 +# define KEY_8 28 +# define KEY_9 25 +# define KEY_TAB 48 +# define KEY_MAJ 272 +# define KEY_SHFT 257 +# define KEY_CTRL 256 +# define KEY_SPACE 49 +# endif + +#endif diff --git a/maps/inv.cub b/maps/inv.cub new file mode 100644 index 0000000..c68428f --- /dev/null +++ b/maps/inv.cub @@ -0,0 +1,11 @@ +NO ./textures/brick.xpm +SO ./textures/blue.xpm +WE ./textures/wood.xpm +EA ./textures/grey.xpm + +F 100,0,100 +C 0,0,0 + +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 diff --git a/maps/inv2.cub b/maps/inv2.cub new file mode 100644 index 0000000..efb15bb --- /dev/null +++ b/maps/inv2.cub @@ -0,0 +1,22 @@ +NO ./textures/brick.xpm +SO ./textures/blue.xpm +WE ./textures/wood.xpm +EA ./textures/grey.xpm + +F 224,172,105 +C 205,249,255 + +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 +1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1 +1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 1 0 0 0 0 1 +1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 1 0 1 1 1 1 0 0 0 1 +1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1 +1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 0 0 0 0 0 0 1 +1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 1 +1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 1 +1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 1 +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 diff --git a/maps/map.cub b/maps/map.cub new file mode 100644 index 0000000..e68fd73 --- /dev/null +++ b/maps/map.cub @@ -0,0 +1,22 @@ +NO ./textures/brick.xpm +SO ./textures/blue.xpm +WE ./textures/wood.xpm +EA ./textures/grey.xpm + +F 224,172,105 +C 205,249,255 + +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 +1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 +1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 1 0 0 0 1 +1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 1 0 1 1 1 1 0 0 0 1 +1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1 +1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 0 0 0 0 0 0 1 +1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 1 +1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 1 +1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 1 +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 diff --git a/maps/sqr.cub b/maps/sqr.cub new file mode 100644 index 0000000..cbd533a --- /dev/null +++ b/maps/sqr.cub @@ -0,0 +1,20 @@ +NO ./textures/brick.xpm +SO ./textures/blue.xpm +WE ./textures/wood.xpm +EA ./textures/grey.xpm + +F 224,172,105 +C 205,249,255 + +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 N 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 diff --git a/sources/main.c b/sources/main.c index 2a23811..cebc755 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,29 +6,35 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/21 19:22:50 by narnaud #+# #+# */ -/* Updated: 2022/05/22 22:34:44 by narnaud ### ########.fr */ +/* Updated: 2022/05/23 09:54:53 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ -#include "cub3d.h" +#include "../includes/cub3d.h" -t_map *parse_map(char *map_filename) +t_map parse_map(char *map_filename) { + static t_map ret; int fd; char *file_line; - t_map *ret; - fd = open(map_filenamem, O_RDONLY); + fd = open(map_filename, O_RDONLY); file_line = get_next_line(fd); - ret = ft_calloc(sizeof(t_map)); - + while (file_line) + { + ret.deep++; + printf("%s", file_line); + free(file_line); + file_line = get_next_line(fd); + } return (ret); } int main(int argc, char **argv) { - t_map *map; + t_map map; + (void)argc; map = parse_map(argv[1]); return (EXIT_SUCCESS); diff --git a/sources/utils/getline.c b/sources/utils/getline.c index 07098ca..d2957e6 100644 --- a/sources/utils/getline.c +++ b/sources/utils/getline.c @@ -9,43 +9,58 @@ /* */ /* ************************************************************************** */ -#include "../../includes/libft.h" +#include "../../includes/cub3d.h" char *save_buffer(char *buffer, int *i) { - int j; + int j; + char *part1; + char *part2; + char *ret; - line = ft_calloc(512); + part1 = ft_calloc(512, sizeof(char)); j = 0; - while (j < 512) + while (j < 512 && buffer[*i]) { - line[j++] = buffer[(*i)++]; - if (buffer[*i] == '\n') - return (line) + part1[j++] = buffer[*i]; + if (buffer[(*i)++] == '\n') + return (part1); } - return (ft_strjoin(line, save_buffer(buffer, i))); + part2 = save_buffer(buffer, i); + ret = ft_strjoin(part1, part2); + free(part1); + free(part2); + return (ret); } char *get_next_line(int fd) { - static char *buffer[BUFFER_SIZE]; + static char buffer[BUFFER_SIZE + 1]; static int i; ssize_t read_size; char *line; + char *ret; + if (!*buffer) + { read_size = read(fd, buffer, BUFFER_SIZE); - line = ft_calloc(1); - while (read_size) + buffer[read_size] = 0; + } + ret = ft_calloc(1, sizeof(char)); + while (buffer[i]) { - line = ft_strjoin(line, save_buffer(buffer, i)); - if (line[i] == '\n') - return (line); + line = ft_strjoin(ret, save_buffer(buffer, &i)); + free(ret); + ret = line; + if (buffer[i - 1] == '\n') + break ; read_size = read(fd, buffer, BUFFER_SIZE); + buffer[read_size] = 0; i = 0; } - if (*line) - return (line); - free(line); + if (*ret) + return (ret); + free(ret); return (NULL); }