-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
176 lines (148 loc) · 4.67 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
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
#include <stdbool.h>
#include <limits.h>
#include <stdio.h>
#include "myLib.h"
#include "dino.h"
#include "img/title.h"
#include "graphics.h"
#include "colors.h"
#include "text.h"
#include "main.h"
#include "game.h"
#include "enemy.h"
#include "img/bird.h"
#include "draw.h"
// Vars
volatile unsigned int counter;
unsigned int highScore = 0;
/*
* Main method
*/
int main(void) {
REG_DISPCNT = MODE3 | BG2_ENABLE;
counter = 0;
GameState state = MENU_DRAW;
AppState appState = initialAppState();
uint_t previousButtons = BUTTONS;
uint_t currentButtons = BUTTONS;
while (true) {
currentButtons = BUTTONS;
counter += 1;
appState.tick += 1;
switch (state) {
case MENU_DRAW:
waitForVblank();
drawMenu();
state = MENU;
break;
case MENU:
if (KEY_JUST_PRESSED(BUTTON_SELECT, previousButtons, currentButtons)) {
state = DRAW_RULES;
}
else if (KEY_JUST_PRESSED(BUTTON_START, previousButtons, currentButtons)) {
// Reset game state
resetAppState(&appState);
// Clear screen
clearScreen();
state = PLAYING;
}
break;
case PLAYING:
updateAppState(&appState, previousButtons, currentButtons);
drawAppState(&appState);
if (KEY_JUST_PRESSED(BUTTON_SELECT, previousButtons, currentButtons)) {
state = DRAW_PAUSED;
}
break;
case DRAW_PAUSED:
drawPauseMenu();
state = PAUSED;
break;
case PAUSED:
if (KEY_JUST_PRESSED(BUTTON_START, previousButtons, currentButtons)) {
// Clear screen
drawRectangle(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, BACKGROUND_COLOR);
state = PLAYING;
}
if (KEY_JUST_PRESSED(BUTTON_SELECT, previousButtons, currentButtons)) {
// Clear screen
state = MENU_DRAW;
}
break;
case LOST:
drawLost();
if (KEY_JUST_PRESSED(BUTTON_START, previousButtons, currentButtons)) {
// Clear screen
state = MENU_DRAW;
}
break;
case DRAW_RULES:
drawRules();
state = RULES;
break;
case RULES:
if (KEY_JUST_PRESSED(BUTTON_START, previousButtons, currentButtons)) {
state = MENU_DRAW;
}
break;
default:
break;
}
previousButtons = currentButtons;
}
return 0;
}
/**
* Logic to draw the main menu
*/
void drawMenu() {
waitForVblank();
drawFullScreenRectangle(BACKGROUND_COLOR);
drawImage(10, 65, 110, 58, title);
drawString(75, 60, "Press start to begin", TEXT_COLOR, BACKGROUND_COLOR);
drawString(85, 42, "Press select to read rules", TEXT_COLOR, BACKGROUND_COLOR);
char buffer[1024];
sprintf(buffer, "High score: %05u", highScore);
drawString(105, 68, buffer, TEXT_COLOR, BACKGROUND_COLOR);
}
/**
* Logic to draw the pause menu
*/
void drawPauseMenu() {
drawRectangle(24, 27, 186, 106, BLACK);
drawRectangle(27, 30, 180, 100, TEXT_COLOR);
drawString(29, 32, "Paused", WHITE, TEXT_COLOR);
drawString(46, 32, "Press start to continue", WHITE, TEXT_COLOR);
drawString(56, 32, "Select to quit", WHITE, TEXT_COLOR);
}
/**
* Logic to draw the rules
*/
void drawRules() {
waitForVblank();
drawRectangle(24, 27, 186, 106, BLACK);
drawRectangle(27, 30, 180, 100, TEXT_COLOR);
drawString(29, 32, "Rules", WHITE, TEXT_COLOR);
drawString(46, 32, "Press \"Up\" to jump", WHITE, TEXT_COLOR);
drawString(56, 32, "Avoid the enemies", WHITE, TEXT_COLOR);
drawString(66, 32, "Have fun", WHITE, TEXT_COLOR);
drawString(76, 32, "That's it :)", WHITE, TEXT_COLOR);
drawString(117, 32, "Start to close", WHITE, TEXT_COLOR);
}
/**
* Logic to draw the lost page
*/
void drawLost() {
waitForVblank();
drawString(29, 32, "You lost :(", WHITE, TEXT_COLOR);
char buffer[1024];
sprintf(buffer, "Your score: %u", score);
drawString(41, 32, buffer, WHITE, TEXT_COLOR);
drawString(117, 32, "Start to close", WHITE, TEXT_COLOR);
}
/**
* Clears the screen to background color
*/
void clearScreen() {
drawRectangle(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, BACKGROUND_COLOR);
}