-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_rendering.c
108 lines (97 loc) · 3.29 KB
/
map_rendering.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* map_rendering.c :+: :+: */
/* +:+ */
/* By: cschuijt <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/11/15 15:15:34 by cschuijt #+# #+# */
/* Updated: 2022/11/15 15:15:34 by cschuijt ######## odam.nl */
/* */
/* ************************************************************************** */
#include "so_long.h"
#include <stdlib.h>
void render_map(t_map *map)
{
size_t i;
render_background_pixels(map);
i = 0;
while (map->sprite_categories[i])
{
if (map->sprite_categories[i] != ' ')
{
if (map->sprite_categories[i] == 'L')
render_background_sprite(map, i, map->lava_sprites);
else if (map->sprite_categories[i] == 'P')
render_pillar(map, i);
else
render_background_sprite(map, i, map->bg_sprites);
}
i++;
}
render_collectibles(map);
render_shadows(map);
render_player(map);
render_patrols(map);
render_gui(map);
mark_sprites_for_animation(map);
}
void render_background_pixels(t_map *map)
{
size_t screen;
size_t width;
size_t height;
size_t i;
width = map->mlx->width;
height = map->mlx->height;
screen = width * height;
map->background = mlx_new_image(map->mlx, width, height);
i = 0;
while (i < screen)
{
mlx_put_pixel(map->background, i % width, i / width, 0x353540FF);
i++;
}
render_gui(map);
mlx_image_to_window(map->mlx, map->background, 0, 0);
mlx_set_instance_depth(map->background->instances, layer_bg);
}
void render_background_sprite(t_map *map, size_t i, uint8_t **sprites)
{
t_sprite *sprite;
size_t sprite_index;
size_t xy[2];
size_t inst;
sprite_index = (size_t)((unsigned char) map->render_terrain[i]);
sprite = find_or_create_sprite(map, sprites, sprite_index);
xy[0] = render_x_pos(map, i);
xy[1] = render_y_pos(map, i);
inst = mlx_image_to_window(map->mlx, sprite->image, xy[0], xy[1]);
mlx_set_instance_depth(&sprite->image->instances[inst], layer_bg_sprites);
}
void render_shadow_sprite(t_map *map, size_t i)
{
t_sprite *sprite;
size_t sprite_index;
size_t xy[2];
size_t inst;
sprite_index = (size_t)(unsigned char) map->render_shadows[i];
sprite = find_or_create_sprite(map, map->shadow_sprites, sprite_index);
xy[0] = render_x_pos(map, i);
xy[1] = render_y_pos(map, i);
inst = mlx_image_to_window(map->mlx, sprite->image, xy[0], xy[1]);
mlx_set_instance_depth(&sprite->image->instances[inst], layer_shadows);
}
void render_pillar(t_map *map, size_t i)
{
t_sprite *sprite;
size_t inst;
size_t x_pos;
size_t y_pos;
render_background_sprite(map, i, map->bg_sprites);
sprite = find_or_create_sprite(map, map->bg_sprites, 138);
x_pos = render_x_pos(map, i);
y_pos = render_y_pos(map, i - map->width);
inst = mlx_image_to_window(map->mlx, sprite->image, x_pos, y_pos);
mlx_set_instance_depth(&sprite->image->instances[inst], layer_pillars);
}