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