norm: norming of parsing/ and source/cleanup.c
This commit is contained in:
parent
6f2677dde3
commit
d66e1ec615
10 changed files with 421 additions and 340 deletions
|
|
@ -6,13 +6,14 @@
|
||||||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/11/11 14:41:07 by bgoulard #+# #+# */
|
/* Created: 2024/11/11 14:41:07 by bgoulard #+# #+# */
|
||||||
/* Updated: 2024/11/18 11:32:00 by bgoulard ### ########.fr */
|
/* Updated: 2024/12/01 17:53:02 by bgoulard ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef CUB3D_PARSING_H
|
#ifndef CUB3D_PARSING_H
|
||||||
# define CUB3D_PARSING_H
|
# define CUB3D_PARSING_H
|
||||||
|
|
||||||
|
# include "cub3d_struct.h"
|
||||||
# include <stdbool.h>
|
# include <stdbool.h>
|
||||||
|
|
||||||
# define C3D_PRS_PLS "SO"
|
# define C3D_PRS_PLS "SO"
|
||||||
|
|
@ -24,30 +25,14 @@
|
||||||
# define C3D_PRS_WLL '1'
|
# define C3D_PRS_WLL '1'
|
||||||
# define C3D_PRS_EMP '0'
|
# define C3D_PRS_EMP '0'
|
||||||
|
|
||||||
enum e_tile_m
|
t_tile *c3_get_cell(t_tile *map, t_ipoint dimensions, t_ipoint pos);
|
||||||
{
|
bool is_identifier(const char *str, const char **id_str);
|
||||||
EMPTY,
|
|
||||||
DOOR_OPEN,
|
|
||||||
DOOR_ANIM,
|
|
||||||
DOOR_CLOSE,
|
|
||||||
WALL_TRAIL,
|
|
||||||
WALL,
|
|
||||||
PLAYER_N,
|
|
||||||
PLAYER_S,
|
|
||||||
PLAYER_E,
|
|
||||||
PLAYER_W
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct s_map_truth
|
void *load_file(void *data);
|
||||||
{
|
void *load_bgs(void *data);
|
||||||
enum e_tile_m tile;
|
void *load_map(void *data);
|
||||||
char chr[2];
|
void *load_textures(void *data);
|
||||||
} t_map_truth;
|
void *load_tiles(void *data);
|
||||||
|
void *traverse_map(void *data);
|
||||||
typedef struct s_tile
|
|
||||||
{
|
|
||||||
enum e_tile_m tile;
|
|
||||||
bool visited;
|
|
||||||
} t_tile;
|
|
||||||
|
|
||||||
#endif /* CUB3D_PARSING_H */
|
#endif /* CUB3D_PARSING_H */
|
||||||
|
|
|
||||||
79
parsing/load_bgs.c
Normal file
79
parsing/load_bgs.c
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* load_bgs.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/12/01 17:47:37 by bgoulard #+# #+# */
|
||||||
|
/* Updated: 2024/12/01 17:52:28 by bgoulard ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "cub3d_struct.h"
|
||||||
|
#include "cub3d_parsing.h"
|
||||||
|
#include "ft_string.h"
|
||||||
|
|
||||||
|
bool color_from_str(const char *str, t_color *color)
|
||||||
|
{
|
||||||
|
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_bgs(void *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);
|
||||||
|
}
|
||||||
34
parsing/load_file.c
Normal file
34
parsing/load_file.c
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* load_file.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/12/01 17:51:46 by bgoulard #+# #+# */
|
||||||
|
/* Updated: 2024/12/01 17:51:54 by bgoulard ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_string.h"
|
||||||
|
#include "cub3d_struct.h"
|
||||||
|
|
||||||
|
void *load_file(void *data)
|
||||||
|
{
|
||||||
|
t_info *info;
|
||||||
|
char *file;
|
||||||
|
|
||||||
|
file = NULL;
|
||||||
|
info = (t_info *)data;
|
||||||
|
file = ft_fd_to_buff(info->map.fd);
|
||||||
|
if (file == NULL)
|
||||||
|
return (info->errno_state = errno, info->last_error = ERROR_READ_FILE,
|
||||||
|
NULL);
|
||||||
|
info->map.fraw = ft_split(file, '\n');
|
||||||
|
if (info->map.fraw == NULL)
|
||||||
|
return (info->errno_state = errno, info->last_error = ERROR_MALLOC,
|
||||||
|
NULL);
|
||||||
|
ft_free((void **)&file);
|
||||||
|
info->last_error = NO_ERROR;
|
||||||
|
return (info);
|
||||||
|
}
|
||||||
65
parsing/load_textures.c
Normal file
65
parsing/load_textures.c
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* load_textures.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/12/01 17:46:52 by bgoulard #+# #+# */
|
||||||
|
/* Updated: 2024/12/01 17:53:44 by bgoulard ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "cub3d_struct.h"
|
||||||
|
#include "cub3d_parsing.h"
|
||||||
|
|
||||||
|
#include "ft_string.h"
|
||||||
|
|
||||||
|
#include "mlx_functions.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
bool load_texture(t_info *info, const char *str, const char **id_str)
|
||||||
|
{
|
||||||
|
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->errno_state = errno,
|
||||||
|
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->errno_state = errno, info->last_error = ERROR_MLX,
|
||||||
|
false);
|
||||||
|
return (info->map.texture_[i] = texture, true);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
|
||||||
|
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], id_str) == false)
|
||||||
|
return (NULL);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (info);
|
||||||
|
}
|
||||||
110
parsing/load_tiles.c
Normal file
110
parsing/load_tiles.c
Normal file
|
|
@ -0,0 +1,110 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* load_tiles.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/12/01 17:47:15 by bgoulard #+# #+# */
|
||||||
|
/* Updated: 2024/12/01 17:53:53 by bgoulard ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "cub3d_struct.h"
|
||||||
|
#include "cub3d_parsing.h"
|
||||||
|
|
||||||
|
#include "ft_string.h"
|
||||||
|
#include "ft_vector.h"
|
||||||
|
#include "ft_math.h"
|
||||||
|
|
||||||
|
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' || str[i] == ' ')
|
||||||
|
tile[i].tile_type = WALL;
|
||||||
|
else
|
||||||
|
tile[i].tile_type = EMPTY;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (i < size)
|
||||||
|
tile[i++].tile_type = 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
int set_player(t_info *info, int i, t_vector *str_map)
|
||||||
|
{
|
||||||
|
t_dpoint pos;
|
||||||
|
|
||||||
|
ft_bzero(&pos, sizeof(t_dpoint));
|
||||||
|
if (info->player.pos.x != 0 || info->player.pos.y != 0 || i == 0)
|
||||||
|
return (ft_vec_destroy(&str_map), info->last_error = ERROR_PARSE, \
|
||||||
|
EXIT_FAILURE);
|
||||||
|
pos.y = i + .5;
|
||||||
|
pos.x = ft_strchrs(ft_vec_at(str_map, i), "SNWE")
|
||||||
|
- ft_vec_at(str_map, i) + .5;
|
||||||
|
info->player.pos = pos;
|
||||||
|
info->player.pos_i = (t_ipoint){.x = (int)pos.x, .y = (int)pos.y};
|
||||||
|
return (EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
if (ft_strchrs(ft_vec_at(str_map, i), "SNWE") && \
|
||||||
|
set_player(info, i, str_map) != EXIT_SUCCESS)
|
||||||
|
return (NULL);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (ft_vec_destroy(&str_map), info);
|
||||||
|
}
|
||||||
297
parsing/map.c
297
parsing/map.c
|
|
@ -6,312 +6,22 @@
|
||||||
/* 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/29 16:46:52 by bgoulard ### ########.fr */
|
/* Updated: 2024/12/01 17:51:13 by bgoulard ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
// proj
|
// proj
|
||||||
#include "cub3d.h"
|
#include "cub3d.h"
|
||||||
#include "cub3d_struct.h"
|
#include "cub3d_struct.h"
|
||||||
|
#include "cub3d_parsing.h"
|
||||||
// mlx
|
// mlx
|
||||||
#include "mlx_functions.h"
|
|
||||||
// libft - types
|
// libft - types
|
||||||
#include "ft_vector_types.h"
|
|
||||||
#include "ft_optional_types.h"
|
#include "ft_optional_types.h"
|
||||||
// libft
|
// libft
|
||||||
#include "ft_math.h"
|
|
||||||
#include "ft_string.h"
|
|
||||||
#include "ft_vector.h"
|
|
||||||
#include "ft_optional.h"
|
#include "ft_optional.h"
|
||||||
|
|
||||||
// sys std
|
// sys std
|
||||||
#include <fcntl.h>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
void *load_file(void *data)
|
|
||||||
{
|
|
||||||
t_info *info;
|
|
||||||
char *file;
|
|
||||||
|
|
||||||
file = NULL;
|
|
||||||
info = (t_info *)data;
|
|
||||||
file = ft_fd_to_buff(info->map.fd);
|
|
||||||
if (file == NULL)
|
|
||||||
return (info->errno_state = errno,
|
|
||||||
info->last_error = ERROR_READ_FILE, NULL);
|
|
||||||
info->map.fraw = ft_split(file, '\n');
|
|
||||||
if (info->map.fraw == NULL)
|
|
||||||
return (info->errno_state = errno,
|
|
||||||
info->last_error = ERROR_MALLOC, NULL);
|
|
||||||
ft_free((void **)&file);
|
|
||||||
info->last_error = NO_ERROR;
|
|
||||||
return (info);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_identifier(const char *str, const char **id_str)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (id_str[i])
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
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->errno_state = errno,
|
|
||||||
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->errno_state = errno,
|
|
||||||
info->last_error = ERROR_MLX, false);
|
|
||||||
return (info->map.texture_[i] = texture, true);
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (false);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool color_from_str(const char *str, t_color *color)
|
|
||||||
{
|
|
||||||
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], id_str) == false)
|
|
||||||
return (NULL);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (info);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *load_bgs(void *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' || str[i] == ' ')
|
|
||||||
tile[i].tile_type = WALL;
|
|
||||||
else
|
|
||||||
tile[i].tile_type = EMPTY;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
while (i < size)
|
|
||||||
tile[i++].tile_type = 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;
|
|
||||||
t_dpoint pos;
|
|
||||||
|
|
||||||
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;
|
|
||||||
ft_bzero(&pos, sizeof(t_dpoint));
|
|
||||||
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);
|
|
||||||
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.y = i + .5;
|
|
||||||
pos.x = ft_strchrs(ft_vec_at(str_map, i), "SNWE") - ft_vec_at(str_map, i) + .5;
|
|
||||||
info->player.pos = pos;
|
|
||||||
info->player.pos_i = (t_ipoint){.x = (int)pos.x, .y = (int)pos.y};
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (ft_vec_destroy(&str_map), info);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_tile *c3_get_cell(t_tile *map, t_ipoint dimensions, t_ipoint pos)
|
|
||||||
{
|
|
||||||
if (pos.x < 0 || pos.y < 0 || pos.x >= dimensions.x || pos.y >= dimensions.y)
|
|
||||||
return (printf("runtime error: %s:%d (pos:%d,%d on dims:%d,%d)\n",
|
|
||||||
__func__, __LINE__, pos.x, pos.y, dimensions.x, dimensions.y),
|
|
||||||
NULL);
|
|
||||||
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;
|
|
||||||
// printf("dbg (marker %s:%d) pos (%03d : %03d), max (%03d, %03d)\n",
|
|
||||||
// __func__, __LINE__, pos.x, pos.y, maxs.x, maxs.y);
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
// printf("dbg marker %s\n", __func__);
|
|
||||||
t_info *info;
|
|
||||||
t_ipoint pos_start;
|
|
||||||
|
|
||||||
info = (t_info *)data;
|
|
||||||
pos_start = (t_ipoint){.x = info->player.pos.x, .y = info->player.pos.y};
|
|
||||||
if (flood_fill(info->map.map, pos_start, info->map.size) == false)
|
|
||||||
return (info->last_error = ERROR_PARSE, NULL);
|
|
||||||
return (info);
|
|
||||||
}
|
|
||||||
|
|
||||||
void parse_map(t_info *info)
|
void parse_map(t_info *info)
|
||||||
{
|
{
|
||||||
|
|
@ -329,7 +39,6 @@ void parse_map(t_info *info)
|
||||||
info->map.path = info->cli_ctx.file;
|
info->map.path = info->cli_ctx.file;
|
||||||
if (ft_optional_chain(&opt, function_list) == false)
|
if (ft_optional_chain(&opt, function_list) == false)
|
||||||
return (c3_perror(info), (void)0);
|
return (c3_perror(info), (void)0);
|
||||||
info->player.plane = (t_dpoint){.x = 0, .y =
|
info->player.plane = (t_dpoint){.x = 0, .y = 2 * atan(deg2rad(FOV / 2))};
|
||||||
2 * atan(deg2rad(FOV / 2))};
|
|
||||||
info->player.dir = (t_dpoint){.x = -1, .y = 0};
|
info->player.dir = (t_dpoint){.x = -1, .y = 0};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
54
parsing/traverse_map.c
Normal file
54
parsing/traverse_map.c
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* traverse_map.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/12/01 17:49:12 by bgoulard #+# #+# */
|
||||||
|
/* Updated: 2024/12/01 17:54:03 by bgoulard ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "cub3d_struct.h"
|
||||||
|
#include "cub3d_parsing.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
bool flood_fill(t_tile *tiles, t_ipoint pos, t_ipoint maxs)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
t_tile *current;
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
t_info *info;
|
||||||
|
t_ipoint pos_start;
|
||||||
|
|
||||||
|
info = (t_info *)data;
|
||||||
|
pos_start = (t_ipoint){.x = info->player.pos.x, .y = info->player.pos.y};
|
||||||
|
if (flood_fill(info->map.map, pos_start, info->map.size) == false)
|
||||||
|
return (info->last_error = ERROR_PARSE, NULL);
|
||||||
|
return (info);
|
||||||
|
}
|
||||||
42
parsing/utils.c
Normal file
42
parsing/utils.c
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/12/01 17:43:17 by bgoulard #+# #+# */
|
||||||
|
/* Updated: 2024/12/01 17:45:58 by bgoulard ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "cub3d_struct.h"
|
||||||
|
|
||||||
|
#include "ft_string.h"
|
||||||
|
|
||||||
|
#include <X11/X.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
t_tile *c3_get_cell(t_tile *map, t_ipoint dimensions, t_ipoint pos)
|
||||||
|
{
|
||||||
|
if (pos.x < 0 || pos.y < 0 || pos.x >= dimensions.x
|
||||||
|
|| pos.y >= dimensions.y)
|
||||||
|
return (printf("runtime error: %s:%d (pos:%d,%d on dims:%d,%d)\n",
|
||||||
|
__func__, __LINE__, pos.x, pos.y, dimensions.x, dimensions.y),
|
||||||
|
NULL);
|
||||||
|
return (map + (pos.y * dimensions.x + pos.x));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_identifier(const char *str, const char **id_str)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (id_str[i])
|
||||||
|
{
|
||||||
|
if (ft_strstart_with(str, id_str[i]))
|
||||||
|
return (true);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/11/09 01:11:01 by bgoulard #+# #+# */
|
/* Created: 2024/11/09 01:11:01 by bgoulard #+# #+# */
|
||||||
/* Updated: 2024/11/29 17:09:01 by bgoulard ### ########.fr */
|
/* Updated: 2024/12/01 17:24:54 by bgoulard ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -32,19 +32,24 @@ static void cleanup_map(t_map *map)
|
||||||
|
|
||||||
static void cleanup_mlx(t_info *info)
|
static void cleanup_mlx(t_info *info)
|
||||||
{
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
if (!info->mlx_ptr)
|
if (!info->mlx_ptr)
|
||||||
return ;
|
return ;
|
||||||
if (info->win_ptr)
|
if (info->win_ptr)
|
||||||
mlx_destroy_window(info->mlx_ptr, info->win_ptr);
|
mlx_destroy_window(info->mlx_ptr, info->win_ptr);
|
||||||
for (int i = 0; i < 4; i++)
|
while (i < sizeof(info->map.texture_) / sizeof(info->map.texture_[0]))
|
||||||
{
|
{
|
||||||
if (info->map.texture_[i].img)
|
if (info->map.texture_[i].img)
|
||||||
mlx_destroy_image(info->mlx_ptr, info->map.texture_[i].img);
|
mlx_destroy_image(info->mlx_ptr, info->map.texture_[i].img);
|
||||||
if (info->map.texture_[i].path)
|
if (info->map.texture_[i++].path)
|
||||||
ft_free((void **)&info->map.texture_[i].path);
|
ft_free((void **)&info->map.texture_[i - 1].path);
|
||||||
if (info->map.texture[i])
|
|
||||||
mlx_destroy_image(info->mlx_ptr, info->map.texture[i]);
|
|
||||||
}
|
}
|
||||||
|
i = 0;
|
||||||
|
while (i < sizeof(info->map.texture) / sizeof(info->map.texture[0]))
|
||||||
|
if (info->map.texture[i++])
|
||||||
|
mlx_destroy_image(info->mlx_ptr, info->map.texture[i - 1]);
|
||||||
if (info->camera.screen_buff)
|
if (info->camera.screen_buff)
|
||||||
mlx_destroy_image(info->mlx_ptr, info->camera.screen_buff);
|
mlx_destroy_image(info->mlx_ptr, info->camera.screen_buff);
|
||||||
mlx_destroy_display(info->mlx_ptr);
|
mlx_destroy_display(info->mlx_ptr);
|
||||||
|
|
|
||||||
|
|
@ -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/29 17:08:31 by bgoulard ### ########.fr */
|
/* Updated: 2024/12/01 17:27:34 by bgoulard ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -20,10 +20,10 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
void dump_map(t_tile *map, t_ipoint size)
|
void dump_map(t_tile *map, t_ipoint size)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < size.y)
|
while (i < size.y)
|
||||||
|
|
@ -40,12 +40,12 @@ void dump_map(t_tile *map, t_ipoint size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// not normed but we'll take care of this as a niceties at the last
|
// not normed but we'll take care of this as a niceties at the last
|
||||||
// possible moment :)
|
// possible moment :)
|
||||||
void dump_info(t_info *info)
|
void dump_info(t_info *info)
|
||||||
{
|
{
|
||||||
const char *bool_str[2] = {"False", "True"};
|
const char *bool_str[2] = {"False", "True"};
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
printf("t_info:\n");
|
printf("t_info:\n");
|
||||||
|
|
@ -54,7 +54,6 @@ void dump_info(t_info *info)
|
||||||
printf("\t\tpos:\t(x:%lf, y:%lf)\n", info->player.pos.x, info->player.pos.y);
|
printf("\t\tpos:\t(x:%lf, y:%lf)\n", info->player.pos.x, info->player.pos.y);
|
||||||
printf("\t\tdir:\t(x:%lf, y:%lf)\n", info->player.dir.x, info->player.dir.y);
|
printf("\t\tdir:\t(x:%lf, y:%lf)\n", info->player.dir.x, info->player.dir.y);
|
||||||
printf("\t\tplane:\t(x:%lf, y:%lf)\n", info->player.plane.x, info->player.plane.y);
|
printf("\t\tplane:\t(x:%lf, y:%lf)\n", info->player.plane.x, info->player.plane.y);
|
||||||
|
|
||||||
printf("\tcli_ctx:\n");
|
printf("\tcli_ctx:\n");
|
||||||
printf("\t\tfile:\t%s\n", info->cli_ctx.file);
|
printf("\t\tfile:\t%s\n", info->cli_ctx.file);
|
||||||
printf("\t\tdebug:\t%s\n", bool_str[info->cli_ctx.debug]);
|
printf("\t\tdebug:\t%s\n", bool_str[info->cli_ctx.debug]);
|
||||||
|
|
@ -90,8 +89,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->errno_state = errno,
|
return (info->errno_state = errno, info->last_error = ERROR_OPEN_FILE,
|
||||||
info->last_error = ERROR_OPEN_FILE, (void)0);
|
(void)0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void run_cub3d(t_info *info)
|
void run_cub3d(t_info *info)
|
||||||
|
|
@ -103,11 +102,10 @@ 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 ;
|
||||||
info->camera.screen_buff = \
|
info->camera.screen_buff = mlx_new_image(info->mlx_ptr, info->screen_size.x,
|
||||||
mlx_new_image(info->mlx_ptr, info->screen_size.x, info->screen_size.y);
|
info->screen_size.y);
|
||||||
info->camera.img_addr = \
|
info->camera.img_addr = mlx_get_data_addr(info->camera.screen_buff,
|
||||||
mlx_get_data_addr(info->camera.screen_buff, &info->camera.bpp, \
|
&info->camera.bpp, &info->camera.line_len, &info->camera.endian);
|
||||||
&info->camera.line_len, &info->camera.endian);
|
|
||||||
mlx_loop(info->mlx_ptr);
|
mlx_loop(info->mlx_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue