-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
139 lines (119 loc) · 4.04 KB
/
main.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
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gba.h"
#include "sound.h"
#include "ai.h"
#include "game.h"
// Include any header files for title screen or exit
// screen images generated by nin10kit. Example for the provided garbage
// image:
#include "images/splash.h"
#include "images/winscreen.h"
#include "images/losescreen.h"
#include "images/diffscreen.h"
#define DIFF_CHOICE_PADDING 3
#define DIFF_CHOICE_HEIGHT (8 + 2 * (DIFF_CHOICE_PADDING))
#define DIFF_CHOICE_OFFSET 40
// Add any additional states you need for your app. You are not requried to use
// these specific provided states.
enum gba_state state;
static short diffChoiceIndex = 0;
static int modulo(int x,int N){
return (x % N + N) %N;
}
static void drawDiffLevel(size_t index, unsigned short fgColor, unsigned short bgColor) {
int rectWidth = 6 * strlen(difficultyOptions[index].name) + 2 * DIFF_CHOICE_PADDING;
drawRectDMA(DIFF_CHOICE_HEIGHT * index + DIFF_CHOICE_OFFSET, (WIDTH - rectWidth) / 2, rectWidth
, DIFF_CHOICE_HEIGHT, bgColor);
drawCenteredString(DIFF_CHOICE_HEIGHT * index + DIFF_CHOICE_OFFSET, 0, WIDTH, DIFF_CHOICE_HEIGHT, (char *) difficultyOptions[index].name, fgColor);
}
int main(void) {
// Manipulate REG_DISPCNT here to set Mode 3. //
REG_DISPCNT = MODE3 | BG2_ENABLE;
enableSound();
// Save current and previous state of button input.
u32 previousButtons = BUTTONS;
u32 currentButtons = BUTTONS;
int selectCounter = 0;
// Load initial application state
state = ENTER_START;
while (1) {
currentButtons = BUTTONS; // Load the current state of the buttons
// Manipulate the state machine below as needed //
// NOTE: Call waitForVBlank() before you draw
switch (state) {
case ENTER_START:
waitForVBlank();
drawFullScreenImageDMA(splash);
state = START;
case START:
if (KEY_JUST_PRESSED(BUTTON_START, currentButtons, previousButtons)) {
state = ENTER_DIFFICULTY;
}
waitForVBlank();
break;
case ENTER_DIFFICULTY:
waitForVBlank();
drawFullScreenImageDMA(diffscreen);
waitForVBlank();
for (size_t i = 0; i < sizeof(difficultyOptions) / sizeof(difficulty); i++) {
drawDiffLevel(i, WHITE, BLACK);
}
state = DIFFICULTY;
case DIFFICULTY:
{
short oldDiffChoiceIndex = diffChoiceIndex;
if (KEY_JUST_PRESSED(BUTTON_UP, currentButtons, previousButtons)) {
diffChoiceIndex = modulo(diffChoiceIndex - 1,sizeof difficultyOptions / sizeof(difficulty));
}
if (KEY_JUST_PRESSED(BUTTON_DOWN, currentButtons, previousButtons)) {
diffChoiceIndex = modulo(diffChoiceIndex + 1,sizeof difficultyOptions / sizeof(difficulty));
}
if (KEY_JUST_PRESSED(BUTTON_START, currentButtons, previousButtons)) {
state = ENTER_PLAY;
}
UNUSED(oldDiffChoiceIndex);
waitForVBlank();
drawDiffLevel(oldDiffChoiceIndex, WHITE, BLACK);
drawDiffLevel(diffChoiceIndex, BLACK, diffChoiceIndex == 4 ? RED : WHITE);
}
break;
case ENTER_PLAY:
gameInit(diffChoiceIndex);
state = PLAY;
case PLAY:
gameUpdate(currentButtons, previousButtons);
break;
case ENTER_WIN:
waitForVBlank();
drawFullScreenImageDMA(winscreen);
state = WIN;
case WIN:
waitForVBlank();
break;
case ENTER_LOSE:
waitForVBlank();
drawFullScreenImageDMA(losescreen);
state = LOSE;
case LOSE:
waitForVBlank();
break;
default:
UNUSED(state);
}
if (KEY_DOWN(BUTTON_SELECT, currentButtons)) {
selectCounter++;
}
else selectCounter = 0;
if (selectCounter >= 60) {
state = ENTER_START;
selectCounter = 0;
}
previousButtons = currentButtons; // Store the current state of the buttons
soundUpdate();
}
UNUSED(previousButtons); // You can remove this once previousButtons is used
return 0;
}