diff --git a/.clangd b/.clangd index 21b8a20..dfa7b67 100644 --- a/.clangd +++ b/.clangd @@ -1,12 +1,11 @@ CompileFlags: Add: - - "-Werror" - - "-Wextra" - - "-Wall" - - "-g3" + - "-Werror -Wextra -Wall" - "-I/opt/X11/include" - "-I/Users/raphael/Documents/42_cursus/circle4/Cub3D/includes" - "-I/Users/raphael/Documents/42_cursus/circle4/Cub3D/includes/include" - # - "-I/Users/raphael/Documents/42_cursus/circle4/Cub3D/minilibx-linux" - "-I/home/iron/Documents/code/42school/Cub3D/includes" - "-I/home/iron/Documents/code/42school/Cub3D/includes/include" + - "-I/home/bgoulard/Documents/projects/shared/common/Cub3D/includes" + - "-I/home/bgoulard/Documents/projects/shared/common/Cub3D/includes/include" + - "-xc" diff --git a/Makefile b/Makefile index 78f9595..e444dbb 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/11/12 06:12:02 by bgoulard ### ########.fr # +# Updated: 2024/11/12 09:17:25 by bgoulard ### ########.fr # # # # **************************************************************************** # @@ -45,6 +45,7 @@ SRC =\ raycast/frame_update.c \ mlx_layer/mlx_init.c \ parsing/arguments.c \ + parsing/map.c \ sources/main.c \ sources/cleanups.c \ sources/options.c \ diff --git a/includes/cub3d.h b/includes/cub3d.h index a46e8c4..b4af24e 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 16:30:26 by rparodi #+# #+# */ -/* Updated: 2024/11/11 21:33:28 by rparodi ### ########.fr */ +/* Updated: 2024/11/12 08:33:12 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,11 +21,16 @@ # include int init_mlx_env(t_info *info); + void cleanup_info(t_info *info); + int c3_options(t_info *info, int argc, char *argv[]); void c3_perror(t_info *info); void print_error(const char *msg); + +void parse_map(t_info *info); void parse_args(char *arg, t_info *inf); + int main(int argc, char *argv[]); #endif /* CUB3D_H */ diff --git a/includes/cub3d_struct.h b/includes/cub3d_struct.h index 7aa4289..ad3cda3 100644 --- a/includes/cub3d_struct.h +++ b/includes/cub3d_struct.h @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/08 23:55:29 by bgoulard #+# #+# */ -/* Updated: 2024/11/11 21:34:38 by rparodi ### ########.fr */ +/* Updated: 2024/11/12 11:05:49 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,7 +63,9 @@ typedef struct s_map t_dpoint player_pos; t_ipoint size; t_tile *map; - char **raw; + char **fraw; + t_img texture[4]; + t_color bg_colors[2]; } t_map; // -- player utils @@ -89,16 +91,17 @@ typedef struct s_cli typedef enum e_error { NO_ERROR = 0, - UNKNOWN_ERROR, - OPEN_FILE_ERROR, - READ_FILE_ERROR, - EXTENSION_FILE_ERROR, - NAME_FILE_ERROR, - MISSING_FILE_ERROR, - MALLOC_ERROR, - PARSE_ERROR, - CLI_ERROR, - MLX_ERROR + ERROR_UNKNOWN, + ERROR_OPEN_FILE, + ERROR_READ_FILE, + ERROR_EXTENSION_FILE, + ERROR_NAME_FILE, + ERROR_MISSING_FILE, + ERROR_MALLOC, + ERROR_PARSE, + ERROR_CLI, + ERROR_MLX, + ERROR_IMPLEM, } t_error; // -- main struct diff --git a/libft/src/ft_string/ft_mem/ft_fd_to_buff.c b/libft/src/ft_string/ft_mem/ft_fd_to_buff.c index 90fc327..72ec5c5 100644 --- a/libft/src/ft_string/ft_mem/ft_fd_to_buff.c +++ b/libft/src/ft_string/ft_mem/ft_fd_to_buff.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/18 19:52:02 by bgoulard #+# #+# */ -/* Updated: 2024/06/13 16:46:24 by bgoulard ### ########.fr */ +/* Updated: 2024/11/12 09:38:37 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,12 +31,12 @@ char *ft_fd_to_buff(int fd) return (NULL); buff[ret] = '\0'; file = ft_strdup(buff); - while (ret == BUFFER_SIZE) + while (ret == BUFFER_SIZE && file) { ret = read(fd, buff, BUFFER_SIZE); if (ret == -1) return (ft_free((void **)&file), NULL); - buff[ret] = '\0'; + buff[ret + 1 * (ret == 0)] = '\0'; prev = file; file = ft_strjoin(file, buff); ft_free((void **)&prev); diff --git a/maps/bad_format_color_c.cub b/maps/bad_format_color_c.cub new file mode 100644 index 0000000..c5ff595 --- /dev/null +++ b/maps/bad_format_color_c.cub @@ -0,0 +1,23 @@ +SO ./textures/png_ugly.png +NO ./textures/png_ugly.png +WE ./textures/png_pretty.png +EA ./textures/png_pretty.png + +F 255,255,255 +C 255,,255 + + + + + + + + + + + + + +111 +1N1 +111 diff --git a/maps/bad_format_color_f.cub b/maps/bad_format_color_f.cub new file mode 100644 index 0000000..04da1ac --- /dev/null +++ b/maps/bad_format_color_f.cub @@ -0,0 +1,23 @@ +SO ./textures/png_ugly.png +NO ./textures/png_ugly.png +WE ./textures/png_pretty.png +EA ./textures/png_pretty.png + +F 255,255, +C 255,255,255 + + + + + + + + + + + + + +111 +1N1 +111 diff --git a/maps/bad_format_map_char.cub b/maps/bad_format_map_char.cub new file mode 100644 index 0000000..49ea9c7 --- /dev/null +++ b/maps/bad_format_map_char.cub @@ -0,0 +1,24 @@ +SO ./textures/png_ugly.png +NO ./textures/png_ugly.png +WE ./textures/png_pretty.png +EA ./textures/png_pretty.png + +F 255,255,255 +C 255,255,255 + + + + + + + + + + + + + +111 +1N11111 +1000xy1 +1111111 diff --git a/maps/bad_format_map_open.cub b/maps/bad_format_map_open.cub new file mode 100644 index 0000000..fe70115 --- /dev/null +++ b/maps/bad_format_map_open.cub @@ -0,0 +1,23 @@ +SO ./textures/png_ugly.png +NO ./textures/png_ugly.png +WE ./textures/png_pretty.png +EA ./textures/png_pretty.png + +F 255,255,255 +C 255,255,255 + + + + + + + + + + + + + +1111 +00N1 +1111 diff --git a/maps/bad_format_texture.cub b/maps/bad_format_texture.cub new file mode 100644 index 0000000..2d0daff --- /dev/null +++ b/maps/bad_format_texture.cub @@ -0,0 +1,23 @@ +SO ./textures/png_ugly.png +NO = ./textures/png_ugly.png +WE ./textures/png_pretty.png +EA ./textures/png_pretty.png + +F 255,255,255 +C 255,255,255 + + + + + + + + + + + + + +111 +1N1 +111 diff --git a/maps/bad_perm.cub b/maps/bad_perm.cub new file mode 100644 index 0000000..07fbde4 --- /dev/null +++ b/maps/bad_perm.cub @@ -0,0 +1,23 @@ +SO ./textures/png_ugly.png +NO ./textures/png_ugly.png +WE ./textures/png_pretty.png +EA ./textures/png_pretty.png + +F 255,255,255 +C 255,255,255 + + + + + + + + + + + + + +111 +1N1 +111 diff --git a/maps/test.cub b/maps/test.cub deleted file mode 100644 index e69de29..0000000 diff --git a/maps/valid_01.cub b/maps/valid_01.cub new file mode 100644 index 0000000..07fbde4 --- /dev/null +++ b/maps/valid_01.cub @@ -0,0 +1,23 @@ +SO ./textures/png_ugly.png +NO ./textures/png_ugly.png +WE ./textures/png_pretty.png +EA ./textures/png_pretty.png + +F 255,255,255 +C 255,255,255 + + + + + + + + + + + + + +111 +1N1 +111 diff --git a/mlx_layer/mlx_init.c b/mlx_layer/mlx_init.c index 1600019..91022d7 100644 --- a/mlx_layer/mlx_init.c +++ b/mlx_layer/mlx_init.c @@ -48,10 +48,10 @@ int init_mlx_env(t_info *info) { info->mlx_ptr = mlx_init(); if (!info->mlx_ptr) - return (MLX_ERROR); + return (ERROR_MLX); info->win_ptr = c3_init_mlx_window(info); if (!info->win_ptr) - return (MLX_ERROR); + return (ERROR_MLX); mlx_hook(info->win_ptr, KeyPress, KeyPressMask, c3_keyhook, info); mlx_hook(info->win_ptr, DestroyNotify, StructureNotifyMask, c3_redcross, info); mlx_loop_hook(info->mlx_ptr, c3_frame_update, info); diff --git a/parsing/arguments.c b/parsing/arguments.c index 43fc103..704e325 100644 --- a/parsing/arguments.c +++ b/parsing/arguments.c @@ -24,11 +24,11 @@ void parse_args(char *arg, t_info *inf) { if (arg == NULL && inf->cli_ctx.file == NULL) - return (inf->last_error = MISSING_FILE_ERROR, (void)0); + return (inf->last_error = ERROR_MISSING_FILE, (void)0); if (arg && ft_strlen(arg) < 5) - return (inf->last_error = NAME_FILE_ERROR, (void)0); + return (inf->last_error = ERROR_NAME_FILE, (void)0); if (arg && ft_strend_with(arg, FILE_EXTENSION) == false) - return (inf->last_error = EXTENSION_FILE_ERROR, (void)0); + return (inf->last_error = ERROR_EXTENSION_FILE, (void)0); if (arg && inf->cli_ctx.file == NULL) inf->cli_ctx.file = ft_strdup(arg); inf->last_error = NO_ERROR; diff --git a/parsing/map.c b/parsing/map.c new file mode 100644 index 0000000..bea8e7b --- /dev/null +++ b/parsing/map.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/12 08:31:06 by bgoulard #+# #+# */ +/* Updated: 2024/11/12 11:29:13 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +#include "ft_string.h" +#include "ft_optional.h" +#include "ft_optional_types.h" +#include +#include + +// steps: +// 1. load_map into a buffer +// 2. store the map while deleting trailing newlines +void *load_map(void *data) +{ + t_info *info = (t_info *)data; + char *file = NULL; + + file = ft_fd_to_buff(info->map.fd); + info->map.fraw = ft_split(file, '\n'); + + ft_free((void **)&file); + return (info->last_error = ERROR_IMPLEM, NULL); +} + +void *traverse_map(void *data) +{ + t_info *info = (t_info *)data; + + info->last_error = ERROR_IMPLEM; + return (NULL); +} + +void parse_map(t_info *info) +{ + t_optional opt = {OPT_NONE, NULL}; + const t_data_tr_i function_list[] = { + load_map, + traverse_map, + NULL, + }; + + opt.val = info; + opt.pres = OPT_SOME; + info->map.path = info->cli_ctx.file; + if (ft_optional_chain(&opt, function_list) == false) + return (c3_perror(info), (void)0); +} diff --git a/sources/cleanups.c b/sources/cleanups.c index fbf9708..a5ff789 100644 --- a/sources/cleanups.c +++ b/sources/cleanups.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/09 01:11:01 by bgoulard #+# #+# */ -/* Updated: 2024/11/12 05:49:23 by bgoulard ### ########.fr */ +/* Updated: 2024/11/12 11:06:44 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,12 +24,16 @@ static void cleanup_map(t_map *map) { if (map->fd) (close(map->fd), map->fd = 0); + if (map->fraw) + (ft_free_2d((void **)map->fraw), map->fraw = NULL); } static void cleanup_mlx(t_info *info) { - mlx_destroy_window(info->mlx_ptr, info->win_ptr); - mlx_destroy_display(info->mlx_ptr); + if (info->mlx_ptr && info->win_ptr) + mlx_destroy_window(info->mlx_ptr, info->win_ptr); + if (info->mlx_ptr) + mlx_destroy_display(info->mlx_ptr); if (info->mlx_ptr) ft_free((void **)&info->mlx_ptr); } diff --git a/sources/error.c b/sources/error.c index 5cbf0bc..1fe0942 100644 --- a/sources/error.c +++ b/sources/error.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/31 11:09:00 by rparodi #+# #+# */ -/* Updated: 2024/11/10 16:13:25 by rparodi ### ########.fr */ +/* Updated: 2024/11/12 08:45:03 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,6 @@ const char *g_error_message[] = { "no error", "unknown error", - "could not open file", "could not read file", "bad file extension", @@ -30,6 +29,7 @@ const char *g_error_message[] = { "parse error", "cli error", "mlx error", + "not implemented", }; void c3_perror(t_info *info) @@ -47,5 +47,5 @@ void print_error(const char *msg) ft_putstr_fd(RED, STDERR_FILENO); ft_putstr_fd(msg, STDERR_FILENO); ft_putstr_fd(RESET, STDERR_FILENO); - ft_putstr_fd("\n", STDERR_FILENO); + ft_putstr_fd(".\n", STDERR_FILENO); } diff --git a/sources/main.c b/sources/main.c index c08801d..043c1f6 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 16:30:03 by rparodi #+# #+# */ -/* Updated: 2024/11/12 06:20:46 by bgoulard ### ########.fr */ +/* Updated: 2024/11/12 09:50:01 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,23 +36,30 @@ void dump_info(t_info *info) printf("\t\tfd:%d\n", info->map.fd); printf("\t\tsize:\t(x:%d, y:%d)\n", info->map.size.x, info->map.size.y); printf("\t\tplayer_pos:\t(x:%lf, y:%lf)\n", info->map.player_pos.x, info->map.player_pos.y); + for (size_t i = 0; info->map.fraw[i]; i++) + printf("\t\tmap.fraw[%zu]: %s\n", i, info->map.fraw[i]); } void check_err(t_info *info) { if (info->cli_ctx.file == NULL) - return (info->last_error = MISSING_FILE_ERROR, (void)0); + return (info->last_error = ERROR_MISSING_FILE, (void)0); else if (ft_strlen(info->cli_ctx.file) < 5) - return (info->last_error = NAME_FILE_ERROR, (void)0); + return (info->last_error = ERROR_NAME_FILE, (void)0); else if (ft_strend_with(info->cli_ctx.file, FILE_EXTENSION) == false) - return (info->last_error = EXTENSION_FILE_ERROR, (void)0); + return (info->last_error = ERROR_EXTENSION_FILE, (void)0); info->map.fd = open(info->cli_ctx.file, O_RDONLY); if (info->map.fd == -1) - return (info->last_error = OPEN_FILE_ERROR, (void)0); + return (info->last_error = ERROR_OPEN_FILE, (void)0); } void run_cub3d(t_info *info) { + parse_map(info); + if (info->cli_ctx.debug) + dump_info(info); + if (info->last_error != NO_ERROR) + return ; // todo: here // - parse map // - validity check @@ -65,6 +72,10 @@ void run_cub3d(t_info *info) // mlx_loop_end } +/// @brief main function of the cub3d executable +/// @param file_arg the file path to the .cub file +/// @param info the info structure +/// @return false (0) if no error, true (1) if an error int main_cub3d(char *file_arg, t_info *info) { if (info->cli_ctx.help) @@ -74,10 +85,8 @@ int main_cub3d(char *file_arg, t_info *info) check_err(info); if (info->last_error != NO_ERROR) return (c3_perror(info), cleanup_info(info), EXIT_FAILURE); - if (info->cli_ctx.debug) - (dump_info(info), printf("file_arg: %s\n", file_arg)); run_cub3d(info); - return (cleanup_info(info), info->last_error); + return (cleanup_info(info), info->last_error != NO_ERROR); } int main(int argc, char *argv[])