v1 of parsing finished

This commit is contained in:
B.Goulard 2024-11-27 12:01:31 +01:00
parent d586acba8f
commit 8211ff19ea
9 changed files with 121 additions and 37 deletions

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/30 16:30:26 by rparodi #+# #+# */ /* Created: 2024/10/30 16:30:26 by rparodi #+# #+# */
/* Updated: 2024/11/13 06:56:05 by bgoulard ### ########.fr */ /* Updated: 2024/11/27 11:56:07 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,7 +30,7 @@ void cleanup_info(t_info *info);
int c3_options(t_info *info, int argc, char *argv[]); int c3_options(t_info *info, int argc, char *argv[]);
void c3_perror(t_info *info); void c3_perror(t_info *info);
void print_error(const char *msg); void print_error(const char *msg, int errno_state);
void parse_map(t_info *info); void parse_map(t_info *info);
void parse_args(char *arg, t_info *inf); void parse_args(char *arg, t_info *inf);

View file

@ -6,13 +6,14 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/09 01:15:15 by bgoulard #+# #+# */ /* Created: 2024/11/09 01:15:15 by bgoulard #+# #+# */
/* Updated: 2024/11/11 21:34:02 by rparodi ### ########.fr */ /* Updated: 2024/11/27 11:35:39 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef CUB3D_OPTIONS_H #ifndef CUB3D_OPTIONS_H
# define CUB3D_OPTIONS_H # define CUB3D_OPTIONS_H
void c3_set_parse_excl(void *usr_control_struct);
void c3_set_file(void *usr_control_struct, const char *arg); void c3_set_file(void *usr_control_struct, const char *arg);
void c3_set_debug(void *usr_control_struct); void c3_set_debug(void *usr_control_struct);
void c3_set_save(void *usr_control_struct); void c3_set_save(void *usr_control_struct);

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/08 23:55:29 by bgoulard #+# #+# */ /* Created: 2024/11/08 23:55:29 by bgoulard #+# #+# */
/* Updated: 2024/11/18 17:07:15 by bgoulard ### ########.fr */ /* Updated: 2024/11/27 11:50:14 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -62,9 +62,19 @@ typedef struct s_dpoint
typedef enum e_tile typedef enum e_tile
{ {
EMPTY, EMPTY = 0,
WALL WALL = 1,
} t_tile; } t_tile_type;
typedef union u_tile
{
int raw_tile;
struct {
unsigned int tile_visited: 1; // parsing
unsigned int other: 27; // disponible
unsigned int tile_type: 4; // 16 tile types possible
};
} t_tile;
typedef struct s_map typedef struct s_map
{ {
@ -94,6 +104,7 @@ typedef struct s_cli
char *file; char *file;
bool save; bool save;
bool help; bool help;
bool no_graphics;
} t_cli; } t_cli;
// -- error utils // -- error utils
@ -120,6 +131,7 @@ typedef enum e_error
typedef struct s_info typedef struct s_info
{ {
t_error last_error; t_error last_error;
int errno_state;
t_xvar *mlx_ptr; t_xvar *mlx_ptr;
t_win_list *win_ptr; t_win_list *win_ptr;

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/30 16:43:20 by rparodi #+# #+# */ /* Created: 2024/10/30 16:43:20 by rparodi #+# #+# */
/* Updated: 2024/11/09 00:42:02 by bgoulard ### ########.fr */ /* Updated: 2024/11/27 11:57:28 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,5 +19,7 @@
# define RED "\x1b[31m" # define RED "\x1b[31m"
# define BOLD_RED "\033[1;31m" # define BOLD_RED "\033[1;31m"
# define YELLOW "\x1b[33m"
# define BOLD_YELLOW "\033[1;33m"
# define RESET "\x1b[K\x1b[0m" # define RESET "\x1b[K\x1b[0m"
#endif #endif

View file

@ -6,21 +6,25 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 08:31:06 by bgoulard #+# #+# */ /* Created: 2024/11/12 08:31:06 by bgoulard #+# #+# */
/* Updated: 2024/11/18 17:21:09 by bgoulard ### ########.fr */ /* Updated: 2024/11/27 11:53:06 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
// proj
#include "cub3d.h" #include "cub3d.h"
#include "cub3d_struct.h" #include "cub3d_struct.h"
// mlx
#include "mlx_functions.h" #include "mlx_functions.h"
// libft - types
#include "ft_string.h"
#include "ft_optional.h"
#include "ft_optional_types.h"
#include "ft_vector.h"
#include "ft_vector_types.h" #include "ft_vector_types.h"
#include "ft_optional_types.h"
// libft
#include "ft_math.h" #include "ft_math.h"
#include "ft_string.h"
#include "ft_vector.h"
#include "ft_optional.h"
// sys std
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
@ -34,10 +38,12 @@ void *load_file(void *data)
info = (t_info *)data; info = (t_info *)data;
file = ft_fd_to_buff(info->map.fd); file = ft_fd_to_buff(info->map.fd);
if (file == NULL) if (file == NULL)
return (info->last_error = ERROR_READ_FILE, NULL); return (info->errno_state = errno,
info->last_error = ERROR_READ_FILE, NULL);
info->map.fraw = ft_split(file, '\n'); info->map.fraw = ft_split(file, '\n');
if (info->map.fraw == NULL) if (info->map.fraw == NULL)
return (info->last_error = ERROR_MALLOC, NULL); return (info->errno_state = errno,
info->last_error = ERROR_MALLOC, NULL);
ft_free((void **)&file); ft_free((void **)&file);
info->last_error = NO_ERROR; info->last_error = NO_ERROR;
return (info); return (info);
@ -69,13 +75,15 @@ bool load_texture(t_info *info, const char *str, const char **id_str)
{ {
texture.path = ft_strtrim(str + ft_strlen(id_str[i]), " "); texture.path = ft_strtrim(str + ft_strlen(id_str[i]), " ");
if (texture.path == NULL) if (texture.path == NULL)
return (info->last_error = ERROR_MALLOC, false); return (info->errno_state = errno,
info->last_error = ERROR_MALLOC, false);
if (ft_strend_with(texture.path, ".xpm") == false) if (ft_strend_with(texture.path, ".xpm") == false)
return (info->last_error = ERROR_TEXTURE_FORMAT, false); return (info->last_error = ERROR_TEXTURE_FORMAT, false);
texture.img = mlx_xpm_file_to_image(info->mlx_ptr, texture.path, \ texture.img = mlx_xpm_file_to_image(info->mlx_ptr, texture.path, \
&texture.width, &texture.height); &texture.width, &texture.height);
if (texture.img == NULL) if (texture.img == NULL)
return (info->last_error = ERROR_MLX, false); return (info->errno_state = errno,
info->last_error = ERROR_MLX, false);
return (info->map.texture[i] = texture, true); return (info->map.texture[i] = texture, true);
} }
i++; i++;
@ -185,13 +193,13 @@ void str_to_tile(const char *str, t_tile *tile, size_t size)
while (str[i]) while (str[i])
{ {
if (str[i] == '1' || i[str] == ' ') if (str[i] == '1' || i[str] == ' ')
tile[i] = WALL; tile[i].raw_tile = WALL;
else else
tile[i] = EMPTY; tile[i].raw_tile = EMPTY;
i++; i++;
} }
while (i < size) while (i < size)
tile[i++] = WALL; tile[i++].raw_tile = WALL;
} }
t_vector *load_vector(t_map *map) t_vector *load_vector(t_map *map)
@ -220,6 +228,7 @@ void *load_tiles(void *data)
t_info *info; t_info *info;
t_vector *str_map; t_vector *str_map;
size_t i; size_t i;
t_dpoint pos;
info = (t_info *)data; info = (t_info *)data;
str_map = load_vector(&info->map); str_map = load_vector(&info->map);
@ -231,25 +240,68 @@ void *load_tiles(void *data)
return (ft_vec_destroy(&str_map), \ return (ft_vec_destroy(&str_map), \
info->last_error = ERROR_MALLOC, NULL); info->last_error = ERROR_MALLOC, NULL);
i = 0; i = 0;
ft_bzero(&pos, sizeof(t_dpoint));
while (ft_vec_at(str_map, i)) while (ft_vec_at(str_map, i))
{ {
str_to_tile(ft_vec_at(str_map, i), info->map.map + \ str_to_tile(ft_vec_at(str_map, i), info->map.map + \
(i * info->map.size.x), info->map.size.x); (i * info->map.size.x), info->map.size.x);
if (ft_strchrs(ft_vec_at(str_map, i), "SNWE"))
{
if (pos.x != 0 || pos.y != 0 || i == 0)
return (ft_vec_destroy(&str_map), \
info->last_error = ERROR_PARSE, NULL);
pos.x = i + .5;
pos.y = ft_strchrs(ft_vec_at(str_map, i), "SNWE") - ft_vec_at(str_map, i) + .5;
info->player.pos = pos;
}
i++; i++;
} }
return (ft_vec_destroy(&str_map), info); return (ft_vec_destroy(&str_map), info);
} }
t_tile *c3_get_cell(t_tile *map, t_ipoint dimensions, t_ipoint pos)
{
return (map + (pos.y * dimensions.x + pos.x));
}
bool flood_fill(t_tile *tiles, t_ipoint pos, t_ipoint maxs)
{
t_tile *current;
size_t i;
const t_ipoint to_check[] = {
(t_ipoint){pos.x + 1, pos.y},
(t_ipoint){pos.x - 1, pos.y},
(t_ipoint){pos.x, pos.y + 1},
(t_ipoint){pos.x, pos.y - 1},
(t_ipoint){pos.x + 1, pos.y + 1},
(t_ipoint){pos.x - 1, pos.y + 1},
(t_ipoint){pos.x + 1, pos.y - 1},
(t_ipoint){pos.x - 1, pos.y - 1},
};
if (pos.x < 0 || pos.y < 0 || pos.x >= maxs.x || pos.y >= maxs.y)
return (false);
current = c3_get_cell(tiles, maxs, pos);
if (current->tile_visited == true || current->tile_type == WALL)
return (true);
current->tile_visited = true;
i = 0;
while (i != (sizeof(to_check) / sizeof(to_check[0])))
if (flood_fill(tiles, to_check[i++], maxs) == false)
return (false);
return (true);
}
void *traverse_map(void *data) void *traverse_map(void *data)
{ {
t_info *info; t_info *info;
t_ipoint pos_start;
info = (t_info *)data; info = (t_info *)data;
/// TODO: pos_start = (t_ipoint){.x = info->player.pos.x, .y = info->player.pos.y};
/// modify tiles to add bit feild to know if visited instead of alloc bool if (flood_fill(info->map.map, pos_start, info->map.size) == false)
/// return (info->last_error = ERROR_PARSE, NULL);
info->last_error = ERROR_IMPLEM; return (info);
return (NULL);
} }
void parse_map(t_info *info) void parse_map(t_info *info)

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 11:09:00 by rparodi #+# #+# */ /* Created: 2024/10/31 11:09:00 by rparodi #+# #+# */
/* Updated: 2024/11/18 11:31:05 by bgoulard ### ########.fr */ /* Updated: 2024/11/27 12:00:34 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,10 +37,10 @@ void c3_perror(t_info *info)
{ {
if (info->last_error == NO_ERROR) if (info->last_error == NO_ERROR)
return ; return ;
print_error(g_error_message[info->last_error]); print_error(g_error_message[info->last_error], info->errno_state);
} }
void print_error(const char *msg) void print_error(const char *msg, int state)
{ {
ft_putstr_fd(BOLD_RED, STDERR_FILENO); ft_putstr_fd(BOLD_RED, STDERR_FILENO);
ft_putstr_fd("Error:\n", STDERR_FILENO); ft_putstr_fd("Error:\n", STDERR_FILENO);
@ -48,5 +48,13 @@ void print_error(const char *msg)
ft_putstr_fd(RED, STDERR_FILENO); ft_putstr_fd(RED, STDERR_FILENO);
ft_putstr_fd(msg, STDERR_FILENO); ft_putstr_fd(msg, STDERR_FILENO);
ft_putstr_fd(RESET, STDERR_FILENO); ft_putstr_fd(RESET, STDERR_FILENO);
if (state != 0)
{
ft_putstr_fd(YELLOW, STDERR_FILENO);
ft_putstr_fd(" due to:\n", STDERR_FILENO);
ft_putstr_fd(BOLD_YELLOW, STDERR_FILENO);
ft_putstr_fd(ft_strerror(state), STDERR_FILENO);
ft_putstr_fd(RESET, STDERR_FILENO);
}
ft_putstr_fd(".\n", STDERR_FILENO); ft_putstr_fd(".\n", STDERR_FILENO);
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/30 16:30:03 by rparodi #+# #+# */ /* Created: 2024/10/30 16:30:03 by rparodi #+# #+# */
/* Updated: 2024/11/18 14:17:27 by bgoulard ### ########.fr */ /* Updated: 2024/11/27 11:51:42 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -57,7 +57,8 @@ void check_err(t_info *info)
return (info->last_error = ERROR_EXTENSION_FILE, (void)0); return (info->last_error = ERROR_EXTENSION_FILE, (void)0);
info->map.fd = open(info->cli_ctx.file, O_RDONLY); info->map.fd = open(info->cli_ctx.file, O_RDONLY);
if (info->map.fd == -1) if (info->map.fd == -1)
return (info->last_error = ERROR_OPEN_FILE, (void)0); return (info->errno_state = errno,
info->last_error = ERROR_OPEN_FILE, (void)0);
} }
void run_cub3d(t_info *info) void run_cub3d(t_info *info)
@ -69,15 +70,14 @@ void run_cub3d(t_info *info)
dump_info(info); dump_info(info);
if (info->last_error != NO_ERROR) if (info->last_error != NO_ERROR)
return ; return ;
if (info->cli_ctx.no_graphics)
printf("no graphics mode\n");
// todo: here // todo: here
// - validity check // - validity check
printf("launching mlx\n"); printf("launching mlx\n");
mlx_loop(info->mlx_ptr); mlx_loop(info->mlx_ptr);
// - game loop : already loops over the mlx_ptr // - game loop : already loops over the mlx_ptr
// -> get events if key pressed move player + run math to re-draw screen // -> get events if key pressed move player + run math to re-draw screen
// - mlx cleanup : already called in parent function deprecated to call here
// -> previous 'segfault' were due to someone calling cleaup instead of
// mlx_loop_end
} }
/// @brief main function of the cub3d executable /// @brief main function of the cub3d executable

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/09 01:14:09 by bgoulard #+# #+# */ /* Created: 2024/11/09 01:14:09 by bgoulard #+# #+# */
/* Updated: 2024/11/15 09:03:03 by bgoulard ### ########.fr */ /* Updated: 2024/11/27 11:35:08 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,6 +25,7 @@ int c3_options(t_info *info, int argc, char *argv[])
{"debug", 'd', c3_set_debug, 0}, {"debug", 'd', c3_set_debug, 0},
{"save", 's', c3_set_save, 0}, {"save", 's', c3_set_save, 0},
{"help", 'h', c3_print_help, 0}, {"help", 'h', c3_print_help, 0},
{"parse-only", 'p', c3_set_parse_excl, 0},
// add more options here put the implementation in options_impl.c // add more options here put the implementation in options_impl.c
// if you want custom option see ft_args_types.h for more info // if you want custom option see ft_args_types.h for more info
// code example in it. // code example in it.

View file

@ -6,13 +6,21 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/09 01:12:16 by bgoulard #+# #+# */ /* Created: 2024/11/09 01:12:16 by bgoulard #+# #+# */
/* Updated: 2024/11/15 09:08:34 by bgoulard ### ########.fr */ /* Updated: 2024/11/27 11:35:14 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cub3d_struct.h" #include "cub3d_struct.h"
#include "ft_string.h" #include "ft_string.h"
void c3_set_parse_excl(void *usr_control_struct)
{
t_cli *cli_ctx;
cli_ctx = (t_cli *)usr_control_struct;
cli_ctx->no_graphics = true;
}
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; t_cli *cli_ctx;