-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.c
389 lines (340 loc) · 11.7 KB
/
menu.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <math.h>
#include <stdio.h>
#include "core.h"
#include "screen.h"
#include "text.h"
#include "resources.h"
#include "init.h"
#include "play.h"
#include "config.h"
#include <libdragon.h>
#define MAX_COLUMN_COUNT 2
#define MAX_ROW_COUNT 4
/**
* Draws a menu item.
* @param frame Id of frame to render to.
* @param label identifies the label of the menu item.
* @param position Ordinal position of the menu item.
* @param drawCursor If true, add the cursor to the menu item.
* @param disabled If true, label will be drawn in a muted colour.
* @param screen The size/position of the gameboy screen.
* @private
*/
static void drawMenuItem(
const display_context_t frame,
const byte playerNumber,
const TextId label,
const byte x,
const byte y,
const bool drawCursor,
const bool disabled,
const Rectangle* screen
) {
const natural xOffset = screen->Width / 2;
const float scale = (float)screen->Width * TEXT_SCALE_FACTOR;
const natural menuItemOffset = 34 * scale;
// space the second column with less items in it, out a little more.
natural top = 0;
if (x == 1) {
top = screen->Top + screen->Height + (menuItemOffset * y * 2);
} else {
top = screen->Top + screen->Height + (menuItemOffset * y);
}
natural left = screen->Left + (xOffset * x);
if (!disabled || !HIDE_DISABLED_OPTIONS) {
string text = "";
getText(label, text);
if (disabled) {
string tmp;
sprintf(tmp, "~%s", text);
strcpy(text, tmp);
}
drawText(frame, text, left, top, scale);
}
if (drawCursor) {
Rectangle border = {left, top, xOffset, menuItemOffset };
drawSolidBorder(frame, &border, 2, SELECTED_MENU_ITEM_COLOUR);
}
}
/**
* When moving between column, calculates the new row position.
* @param oldRowCount number of items in previous column.
* @param newRowCount nember of items in new column.
* @param currentRow current position of the cursor.
* @return New position in the new column.
* @private
*/
static byte getNewRow(const float oldRowCount, const float newRowCount, const float currentRow) {
float ratio = oldRowCount / newRowCount;
if (oldRowCount > newRowCount) {
return ((byte)ceil((currentRow + 1) / ratio) - 1);
} else {
// If we're moving from the smaller column to the larger one, we need to appear at the top of the group.
return (byte)(ceil((currentRow + 1) / ratio) - (ratio + 1));
}
}
/**
* Exits the menu by setting the player's mode back to "Play"
* @param playerState state of the given player.
* @private
*/
static void resumePlay(PlayerState* playerState) {
playerState->MenuCursorRow = -1;
rootState.RequiresRepaint = false;
playerState->ActiveMode = Play;
}
/**
* Exits the main menu and brings up the options menu.
* @param playerState state of the player going to the options menu.
* @private
*/
static void showOptionsMenu(PlayerState* playerState) {
playerState->MenuCursorRow = -1;
playerState->ActiveMode = Options;
}
/**
* Resets the emulator back to it's initial state and spins it up again.
* @param state Program state.
* @param playerNumber player that wants to reset.
*/
static void resetGame(const byte playerNumber) {
resetPlayState(&rootState.Players[playerNumber]);
rootState.Players[playerNumber].ActiveMode = Play;
}
/**
* Resets the given player back to cartridge load mode so they can change to a different game.
* @param state Program state.
* @param playerNumber Player that wants to change cartridge.
*/
static void changeGame(const byte playerNumber) {
// Dump Save RAM first, or nah?
// Reset state and send back to init.
freeByteArray(&rootState.Players[playerNumber].EmulationState.Cartridge.Rom);
freeByteArray(&rootState.Players[playerNumber].EmulationState.Cartridge.Ram);
rootState.Players[playerNumber].MenuCursorRow = -1;
rootState.Players[playerNumber].ActiveMode = Init;
rootState.Players[playerNumber].InitState = InitChanging;
rootState.RequiresRepaint = true;
}
/**
* Initialises another player with the cartridge plugged into controller slot 2
* @return error code
* @private
*/
static char addGame() {
if (rootState.PlayerCount >= MAX_PLAYERS) {
// No more players supported.
return -2;
}
PlayerState* newPlayer = &rootState.Players[rootState.PlayerCount];
generatePlayerState(newPlayer);
rootState.PlayerCount++;
return 0;
}
/**
* Initialises another player, using the same ROM & Save as in controller slot 1
* @return error code.
* @private
*/
static char addPlayer() {
if (rootState.PlayerCount < 1) {
// Can't copy from 1 if there is no 1
return -1;
} else if (rootState.PlayerCount >= MAX_PLAYERS) {
// No more players supported.
return -2;
}
PlayerState* newPlayer = &rootState.Players[rootState.PlayerCount];
const GameBoyCartridge* playerOne = &rootState.Players[0].EmulationState.Cartridge;
generatePlayerState(newPlayer);
// Copy the most of the cartridge data by reference. It won't be changing.
newPlayer->EmulationState.Cartridge.IsGbcSupported = playerOne->IsGbcSupported;
newPlayer->EmulationState.Cartridge.Header = playerOne->Header;
newPlayer->EmulationState.Cartridge.Rom = playerOne->Rom;
// But maintain a different copy of the save file. We don't want to have two instances messing with a single save file.
// Non-player-1 save files will never be written back to the cartridge.
importCartridgeRam(0, &newPlayer->EmulationState.Cartridge);
resetPlayState(newPlayer);
newPlayer->ActiveMode = Play;
rootState.PlayerCount++;
return 0;
}
/**
* Carries out the option selected from the menu.
* @param playerNumber number of player selecting an option.
* @param x column of selected item.
* @param y row of selected item.
*/
static void executeMenuItem(const byte playerNumber, const byte x, const byte y) {
typedef enum { Resume, Reset, Change, Options, AddPlayer, AddGame } items;
byte position = 0;
bool done = false;
for(byte col = 0; col <= x && !done; col++) {
for(byte row = 0; row < rootState.Players[playerNumber].MenuLayout[col] && !done; row++) {
if (col == x && row == y) {
done = true;
} else {
position++;
}
}
}
bool noFlush = false;
switch((items)position) {
case Resume:
resumePlay(&rootState.Players[playerNumber]);
break;
case Reset:
resetGame(playerNumber);
break;
case Change:
changeGame(playerNumber);
break;
case AddPlayer:
addPlayer();
break;
case AddGame:
addGame();
break;
case Options:
showOptionsMenu(&rootState.Players[playerNumber]);
noFlush = true;
break;
default: ; break;
}
if (!noFlush) {
flushScreen();
}
}
/**
* Handles the pause menu for given player.
* @param state Program state.
* @param playerNumber player in menu mode.
*/
void menuLogic(const byte playerNumber) {
PlayerState* playerState = &rootState.Players[playerNumber];
playerState->MenuLayout[0] = 4;
// If next controller not plugged in, disable add player & add game options.
if (rootState.PlayerCount > 1 || !(rootState.ControllersPresent & (CONTROLLER_2_INSERTED))) {
playerState->MenuLayout[1] = 0;
// Can't be in the second column if there is no second column.
playerState->MenuCursorColumn = 0;
} else {
playerState->MenuLayout[1] = 2;
}
bool pressedButtons[N64_BUTTON_COUNT] = {};
getPressedButtons(&rootState.KeysReleased, playerNumber, pressedButtons);
const bool menuPressed = pressedButtons[playerState->SystemMenuButton];
bool repaintRequired = true;
bool ctrlReadRequired = true;
byte column = playerState->MenuCursorColumn;
// First entering the loop.
if (playerState->MenuCursorRow == -1) {
playerState->MenuCursorRow = 0;
// UP AND DOWN
} else if (pressedButtons[Up]) {
if (playerState->MenuCursorRow > 0) {
playerState->MenuCursorRow--;
} else {
playerState->MenuCursorRow = playerState->MenuLayout[column] - 1;
}
} else if (pressedButtons[Down]) {
if (playerState->MenuCursorRow < playerState->MenuLayout[column] - 1) {
playerState->MenuCursorRow++;
} else {
playerState->MenuCursorRow = 0;
}
// LEFT AND RIGHT
} else if (pressedButtons[Left]) {
if (playerState->MenuCursorColumn > 0) {
playerState->MenuCursorColumn--;
} else {
// Don't move the cursor if there are no rows next door.
if (playerState->MenuLayout[MAX_COLUMN_COUNT - 1]) {
playerState->MenuCursorColumn = MAX_COLUMN_COUNT - 1;
} else {
return;
}
}
playerState->MenuCursorRow = getNewRow(
playerState->MenuLayout[column],
playerState->MenuLayout[(byte)playerState->MenuCursorColumn],
playerState->MenuCursorRow
);
} else if (pressedButtons[Right]) {
if (playerState->MenuCursorColumn < MAX_COLUMN_COUNT - 1) {
// Don't move the cursor if there are no rows next door.
if (playerState->MenuLayout[column + 1]) {
playerState->MenuCursorColumn++;
} else {
return;
}
} else {
playerState->MenuCursorColumn = 0;
}
playerState->MenuCursorRow = getNewRow(
playerState->MenuLayout[column],
playerState->MenuLayout[(byte)playerState->MenuCursorColumn],
playerState->MenuCursorRow
);
// BACK, SELECT ETC,
} else if (pressedButtons[B] || menuPressed) {
resumePlay(playerState);
} else if (pressedButtons[A]) {
executeMenuItem(playerNumber, playerState->MenuCursorColumn, playerState->MenuCursorRow);
} else {
repaintRequired = false;
ctrlReadRequired = false;
}
rootState.RequiresRepaint |= repaintRequired;
rootState.RequiresControllerRead |= ctrlReadRequired;
}
/**
* Displays the pause menu for given player.
* @param state Program state.
* @param playerNumber player in menu mode.
*/
void menuDraw(const byte playerNumber) {
Rectangle screen = {};
getScreenPosition(playerNumber, &screen);
prepareRdpForSprite(rootState.Frame);
loadSprite(getSpriteSheet(), BLUE_BG_TEXTURE, MIRROR_XY);
// Cover menu section.
rdp_draw_textured_rectangle(
0,
0,
screen.Top + screen.Height,
RESOLUTION_X,
RESOLUTION_Y - screen.Top + screen.Height,
MIRROR_DISABLED
);
if (rootState.Players[playerNumber].ActiveMode != Menu) {
rootState.RequiresRepaint = true;
return;
}
TextId labels[MAX_COLUMN_COUNT * MAX_ROW_COUNT] = {
TextMenuResume,
TextMenuReset,
TextMenuChangeCart,
TextMenuOptions,
TextMenuAddPlayer,
TextMenuAddGame
};
// Draw menu items in order.
byte position = 0;
for(byte x = 0; x < MAX_COLUMN_COUNT; x++) {
for(byte y = 0; y < MAX_ROW_COUNT; y++) {
drawMenuItem(
rootState.Frame,
playerNumber,
labels[position],
x,
y,
rootState.Players[playerNumber].MenuCursorRow == y && rootState.Players[playerNumber].MenuCursorColumn == x,
y >= rootState.Players[playerNumber].MenuLayout[x],
&screen
);
position++;
}
}
rdp_detach_display();
}