-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_handler.c
116 lines (106 loc) · 3.21 KB
/
map_handler.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_handler.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: danimart <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/07 13:13:58 by danimart #+# #+# */
/* Updated: 2022/05/10 14:18:14 by danimart ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
t_map cache_images(t_map map, int d)
{
int z;
z = 0;
map.mlx = mlx_init();
if (map.mlx != NULL)
map.win = mlx_new_window(map.mlx, map.length * d, map.height * d, NAME);
if (map.win != NULL)
{
map.wall = mlx_xpm_file_to_image(map.mlx, "./img/wall.xpm", &d, &d);
map.key = mlx_xpm_file_to_image(map.mlx, "./img/key.xpm", &d, &d);
map.ext = mlx_xpm_file_to_image(map.mlx, "./img/door.xpm", &d, &d);
map.bg = mlx_xpm_file_to_image(map.mlx, "./img/bg.xpm", &d, &d);
map.pl.txt = mlx_xpm_file_to_image(map.mlx, "./img/player.xpm", &d, &d);
}
return (map);
}
void draw_texture(t_map *map, int x, int y, int d)
{
if (map->arr[y][x] == '1')
mlx_put_image_to_window(map->mlx, map->win, map->wall, x * d, y * d);
else if (map->arr[y][x] == 'C')
mlx_put_image_to_window(map->mlx, map->win, map->key, x * d, y * d);
else if (map->arr[y][x] == '0')
mlx_put_image_to_window(map->mlx, map->win, map->bg, x * d, y * d);
else if (map->arr[y][x] == 'E')
mlx_put_image_to_window(map->mlx, map->win, map->ext, x * d, y * d);
else if (map->arr[y][x] == 'P')
{
map->pl.x = x;
map->pl.y = y;
mlx_put_image_to_window(map->mlx, map->win, map->pl.txt, x * d, y * d);
}
}
int draw_map(t_map *map)
{
int x;
int y;
x = 0;
y = 0;
*map = cache_images(*map, 16);
if (map->mlx == NULL || map->win == NULL)
return (map_free(map->arr, map->height, 8));
while (y < map->height)
{
x = 0;
while (x < map->length)
{
draw_texture(map, x, y, 16);
x++;
}
y++;
}
mlx_key_hook(map->win, handle_key, map);
mlx_hook(map->win, 17, 0L, user_end, map);
mlx_loop(map->mlx);
return (map_free(map->arr, map->height, 0));
}
t_map create_base_map(char **map, int map_height)
{
t_map res;
res.arr = map;
res.height = map_height;
res.exits = 0;
res.collectables = 0;
res.movements = 0;
return (res);
}
int validate_map_objects(t_map *map, int player_amount)
{
int y;
int x;
y = 0;
while (y < map->height)
{
x = 0;
while (x < map->length)
{
if (map->arr[y][x] == 'E')
map->exits++;
else if (map->arr[y][x] == 'C')
map->collectables++;
else if (map->arr[y][x] == 'P')
player_amount++;
x++;
}
y++;
}
if (map->exits == 0 || map->collectables == 0)
return (map_free(map->arr, map->height, 10));
else if (player_amount != 1)
return (map_free(map->arr, map->height, 11));
return (draw_map(map));
}