-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.c
114 lines (104 loc) · 3.7 KB
/
gui.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* gui.c :+: :+: */
/* +:+ */
/* By: cschuijt <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/12/22 22:51:14 by cschuijt #+# #+# */
/* Updated: 2022/12/22 22:51:14 by cschuijt ######## odam.nl */
/* */
/* ************************************************************************** */
#include "so_long.h"
#include <stdlib.h>
void render_gui(t_map *map)
{
size_t i;
sprite_buffer_to_image(map->gui_bg_sprites[0], map->background, 16, 16);
sprite_buffer_to_image(map->gui_bg_sprites[3], map->background, 16, 48);
sprite_buffer_to_image(map->gui_bg_sprites[6], map->background, 16, 80);
i = 1;
while (i < map->width - 2)
{
sprite_buffer_to_image(map->gui_bg_sprites[1], \
map->background, (i * 32) + 16, 16);
sprite_buffer_to_image(map->gui_bg_sprites[4], \
map->background, (i * 32) + 16, 48);
sprite_buffer_to_image(map->gui_bg_sprites[7], \
map->background, (i * 32) + 16, 80);
i++;
}
sprite_buffer_to_image(map->gui_bg_sprites[2], map->background, \
(i * 32) + 16, 16);
sprite_buffer_to_image(map->gui_bg_sprites[5], map->background, \
(i * 32) + 16, 48);
sprite_buffer_to_image(map->gui_bg_sprites[8], map->background, \
(i * 32) + 16, 80);
render_gui_static_strings(map);
initialize_gui_dynamic_strings(map);
render_gui_dynamic_strings(map);
}
void render_gui_static_strings(t_map *map)
{
size_t xy[2];
xy[0] = 36;
xy[1] = 26;
string_to_image(map->map_name, map->background, map->gui_charset, xy);
xy[0] = 36;
xy[1] = 50;
string_to_image("Moves:", map->background, map->gui_charset, xy);
xy[0] = 36;
xy[1] = 74;
string_to_image("Gold:", map->background, map->gui_charset, xy);
}
void render_gui_dynamic_strings(t_map *map)
{
size_t collectible_offset;
char *str;
size_t xy[2];
collectible_offset = (ft_digitcount(map->col_grabbed) + 1) * FONT_W;
xy[0] = 0;
xy[1] = 0;
str = ft_itoa(map->player->moves_taken);
if (!str)
exit_perror("malloc error");
string_to_image(str, map->move_counter, map->gui_charset, xy);
free(str);
str = ft_itoa(map->col_grabbed);
if (!str)
exit_perror("malloc error");
xy[0] = 0;
xy[1] = 0;
string_to_image(str, map->col_counter, map->gui_charset, xy);
xy[0] = collectible_offset;
xy[1] = 0;
string_to_image("/", map->col_counter, map->gui_charset, xy);
xy[0] += FONT_W;
xy[1] = 0;
string_to_image(map->col_total_str, map->col_counter, map->gui_charset, xy);
free(str);
}
void initialize_gui_dynamic_strings(t_map *map)
{
size_t instance;
if (!map->move_counter)
{
map->move_counter = mlx_new_image(map->mlx, 300, 32);
instance = mlx_image_to_window(map->mlx, map->move_counter, 115, 50);
mlx_set_instance_depth(&map->move_counter->instances[instance], \
layer_gui_moves);
}
if (!map->col_counter)
{
map->col_counter = mlx_new_image(map->mlx, 300, 32);
instance = mlx_image_to_window(map->mlx, map->col_counter, 104, 74);
mlx_set_instance_depth(&map->col_counter->instances[instance], \
layer_gui_cols);
}
}
void update_gui(t_map *map)
{
ft_memset((void *)map->move_counter->pixels, '\0', 32 * 300 * 4);
ft_memset((void *)map->col_counter->pixels, '\0', 32 * 300 * 4);
render_gui_dynamic_strings(map);
}