/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* map_utils2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/04 15:12:08 by mcolonna #+# #+# */ /* Updated: 2024/10/15 16:27:41 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ #include "map_utils.h" #include "map_mapping.h" #include "map.h" #include /// @brief Read the map description to get the dimensions. /// The map must be at least 1x1. /// /// @param map The .width and .height members will be set. /// @param stream Stream to use. /// @return true if success, false if error. static bool get_map_dimensions(t_map *map, t_stream *stream) { unsigned int x; unsigned int y; map->width = 0; map->height = 0; y = 0; while (stream->str[stream->i]) { x = 0; while (stream->str[stream->i] && stream->str[stream->i] != '\n') { stream->i++; x++; } if (x > map->width) map->width = x; if (stream->str[stream->i] == '\n') stream->i++; y++; } map->height = y; return (map->width > 0 && map->height > 0); } bool get_case(t_map_case *dest, char name) { int i; char tmp[2]; i = -1; while (g_map_mapping[++i].name) { if (name == g_map_mapping[i].name) { *dest = g_map_mapping[i].value; return (true); } } tmp[0] = name; tmp[1] = '\0'; write_err("Character '", tmp, "' unexpected\n", NULL); return (false); } void fill_zeros(void *dest, size_t size) { size_t i; i = -1; while (++i < size) ((char *)dest)[i] = '\0'; } bool read_map_description(t_map *map, t_stream *stream) { t_stream stream2; unsigned int x; unsigned int y; stream2 = *stream; if (!get_map_dimensions(map, &stream2)) return (false); map->cases = malloc((map->width * map->height) * sizeof(t_map_case)); y = -1; fill_zeros(map->cases, (map->width * map->height) * sizeof(t_map_case)); while (++y < map->height) { x = 0; while (stream->str[stream->i] && stream->str[stream->i] != '\n') { if (!get_case(&map->cases[y * map->width + x], stream->str[stream->i])) return (false); stream->i++; x++; } stream->i++; } return (true); }