Fix: check on background + check for metainfo in the map + fix tilemap size calculation

This commit is contained in:
B.Goulard 2024-12-19 22:35:37 +01:00
parent 7f1091b721
commit 820b5f3bcc
5 changed files with 72 additions and 45 deletions

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/28 13:53:54 by bgoulard #+# #+# */ /* Created: 2024/11/28 13:53:54 by bgoulard #+# #+# */
/* Updated: 2024/12/17 17:21:45 by bgoulard ### ########.fr */ /* Updated: 2024/12/19 22:29:11 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -120,6 +120,7 @@ typedef struct s_cli
// -- error utils // -- error utils
// keep ERROR_IMPLEM as last (used to get enum size)
typedef enum e_error typedef enum e_error
{ {
NO_ERROR = 0, NO_ERROR = 0,
@ -133,7 +134,9 @@ typedef enum e_error
ERROR_PARSE, ERROR_PARSE,
ERROR_MAP_OPEN, ERROR_MAP_OPEN,
ERROR_PARSE_BG_COLOR_FORMAT, ERROR_PARSE_BG_COLOR_FORMAT,
ERROR_PARSE_NO_BG_COLOR,
ERROR_PARSE_ALREADY_SET, ERROR_PARSE_ALREADY_SET,
ERROR_PARSE_META_IN_MAP,
ERROR_CLI, ERROR_CLI,
ERROR_MLX, ERROR_MLX,
ERROR_TEXTURE_FORMAT, ERROR_TEXTURE_FORMAT,

View file

@ -1,20 +1,23 @@
SO ./textures/b.xpm SO ./textures/chiseled_tuff.xpm
NO ./textures/wasteland_32.xpm NO ./textures/chiseled_tuff_bricks.xpm
WE ./textures/sandy_32.xpm WE ./textures/chiseled_stone_bricks.xpm
EA ./textures/cobblestone_32.xpm EA ./textures/chiseled_tuff_bricks_top.xpm
F 0,255,0 F 0,255,0
C 0,123,255 C 0,255,255
11111111111111111
10000000000000001
11111111111111111
11111111111111111111 11111111111111111111
10000011000001010101 10000011000001010101
10000011000000000001 10000011000000000001
10111111111111000101 10111111111111000101
1011 1000000 1011 1000001
10111111 11000100 10111111 11000101
10000011111110000000 10000011111110000001
100000000000000N0000 100000000000000N0001
11111111111111111111 11111111111111111111

View file

@ -6,11 +6,12 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/01 17:47:37 by bgoulard #+# #+# */ /* Created: 2024/12/01 17:47:37 by bgoulard #+# #+# */
/* Updated: 2024/12/16 09:36:28 by bgoulard ### ########.fr */ /* Updated: 2024/12/19 22:10:37 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_addons.h" #include "ft_addons.h"
#include "cub3d.h"
#include "cub3d_struct.h" #include "cub3d_struct.h"
#include "cub3d_parsing.h" #include "cub3d_parsing.h"
#include "ft_string.h" #include "ft_string.h"
@ -43,7 +44,7 @@ bool color_from_str(const char *str, t_color *color)
return (true); return (true);
} }
bool load_bg(t_info *info, const char *line, const char **id_str) bool load_bg(t_info *info, const char *line, const char **id_str, bool *tpl)
{ {
t_color color; t_color color;
size_t id_idx; size_t id_idx;
@ -60,6 +61,7 @@ bool load_bg(t_info *info, const char *line, const char **id_str)
info->map.bg_colors[BG_SKY] = color; info->map.bg_colors[BG_SKY] = color;
else else
return (info->last_error = ERROR_PARSE_ALREADY_SET, false); return (info->last_error = ERROR_PARSE_ALREADY_SET, false);
tpl[id_idx] = true;
return (true); return (true);
} }
@ -68,15 +70,19 @@ void *load_bgs(void *data)
const char *id_str[] = {[BG_FLR] = "F ", [BG_SKY] = "C ", [2] = NULL}; const char *id_str[] = {[BG_FLR] = "F ", [BG_SKY] = "C ", [2] = NULL};
t_info *info; t_info *info;
size_t i; size_t i;
bool tuple[2];
i = 0; i = 0;
info = (t_info *)data; info = (t_info *)data;
ft_bzero(tuple, sizeof(bool) * 2);
while (info->map.fraw[i]) while (info->map.fraw[i])
{ {
if (is_identifier(info->map.fraw[i], id_str) && \ if (is_identifier(info->map.fraw[i], id_str) && \
load_bg(info, info->map.fraw[i], id_str) == false) load_bg(info, info->map.fraw[i], id_str, tuple) == false)
return (NULL); return (NULL);
i++; i++;
} }
if (tuple[BG_FLR] == false || tuple[BG_SKY] == false)
return (sv_errno(info, ERROR_PARSE_NO_BG_COLOR), NULL);
return (info); return (info);
} }

View file

@ -6,10 +6,11 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/01 17:47:15 by bgoulard #+# #+# */ /* Created: 2024/12/01 17:47:15 by bgoulard #+# #+# */
/* Updated: 2024/12/16 09:33:53 by bgoulard ### ########.fr */ /* Updated: 2024/12/19 22:32:16 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cub3d.h"
#include "cub3d_struct.h" #include "cub3d_struct.h"
#include "cub3d_parsing.h" #include "cub3d_parsing.h"
@ -30,7 +31,7 @@ void *ft_strchrs(const char *str, const char *chrs)
return (NULL); return (NULL);
} }
void str_to_tile(const char *str, t_tile *tile, size_t size) int str_to_tile(const char *str, t_tile *tile, size_t size)
{ {
size_t i; size_t i;
@ -39,12 +40,15 @@ void str_to_tile(const char *str, t_tile *tile, size_t size)
{ {
if (str[i] == '1' || str[i] == ' ') if (str[i] == '1' || str[i] == ' ')
tile[i].tile_type = WALL; tile[i].tile_type = WALL;
else if (!ft_strchr("0NSWE", str[i]))
return (-1);
else else
tile[i].tile_type = EMPTY; tile[i].tile_type = EMPTY;
i++; i++;
} }
while (i < size) while (i < size)
tile[i++].tile_type = WALL; tile[i++].tile_type = WALL;
return (0);
} }
t_vector *load_vector(t_map *map) t_vector *load_vector(t_map *map)
@ -52,8 +56,10 @@ t_vector *load_vector(t_map *map)
const char *id_str__all[] = {"NO ", "SO ", "WE ", "EA ", "F ", "C ", NULL}; const char *id_str__all[] = {"NO ", "SO ", "WE ", "EA ", "F ", "C ", NULL};
t_vector *str_map; t_vector *str_map;
size_t i; size_t i;
size_t max;
str_map = ft_vec_new(); str_map = ft_vec_new();
max = 0;
if (str_map == NULL) if (str_map == NULL)
return (NULL); return (NULL);
i = 0; i = 0;
@ -61,9 +67,10 @@ t_vector *load_vector(t_map *map)
i++; i++;
while (map->fraw[i + map->size.y]) while (map->fraw[i + map->size.y])
{ {
map->size.x = ft_max(map->size.x, ft_strlen(map->fraw[i])); max = ft_max(max, ft_strlen(map->fraw[i + map->size.y]));
ft_vec_add(&str_map, map->fraw[i + map->size.y++]); ft_vec_add(&str_map, map->fraw[i + map->size.y++]);
} }
map->size.x = max;
return (str_map); return (str_map);
} }
@ -95,6 +102,8 @@ int set_player(t_info *info, int i, t_vector *str_map)
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }
#define EPMIM ERROR_PARSE_META_IN_MAP
void *load_tiles(void *data) void *load_tiles(void *data)
{ {
t_info *info; t_info *info;
@ -104,17 +113,17 @@ void *load_tiles(void *data)
info = (t_info *)data; info = (t_info *)data;
str_map = load_vector(&info->map); str_map = load_vector(&info->map);
if (!str_map) if (!str_map)
return (info->last_error = ERROR_MALLOC, NULL); return (sv_errno(info, ERROR_MALLOC), NULL);
info->map.map = ft_calloc(sizeof(t_tile), (info->map.size.y info->map.map = ft_calloc(sizeof(t_tile), (info->map.size.y
* info->map.size.x)); * info->map.size.x));
if (!info->map.map) if (!info->map.map)
return (ft_vec_destroy(&str_map), info->last_error = ERROR_MALLOC, return (ft_vec_destroy(&str_map), sv_errno(info, ERROR_MALLOC), NULL);
NULL);
i = 0; i = 0;
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 + (i if (str_to_tile(ft_vec_at(str_map, i), info->map.map + (i \
* info->map.size.x), info->map.size.x); * info->map.size.x), info->map.size.x) == -1)
return (ft_vec_destroy(&str_map), sv_errno(info, EPMIM), NULL);
if (ft_strchrs(ft_vec_at(str_map, i), "SNWE") && \ if (ft_strchrs(ft_vec_at(str_map, i), "SNWE") && \
set_player(info, i, str_map) != EXIT_SUCCESS) set_player(info, i, str_map) != EXIT_SUCCESS)
return (NULL); return (NULL);

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/12/17 17:06:42 by bgoulard ### ########.fr */ /* Updated: 2024/12/19 22:29:32 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,36 +20,42 @@
const char **get_error_message(void) const char **get_error_message(void)
{ {
static const char *error_messages[] = { static const char *err_msg[ERROR_IMPLEM + 1] = { NULL };
[NO_ERROR] = "no error",
[ERROR_UNKNOWN] = "unknown error",
[ERROR_OPEN_FILE] = "could not open file",
[ERROR_READ_FILE] = "could not read file",
[ERROR_EXTENSION_FILE] = "bad file extension",
[ERROR_NAME_FILE] = "invalid file name",
[ERROR_MISSING_FILE] = "missing file",
[ERROR_MALLOC] = "malloc error",
[ERROR_PARSE] = "parse error",
[ERROR_MAP_OPEN] = "map open error",
[ERROR_PARSE_BG_COLOR_FORMAT] = "bad format for background color",
[ERROR_PARSE_ALREADY_SET] = "variable was set multiple times",
[ERROR_CLI] = "cli error",
[ERROR_MLX] = "mlx error",
[ERROR_TEXTURE_FORMAT] = "texture format error",
[ERROR_IMPLEM] = "not implemented",
[ERROR_TEXTURE_MISSING] = "texture missing",
};
return (error_messages); err_msg[NO_ERROR] = "no error";
err_msg[ERROR_UNKNOWN] = "unknown error";
err_msg[ERROR_OPEN_FILE] = "could not open file";
err_msg[ERROR_READ_FILE] = "could not read file";
err_msg[ERROR_EXTENSION_FILE] = "bad file extension";
err_msg[ERROR_NAME_FILE] = "invalid file name";
err_msg[ERROR_MISSING_FILE] = "missing file";
err_msg[ERROR_MALLOC] = "malloc error";
err_msg[ERROR_PARSE] = "parse error";
err_msg[ERROR_MAP_OPEN] = "map open error";
err_msg[ERROR_PARSE_BG_COLOR_FORMAT] = "bad format for background color";
err_msg[ERROR_PARSE_ALREADY_SET] = "variable was set multiple times";
err_msg[ERROR_CLI] = "cli error";
err_msg[ERROR_MLX] = "mlx error";
err_msg[ERROR_TEXTURE_FORMAT] = "texture format error";
err_msg[ERROR_IMPLEM] = "not implemented";
err_msg[ERROR_TEXTURE_MISSING] = "texture missing";
err_msg[ERROR_PARSE_NO_BG_COLOR] = "no background color provided";
err_msg[ERROR_PARSE_META_IN_MAP] = "meta data in map (should be above)";
return (err_msg);
} }
void c3_perror(t_info *info) void c3_perror(t_info *info)
{ {
const char **errs_msg = get_error_message(); const char **errs_msg = get_error_message();
const char *msg;
if (info->last_error == NO_ERROR) if (info->last_error == NO_ERROR)
return ; return ;
print_error(errs_msg[info->last_error], info->errno_state); msg = errs_msg[info->last_error];
if (!msg)
msg = errs_msg[ERROR_UNKNOWN];
print_error(msg, info->errno_state);
} }
void print_error(const char *msg, int state) void print_error(const char *msg, int state)