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

@ -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"

View file

@ -6,7 +6,7 @@
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 \

View file

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

View file

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

View file

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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

23
maps/bad_perm.cub Normal file
View file

@ -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

View file

23
maps/valid_01.cub Normal file
View file

@ -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

View file

@ -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);

View file

@ -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;

58
parsing/map.c Normal file
View file

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdio.h>
#include <sys/types.h>
// 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);
}

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,11 +24,15 @@ 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)
{
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[])