-
Notifications
You must be signed in to change notification settings - Fork 0
/
so_long.c
69 lines (62 loc) · 2.14 KB
/
so_long.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* so_long.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbiodies <rbiodies@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/20 13:03:06 by rbiodies #+# #+# */
/* Updated: 2021/11/25 13:11:57 by rbiodies ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static void *init_img_util(t_mlx *mlx, char *filename)
{
void *result;
int width;
int height;
width = 0;
height = 0;
result = mlx_xpm_file_to_image(mlx->mlx, filename, &width, &height);
if (height == 0 || width == 0)
{
perror("img");
exit (EXIT_FAILURE);
}
return (result);
}
static void init_img(t_mlx *mlx, t_img *img)
{
img->player = init_img_util(mlx, "img/player1.xpm");
img->collect = init_img_util(mlx, "img/collect1.xpm");
img->road = init_img_util(mlx, "img/road.xpm");
img->wall = init_img_util(mlx, "img/wall.xpm");
img->exit = init_img_util(mlx, "img/exit.xpm");
}
static int program_close(void)
{
write(1, "\033[0;34mExit program!\033[0\n", 24);
exit(EXIT_SUCCESS);
}
void so_long(char *filename)
{
t_map map;
t_mlx mlx;
t_img img;
int width;
int height;
parsing_map(&map, filename);
mlx.mlx = mlx_init();
width = map.width * 32;
height = map.height * 32;
mlx.win = mlx_new_window(mlx.mlx, width, height, "so_long");
init_img(&mlx, &img);
paint_map(&mlx, &map, &img);
map.mlx = mlx;
map.img = img;
mlx_hook(mlx.win, X_EVENT_KEY_PRESS, X_NO_EVENT_MASK, &key_press, &map);
mlx_hook(\
mlx.win, X_EVENT_DESTROY_NOTIFY, X_NO_EVENT_MASK, &program_close, NULL);
mlx_loop(mlx.mlx);
mlx_clear_window(mlx.mlx, mlx.win);
}