feat: moved parsing to new 'bgoulard' branch, parsing on texteures added
now parses textures using the ol'optional chain functions.
This commit is contained in:
parent
97f2e348c9
commit
7321db26d9
3 changed files with 174 additions and 8 deletions
77
includes/cub3d_parsing.h
Normal file
77
includes/cub3d_parsing.h
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* cub3d_parsing.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/11 14:41:07 by bgoulard #+# #+# */
|
||||||
|
/* Updated: 2024/11/11 17:43:12 by bgoulard ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef CUB3D_PARSING_H
|
||||||
|
# define CUB3D_PARSING_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
# define C3D_PRS_PLS "SO"
|
||||||
|
# define C3D_PRS_PLW "WE"
|
||||||
|
# define C3D_PRS_PLN "NO"
|
||||||
|
# define C3D_PRS_PLE "EA"
|
||||||
|
|
||||||
|
# define C3D_PRS_WLL_TRAIL ' '
|
||||||
|
# define C3D_PRS_WLL '1'
|
||||||
|
# define C3D_PRS_EMP '0'
|
||||||
|
|
||||||
|
enum e_tile_m {
|
||||||
|
EMPTY,
|
||||||
|
DOOR_OPEN,
|
||||||
|
DOOR_ANIM,
|
||||||
|
DOOR_CLOSE,
|
||||||
|
WALL_TRAIL,
|
||||||
|
WALL,
|
||||||
|
PLAYER_N,
|
||||||
|
PLAYER_S,
|
||||||
|
PLAYER_E,
|
||||||
|
PLAYER_W
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct s_map_truth {
|
||||||
|
enum e_tile_m tile;
|
||||||
|
char chr[2];
|
||||||
|
} t_map_truth;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
#endif /* CUB3D_PARSING_H */
|
||||||
|
|
@ -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/12 11:05:49 by bgoulard ### ########.fr */
|
/* Updated: 2024/11/13 07:15:14 by bgoulard ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -36,6 +36,14 @@ typedef struct s_color
|
||||||
};
|
};
|
||||||
} t_color;
|
} t_color;
|
||||||
|
|
||||||
|
typedef struct s_texture
|
||||||
|
{
|
||||||
|
t_img *img;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
char *path;
|
||||||
|
} t_texture;
|
||||||
|
|
||||||
typedef struct s_point
|
typedef struct s_point
|
||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
|
|
@ -64,7 +72,7 @@ typedef struct s_map
|
||||||
t_ipoint size;
|
t_ipoint size;
|
||||||
t_tile *map;
|
t_tile *map;
|
||||||
char **fraw;
|
char **fraw;
|
||||||
t_img texture[4];
|
t_texture texture[4];
|
||||||
t_color bg_colors[2];
|
t_color bg_colors[2];
|
||||||
} t_map;
|
} t_map;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* 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/12 11:29:13 by bgoulard ### ########.fr */
|
/* Updated: 2024/11/13 07:20:15 by bgoulard ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -15,22 +15,100 @@
|
||||||
#include "ft_string.h"
|
#include "ft_string.h"
|
||||||
#include "ft_optional.h"
|
#include "ft_optional.h"
|
||||||
#include "ft_optional_types.h"
|
#include "ft_optional_types.h"
|
||||||
|
#include "mlx_functions.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
// steps:
|
// steps:
|
||||||
// 1. load_map into a buffer
|
// 1. load_file into a buffer
|
||||||
// 2. store the map while deleting trailing newlines
|
// 2. store the map while deleting trailing newlines
|
||||||
void *load_map(void *data)
|
void *load_file(void *data)
|
||||||
{
|
{
|
||||||
t_info *info = (t_info *)data;
|
t_info *info = (t_info *)data;
|
||||||
char *file = NULL;
|
char *file = NULL;
|
||||||
|
|
||||||
file = ft_fd_to_buff(info->map.fd);
|
file = ft_fd_to_buff(info->map.fd);
|
||||||
|
if (file == NULL)
|
||||||
|
return (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)
|
||||||
|
return (info->last_error = ERROR_MALLOC, NULL);
|
||||||
ft_free((void **)&file);
|
ft_free((void **)&file);
|
||||||
return (info->last_error = ERROR_IMPLEM, NULL);
|
info->last_error = NO_ERROR;
|
||||||
|
return (info);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_identifier(const char *str, const char *id_str[4])
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < 4)
|
||||||
|
{
|
||||||
|
if (ft_strstart_with(str, id_str[i]))
|
||||||
|
return (true);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool load_texture(t_info *info, const char *str)
|
||||||
|
{
|
||||||
|
const char *id_str[4] = {"NO", "SO", "WE", "EA"};
|
||||||
|
size_t i;
|
||||||
|
t_texture texture;
|
||||||
|
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < 4)
|
||||||
|
{
|
||||||
|
if (ft_strstart_with(str, id_str[i]))
|
||||||
|
{
|
||||||
|
texture.path = ft_strtrim(str + ft_strlen(id_str[i]), " ");
|
||||||
|
if (texture.path == NULL)
|
||||||
|
return (info->last_error = ERROR_MALLOC, 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);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *load_textures(void *data)
|
||||||
|
{
|
||||||
|
const char *id_str[4] = {"NO", "SO", "WE", "EA"};
|
||||||
|
t_info *info = (t_info *)data;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
while (info->map.fraw[i])
|
||||||
|
{
|
||||||
|
if (is_identifier(info->map.fraw[i], id_str) && \
|
||||||
|
(load_texture(info, info->map.fraw[i]) == false))
|
||||||
|
return (NULL);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (info);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *load_bgs(void *data)
|
||||||
|
{
|
||||||
|
t_info *info = (t_info *)data;
|
||||||
|
|
||||||
|
info->last_error = ERROR_IMPLEM;
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *load_tiles(void *data)
|
||||||
|
{
|
||||||
|
t_info *info = (t_info *)data;
|
||||||
|
|
||||||
|
info->last_error = ERROR_IMPLEM;
|
||||||
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *traverse_map(void *data)
|
void *traverse_map(void *data)
|
||||||
|
|
@ -45,7 +123,10 @@ void parse_map(t_info *info)
|
||||||
{
|
{
|
||||||
t_optional opt = {OPT_NONE, NULL};
|
t_optional opt = {OPT_NONE, NULL};
|
||||||
const t_data_tr_i function_list[] = {
|
const t_data_tr_i function_list[] = {
|
||||||
load_map,
|
load_file,
|
||||||
|
load_textures,
|
||||||
|
load_bgs,
|
||||||
|
load_tiles,
|
||||||
traverse_map,
|
traverse_map,
|
||||||
NULL,
|
NULL,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue