-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
89 lines (82 loc) · 2.49 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ltombell <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/03 15:11:46 by ltombell #+# #+# */
/* Updated: 2022/11/09 12:24:22 by ltombell ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
int destroywin(t_program *dt)
{
fre_all(dt);
exit(0);
}
int handlenoevents(t_program *dt)
{
static int frame;
frame++;
if (frame == 30000 && dt->win == 0)
animatepg(dt);
if (frame == 60000 && dt->win == 0)
{
randandmove(dt);
frame = 0;
}
if (dt->ep.x == dt->sp.x && dt->ep.x == dt->sp.y)
dt->win = -1;
return (0);
}
int handlekeys(int ks, t_program *dt)
{
mlx_clear_window(dt->mlx, dt->wd);
if (ks == 65307)
{
fre_all(dt);
exit(0);
}
if (ks == 100 && dt->map[(dt->sp.y / TS)][(dt->sp.x / TS) + 1]
!= '1' && dt->win == 0)
go_right(dt);
if (ks == 97 && dt->map[(dt->sp.y / TS)][(dt->sp.x / TS) - 1]
!= '1' && dt->win == 0)
go_left(dt);
if (ks == 119 && dt->map[(dt->sp.y / TS) - 1][(dt->sp.x / TS)]
!= '1' && dt->win == 0)
go_up(dt);
if (ks == 115 && dt->map[(dt->sp.y / TS) + 1][(dt->sp.x / TS)]
!= '1' && dt->win == 0)
go_down(dt);
generate_map(dt);
return (0);
}
int main(int argc, char **argv)
{
t_program dt;
if (argc != 2)
args_error();
if (checkextension(argv[1]) == 0)
wrong_ext();
mapreader(argv[1], &dt);
if (check_map(&dt) == 1)
invalid_map(&dt);
dt.md.y = (count_height(dt.map) * TS) + 16;
dt.md.x = count_base(dt.map) * TS;
if (dt.md.x > 1920 || dt.md.y > 1080)
map_too_big(&dt);
dt.cc = count_coins(dt.map);
dt.mlx = mlx_init();
populate_images(&dt);
populate_font(&dt);
dt.wd = mlx_new_window(dt.mlx, dt.md.x, dt.md.y + TS, "lapenji");
generate_map(&dt);
mlx_loop_hook(dt.mlx, handlenoevents, &dt);
mlx_key_hook(dt.wd, handlekeys, &dt);
mlx_hook(dt.wd, 17, 0, destroywin, &dt);
mlx_loop(dt.mlx);
mlx_destroy_display(dt.mlx);
free(dt.mlx);
}