-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlauncher.c
More file actions
222 lines (187 loc) · 6.94 KB
/
Copy pathlauncher.c
File metadata and controls
222 lines (187 loc) · 6.94 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "launcher.h"
#include "context.h"
#include "display.h"
#include "input.h"
#include "audio.h"
#include "storage.h"
#define COLOR_CYAN RGB565(0, 255, 255)
#define COLOR_YELLOW RGB565(255, 255, 0)
// --- Launcher ---
static HighScore s_scores[3];
static bool s_dirty;
static bool s_firstDraw;
static uint8_t s_menuIndex;
static uint8_t s_menuCount;
static bool s_canContinue;
extern bool g_isContinuing;
extern char g_playerName[4];
static const char* GetGameName(uint8_t id) {
if (id == GAME_ID_SNAKE) return "SNAKE";
if (id == GAME_ID_TETRIS) return "TETRIS";
if (id == GAME_ID_BRICKS) return "BREAKOUT";
return "UNKNOWN";
}
void launcherEnter(void) {
storageGetHighScores(g_context.selectedGameId, s_scores);
s_dirty = true;
s_firstDraw = true;
s_menuIndex = 0;
s_canContinue = g_context.hasState[g_context.selectedGameId];
s_menuCount = s_canContinue ? 2 : 1;
}
void launcherUpdate(uint32_t nowMs, uint16_t deltaMs) {
(void)nowMs; (void)deltaMs;
if (inputWasPressed(BUTTON_HOME)) {
contextRequestScreen(SCREEN_MENU);
return;
}
bool up = inputWasPressed(BUTTON_UP);
bool down = inputWasPressed(BUTTON_DOWN);
if (up && s_menuIndex > 0) {
s_menuIndex--;
s_dirty = true;
audioPlaySfxMenuMove();
} else if (down && s_menuIndex < s_menuCount - 1) {
s_menuIndex++;
s_dirty = true;
audioPlaySfxMenuMove();
}
if (inputWasPressed(BUTTON_START) || inputWasPressed(BUTTON_SELECT)) {
audioPlaySfxMenuSelect();
if (s_canContinue && s_menuIndex == 0) {
g_isContinuing = true;
if (g_context.selectedGameId == GAME_ID_SNAKE) {
contextRequestScreen(SCREEN_GAME_SNAKE);
} else if (g_context.selectedGameId == GAME_ID_TETRIS) {
contextRequestScreen(SCREEN_GAME_TETRIS);
} else if (g_context.selectedGameId == GAME_ID_BRICKS) {
contextRequestScreen(SCREEN_GAME_BRICKS);
}
} else {
g_isContinuing = false;
contextRequestScreen(SCREEN_NAME_ENTRY);
}
}
}
void launcherDraw(void) {
if (!s_dirty) return;
s_dirty = false;
if (s_firstDraw) {
displayFillScreen(COLOR_BLACK);
displayDrawTextScaled(10, 5, GetGameName(g_context.selectedGameId), COLOR_CYAN, COLOR_BLACK, 2);
displayDrawText(10, 30, "HIGH SCORES", COLOR_WHITE, COLOR_BLACK);
for (uint8_t i = 0; i < 3; i++) {
uint8_t y = 45 + (i * 12);
char buf[12];
buf[0] = s_scores[i].name[0];
buf[1] = s_scores[i].name[1];
buf[2] = s_scores[i].name[2];
buf[3] = ' ';
buf[4] = ' ';
uint16_t score = s_scores[i].score;
buf[5] = (score / 1000) % 10 + '0';
buf[6] = (score / 100) % 10 + '0';
buf[7] = (score / 10) % 10 + '0';
buf[8] = score % 10 + '0';
buf[9] = '\0';
displayDrawText(15, y, buf, COLOR_WHITE, COLOR_BLACK);
}
s_firstDraw = false;
}
uint8_t menuY = 90;
if (s_canContinue) {
if (s_menuIndex == 0) {
displayDrawText(10, menuY, "> Continue", COLOR_GREEN, COLOR_BLACK);
displayDrawText(10, menuY + 15, " New Game", COLOR_WHITE, COLOR_BLACK);
} else {
displayDrawText(10, menuY, " Continue", COLOR_WHITE, COLOR_BLACK);
displayDrawText(10, menuY + 15, "> New Game", COLOR_GREEN, COLOR_BLACK);
}
} else {
displayDrawText(10, menuY + 15, "> New Game", COLOR_GREEN, COLOR_BLACK);
}
}
// --- Name Entry ---
static uint8_t s_charIndex;
static char s_tempName[6];
static bool s_nameDirty;
static bool s_nameFirstDraw;
void NameEntry_Enter(void) {
s_charIndex = 0;
s_tempName[0] = 'A';
s_tempName[1] = 'A';
s_tempName[2] = 'A';
s_tempName[3] = '\0';
s_nameDirty = true;
s_nameFirstDraw = true;
}
void NameEntry_Update(uint32_t nowMs, uint16_t deltaMs) {
(void)nowMs; (void)deltaMs;
if (inputWasPressed(BUTTON_HOME)) {
contextRequestScreen(SCREEN_GAME_LAUNCHER);
return;
}
bool up = inputIsRepeated(BUTTON_UP);
bool down = inputIsRepeated(BUTTON_DOWN);
bool left = inputIsRepeated(BUTTON_LEFT);
bool right = inputIsRepeated(BUTTON_RIGHT);
if (up) {
if (s_tempName[s_charIndex] == 'A') s_tempName[s_charIndex] = 'Z';
else s_tempName[s_charIndex]--;
s_nameDirty = true;
audioPlaySfxMenuMove();
} else if (down) {
if (s_tempName[s_charIndex] == 'Z') s_tempName[s_charIndex] = 'A';
else s_tempName[s_charIndex]++;
s_nameDirty = true;
audioPlaySfxMenuMove();
}
if (left && s_charIndex > 0) {
s_charIndex--;
s_nameDirty = true;
audioPlaySfxMenuMove();
} else if (right && s_charIndex < 2) {
s_charIndex++;
s_nameDirty = true;
audioPlaySfxMenuMove();
}
if (inputWasPressed(BUTTON_START) || inputWasPressed(BUTTON_SELECT)) {
audioPlaySfxMenuSelect();
g_playerName[0] = s_tempName[0];
g_playerName[1] = s_tempName[1];
g_playerName[2] = s_tempName[2];
g_playerName[3] = '\0';
if (g_context.selectedGameId == GAME_ID_SNAKE) {
contextRequestScreen(SCREEN_GAME_SNAKE);
} else if (g_context.selectedGameId == GAME_ID_TETRIS) {
contextRequestScreen(SCREEN_GAME_TETRIS);
} else if (g_context.selectedGameId == GAME_ID_BRICKS) {
contextRequestScreen(SCREEN_GAME_BRICKS);
}
}
}
void NameEntry_Draw(void) {
if (!s_nameDirty) return;
s_nameDirty = false;
if (s_nameFirstDraw) {
displayFillScreen(COLOR_BLACK);
displayDrawTextScaled(4, 10, "ENTER NAME", COLOR_YELLOW, COLOR_BLACK, 2);
s_nameFirstDraw = false;
}
// Draw the 5 characters
char str[2] = {0, 0};
for (uint8_t i = 0; i < 3; i++) {
uint16_t color = (i == s_charIndex) ? COLOR_RED : COLOR_WHITE;
str[0] = s_tempName[i];
displayDrawTextScaled(35 + (i * 20), 50, str, color, COLOR_BLACK, 3);
// Draw little arrows above/below active char
if (i == s_charIndex) {
displayDrawText(35 + (i * 20) + 4, 35, "^", COLOR_CYAN, COLOR_BLACK);
displayDrawText(35 + (i * 20) + 4, 80, "v", COLOR_CYAN, COLOR_BLACK);
} else {
displayDrawText(35 + (i * 20) + 4, 35, " ", COLOR_BLACK, COLOR_BLACK);
displayDrawText(35 + (i * 20) + 4, 80, " ", COLOR_BLACK, COLOR_BLACK);
}
}
displayDrawText(15, 105, "START to save", COLOR_WHITE, COLOR_BLACK);
}