-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
114 lines (104 loc) · 2.77 KB
/
parse.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mlegendr <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/06 10:52:11 by mlegendr #+# #+# */
/* Updated: 2014/12/08 16:59:52 by mlegendr ### ########.fr */
/* */
/* ************************************************************************** */
#include "gnl/get_next_line.h"
#include <fcntl.h>
#include "wolf3d.h"
static int how_much_col(char *str)
{
int i;
int cp;
i = 0;
cp = 1;
while (str[i])
{
if (str[i] == ' ')
{
++cp;
while (str[i] == ' ')
++i;
}
++i;
}
return (cp);
}
static t_map_info init_tab(char *file)
{
char *line;
t_map_info info;
int i;
int fd;
line = NULL;
if ((fd = open(file, O_RDONLY)) < 1)
ret_error("Open failed.");
info.height = 1;
get_next_line(fd, &line);
info.width = how_much_col(line);
while (get_next_line(fd, &line) > 0)
++info.height;
i = 0;
if (!(info.map = (t_map **)malloc(sizeof(t_map *) * info.height)))
ret_error("Malloc failed.");
while (i < info.height)
{
if (!(info.map[i] = (t_map *)malloc(sizeof(t_map) * info.width)))
ret_error("Malloc failed.");
++i;
}
return (info);
}
static void textures(t_map *map, void *mlx_ptr, int j)
{
map[j].textures.bot = new_mlx_xpm_image_8u(mlx_ptr, "texture/bot.xpm");
map[j].textures.top = new_mlx_xpm_image_8u(mlx_ptr, "texture/top.xpm");
map[j].textures.left = new_mlx_xpm_image_8u(mlx_ptr, "texture/left.xpm");
map[j].textures.right = new_mlx_xpm_image_8u(mlx_ptr, "texture/right.xpm");
}
static void fill_line(char *line, t_map *map, void *mlx_ptr)
{
int i;
int j;
j = -1;
i = 0;
while (line[i])
{
if (line[i] >= '0' && line[i] <= '9')
{
map[++j].wall_type = ft_atoi(&line[i]);
if (map[j].wall_type != 0)
textures(map, mlx_ptr, j);
}
while (line[i] >= '0' && line[i] <= '9')
++i;
while (line[i] == ' ')
++i;
}
}
t_map_info parse(char *file, void *mlx_ptr)
{
char *line;
int fd;
t_map_info info;
int i;
line = NULL;
info = init_tab(file);
if ((fd = open(file, O_RDONLY)) < 1)
ret_error("Open failed.");
i = 0;
while (get_next_line(fd, &line) > 0)
{
fill_line(line, info.map[i], mlx_ptr);
++i;
}
if ((fd = close(fd)) < 0)
ret_error("Close failed.");
return (info);
}