-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_map.c
108 lines (102 loc) · 4.32 KB
/
draw_map.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rboudwin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/21 10:26:11 by rboudwin #+# #+# */
/* Updated: 2023/12/29 09:40:55 by rboudwin ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void determine_tile_size(t_map *map)
{
map->tile_length = map->window_width / map->line_length;
map->tile_width = map->window_height / map->line_count;
if (map->tile_length <= map->tile_width)
map->tile_sq = map->tile_length;
else
map->tile_sq = map->tile_width;
map->moves = 0;
map->curr_col = 0;
}
void load_textures(mlx_t *mlx, t_img *p)
{
p->txt_0 = mlx_load_png("./assets/tiles/floor/floor_1.png");
p->txt_1 = mlx_load_png("./assets/tiles/wall/wall_1.png");
p->txt_c = mlx_load_png("./assets/props_itens/chest_closed_anim_f0.png");
p->txt_e = mlx_load_png("./assets/tiles/wall/door_closed.png");
p->txt_p = mlx_load_png("./assets/tiles/floor/floor_2.png");
p->txt_k = mlx_load_png("./assets/heroes/knight/knight_idle_anim_f0.png");
p->txt_m = mlx_load_png("./assets/minotaur.png");
p->txt_cc = mlx_load_png("./assets/props_itens/chest_open.png");
p->txt_eo = mlx_load_png("./assets/tiles/wall/door_fullyopen.png");
p->img_k = mlx_texture_to_image(mlx, p->txt_k);
p->img_0 = mlx_texture_to_image(mlx, p->txt_0);
p->img_1 = mlx_texture_to_image(mlx, p->txt_1);
p->img_c = mlx_texture_to_image(mlx, p->txt_c);
p->img_e = mlx_texture_to_image(mlx, p->txt_e);
p->img_p = mlx_texture_to_image(mlx, p->txt_p);
p->img_m = mlx_texture_to_image(mlx, p->txt_m);
p->img_cc = mlx_texture_to_image(mlx, p->txt_cc);
p->img_eo = mlx_texture_to_image(mlx, p->txt_eo);
}
void initialize_images(mlx_t *mlx, t_map *map, t_img *p)
{
map->x = 0;
map->y = 0;
determine_tile_size(map);
p->img = mlx_new_image(mlx, map->window_width, map->window_height);
ft_memset(p->img->pixels, 150, p->img->width * p->img->height * BPP);
mlx_image_to_window(mlx, p->img, 0, 0);
load_textures(mlx, p);
mlx_resize_image(p->img_k, map->tile_sq, map->tile_sq);
mlx_resize_image(p->img_0, map->tile_sq, map->tile_sq);
mlx_resize_image(p->img_1, map->tile_sq, map->tile_sq);
mlx_resize_image(p->img_c, map->tile_sq, map->tile_sq);
mlx_resize_image(p->img_cc, map->tile_sq, map->tile_sq);
mlx_resize_image(p->img_e, map->tile_sq, map->tile_sq);
mlx_resize_image(p->img_p, map->tile_sq, map->tile_sq);
mlx_resize_image(p->img_m, map->tile_sq, map->tile_sq);
mlx_resize_image(p->img_eo, map->tile_sq, map->tile_sq);
}
void draw_row(mlx_t *mlx, t_map *map, t_img *p)
{
while (map->grid[map->y][map->x] != '\0')
{
if (map->grid[map->y][map->x] == '0')
mlx_image_to_window(mlx, p->img_0,
map->x * map->tile_sq, map->y * map->tile_sq);
else if (map->grid[map->y][map->x] == '1')
mlx_image_to_window(mlx, p->img_1,
map->x * map->tile_sq, map->y * map->tile_sq);
else if (map->grid[map->y][map->x] == 'C')
mlx_image_to_window(mlx, p->img_c,
map->x * map->tile_sq, map->y * map->tile_sq);
else if (map->grid[map->y][map->x] == 'E')
mlx_image_to_window(mlx, p->img_e,
map->x * map->tile_sq, map->y * map->tile_sq);
else if (map->grid[map->y][map->x] == 'P')
mlx_image_to_window(mlx, p->img_p,
map->x * map->tile_sq, map->y * map->tile_sq);
else if (map->grid[map->y][map->x] == 'c')
mlx_image_to_window(mlx, p->img_cc,
map->x * map->tile_sq, map->y * map->tile_sq);
else if (map->grid[map->y][map->x] == 'e')
mlx_image_to_window(mlx, p->img_eo,
map->x * map->tile_sq, map->y * map->tile_sq);
map->x++;
}
}
void draw_map(mlx_t *mlx, t_map *map, t_img *p)
{
while (map->grid[map->y] != NULL)
{
draw_row(mlx, map, p);
map->y++;
map->x = 0;
}
mlx_image_to_window(mlx, p->img_m,
map->charx * map->tile_sq, map->chary * map->tile_sq);
}