forked from AndreIglesias/fdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.c
More file actions
126 lines (115 loc) · 2.54 KB
/
map.c
File metadata and controls
126 lines (115 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ksoto <ksoto@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/21 22:35:32 by ciglesia #+# #+# */
/* Updated: 2021/10/14 15:25:14 by ksoto ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
#include <stdio.h>
void ft_extract_color(t_fdf *fdf, char *str, int y, int x)
{
int i;
int j;
int len;
char *tmp;
i = 0;
while (str[i])
{
if (str[i] == ',')
{
while (str[i] == ',' || str[i] == '0' || str[i] == 'x')
i++;
len = i;
while (str[len])
len++;
tmp = ft_memalloc(sizeof(char) * (len - i + 1));
j = 0;
while (str[i])
tmp[j++] = str[i++];
tmp[j] = '\0';
fdf->map[y][x].color = ft_atoi_base(tmp, 16);
free(tmp);
break;
}
i++;
}
}
static int extract_line(t_fdf *fdf, char *line, int y)
{
char **tab;
int i;
tab = ft_strsplit(line, ' ');
i = 0;
while (i < fdf->mapx)
{
fdf->map[y][i].color = -1;
ft_extract_color(fdf, tab[i], y, i);
fdf->map[y][i].z = ft_atoi(tab[i]);
free(tab[i]);
i++;
}
free(tab);
return (1);
}
void free_map(void)
{
int i;
t_fdf *fdf;
fdf = ft_fdf(NULL);
i = 0;
while (i < fdf->mapy)
{
if (fdf->map[i])
free(fdf->map[i++]);
}
free(fdf->map);
}
static void extract_lines(t_fdf *fdf, char *line, int fd)
{
int y;
y = 0;
while (get_next_line(fd, &line) > 0)
{
if (!extract_line(fdf, line, y))
{
get_next_line(-1, &line);
free(line);
free_map();
close(fd);
exit(1);
}
free(line);
y++;
}
free(line);
close(fd);
}
void wireframe(char *name)
{
int fd;
int size;
t_fdf *fdf;
fdf = ft_fdf(NULL);
fd = open(name, O_RDONLY);
if (fd < 0)
exit(1);
fdf->map = ft_memalloc(sizeof(t_x *) * (fdf->mapy + 1));
size = 0;
while (size < fdf->mapy)
{
fdf->map[size++] = ft_memalloc(sizeof(t_x) * (fdf->mapx + 1));
if (!fdf->map[size - 1])
{
free_map();
exit(1);
}
}
if (!fdf->map)
exit(1);
extract_lines(fdf, NULL, fd);
}