feat: added maps, added readfile to parsing

This commit is contained in:
Baptiste GOULARD 2024-11-12 11:30:30 +01:00
parent 4db733648e
commit e4d2dfda21
19 changed files with 282 additions and 41 deletions

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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[])