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,11 +6,12 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 "cub3d.h"
#include "cub3d_struct.h"
#include "cub3d_parsing.h"
#include "ft_string.h"
@ -43,7 +44,7 @@ bool color_from_str(const char *str, t_color *color)
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;
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;
else
return (info->last_error = ERROR_PARSE_ALREADY_SET, false);
tpl[id_idx] = true;
return (true);
}
@ -68,15 +70,19 @@ void *load_bgs(void *data)
const char *id_str[] = {[BG_FLR] = "F ", [BG_SKY] = "C ", [2] = NULL};
t_info *info;
size_t i;
bool tuple[2];
i = 0;
info = (t_info *)data;
ft_bzero(tuple, sizeof(bool) * 2);
while (info->map.fraw[i])
{
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);
i++;
}
if (tuple[BG_FLR] == false || tuple[BG_SKY] == false)
return (sv_errno(info, ERROR_PARSE_NO_BG_COLOR), NULL);
return (info);
}

View file

@ -6,10 +6,11 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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_parsing.h"
@ -30,7 +31,7 @@ void *ft_strchrs(const char *str, const char *chrs)
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;
@ -39,12 +40,15 @@ void str_to_tile(const char *str, t_tile *tile, size_t size)
{
if (str[i] == '1' || str[i] == ' ')
tile[i].tile_type = WALL;
else if (!ft_strchr("0NSWE", str[i]))
return (-1);
else
tile[i].tile_type = EMPTY;
i++;
}
while (i < size)
tile[i++].tile_type = WALL;
return (0);
}
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};
t_vector *str_map;
size_t i;
size_t max;
str_map = ft_vec_new();
max = 0;
if (str_map == NULL)
return (NULL);
i = 0;
@ -61,9 +67,10 @@ t_vector *load_vector(t_map *map)
i++;
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++]);
}
map->size.x = max;
return (str_map);
}
@ -95,6 +102,8 @@ int set_player(t_info *info, int i, t_vector *str_map)
return (EXIT_SUCCESS);
}
#define EPMIM ERROR_PARSE_META_IN_MAP
void *load_tiles(void *data)
{
t_info *info;
@ -104,17 +113,17 @@ void *load_tiles(void *data)
info = (t_info *)data;
str_map = load_vector(&info->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.size.x));
if (!info->map.map)
return (ft_vec_destroy(&str_map), info->last_error = ERROR_MALLOC,
NULL);
return (ft_vec_destroy(&str_map), sv_errno(info, 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 (str_to_tile(ft_vec_at(str_map, i), info->map.map + (i \
* 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") && \
set_player(info, i, str_map) != EXIT_SUCCESS)
return (NULL);