-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
124 lines (112 loc) · 3.4 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hgrissen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/04 10:23:28 by hgrissen #+# #+# */
/* Updated: 2021/11/06 15:02:30 by hgrissen ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void draw_others(t_mlx *mlx, char **map)
{
int i;
int j;
i = 0;
while (map[i])
{
j = 0;
while (map[i][j])
{
if (map[i][j] != '1')
rect(mlx, j * TILESIZE, i * TILESIZE, mlx->tex.w);
if (map[i][j] == 'P')
rect(mlx, j * TILESIZE, i * TILESIZE, mlx->tex.p);
else if (map[i][j] == 'C')
rect(mlx, j * TILESIZE, i * TILESIZE, mlx->tex.c);
else if (map[i][j] == 'E')
rect(mlx, j * TILESIZE, i * TILESIZE, mlx->tex.e);
j++;
}
i++;
}
}
void draw_map(t_mlx *mlx, char **map)
{
int i;
int j;
i = 0;
while (map[i])
{
j = 0;
while (map[i][j])
{
if (map[i][j] == '1')
rect(mlx, j * TILESIZE, i * TILESIZE, mlx->tex.s);
else if (map[i][j] != '1')
rect(mlx, j * TILESIZE, i * TILESIZE, mlx->tex.w);
j++;
}
i++;
}
}
void check_tex_err(t_mlx *mlx, int code)
{
if (code == 0)
{
if (!mlx->tex.s.img || !mlx->tex.w.img
|| !mlx->tex.e.img || !mlx->tex.c.img || !mlx->tex.p.img)
exit_error(3);
}
else if (code == 1)
{
if (!mlx->tex.s.addr || !mlx->tex.w.addr
|| !mlx->tex.e.addr ||!mlx->tex.c.addr || !mlx->tex.p.addr)
exit_error(3);
}
}
void get_texture(t_mlx *mlx)
{
int w;
int h;
mlx->tex.c.img = mlx_xpm_file_to_image(mlx->mlx,
"./textures/col.xpm", &w, &h);
mlx->tex.e.img = mlx_xpm_file_to_image(mlx->mlx,
"./textures/exit.xpm", &w, &h);
mlx->tex.p.img = mlx_xpm_file_to_image(mlx->mlx,
"./textures/player.xpm", &w, &h);
mlx->tex.s.img = mlx_xpm_file_to_image(mlx->mlx,
"./textures/grass.xpm", &w, &h);
mlx->tex.w.img = mlx_xpm_file_to_image(mlx->mlx,
"./textures/floor.xpm", &w, &h);
check_tex_err(mlx, 0);
mlx->tex.c.addr = (unsigned int *)mlx_get_data_addr(mlx->tex.c.img,
&mlx->tex.c.bpp, &mlx->tex.c.ll, &mlx->tex.c.end);
mlx->tex.e.addr = (unsigned int *)mlx_get_data_addr(mlx->tex.e.img,
&mlx->tex.e.bpp, &mlx->tex.e.ll, &mlx->tex.e.end);
mlx->tex.p.addr = (unsigned int *)mlx_get_data_addr(mlx->tex.p.img,
&mlx->tex.p.bpp, &mlx->tex.p.ll, &mlx->tex.p.end);
mlx->tex.w.addr = (unsigned int *)mlx_get_data_addr(mlx->tex.w.img,
&mlx->tex.w.bpp, &mlx->tex.w.ll, &mlx->tex.w.end);
mlx->tex.s.addr = (unsigned int *)mlx_get_data_addr(mlx->tex.s.img,
&mlx->tex.s.bpp, &mlx->tex.s.ll, &mlx->tex.s.end);
}
int main(int ac, char **av)
{
t_mlx *mlx;
char **map;
if (ac < 2)
return (exit_error(0));
map = get_map(av + 1);
resolution(map);
mlx = init_canvas();
mlx->av = av;
get_texture(mlx);
check_tex_err(mlx, 1);
init_player(mlx, map);
starttime = get_time();
mlx_loop_hook(mlx->mlx, update, mlx);
mlx_loop(mlx);
}