/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* map_create.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/23 12:21:13 by whaffman #+# #+# */ /* Updated: 2025/05/28 13:20:00 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" static int map_allocate(t_game **game) { (*game)->map = malloc(sizeof(t_map)); if (!(*game)->map) { perror("Error allocating memory for (*game)->map"); return (FAILURE); } ft_memset((*game)->map, 0, sizeof(t_map)); (*game)->map->sprite_lib = malloc(sizeof(t_sprite_lib) * 26); if (!(*game)->map->sprite_lib) { perror("Error allocating memory for (*game)->map->sprite_lib"); free((*game)->map); return (FAILURE); } ft_memset((*game)->map->sprite_lib, 0, sizeof(t_sprite_lib) * 26); return (SUCCESS); } static int map_parse_and_copy(t_game **game, const char *mapfile, t_tile ***out_grid) { t_tile **grid; if (!parse_args(mapfile, (*game))) return (FAILURE); grid = copy_map((*game)->map->grid, (*game)->map->width, (*game)->map->height); if (!grid) { perror("Error copying (*game)->map"); free((*game)->map); return (FAILURE); } *out_grid = grid; return (SUCCESS); } static int map_validate_and_finalize(t_game **game, t_tile **grid) { if (!enclosed_map((*game)->map)) { ft_putendl_fd("Map is not enclosed", STDERR_FILENO); grid_free(grid, (*game)->map->height); map_free((*game)->map); return (FAILURE); } grid_free((*game)->map->grid, (*game)->map->height); (*game)->map->grid = grid; return (SUCCESS); } int map_create(t_game **game, const char *mapfile) { t_tile **grid; if (!(*game)) return (FAILURE); if (map_allocate(game) == FAILURE) return (FAILURE); if (map_parse_and_copy(game, mapfile, &grid) == FAILURE) return (FAILURE); return (map_validate_and_finalize(game, grid)); }