diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4f3ff34 --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +* + +!.gitignore +!LICENSE +!Makefile +!README.md + +!*.h +!includes/ + +!*.a +!lib/ + +!*.cub +!maps/ +!maps/tests/ + +!*.c +!src/ +!src/mlx/ +!src/parsing/ +!src/render/ + +!*.xpm +!textures/ diff --git a/Makefile b/Makefile index 567c17b..cbb2712 100644 --- a/Makefile +++ b/Makefile @@ -14,12 +14,12 @@ RM = rm -rf UNAME_S := $(shell uname -s) ifeq ($(UNAME_S), Linux) - LFLAGS = -lXext -lX11 -lm -lz -Llib -lft -lmlx_Linux + LFLAGS = -lXext -lX11 -lm -lz -Llib -lft -lmlx_Linux endif ifeq ($(UNAME_S), Darwin) LFLAGS = -framework OpenGL -framework AppKit -Llib -lft -lmlx_Mac endif -CFLAGS = -Werror -Wall -Wextra -O3 -ffast-math -funsafe-math-optimizations +CFLAGS = -g -Werror -Wall -Wextra -O3 -ffast-math -funsafe-math-optimizations %.o:%.c ${CC} ${CFLAGS} -c $< -o ${<:.c=.o} diff --git a/README.md b/README.md index d18125e..29ec7e2 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,8 @@ CUBE3D - [x] save player pos, - [ ] handle errors, - [x] not closed map - - [ ] texture files not exist, not permited or with wrong extension - - [ ] impossible color used + - [x] texture files not exist, not permited or with wrong extension + - [x] impossible color used - [-] missing map ### Quit - [ ] clean and exit diff --git a/includes/cub3d.h b/includes/cub3d.h index fd2c706..a2f2439 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/21 19:24:12 by narnaud #+# #+# */ -/* Updated: 2022/11/14 10:35:59 by narnaud ### ########.fr */ +/* Updated: 2022/11/14 16:33:15 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -118,7 +118,7 @@ int mouse_move_hook(int x, int y, t_env *env); int mouse_up_hook(int buton, int x, int y, t_env *env); int update_hook(t_env *env); void init_window(t_env *nv); -void init_game(t_env *env); +int init_game(t_env *env); // Utils void vec_set(t_vec *vec, int x, int y); diff --git a/lib/libft.a b/lib/libft.a index a22a09a..8630f89 100644 Binary files a/lib/libft.a and b/lib/libft.a differ diff --git a/lib/libmlx_Linux.a b/lib/libmlx_Linux.a new file mode 100644 index 0000000..b794174 Binary files /dev/null and b/lib/libmlx_Linux.a differ diff --git a/maps/tester.cub b/maps/tester.cub index 4492508..d84e4ee 100644 --- a/maps/tester.cub +++ b/maps/tester.cub @@ -1,11 +1,13 @@ NO ./textures/wood.xpm +EA ./textures/wood.xpm EA ./textures/colorstone.xpm SO ./textures/wood.xpm + WE ./textures/wood.xpm DO ./textures/wood.xpm -F 500, 156, 8 -C 27, 210,27 +F 250, 156,150 +C 27, 210,200 111111111111111111111111111111111111111111111111 100000000000000000010000000000000000000000000001 diff --git a/maps/tests/empty_map_line.cub b/maps/tests/empty_map_line.cub new file mode 100644 index 0000000..330650d --- /dev/null +++ b/maps/tests/empty_map_line.cub @@ -0,0 +1,14 @@ +NO ./textures/redbrick.xpm +EA ./textures/colorstone.xpm +SO ./textures/redbrick.xpm +WE ./textures/redbrick.xpm +DO ./textures/wood.xpm + +F 147, 159, 153 +C 219, 213, 213 + +1111111 +100N001 + +1000001 +1111111 diff --git a/maps/tests/invalid_line_after_map.cub b/maps/tests/invalid_line_after_map.cub new file mode 100644 index 0000000..d57e589 --- /dev/null +++ b/maps/tests/invalid_line_after_map.cub @@ -0,0 +1,15 @@ +NO ./textures/redbrick.xpm +EA ./textures/colorstone.xpm +SO ./textures/redbrick.xpm +WE ./textures/redbrick.xpm +DO ./textures/wood.xpm + +F 147, 159, 153 +C 219, 213, 213 + +1111111 +100N001 +1000001 +1111111 + +test diff --git a/maps/tests/missing_color.cub b/maps/tests/missing_color.cub new file mode 100644 index 0000000..a650306 --- /dev/null +++ b/maps/tests/missing_color.cub @@ -0,0 +1,12 @@ +NO ./textures/redbrick.xpm +EA ./textures/colorstone.xpm +SO ./textures/redbrick.xpm +WE ./textures/redbrick.xpm +DO ./textures/wood.xpm + +F 147, 159, 153 + +1111111 +100N001 +1000001 +1111111 diff --git a/maps/tests/missing_color_comp.cub b/maps/tests/missing_color_comp.cub new file mode 100644 index 0000000..51b80b5 --- /dev/null +++ b/maps/tests/missing_color_comp.cub @@ -0,0 +1,13 @@ +NO ./textures/redbrick.xpm +EA ./textures/colorstone.xpm +SO ./textures/redbrick.xpm +WE ./textures/redbrick.xpm +DO ./textures/wood.xpm + +F 147, 159, 153 +C 219, 213 + +1111111 +100N001 +1000001 +1111111 diff --git a/maps/tests/no_door_tex.cub b/maps/tests/no_door_tex.cub new file mode 100644 index 0000000..9f56dc6 --- /dev/null +++ b/maps/tests/no_door_tex.cub @@ -0,0 +1,12 @@ +NO ./textures/redbrick.xpm +EA ./textures/colorstone.xpm +SO ./textures/redbrick.xpm +WE ./textures/redbrick.xpm + +F 147, 159, 153 +C 219, 213, 213 + +1111111 +100N001 +1000001 +1111111 diff --git a/maps/tests/open_map.cub b/maps/tests/open_map.cub new file mode 100644 index 0000000..e647089 --- /dev/null +++ b/maps/tests/open_map.cub @@ -0,0 +1,13 @@ +NO ./textures/redbrick.xpm +EA ./textures/colorstone.xpm +SO ./textures/redbrick.xpm +WE ./textures/redbrick.xpm +DO ./textures/wood.xpm + +F 147, 159, 153 +C 219, 213, 213 + +1111111 +100N000 +1000001 +1111111 diff --git a/maps/tests/simple.cub b/maps/tests/simple.cub new file mode 100755 index 0000000..cd72949 --- /dev/null +++ b/maps/tests/simple.cub @@ -0,0 +1,13 @@ +NO ./textures/redbrick.xpm +EA ./textures/colorstone.xpm +SO ./textures/redbrick.xpm +WE ./textures/redbrick.xpm +DO ./textures/wood.xpm + +F 147, 159, 153 +C 219, 213, 213 + +1111111 +100N001 +1000001 +1111111 diff --git a/maps/tests/unexistant_folder_tex.cub b/maps/tests/unexistant_folder_tex.cub new file mode 100644 index 0000000..7984804 --- /dev/null +++ b/maps/tests/unexistant_folder_tex.cub @@ -0,0 +1,13 @@ +NO ./textures/redbrick.xpm +EA ./textures/colorstone.xpm +SO ./textures/redbrick.xpm +WE ./textures/redbrick.xpm +DO ./text/wood.xpm + +F 147, 159, 153 +C 219, 213, 213 + +1111111 +100N001 +10002001 +11111111 diff --git a/maps/tests/unexistant_tex_file.cub b/maps/tests/unexistant_tex_file.cub new file mode 100644 index 0000000..ea90e03 --- /dev/null +++ b/maps/tests/unexistant_tex_file.cub @@ -0,0 +1,13 @@ +NO ./textures/redbrick.xpm +EA ./textures/colorstone.xpm +SO ./textures/redbrick.xpm +WE ./textures/redbrick.xpm +DO ./textures/woody.xpm + +F 147, 159, 153 +C 219, 213, 213 + +1111111 +100N001 +1000001 +1111111 diff --git a/src/main.c b/src/main.c index 353c1df..48ed8e5 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/21 19:22:50 by narnaud #+# #+# */ -/* Updated: 2022/11/14 10:39:10 by narnaud ### ########.fr */ +/* Updated: 2022/11/14 16:35:36 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,13 +17,14 @@ int main(int argc, char **argv) t_env *env; if (argc != 2) - return (EXIT_FAILURE); + return (printf("Usage: ./cub3d mapfile.cub\n"), EXIT_FAILURE); env = load_map(argv[1]); if (!env) return (EXIT_FAILURE); init_window(env); - init_game(env); - render(env); + if (init_game(env) == EXIT_FAILURE) + exit(EXIT_FAILURE); + render(env); mlx_loop_hook(env->mlx, update_hook, env); mlx_loop(env->mlx); mlx_destroy_image(env->mlx, env->img); diff --git a/src/mlx/mlx_1.c b/src/mlx/mlx_1.c index 74e08ba..f37a369 100644 --- a/src/mlx/mlx_1.c +++ b/src/mlx/mlx_1.c @@ -2,11 +2,10 @@ /* */ /* ::: :::::::: */ /* mlx_1.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: narnaud +#+ +:+ +#+ */ +/* +:+ +:+ +:+ */ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/25 14:14:46 by narnaud #+# #+# */ -/* Updated: 2022/08/25 14:18:18 by narnaud ### ########.fr */ +/* Updated: 2022/11/14 16:28:51 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +22,7 @@ void init_window(t_env *env) env->mlx = mlx; window = mlx_new_window(mlx, WIN_X_SZ, WIN_Y_SZ, "Cub3D"); env->win = window; - mlx_mouse_hide(mlx, win); + mlx_mouse_hide(mlx, window); } int mouse_move_hook(int x, int y, t_env *env) @@ -67,7 +66,7 @@ int mouse_move_hook(int x, int y, t_env *env) } #endif -void init_game(t_env *env) +int init_game(t_env *env) { int y; @@ -76,6 +75,8 @@ void init_game(t_env *env) { env->tex[y].img = mlx_xpm_file_to_image(env->mlx, env->tex[y].file, &env->tex[y].width, &env->tex[y].height); + if (env->tex[y].img == NULL) + return (printf("Error:\nCan't open a texture\n"), EXIT_FAILURE); env->tex[y].buffer = (int *)mlx_get_data_addr(env->tex[y].img, \ &env->tex[y].pixel_bits, &env->tex[y].line_bytes, @@ -90,4 +91,5 @@ void init_game(t_env *env) mlx_hook(env->win, 2, 1L << 0, key_press_hook, env); mlx_hook(env->win, 3, 1L << 1, key_release_hook, env); mlx_hook(env->win, 6, 0L, mouse_move_hook, env); + return (EXIT_SUCCESS); } diff --git a/src/parsing/getline.c b/src/parsing/getline.c index 0ac3ed1..27bd7e9 100644 --- a/src/parsing/getline.c +++ b/src/parsing/getline.c @@ -44,6 +44,7 @@ char *get_next_line(int fd) ssize_t read_size; char *line; char *ret; + char *next; if (!*buffer) { @@ -53,8 +54,10 @@ char *get_next_line(int fd) ret = ft_calloc(1, sizeof(char)); while (buffer[i]) { - line = ft_strjoin(ret, save_buffer(buffer, &i)); + next = save_buffer(buffer, &i); + line = ft_strjoin(ret, next); ret = (free(ret), line); + free(next); if (buffer[i - 1] == '\n') break ; read_size = read(fd, buffer, BUFFER_SIZE); diff --git a/src/parsing/parsing.c b/src/parsing/parsing.c index 9c3fdff..7579064 100644 --- a/src/parsing/parsing.c +++ b/src/parsing/parsing.c @@ -33,16 +33,16 @@ void register_settings(int *progress, t_env *env, char *line) elem = ft_split(line, ' '); if (!elem[0] || !elem[1]) - return ; - if (!ft_strncmp(elem[0], "NO", 3)) + return (ft_free_split(elem)); + if (!env->tex[0].file && !ft_strncmp(elem[0], "NO", 3)) env->tex[0].file = ft_strtrim(elem[1], "\n"); - else if (!ft_strncmp(elem[0], "SO", 3)) + else if (!env->tex[1].file && !ft_strncmp(elem[0], "SO", 3)) env->tex[1].file = ft_strtrim(elem[1], "\n"); - else if (!ft_strncmp(elem[0], "WE", 3)) + else if (!env->tex[2].file && !ft_strncmp(elem[0], "WE", 3)) env->tex[2].file = ft_strtrim(elem[1], "\n"); - else if (!ft_strncmp(elem[0], "EA", 3)) + else if (!env->tex[3].file && !ft_strncmp(elem[0], "EA", 3)) env->tex[3].file = ft_strtrim(elem[1], "\n"); - else if (!ft_strncmp(elem[0], "DO", 3)) + else if (!env->tex[4].file && !ft_strncmp(elem[0], "DO", 3)) env->tex[4].file = ft_strtrim(elem[1], "\n"); else if (!ft_strncmp(elem[0], "F", 2)) env->floorColor = rgb_to_int(elem + 1); @@ -117,7 +117,7 @@ t_env *load_map(char *filename) line = get_next_line(fd); } if (progress < 7 && cleanup_datas(env)) - return (NULL); + return (printf("Error\nYour map isn't valid.\n"), NULL); env->map = create_map_array(e_map, env->wide, env->deep); if (!is_valid_map(env) && cleanup_datas(env)) return (NULL); diff --git a/src/parsing/utils.c b/src/parsing/utils.c index 12b6af8..121fa40 100644 --- a/src/parsing/utils.c +++ b/src/parsing/utils.c @@ -24,7 +24,7 @@ int rgb_to_int(char **rgb) i = 0; ret = 0; colors = ft_split(*rgb, ','); - while (c < 3 && rgb) + while (c < 3 && rgb && colors) { if (!colors[i]) { @@ -39,6 +39,6 @@ int rgb_to_int(char **rgb) ret |= color << (8 * (2 - c++)); } if (c < 3) - return (ft_free_split(colors), printf("Error:\nInvalid color.\n"), -1); + return (printf("Error:\nInvalid color.\n"), -1); return (ft_free_split(colors), ret); } diff --git a/src/render/render.c b/src/render/render.c index bad9083..5b412f8 100644 --- a/src/render/render.c +++ b/src/render/render.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/31 09:33:02 by narnaud #+# #+# */ -/* Updated: 2022/08/23 16:19:08 by narnaud ### ########.fr */ +/* Updated: 2022/11/14 16:45:42 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -91,6 +91,6 @@ void render(t_env *env) bzero(env->buffer, WIN_Y_SZ * WIN_X_SZ * sizeof(int)); render_view(env); if (env->minimap) - render_minimap(env, (t_vec){env->wide * 8, env->deep * 8}); + render_minimap(env, (t_vec){WIN_X_SZ / 3, WIN_Y_SZ / 3}); mlx_put_image_to_window(env->mlx, env->win, env->img, 0, 0); }