-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphic.c
102 lines (94 loc) · 2.89 KB
/
graphic.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* graphic.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wboutzou <wboutzou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/25 19:16:20 by wboutzou #+# #+# */
/* Updated: 2022/06/26 00:34:58 by wboutzou ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void draw(t_game *game, int i, int j)
{
if (game->map[i][j] == '1')
mlx_put_image_to_window(game->mlx \
, game->mlx_win, game->wall, j * 50, i * 50);
else if (game->map[i][j] == '0')
mlx_put_image_to_window(game->mlx \
, game->mlx_win, game->o, j * 50, i * 50);
else if (game->map[i][j] == 'P')
{
mlx_put_image_to_window(game->mlx \
, game->mlx_win, game->p, j * 50, i * 50);
game->pos.player_y = i;
game->pos.player_x = j;
}
else if (game->map[i][j] == 'E')
mlx_put_image_to_window(game->mlx \
, game->mlx_win, game->e, j * 50, i * 50);
else if (game->map[i][j] == 'C')
mlx_put_image_to_window(game->mlx \
, game->mlx_win, game->c, j * 50, i * 50);
}
void render(t_game *game)
{
int i;
int j;
i = 0;
j = 0;
while (i < game->height)
{
j = 0;
while (j < game->width)
{
draw(game, i, j);
j++;
}
i++;
}
}
void move(t_game *game, int step_y, int step_x)
{
if (game->map[game->pos.player_y + step_y] \
[game->pos.player_x + step_x] != '1')
{
check_move(game, step_y, step_x);
game->pos.player_x = game->pos.player_x + step_x;
game->pos.player_y = game->pos.player_y + step_y;
}
}
int key_hook(int keycode, t_game *game)
{
if (keycode == 53 || keycode < 0)
exit(1);
else if (keycode == 13)
move(game, -1, 0);
else if (keycode == 1)
move(game, 1, 0);
else if (keycode == 0)
move(game, 0, -1);
else if (keycode == 2)
move(game, 0, 1);
render(game);
return (0);
}
void window(t_game *game)
{
int size;
size = 50;
game->mlx = mlx_init();
game->mlx_win = mlx_new_window(game->mlx \
, 50 * game->width, 50 * game->height, "so long");
game->e = mlx_xpm_file_to_image(game->mlx \
, "./assets/E/1.xpm", &size, &size);
game->p = mlx_xpm_file_to_image(game->mlx \
, "./assets/player/2.xpm", &size, &size);
game->c = mlx_xpm_file_to_image(game->mlx \
, "./assets/P/1.xpm", &size, &size);
game->o = mlx_xpm_file_to_image(game->mlx \
, "./assets/O/1.xpm", &size, &size);
game->wall = mlx_xpm_file_to_image(game->mlx \
, "./assets/wall/1.xpm", &size, &size);
}