42_solong/map.c

78 lines
2.1 KiB
C
Raw Normal View History

2023-01-05 13:04:29 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 13:51:49 by cchauvet #+# #+# */
2023-01-06 13:37:36 -05:00
/* Updated: 2023/01/06 19:36:36 by cchauvet ### ########.fr */
2023-01-05 13:04:29 -05:00
/* */
/* ************************************************************************** */
#include "solong.h"
char **ft_readfile(char *path)
{
char **map;
int fd;
2023-01-06 13:37:36 -05:00
size_t nb_line;
2023-01-05 13:04:29 -05:00
fd = open(path, O_RDONLY);
nb_line = 1;
map = malloc(sizeof(char *) * 2);
if (map == NULL)
return (NULL);
map[0] = get_next_line(fd);
while (map[nb_line - 1] != NULL)
{
2023-01-06 13:37:36 -05:00
ft_strchr(map[nb_line - 1], '\n')[0] = '\0';
2023-01-05 13:04:29 -05:00
map[nb_line] = get_next_line(fd);
map = ft_tabrealloc(map, nb_line + 1, nb_line + 2);
if (map == NULL)
return (NULL);
nb_line++;
}
return (map);
}
2023-01-06 13:37:36 -05:00
t_map *ft_getmap(char *path)
{
t_map *map;
map = malloc(sizeof(t_map));
if (map == NULL)
return (NULL);
map->patern = ft_readfile(path);
if (ft_map_is_correct(map) == 0)
return (NULL);
return (map);
}
char **ft_get_player_map(t_map map)
{
char **player_map;
ssize_t y;
ssize_t i;
player_map = malloc(RENDER_DISTANCE * 2 + 2);
if (player_map == NULL)
return (NULL);
y = -RENDER_DISTANCE;
while (y < map.y_len - map.player_pos[1] - RENDER_DISTANCE)
{
i = map.player_pos[1] + y;
player_map[y] = ft_strndup(map.patern[i], RENDER_DISTANCE * 2 + 1);
if (player_map[y] == NULL)
{
ft_cancel(player_map, RENDER_DISTANCE - y);
return (NULL);
}
y++;
}
player_map[y] = NULL;
return (player_map);
}