fdf/src/map_init.c
2017-03-03 21:51:09 +02:00

74 lines
No EOL
1.1 KiB
C

#include "fdf.h"
void fill_row(t_list **list, int i, char *row, t_map *map)
{
int j;
char **row_numbers;
t_vec vec3;
j = 0;
vec3.y = i;
row_numbers = ft_strsplit(row, ' ');
while (row_numbers[j])
{
vec3.x = j;
vec3.z = ft_atoi(row_numbers[j]);
ft_lstadd_back(list, &vec3, sizeof(t_vec));
j++;
}
free(row_numbers);
map->width = j;
}
t_list *to_lst_map(char **tab_map, t_map *map)
{
int i;
t_list *lst_map;
lst_map = NULL;
i = 0;
while (tab_map[i])
{
fill_row(&lst_map, i, tab_map[i], map);
i++;
}
return (lst_map);
}
char *to_str_map(char *path)
{
int fd;
fd = open(path, O_RDONLY);
if (fd != -1)
return (ft_read_file(fd));
return (0);
}
int map_heigth(char *str_map)
{
int h;
h = 0;
while (*str_map++)
if (*str_map == '\n')
h++;
return (h);
}
t_map *map_init(char *path)
{
char *str_map;
char **tab_map;
t_map *map;
map = malloc(sizeof(t_map));
str_map = to_str_map(path);
printf("%s\n", str_map);
map->height = map_heigth(str_map);
tab_map = ft_strsplit(str_map, '\n');
map->lst_map = to_lst_map(tab_map, map);
free(str_map);
free_tab(tab_map);
return (map);
}