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
|
#include "app.h"
#include "font.h"
#include <stdio.h>
#define MENU_ITEM_TOP_YPOS_RELATIVE 0.3
#define MENU_ITEM_YOFFSET_RELATIVE 0.05
#define UNSELECTED_ITEM_SCALE 2
#define SELECTED_ITEM_SCALE 2.4
#define MINIMUM_TIMED_MODE_TIME 5
#define TIMED_MODE_STEP 3
typedef enum {
timed_mode_option = 0,
exit_option
} menu_option;
menu_option selected_option;
int timed_mode_time;
sprite_font text_font;
extern SDL_Window* window;
extern application_state app_state;
void init_menu(SDL_Renderer* renderer) {
if (text_font.spritesheet == NULL) {
load_sprite_font(renderer,
"assets"DIRSEP"Fonts"DIRSEP"Peaberry-Font-v2.0"DIRSEP"Peaberry Bitmap Fonts"DIRSEP"1. WhitePeaberry"DIRSEP"2. White Peaberry"DIRSEP"WhitePeaberry.txt",
&text_font);
}
selected_option = timed_mode_option;
timed_mode_time = MINIMUM_TIMED_MODE_TIME + 2*TIMED_MODE_STEP;
}
void draw_menu(SDL_Renderer* renderer) {
SDL_RenderClear(renderer);
float yp = MENU_ITEM_TOP_YPOS_RELATIVE;
for (menu_option i = timed_mode_option; i <= exit_option; i++) {
float scale = i == selected_option ? SELECTED_ITEM_SCALE : UNSELECTED_ITEM_SCALE;
if (i == timed_mode_option) {
char timed_mode_label[100];
snprintf(timed_mode_label, 100, "Timed Mode <%d>", timed_mode_time);
draw_text_with_spritefont_extended(renderer,
&text_font,
(unsigned char*)timed_mode_label,
get_window_relative_xpos(0.5),
get_window_relative_ypos(yp),
scale
);
} else if (i == exit_option) {
draw_text_with_spritefont_extended(renderer,
&text_font,
(unsigned char*)"Exit",
get_window_relative_xpos(0.5),
get_window_relative_ypos(yp),
scale
);
}
yp += MENU_ITEM_YOFFSET_RELATIVE;
}
SDL_RenderPresent(renderer);
}
static inline void select_next_option() {
selected_option = SDL_min(selected_option+1, exit_option);
}
static inline void select_previous_option() {
if (selected_option > timed_mode_option) {
selected_option--;
}
}
SDL_AppResult menu_execute_action() {
if (selected_option == exit_option) {
return SDL_APP_SUCCESS;
} if (selected_option == timed_mode_option) {
init_timed_game(NULL, NULL, timed_mode_time);
app_state = game_state;
}
return SDL_APP_CONTINUE;
}
SDL_AppResult menu_event(SDL_Event* e) {
if (e->type == SDL_EVENT_KEY_DOWN) {
SDL_Scancode pressed_button = e->key.scancode;
if (pressed_button == SDL_SCANCODE_RIGHT && selected_option == timed_mode_option) {
timed_mode_time += TIMED_MODE_STEP;
} else if (pressed_button == SDL_SCANCODE_LEFT && selected_option == timed_mode_option) {
timed_mode_time = SDL_max(MINIMUM_TIMED_MODE_TIME, timed_mode_time-TIMED_MODE_STEP);
} else if (pressed_button == SDL_SCANCODE_RETURN) {
return menu_execute_action();
} else if (pressed_button == SDL_SCANCODE_DOWN) {
select_next_option();
} else if (pressed_button == SDL_SCANCODE_UP) {
select_previous_option();
}
}
return SDL_APP_CONTINUE;
}
void free_menu() {
free_sprite_font(&text_font);
}
|