-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphic_management.c
101 lines (93 loc) · 3.47 KB
/
graphic_management.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* graphic_management.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fatkeski <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/03 21:10:48 by fatkeski #+# #+# */
/* Updated: 2024/05/04 19:46:02 by fatkeski ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/so_long.h"
int exit_game(t_game *game)
{
mlx_destroy_window(game->mlx, game->mlx_window);
free_map(game->map->vector);
exit(0);
return (0);
}
static void init_img_to_mlx(t_game *game)
{
int width;
int height;
(game->img_ptr).floor = mlx_xpm_file_to_image(game->mlx,
"textures/floor.xpm", &width, &height);
if (!((game->img_ptr).floor))
print_error_message("Error: floor image cannot initialized\n", game);
(game->img_ptr).wall = mlx_xpm_file_to_image(game->mlx, "textures/wall.xpm",
&width, &height);
if (!((game->img_ptr).wall))
print_error_message("Error: wall image cannot initialized\n", game);
(game->img_ptr).collectible = mlx_xpm_file_to_image(game->mlx,
"textures/collectible.xpm", &width, &height);
if (!((game->img_ptr).collectible))
print_error_message("Error: collectible image cannot initialized\n",
game);
(game->img_ptr).player = mlx_xpm_file_to_image(game->mlx,
"textures/player.xpm", &width, &height);
if (!((game->img_ptr).player))
print_error_message("Error: player image cannot initialized\n", game);
(game->img_ptr).exit = mlx_xpm_file_to_image(game->mlx, "textures/exit.xpm",
&width, &height);
if (!((game->img_ptr).exit))
print_error_message("Error: exit image cannot initialized\n", game);
}
static void put_image(t_game *game, char c, int x, int y)
{
mlx_put_image_to_window(game->mlx, game->mlx_window, (game->img_ptr).floor,
x, y);
if (c == WALL)
mlx_put_image_to_window(game->mlx, game->mlx_window,
(game->img_ptr).wall, x, y);
else if (c == COLLECTIBLE)
mlx_put_image_to_window(game->mlx, game->mlx_window,
(game->img_ptr).collectible, x, y);
else if (c == PLAYER)
mlx_put_image_to_window(game->mlx, game->mlx_window,
(game->img_ptr).player, x, y);
if (c == EXIT)
mlx_put_image_to_window(game->mlx, game->mlx_window,
(game->img_ptr).exit, x, y);
}
void put_image_to_window(t_game *game)
{
int i;
int j;
i = 0;
while (i < game->map->height)
{
j = 0;
while (j < game->map->width)
{
put_image(game, game->map->vector[i][j], (j * 64), (i * 64));
j++;
}
i++;
}
}
void create_game_interface(t_game *game)
{
game->mlx = mlx_init();
if (!(game->mlx))
print_error_message("Error: mlx cannot initialized\n", game);
game->mlx_window = mlx_new_window((game->mlx), (game->map->width * 64),
(game->map->height * 64), "so_long");
if (!(game->mlx_window))
print_error_message("Error: mlx window cannot initialized\n", game);
init_img_to_mlx(game);
put_image_to_window(game);
mlx_hook(game->mlx_window, KEY_PRESS, 0, key_handler, game);
mlx_hook(game->mlx_window, CROSS, 0, exit_game, game);
mlx_loop(game->mlx);
}