Project Submission (late)
Dependencies: mbed
main.cpp@3:83e79d31930c, 2019-05-10 (annotated)
- Committer:
- el17tc
- Date:
- Fri May 10 14:52:28 2019 +0000
- Revision:
- 3:83e79d31930c
- Parent:
- 2:43bb635db736
final commit, API is added.; I'm not sure if there is a specific statement of academic integrity wanted but- I declare that this work is 100% my own, I have not plagiarised any work or attempted to fabricate any part of it for extra marks.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17tc | 3:83e79d31930c | 1 | /* |
el17tc | 3:83e79d31930c | 2 | ELEC2645 Embedded Systems Project |
el17tc | 3:83e79d31930c | 3 | School of Electronic & Electrical Engineering |
el17tc | 3:83e79d31930c | 4 | University of Leeds |
el17tc | 3:83e79d31930c | 5 | Name: Thomas Caine |
el17tc | 3:83e79d31930c | 6 | Username: el175c |
el17tc | 3:83e79d31930c | 7 | Student ID Number: 201127594 |
el17tc | 3:83e79d31930c | 8 | Date: 10/05/2019 |
el17tc | 3:83e79d31930c | 9 | */ |
el17tc | 3:83e79d31930c | 10 | |
el17tc | 3:83e79d31930c | 11 | |
el17tc | 0:72f372170a73 | 12 | #include "mbed.h" |
el17tc | 0:72f372170a73 | 13 | #include "Gamepad.h" |
el17tc | 0:72f372170a73 | 14 | #include "N5110.h" |
el17tc | 0:72f372170a73 | 15 | #include "Maze.h" |
el17tc | 0:72f372170a73 | 16 | #include "Vector2Di.h" |
el17tc | 0:72f372170a73 | 17 | #include "Player.h" |
el17tc | 0:72f372170a73 | 18 | #include "Drawer.h" |
el17tc | 0:72f372170a73 | 19 | #include "MainMenu.h" |
el17tc | 2:43bb635db736 | 20 | #include "DefeatMenu.h" |
el17tc | 2:43bb635db736 | 21 | #include "VictoryMenu.h" |
el17tc | 0:72f372170a73 | 22 | |
el17tc | 3:83e79d31930c | 23 | /* Note: |
el17tc | 3:83e79d31930c | 24 | If it were up to me almost everything in this file would be in a Game object and main would only have game.init(); game.run(); in it |
el17tc | 3:83e79d31930c | 25 | Unfortunately I have been plagued by L6312W the empty execution region error (not the warning) all the way through this project |
el17tc | 3:83e79d31930c | 26 | and this is as good as I could get it in terms of object orientation. |
el17tc | 3:83e79d31930c | 27 | As far as I'm aware this error is a bug with the mbed compiler of this version but it can be worked around. |
el17tc | 3:83e79d31930c | 28 | Dock marks as you see fit for not encapsulating the other headers into a single class. |
el17tc | 3:83e79d31930c | 29 | (but the other classes are nice and object-oriented, right?) |
el17tc | 3:83e79d31930c | 30 | */ |
el17tc | 3:83e79d31930c | 31 | |
el17tc | 3:83e79d31930c | 32 | |
el17tc | 0:72f372170a73 | 33 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el17tc | 2:43bb635db736 | 34 | Gamepad gamepad; |
el17tc | 3:83e79d31930c | 35 | /** Causes the currentMenu's button index to be decremented, moving the cursor to the higher button |
el17tc | 3:83e79d31930c | 36 | */ |
el17tc | 2:43bb635db736 | 37 | void buttonPressedUp() { |
el17tc | 2:43bb635db736 | 38 | if (currentMenu->buttonIndex > 0) { |
el17tc | 2:43bb635db736 | 39 | currentMenu->buttonIndex -= 1; |
el17tc | 2:43bb635db736 | 40 | currentButton = currentMenu->buttons[currentMenu->buttonIndex]; |
el17tc | 2:43bb635db736 | 41 | } |
el17tc | 2:43bb635db736 | 42 | printf("currentMenu->buttonIndex = %d\n", currentMenu->buttonIndex); |
el17tc | 2:43bb635db736 | 43 | } |
el17tc | 2:43bb635db736 | 44 | |
el17tc | 3:83e79d31930c | 45 | /** Causes the currentMenu's button index to be incremented, moving the cursor to the lower button |
el17tc | 3:83e79d31930c | 46 | */ |
el17tc | 2:43bb635db736 | 47 | void buttonPressedDown() { |
el17tc | 2:43bb635db736 | 48 | if (currentMenu->buttonIndex < (currentMenu->numOfButtons - 1)) { |
el17tc | 2:43bb635db736 | 49 | currentMenu->buttonIndex += 1; |
el17tc | 2:43bb635db736 | 50 | currentButton = currentMenu->buttons[currentMenu->buttonIndex]; |
el17tc | 2:43bb635db736 | 51 | } |
el17tc | 2:43bb635db736 | 52 | printf("currentMenu->buttonIndex = %d\n", currentMenu->buttonIndex); |
el17tc | 2:43bb635db736 | 53 | } |
el17tc | 2:43bb635db736 | 54 | |
el17tc | 2:43bb635db736 | 55 | Ticker arrowFlicker; |
el17tc | 2:43bb635db736 | 56 | Ticker ingameCounter; |
el17tc | 2:43bb635db736 | 57 | bool arrowFlickFlag = 1; |
el17tc | 2:43bb635db736 | 58 | int ingameTime; |
el17tc | 2:43bb635db736 | 59 | |
el17tc | 3:83e79d31930c | 60 | /** Funciton for drawing the ingame Timer |
el17tc | 3:83e79d31930c | 61 | */ |
el17tc | 2:43bb635db736 | 62 | void drawTimer() { |
el17tc | 2:43bb635db736 | 63 | lcd.drawRect(1,1,20,8,FILL_WHITE); |
el17tc | 2:43bb635db736 | 64 | std::stringstream ss; |
el17tc | 2:43bb635db736 | 65 | ss << ingameTime; |
el17tc | 2:43bb635db736 | 66 | std::string timeStr = "Time:" + ss.str(); |
el17tc | 2:43bb635db736 | 67 | lcd.printString(timeStr.c_str(), 1,0); |
el17tc | 2:43bb635db736 | 68 | } |
el17tc | 3:83e79d31930c | 69 | /** Timer increment/decrement function |
el17tc | 3:83e79d31930c | 70 | * @brief depending on the timerFlag (toggled on or off) it will count up or down |
el17tc | 3:83e79d31930c | 71 | */ |
el17tc | 2:43bb635db736 | 72 | void timerFunc() { |
el17tc | 2:43bb635db736 | 73 | if (timerFlag) |
el17tc | 2:43bb635db736 | 74 | ingameTime--; |
el17tc | 2:43bb635db736 | 75 | else |
el17tc | 2:43bb635db736 | 76 | ingameTime++; |
el17tc | 2:43bb635db736 | 77 | } |
el17tc | 3:83e79d31930c | 78 | |
el17tc | 3:83e79d31930c | 79 | /** Funciton making the button selection arrow flicker |
el17tc | 3:83e79d31930c | 80 | * @brief aesthetically pleasing (i think). |
el17tc | 3:83e79d31930c | 81 | */ |
el17tc | 2:43bb635db736 | 82 | void selectionArrowFunc() { |
el17tc | 2:43bb635db736 | 83 | arrowFlickFlag = !arrowFlickFlag; |
el17tc | 0:72f372170a73 | 84 | }; |
el17tc | 0:72f372170a73 | 85 | |
el17tc | 3:83e79d31930c | 86 | /** The main function |
el17tc | 3:83e79d31930c | 87 | * Due to constantly getting Empty Execution region errors this is a lot more full than it should be |
el17tc | 3:83e79d31930c | 88 | * refer to note at the top of main.cpp for more comments on the issue. |
el17tc | 3:83e79d31930c | 89 | */ |
el17tc | 0:72f372170a73 | 90 | int main() { |
el17tc | 2:43bb635db736 | 91 | gamepad.init(); |
el17tc | 0:72f372170a73 | 92 | lcd.init(); |
el17tc | 0:72f372170a73 | 93 | lcd.setContrast(0.5); |
el17tc | 0:72f372170a73 | 94 | lcd.normalMode(); |
el17tc | 0:72f372170a73 | 95 | lcd.setBrightness(0.5); |
el17tc | 0:72f372170a73 | 96 | |
el17tc | 2:43bb635db736 | 97 | arrowFlicker.attach(&selectionArrowFunc,0.9); |
el17tc | 0:72f372170a73 | 98 | currentMenu = new MainMenu(&lcd); |
el17tc | 0:72f372170a73 | 99 | |
el17tc | 2:43bb635db736 | 100 | // program loop - never exits |
el17tc | 0:72f372170a73 | 101 | while (true) { |
el17tc | 2:43bb635db736 | 102 | // menu loop |
el17tc | 2:43bb635db736 | 103 | gamepad.check_event(Gamepad::START_PRESSED); // consume trailing input from last loop |
el17tc | 2:43bb635db736 | 104 | while (true) { |
el17tc | 2:43bb635db736 | 105 | |
el17tc | 2:43bb635db736 | 106 | if (gamepad.check_event(Gamepad::A_PRESSED)) { |
el17tc | 2:43bb635db736 | 107 | buttonPressedDown(); |
el17tc | 2:43bb635db736 | 108 | } |
el17tc | 2:43bb635db736 | 109 | if (gamepad.check_event(Gamepad::Y_PRESSED)) { |
el17tc | 2:43bb635db736 | 110 | buttonPressedUp(); |
el17tc | 2:43bb635db736 | 111 | } |
el17tc | 2:43bb635db736 | 112 | if (gamepad.check_event(Gamepad::X_PRESSED)) { |
el17tc | 2:43bb635db736 | 113 | currentButton->runBack(); |
el17tc | 2:43bb635db736 | 114 | gamepad.check_event(Gamepad::X_PRESSED); // consume second input if it appeared |
el17tc | 2:43bb635db736 | 115 | } |
el17tc | 2:43bb635db736 | 116 | if (gamepad.check_event(Gamepad::B_PRESSED)) { |
el17tc | 2:43bb635db736 | 117 | currentButton->run(); |
el17tc | 2:43bb635db736 | 118 | gamepad.check_event(Gamepad::B_PRESSED); // consume second input if it appeared |
el17tc | 2:43bb635db736 | 119 | } |
el17tc | 2:43bb635db736 | 120 | if (gamepad.check_event(Gamepad::START_PRESSED)) { |
el17tc | 2:43bb635db736 | 121 | currentButton->run(); |
el17tc | 2:43bb635db736 | 122 | gamepad.check_event(Gamepad::START_PRESSED); // consume second input if it appeared |
el17tc | 2:43bb635db736 | 123 | } |
el17tc | 2:43bb635db736 | 124 | if (gamepad.check_event(Gamepad::BACK_PRESSED)) { |
el17tc | 2:43bb635db736 | 125 | delete currentMenu; |
el17tc | 2:43bb635db736 | 126 | currentMenu = new MainMenu(&lcd); |
el17tc | 2:43bb635db736 | 127 | } |
el17tc | 2:43bb635db736 | 128 | |
el17tc | 2:43bb635db736 | 129 | lcd.clear(); |
el17tc | 2:43bb635db736 | 130 | currentMenu->draw(); |
el17tc | 2:43bb635db736 | 131 | |
el17tc | 2:43bb635db736 | 132 | if (arrowFlickFlag) |
el17tc | 2:43bb635db736 | 133 | lcd.drawSprite((currentButton->x)-2,(currentButton->y),7,4, (int *)selectArrow2); |
el17tc | 2:43bb635db736 | 134 | |
el17tc | 2:43bb635db736 | 135 | lcd.refresh(); |
el17tc | 2:43bb635db736 | 136 | if (beginFlag) { |
el17tc | 2:43bb635db736 | 137 | beginFlag = false; |
el17tc | 2:43bb635db736 | 138 | printf("begin flag is true\n"); |
el17tc | 2:43bb635db736 | 139 | break; |
el17tc | 2:43bb635db736 | 140 | } |
el17tc | 2:43bb635db736 | 141 | sleep(); |
el17tc | 2:43bb635db736 | 142 | } |
el17tc | 2:43bb635db736 | 143 | delete currentMenu; |
el17tc | 2:43bb635db736 | 144 | |
el17tc | 2:43bb635db736 | 145 | Maze maze; |
el17tc | 2:43bb635db736 | 146 | Player player(&maze); |
el17tc | 2:43bb635db736 | 147 | maze.selectMaze(mazeSize); |
el17tc | 2:43bb635db736 | 148 | player.pos.x = maze.startX; |
el17tc | 2:43bb635db736 | 149 | player.pos.y = maze.startY; |
el17tc | 2:43bb635db736 | 150 | Drawer drawer(&player, &lcd); |
el17tc | 2:43bb635db736 | 151 | |
el17tc | 2:43bb635db736 | 152 | if (timerFlag) |
el17tc | 2:43bb635db736 | 153 | ingameTime = maze.timeToFinish; |
el17tc | 2:43bb635db736 | 154 | else |
el17tc | 2:43bb635db736 | 155 | ingameTime = 0; |
el17tc | 2:43bb635db736 | 156 | ingameCounter.attach(&timerFunc, 1); |
el17tc | 2:43bb635db736 | 157 | arrowFlicker.detach(); |
el17tc | 2:43bb635db736 | 158 | bool winFlag = false; |
el17tc | 2:43bb635db736 | 159 | |
el17tc | 2:43bb635db736 | 160 | // Main game loop |
el17tc | 2:43bb635db736 | 161 | while (true) { |
el17tc | 2:43bb635db736 | 162 | if (gamepad.check_event(Gamepad::Y_PRESSED)) { |
el17tc | 2:43bb635db736 | 163 | if (player.checkLocation(player.pos, 0, 2)) { |
el17tc | 2:43bb635db736 | 164 | winFlag = true; |
el17tc | 2:43bb635db736 | 165 | } |
el17tc | 2:43bb635db736 | 166 | player.walk(); |
el17tc | 2:43bb635db736 | 167 | wait(0.2); |
el17tc | 2:43bb635db736 | 168 | if (gamepad.check_event(Gamepad::Y_PRESSED)) { |
el17tc | 2:43bb635db736 | 169 | printf("walked but input fired twice\n"); |
el17tc | 2:43bb635db736 | 170 | } |
el17tc | 2:43bb635db736 | 171 | } |
el17tc | 2:43bb635db736 | 172 | if (gamepad.check_event(Gamepad::X_PRESSED)) { |
el17tc | 2:43bb635db736 | 173 | player.turnLeft(); |
el17tc | 2:43bb635db736 | 174 | wait(0.2); |
el17tc | 2:43bb635db736 | 175 | if (gamepad.check_event(Gamepad::X_PRESSED)) { |
el17tc | 2:43bb635db736 | 176 | printf("turned left but input fired twice\n"); |
el17tc | 2:43bb635db736 | 177 | } |
el17tc | 2:43bb635db736 | 178 | } |
el17tc | 2:43bb635db736 | 179 | if (gamepad.check_event(Gamepad::B_PRESSED)) { |
el17tc | 2:43bb635db736 | 180 | player.turnRight(); |
el17tc | 2:43bb635db736 | 181 | wait(0.2); |
el17tc | 2:43bb635db736 | 182 | if (gamepad.check_event(Gamepad::B_PRESSED)) { |
el17tc | 2:43bb635db736 | 183 | printf("turned right but input fired twice\n"); |
el17tc | 2:43bb635db736 | 184 | } |
el17tc | 2:43bb635db736 | 185 | } |
el17tc | 2:43bb635db736 | 186 | if (gamepad.check_event(Gamepad::A_PRESSED)) { |
el17tc | 2:43bb635db736 | 187 | player.stepBack(); |
el17tc | 2:43bb635db736 | 188 | wait(0.2); |
el17tc | 2:43bb635db736 | 189 | if (gamepad.check_event(Gamepad::A_PRESSED)) { |
el17tc | 2:43bb635db736 | 190 | printf("stepped back but input fired twice\n"); |
el17tc | 2:43bb635db736 | 191 | } |
el17tc | 2:43bb635db736 | 192 | } |
el17tc | 2:43bb635db736 | 193 | if (gamepad.check_event(Gamepad::R_PRESSED)) { |
el17tc | 2:43bb635db736 | 194 | player.turnRight(); |
el17tc | 2:43bb635db736 | 195 | wait(0.2); |
el17tc | 2:43bb635db736 | 196 | if (gamepad.check_event(Gamepad::R_PRESSED)) { |
el17tc | 2:43bb635db736 | 197 | printf("turned right but input fired twice\n"); |
el17tc | 2:43bb635db736 | 198 | } |
el17tc | 2:43bb635db736 | 199 | } |
el17tc | 2:43bb635db736 | 200 | if (gamepad.check_event(Gamepad::L_PRESSED)) { |
el17tc | 2:43bb635db736 | 201 | player.turnLeft(); |
el17tc | 2:43bb635db736 | 202 | wait(0.2); |
el17tc | 2:43bb635db736 | 203 | if (gamepad.check_event(Gamepad::L_PRESSED)) { |
el17tc | 2:43bb635db736 | 204 | printf("turned left but input fired twice\n"); |
el17tc | 2:43bb635db736 | 205 | } |
el17tc | 2:43bb635db736 | 206 | } |
el17tc | 2:43bb635db736 | 207 | |
el17tc | 2:43bb635db736 | 208 | if (winFlag) { |
el17tc | 2:43bb635db736 | 209 | printf("Player has escaped the maze\n"); |
el17tc | 2:43bb635db736 | 210 | // player has won, end the game loop |
el17tc | 2:43bb635db736 | 211 | winFlag = false; |
el17tc | 2:43bb635db736 | 212 | currentMenu = new VictoryMenu(&lcd); |
el17tc | 2:43bb635db736 | 213 | if (timerFlag) { |
el17tc | 2:43bb635db736 | 214 | currentMenu->score = maze.timeToFinish + (ingameTime * 2); |
el17tc | 2:43bb635db736 | 215 | } else { |
el17tc | 2:43bb635db736 | 216 | currentMenu->score = maze.timeToFinish + ((maze.timeToFinish*10)/ingameTime); |
el17tc | 2:43bb635db736 | 217 | } |
el17tc | 2:43bb635db736 | 218 | break; |
el17tc | 2:43bb635db736 | 219 | } |
el17tc | 2:43bb635db736 | 220 | if (timerFlag && (ingameTime <= 0)) { |
el17tc | 2:43bb635db736 | 221 | printf("Player has ran out of time\n"); |
el17tc | 2:43bb635db736 | 222 | // player has lost, end the game loop |
el17tc | 2:43bb635db736 | 223 | currentMenu = new DefeatMenu(&lcd); |
el17tc | 2:43bb635db736 | 224 | currentMenu->score = 0; |
el17tc | 2:43bb635db736 | 225 | break; |
el17tc | 2:43bb635db736 | 226 | } |
el17tc | 2:43bb635db736 | 227 | printf("========\n"); |
el17tc | 2:43bb635db736 | 228 | |
el17tc | 2:43bb635db736 | 229 | lcd.clear(); |
el17tc | 2:43bb635db736 | 230 | drawer.drawScreen(); |
el17tc | 2:43bb635db736 | 231 | drawTimer(); |
el17tc | 2:43bb635db736 | 232 | lcd.refresh(); |
el17tc | 2:43bb635db736 | 233 | |
el17tc | 2:43bb635db736 | 234 | sleep(); |
el17tc | 2:43bb635db736 | 235 | } |
el17tc | 2:43bb635db736 | 236 | |
el17tc | 2:43bb635db736 | 237 | arrowFlicker.attach(&selectionArrowFunc,0.9); |
el17tc | 2:43bb635db736 | 238 | gamepad.check_event(Gamepad::START_PRESSED); |
el17tc | 2:43bb635db736 | 239 | while (true) { |
el17tc | 2:43bb635db736 | 240 | if (gamepad.check_event(Gamepad::B_PRESSED)) { |
el17tc | 2:43bb635db736 | 241 | buttonPressedDown(); |
el17tc | 2:43bb635db736 | 242 | } |
el17tc | 2:43bb635db736 | 243 | if (gamepad.check_event(Gamepad::X_PRESSED)) { |
el17tc | 2:43bb635db736 | 244 | buttonPressedUp(); |
el17tc | 2:43bb635db736 | 245 | } |
el17tc | 2:43bb635db736 | 246 | if (gamepad.check_event(Gamepad::A_PRESSED)) { |
el17tc | 2:43bb635db736 | 247 | //buttonActivate(*currentButton); |
el17tc | 2:43bb635db736 | 248 | } |
el17tc | 2:43bb635db736 | 249 | if (gamepad.check_event(Gamepad::START_PRESSED)) { |
el17tc | 2:43bb635db736 | 250 | currentButton->run(); |
el17tc | 2:43bb635db736 | 251 | } |
el17tc | 2:43bb635db736 | 252 | lcd.clear(); |
el17tc | 2:43bb635db736 | 253 | currentMenu->draw(); |
el17tc | 2:43bb635db736 | 254 | if (arrowFlickFlag) { |
el17tc | 2:43bb635db736 | 255 | lcd.drawSprite((currentButton->x)-2,(currentButton->y),7,4, |
el17tc | 2:43bb635db736 | 256 | (int *)selectArrow2); |
el17tc | 2:43bb635db736 | 257 | } |
el17tc | 2:43bb635db736 | 258 | if (restartFlag) { |
el17tc | 2:43bb635db736 | 259 | restartFlag = false; |
el17tc | 2:43bb635db736 | 260 | delete currentMenu; |
el17tc | 2:43bb635db736 | 261 | currentMenu = new StartMenu(&lcd); |
el17tc | 2:43bb635db736 | 262 | break; |
el17tc | 2:43bb635db736 | 263 | } |
el17tc | 2:43bb635db736 | 264 | if (menuFlag) { |
el17tc | 2:43bb635db736 | 265 | menuFlag = false; |
el17tc | 2:43bb635db736 | 266 | delete currentMenu; |
el17tc | 2:43bb635db736 | 267 | currentMenu = new MainMenu(&lcd); |
el17tc | 2:43bb635db736 | 268 | break; |
el17tc | 2:43bb635db736 | 269 | } |
el17tc | 2:43bb635db736 | 270 | lcd.refresh(); |
el17tc | 2:43bb635db736 | 271 | sleep(); |
el17tc | 2:43bb635db736 | 272 | } |
el17tc | 2:43bb635db736 | 273 | } |
el17tc | 2:43bb635db736 | 274 | } |