-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu_utilities.c
195 lines (188 loc) · 7.47 KB
/
menu_utilities.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @file menu_utilities.c
* @author Petr Kucera (kucerp28@fel.cvut.cz)
* @brief Module for drawing menu, setting game settings and starting the game
* @version 0.1
* @date 2021-05-04
*
* @copyright Copyright (c) 2021
*
*/
#include <stdio.h>
#include "menu_utilities.h"
#include "map_template.h"
#include "data_structures.h"
#include "draw_shapes.h"
#include "update_peripherals.h"
#include "text_fb.h"
#include "game.h"
#include "final_score.h"
#include "config.h"
#include <unistd.h>
#define HEIGHT_M frame_buff->height
void run_init_game_menu(fb_data *frame_buff, unsigned char *lcd_mem_base,
font_descriptor_t *font, peripherals_data_t peripherals)
{
// init game menu
set_background(frame_buff, 0);
draw_text_center(frame_buff, "PAC-MAN", frame_buff->width / 2, HEIGHT_M / 2, 5, font, 0xffff);
lcd_from_fb(frame_buff, lcd_mem_base);
sleep(2);
bool play_game_again = true;
game_init_data_t game = {.pacman_lives = 3, .ghost_nr = 3, .map = &map_circles};
while (play_game_again)
{
// menu with context
draw_menu(frame_buff, font, game);
lcd_from_fb(frame_buff, lcd_mem_base);
// listen symbol
char read = ' ';
while (read != 's' && read != 't' && read != 'S' && read != 'T')
{
// scan input
pthread_mutex_lock(&mtx);
pthread_cond_wait(&character_has_been_read, &mtx);
read = read_thread_data.last_read;
pthread_mutex_unlock(&mtx);
if (read == 'l' || read == 'L')
{
game = sub_menu_lives(frame_buff, lcd_mem_base, font, game);
}
if (read == 'm' || read == 'M')
{
game = sub_menu_map(frame_buff, lcd_mem_base, font, game);
}
if (read == 'g' || read == 'G')
{
game = sub_menu_ghosts(frame_buff, lcd_mem_base, font, game);
}
// redraw menu
draw_menu(frame_buff, font, game);
lcd_from_fb(frame_buff, lcd_mem_base);
}
if (read != 't' && read != 'T')
{
// run game
int game_score = run_game(&game, &peripherals);
// draw packman score
play_game_again = draw_final_score(game_score, frame_buff, lcd_mem_base, font);
}
else
{
play_game_again = false;
}
}
}
void draw_menu(fb_data *frame_buff, font_descriptor_t *font, game_init_data_t game_data)
{
// menu with context
set_background(frame_buff, 0);
draw_text_center(frame_buff, "HLAVNI MENU", frame_buff->width / 2, HEIGHT_M / 10, 3, font, 0xffff);
char string_tmp[40];
snprintf(string_tmp, 40, "pocet zivotu: %d [L]", game_data.pacman_lives);
draw_text_center(frame_buff, string_tmp, frame_buff->width / 2, HEIGHT_M / 2 - HEIGHT_M / 7, 2, font, 0xffff);
snprintf(string_tmp, 40, "mapa: %s [M]", game_data.map->name);
draw_text_center(frame_buff, string_tmp, frame_buff->width / 2, HEIGHT_M / 2, 2, font, 0xffff);
snprintf(string_tmp, 40, "pocet duchu: %d [G]", game_data.ghost_nr);
draw_text_center(frame_buff, string_tmp, frame_buff->width / 2, HEIGHT_M / 2 + HEIGHT_M / 7, 2, font, 0xffff);
draw_text_center(frame_buff, "SPUSIT HRU: [S] KONEC HRY: [T]", frame_buff->width / 2, HEIGHT_M - HEIGHT_M / 10, 2, font, 0xffff);
}
game_init_data_t sub_menu_lives(fb_data *frame_buff, unsigned char *lcd_mem_base, font_descriptor_t *font, game_init_data_t game_data)
{
char c = ' ';
while (c != 'c' && c != 'C')
{
set_background(frame_buff, 0);
draw_text_center(frame_buff, "NASTAVENI ZIVOTU", frame_buff->width / 2, HEIGHT_M / 10, 3, font, 0xffff);
draw_text_center(frame_buff, "aktualni pocet zivotu", frame_buff->width / 2, HEIGHT_M / 2 - HEIGHT_M / 7, 2, font, 0xffff);
char string_tmp[40];
snprintf(string_tmp, 40, "%d", game_data.pacman_lives);
draw_text_center(frame_buff, string_tmp, frame_buff->width / 2, HEIGHT_M / 2, 3, font, 0xffff);
snprintf(string_tmp, 40, "zmackni cislo (max %c)", MAX_LIVES);
draw_text_center(frame_buff, string_tmp, frame_buff->width / 2, HEIGHT_M / 2 + HEIGHT_M / 7, 2, font, 0xffff);
draw_text_center(frame_buff, "POTVRDIT: [C]", frame_buff->width / 2, HEIGHT_M - HEIGHT_M / 10, 2, font, 0xffff);
// update display
lcd_from_fb(frame_buff, lcd_mem_base);
// scan input
pthread_mutex_lock(&mtx);
pthread_cond_wait(&character_has_been_read, &mtx);
c = read_thread_data.last_read;
pthread_mutex_unlock(&mtx);
// listen orders
if (c > '0' && c <= MAX_LIVES)
{
game_data.pacman_lives = c - '0';
}
}
return game_data;
}
game_init_data_t sub_menu_map(fb_data *frame_buff, unsigned char *lcd_mem_base, font_descriptor_t *font, game_init_data_t game_data)
{
static int i = 0;
map_template *map_templates[] = {&map_circles, &map_star, &map_conch};
size_t map_templates_len = sizeof(map_templates) / sizeof(map_templates[0]);
while (map_templates[i]->name != game_data.map->name)
{
++i;
}
char c = ' ';
while (c != 'c' && c != 'C')
{
set_background(frame_buff, 0);
draw_text_center(frame_buff, "VOLBA MAPY", frame_buff->width / 2, HEIGHT_M / 10, 3, font, 0xffff);
draw_text_center(frame_buff, "aktualni mapa", frame_buff->width / 2, HEIGHT_M / 2 - HEIGHT_M / 7, 2, font, 0xffff);
char string_tmp[40];
snprintf(string_tmp, 40, "< %s >", game_data.map->name);
draw_text_center(frame_buff, string_tmp, frame_buff->width / 2, HEIGHT_M / 2, 3, font, 0xffff);
draw_text_center(frame_buff, "vybirej klavesami [A] [D]", frame_buff->width / 2, HEIGHT_M / 2 + HEIGHT_M / 7, 2, font, 0xffff);
draw_text_center(frame_buff, "POTVRDIT: [C]", frame_buff->width / 2, HEIGHT_M - HEIGHT_M / 10, 2, font, 0xffff);
// update display
lcd_from_fb(frame_buff, lcd_mem_base);
// scan input
pthread_mutex_lock(&mtx);
pthread_cond_wait(&character_has_been_read, &mtx);
c = read_thread_data.last_read;
pthread_mutex_unlock(&mtx);
// listen orders
if (c == 'a' || c == 'A')
{
i = (i - 1 + map_templates_len) % map_templates_len;
}
if (c == 'd' || c == 'D')
{
i = (i + 1 + map_templates_len) % map_templates_len;
}
game_data.map = map_templates[i];
}
return game_data;
}
game_init_data_t sub_menu_ghosts(fb_data *frame_buff, unsigned char *lcd_mem_base, font_descriptor_t *font, game_init_data_t game_data)
{
char c = ' ';
while (c != 'c' && c != 'C')
{
// set buffer
set_background(frame_buff, 0);
draw_text_center(frame_buff, "NASTAVENI DUCHU", frame_buff->width / 2, HEIGHT_M / 10, 3, font, 0xffff);
draw_text_center(frame_buff, "aktualni pocet duchu", frame_buff->width / 2, HEIGHT_M / 2 - HEIGHT_M / 7, 2, font, 0xffff);
char string_tmp[40];
snprintf(string_tmp, 40, "%d", game_data.ghost_nr);
draw_text_center(frame_buff, string_tmp, frame_buff->width / 2, HEIGHT_M / 2, 3, font, 0xffff);
snprintf(string_tmp, 40, "zmackni cislo (max %c)", MAX_GHOSTS);
draw_text_center(frame_buff, string_tmp, frame_buff->width / 2, HEIGHT_M / 2 + HEIGHT_M / 7, 2, font, 0xffff);
draw_text_center(frame_buff, "POTVRDIT: [C]", frame_buff->width / 2, HEIGHT_M - HEIGHT_M / 10, 2, font, 0xffff);
// update display
lcd_from_fb(frame_buff, lcd_mem_base);
// scan input
pthread_mutex_lock(&mtx);
pthread_cond_wait(&character_has_been_read, &mtx);
c = read_thread_data.last_read;
pthread_mutex_unlock(&mtx);
// listen orders
if (c > '0' && c <= MAX_GHOSTS)
{
game_data.ghost_nr = c - '0';
}
}
return game_data;
}