diff --git a/includes/cub3d_parsing.h b/includes/cub3d_parsing.h index 38c94e9..9e424b4 100644 --- a/includes/cub3d_parsing.h +++ b/includes/cub3d_parsing.h @@ -6,14 +6,15 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/11 14:41:07 by bgoulard #+# #+# */ -/* Updated: 2024/11/11 17:43:12 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 11:32:00 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef CUB3D_PARSING_H # define CUB3D_PARSING_H -#include +# include + # define C3D_PRS_PLS "SO" # define C3D_PRS_PLW "WE" # define C3D_PRS_PLN "NO" @@ -23,7 +24,8 @@ # define C3D_PRS_WLL '1' # define C3D_PRS_EMP '0' -enum e_tile_m { +enum e_tile_m +{ EMPTY, DOOR_OPEN, DOOR_ANIM, @@ -36,42 +38,16 @@ enum e_tile_m { PLAYER_W }; -typedef struct s_map_truth { +typedef struct s_map_truth +{ enum e_tile_m tile; char chr[2]; -} t_map_truth; +} t_map_truth; -typedef struct s_tile { +typedef struct s_tile +{ enum e_tile_m tile; bool visited; -} t_tile; - -extern const t_map_truth g_map_table[]; -/* -= { -{EMPTY, C3D_PRS_EMP}, -{WALL_TRAIL, C3D_PRS_WLL_TRAIL}, -{WALL, C3D_PRS_WLL}, -{PLAYER_N, C3D_PRS_PLN}, -{PLAYER_S, C3D_PRS_PLS}, -{PLAYER_E, C3D_PRS_PLE}, -{PLAYER_W, C3D_PRS_PLW}, -}; -*/ - -enum e_identifiers { - UNKNOWN = 0, - TEXTURE_N, - TEXTURE_S, - TEXTURE_E, - TEXTURE_W, - FLOOR, - CEILING, -}; - -typedef struct s_identifiers { - enum e_identifiers id; - char *path; -} t_identifiers; +} t_tile; #endif /* CUB3D_PARSING_H */ diff --git a/includes/cub3d_struct.h b/includes/cub3d_struct.h index ae4be11..969abf8 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/13 07:15:14 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 17:07:15 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,8 @@ # define FILE_EXTENSION ".cub" # define FILE_EXTENSION_LEN 4 +# define BG_CLG 0 +# define BG_FLR 1 // -- graphic utils @@ -109,6 +111,7 @@ typedef enum e_error ERROR_PARSE, ERROR_CLI, ERROR_MLX, + ERROR_TEXTURE_FORMAT, ERROR_IMPLEM, } t_error; diff --git a/libft/src/ft_vector/ft_vec_at.c b/libft/src/ft_vector/ft_vec_at.c index e078789..67f647f 100644 --- a/libft/src/ft_vector/ft_vec_at.c +++ b/libft/src/ft_vector/ft_vec_at.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/27 16:03:56 by bgoulard #+# #+# */ -/* Updated: 2023/12/04 10:38:27 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 16:38:56 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,5 +15,7 @@ // return elem n void *ft_vec_at(t_vector *vec, size_t n) { + if (n >= vec->count) + return (NULL); return (vec->datas[n]); } diff --git a/mlx_layer/mlx_init.c b/mlx_layer/mlx_init.c index 91022d7..57a5a0b 100644 --- a/mlx_layer/mlx_init.c +++ b/mlx_layer/mlx_init.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/11 19:53:42 by rparodi #+# #+# */ -/* Updated: 2024/11/12 06:19:04 by bgoulard ### ########.fr */ +/* Updated: 2024/11/15 09:10:15 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,11 +20,11 @@ int c3_frame_update(void *inf_ptr); +/* move player w keys and call to redraw screen */ int c3_keyhook(int keycode, t_info *info) { if (keycode == XK_Escape || keycode == 65307) return (mlx_loop_end(info->mlx_ptr), EXIT_SUCCESS); - /* move player w keys and call to redraw screen */ return (EXIT_SUCCESS); } @@ -35,8 +35,8 @@ int c3_redcross(t_info *info) t_win_list *c3_init_mlx_window(t_info *info) { - int x; - int y; + int x; + int y; x = 0; y = 0; @@ -53,7 +53,8 @@ int init_mlx_env(t_info *info) if (!info->win_ptr) return (ERROR_MLX); mlx_hook(info->win_ptr, KeyPress, KeyPressMask, c3_keyhook, info); - mlx_hook(info->win_ptr, DestroyNotify, StructureNotifyMask, c3_redcross, info); + mlx_hook(info->win_ptr, DestroyNotify, StructureNotifyMask, c3_redcross, \ + info); mlx_loop_hook(info->mlx_ptr, c3_frame_update, info); return (NO_ERROR); } diff --git a/parsing/map.c b/parsing/map.c index bd752f4..04d829f 100644 --- a/parsing/map.c +++ b/parsing/map.c @@ -6,27 +6,32 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/12 08:31:06 by bgoulard #+# #+# */ -/* Updated: 2024/11/13 07:20:15 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 17:21:09 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" +#include "cub3d_struct.h" +#include "mlx_functions.h" #include "ft_string.h" #include "ft_optional.h" #include "ft_optional_types.h" -#include "mlx_functions.h" +#include "ft_vector.h" +#include "ft_vector_types.h" +#include "ft_math.h" + +#include #include #include -// steps: -// 1. load_file into a buffer -// 2. store the map while deleting trailing newlines -void *load_file(void *data) +void *load_file(void *data) { - t_info *info = (t_info *)data; - char *file = NULL; + t_info *info; + char *file; + file = NULL; + info = (t_info *)data; file = ft_fd_to_buff(info->map.fd); if (file == NULL) return (info->last_error = ERROR_READ_FILE, NULL); @@ -38,12 +43,12 @@ void *load_file(void *data) return (info); } -bool is_identifier(const char *str, const char *id_str[4]) +bool is_identifier(const char *str, const char **id_str) { size_t i; i = 0; - while (i < 4) + while (id_str[i]) { if (ft_strstart_with(str, id_str[i])) return (true); @@ -52,13 +57,11 @@ bool is_identifier(const char *str, const char *id_str[4]) return (false); } -bool load_texture(t_info *info, const char *str) +bool load_texture(t_info *info, const char *str, const char **id_str) { - const char *id_str[4] = {"NO", "SO", "WE", "EA"}; size_t i; t_texture texture; - i = 0; while (i < 4) { @@ -67,62 +70,192 @@ bool load_texture(t_info *info, const char *str) texture.path = ft_strtrim(str + ft_strlen(id_str[i]), " "); if (texture.path == NULL) return (info->last_error = ERROR_MALLOC, false); + if (ft_strend_with(texture.path, ".xpm") == false) + return (info->last_error = ERROR_TEXTURE_FORMAT, false); texture.img = mlx_xpm_file_to_image(info->mlx_ptr, texture.path, \ &texture.width, &texture.height); if (texture.img == NULL) - return (info->last_error = ERROR_MLX, false); - info->map.texture[i] = texture; - return (true); + return (info->last_error = ERROR_MLX, false); + return (info->map.texture[i] = texture, true); } i++; } return (false); } -void *load_textures(void *data) +bool color_from_str(const char *str, t_color *color) { - const char *id_str[4] = {"NO", "SO", "WE", "EA"}; - t_info *info = (t_info *)data; - size_t i; + int col[4]; + char **split; + split = ft_split(str, ','); + if (split == NULL) + return (false); + col[0] = ft_atoi(split[0]); + col[1] = ft_atoi(split[1]); + col[2] = ft_atoi(split[2]); + if (split[3]) + col[3] = ft_atoi(split[3]); + else + col[3] = 255; + if (col[0] < 0 || col[0] > 255 || col[1] < 0 || col[1] > 255 || \ + col[2] < 0 || col[2] > 255 || col[3] < 0 || col[3] > 255) + return (false); + *color = (t_color){.r = col[0], .g = col[1], .b = col[2], .a = col[3]}; + ft_free_2d((void **)split); + return (true); +} + +bool load_bg(t_info *info, const char *line, const char **id_str) +{ + t_color color; + size_t i; + + i = 0; + ft_bzero(&color, sizeof(t_color)); + while (id_str[i]) + { + if (ft_strstart_with(line, id_str[i]) && \ + (color_from_str(line + ft_strlen(id_str[i]), &color) == false || + color.a < 255)) + return (info->last_error = ERROR_PARSE, false); + if (ft_strstart_with(id_str[i], "F ")) + info->map.bg_colors[BG_FLR] = color; + else + info->map.bg_colors[BG_CLG] = color; + i++; + } + if (color.color == 0) + return (info->last_error = ERROR_PARSE, false); + return (true); +} + +void *load_textures(void *data) +{ + const char *id_str[] = {"NO ", "SO ", "WE ", "EA ", NULL}; + t_info *info; + size_t i; + + i = 0; + info = (t_info *)data; while (info->map.fraw[i]) { if (is_identifier(info->map.fraw[i], id_str) && \ - (load_texture(info, info->map.fraw[i]) == false)) - return (NULL); + load_texture(info, info->map.fraw[i], id_str) == false) + return (NULL); i++; } return (info); } -void *load_bgs(void *data) +void *load_bgs(void *data) { - t_info *info = (t_info *)data; + const char *id_str[] = {"F ", "C ", NULL}; + t_info *info; + size_t i; + i = 0; + info = (t_info *)data; + while (info->map.fraw[i]) + { + if (is_identifier(info->map.fraw[i], id_str) && \ + load_bg(info, info->map.fraw[i], id_str) == false) + return (NULL); + i++; + } + return (info); +} + +void *ft_strchrs(const char *str, const char *chrs) +{ + while (*str) + { + if (ft_strchr(chrs, *str)) + return ((void *)str); + str++; + } + return (NULL); +} + +void str_to_tile(const char *str, t_tile *tile, size_t size) +{ + size_t i; + + i = 0; + while (str[i]) + { + if (str[i] == '1' || i[str] == ' ') + tile[i] = WALL; + else + tile[i] = EMPTY; + i++; + } + while (i < size) + tile[i++] = WALL; +} + +t_vector *load_vector(t_map *map) +{ + const char *id_str__all[] = {"NO ", "SO ", "WE ", "EA ", "F ", "C ", NULL}; + t_vector *str_map; + size_t i; + + str_map = ft_vec_new(); + if (str_map == NULL) + return (NULL); + i = 0; + while (map->fraw[i] && + is_identifier(map->fraw[i], id_str__all)) + i++; + while (map->fraw[i + map->size.y]) + { + map->size.x = ft_max(map->size.x, ft_strlen(map->fraw[i])); + ft_vec_add(&str_map, map->fraw[i + map->size.y++]); + } + return (str_map); +} + +void *load_tiles(void *data) +{ + t_info *info; + t_vector *str_map; + size_t i; + + info = (t_info *)data; + str_map = load_vector(&info->map); + if (!str_map) + return (info->last_error = ERROR_MALLOC, NULL); + info->map.map = ft_calloc(sizeof(t_tile), \ + (info->map.size.y * info->map.size.x)); + if (!info->map.map) + return (ft_vec_destroy(&str_map), \ + info->last_error = ERROR_MALLOC, NULL); + i = 0; + while (ft_vec_at(str_map, i)) + { + str_to_tile(ft_vec_at(str_map, i), info->map.map + \ + (i * info->map.size.x), info->map.size.x); + i++; + } + return (ft_vec_destroy(&str_map), info); +} + +void *traverse_map(void *data) +{ + t_info *info; + + info = (t_info *)data; + /// TODO: + /// modify tiles to add bit feild to know if visited instead of alloc bool + /// info->last_error = ERROR_IMPLEM; return (NULL); } -void *load_tiles(void *data) +void parse_map(t_info *info) { - t_info *info = (t_info *)data; - - info->last_error = ERROR_IMPLEM; - return (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[] = { + t_optional opt; + const t_data_tr_i function_list[] = { load_file, load_textures, load_bgs, @@ -131,8 +264,7 @@ void parse_map(t_info *info) NULL, }; - opt.val = info; - opt.pres = OPT_SOME; + opt = (t_optional){.val = info, .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/raycast/frame_update.c b/raycast/frame_update.c index d0f9541..2540ffb 100644 --- a/raycast/frame_update.c +++ b/raycast/frame_update.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/12 06:02:54 by bgoulard #+# #+# */ -/* Updated: 2024/11/12 06:21:09 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 12:12:29 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,6 @@ int c3_frame_update(void *inf_ptr) info = inf_ptr; mlx_clear_window(info->mlx_ptr, info->win_ptr); - ft_putendl_fd("update called\n", STDOUT_FILENO); +// ft_putendl_fd("update called\n", STDOUT_FILENO); return (EXIT_SUCCESS); } diff --git a/sources/cleanups.c b/sources/cleanups.c index a5ff789..1e10f6e 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 11:06:44 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 17:14:06 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,16 +26,25 @@ static void cleanup_map(t_map *map) (close(map->fd), map->fd = 0); if (map->fraw) (ft_free_2d((void **)map->fraw), map->fraw = NULL); + if (map->map) + ft_free((void **)&map->map); } static void cleanup_mlx(t_info *info) { - if (info->mlx_ptr && info->win_ptr) + if (!info->mlx_ptr) + return ; + if (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); + for (int i = 0; i < 4; i++) + { + if (info->map.texture[i].img) + mlx_destroy_image(info->mlx_ptr, info->map.texture[i].img); + if (info->map.texture[i].path) + ft_free((void **)&info->map.texture[i].path); + } + mlx_destroy_display(info->mlx_ptr); + ft_free((void **)&info->mlx_ptr); } void cleanup_info(t_info *info) diff --git a/sources/error.c b/sources/error.c index 1fe0942..0532121 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/12 08:45:03 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 11:31:05 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ #include -const char *g_error_message[] = { +const char *const g_error_message[] = { "no error", "unknown error", "could not open file", @@ -29,6 +29,7 @@ const char *g_error_message[] = { "parse error", "cli error", "mlx error", + "texture format error", "not implemented", }; diff --git a/sources/main.c b/sources/main.c index 0a5fdf6..6d3ebe7 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/13 06:56:50 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 14:17:27 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,13 +18,16 @@ #include #include #include +#include // not normed but we'll take care of this as a niceties at the last // possible moment :) void dump_info(t_info *info) { - const char *bool_str[2] = { "True", "False"}; + const char *bool_str[2] = {"True", "False"}; + size_t i; + i = 0; printf("t_info:\n"); printf("\tcli_ctx:\n"); printf("\t\tfile: %s\n", info->cli_ctx.file); @@ -35,9 +38,13 @@ void dump_info(t_info *info) printf("\t\tpath:%s\n", info->map.path); 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\tplayer_pos:\t(x:%lf, y:%lf)\n", info->map.player_pos.x, \ + info->map.player_pos.y); + while (info->map.fraw[i]) + { printf("\t\tmap.fraw[%zu]: %s\n", i, info->map.fraw[i]); + i++; + } } void check_err(t_info *info) @@ -55,6 +62,8 @@ void check_err(t_info *info) void run_cub3d(t_info *info) { + if (init_mlx_env(info) != NO_ERROR) + return ; parse_map(info); if (info->cli_ctx.debug) dump_info(info); @@ -62,7 +71,7 @@ void run_cub3d(t_info *info) return ; // todo: here // - validity check - init_mlx_env(info); + printf("launching mlx\n"); mlx_loop(info->mlx_ptr); // - game loop : already loops over the mlx_ptr // -> get events if key pressed move player + run math to re-draw screen @@ -75,7 +84,7 @@ void run_cub3d(t_info *info) /// @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) +int main_cub3d(char *file_arg, t_info *info) { if (info->cli_ctx.help) return (cleanup_info(info), EXIT_SUCCESS); diff --git a/sources/options.c b/sources/options.c index ca4808c..68caa7d 100644 --- a/sources/options.c +++ b/sources/options.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/09 01:14:09 by bgoulard #+# #+# */ -/* Updated: 2024/11/09 01:37:41 by bgoulard ### ########.fr */ +/* Updated: 2024/11/15 09:03:03 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,21 +19,21 @@ int c3_options(t_info *info, int argc, char *argv[]) { - int parsed_args; + int parsed_args; const t_opt opts[] = { - {"file", 'f', c3_set_file, OPT_ARG | OPT_EQSIGN | OPT_OTHER}, - {"debug", 'd', c3_set_debug, 0}, - {"save", 's', c3_set_save, 0}, - {"help", 'h', c3_print_help, 0}, + {"file", 'f', c3_set_file, OPT_ARG | OPT_EQSIGN | OPT_OTHER}, + {"debug", 'd', c3_set_debug, 0}, + {"save", 's', c3_set_save, 0}, + {"help", 'h', c3_print_help, 0}, // add more options here put the implementation in options_impl.c // if you want custom option see ft_args_types.h for more info // code example in it. - {NULL, 0, NULL, 0} + {NULL, 0, NULL, 0} }; (void)argc; ft_bzero(info, sizeof(t_info)); - ft_setup_prog((const char * const *)argv); + ft_setup_prog((const char *const *)argv); ft_set_opt_list(opts); parsed_args = ft_parse_args((const char **)argv, &(info->cli_ctx)); return (parsed_args); diff --git a/sources/options_impl.c b/sources/options_impl.c index a01c3ef..3714df1 100644 --- a/sources/options_impl.c +++ b/sources/options_impl.c @@ -6,14 +6,14 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/09 01:12:16 by bgoulard #+# #+# */ -/* Updated: 2024/11/09 01:36:19 by bgoulard ### ########.fr */ +/* Updated: 2024/11/15 09:08:34 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d_struct.h" #include "ft_string.h" -void c3_set_file(void *usr_control_struct, const char *arg) +void c3_set_file(void *usr_control_struct, const char *arg) { t_cli *cli_ctx; @@ -21,7 +21,7 @@ void c3_set_file(void *usr_control_struct, const char *arg) cli_ctx->file = ft_strdup(arg); } -void c3_set_debug(void *usr_control_struct) +void c3_set_debug(void *usr_control_struct) { t_cli *cli_ctx; @@ -29,7 +29,7 @@ void c3_set_debug(void *usr_control_struct) cli_ctx->debug = true; } -void c3_set_save(void *usr_control_struct) +void c3_set_save(void *usr_control_struct) { t_cli *cli_ctx; @@ -37,16 +37,17 @@ void c3_set_save(void *usr_control_struct) cli_ctx->save = true; } -void c3_print_help(void *usr_control_struct) +void c3_print_help(void *usr_control_struct) { - t_cli *cli_ctx; + t_cli *cli_ctx; + const char *help_str = \ + "Usage: cub3d [options] \nOptions:\n" \ + "\t-f, --file : specify the file to load\n" \ + "\t-d, --debug : enable debug mode\n" \ + "\t-s, --save : save the state of the 'game' when closing\n" \ + "\t-h, --help : print this help\n"; cli_ctx = (t_cli *)usr_control_struct; cli_ctx->help = true; - ft_putstr_fd("Usage: cub3d [options] \n", STDOUT_FILENO); - ft_putstr_fd("Options:\n", STDOUT_FILENO); - ft_putstr_fd("\t-f, --file : specify the file to load\n", STDOUT_FILENO); - ft_putstr_fd("\t-d, --debug : enable debug mode\n", STDOUT_FILENO); - ft_putstr_fd("\t-s, --save : save the state of the 'game' when closing\n", STDOUT_FILENO); - ft_putstr_fd("\t-h, --help : print this help\n", STDOUT_FILENO); + ft_putstr_fd(help_str, STDOUT_FILENO); }