Adam Baker 201166301
Dependencies: mbed Gamepad N5110
main.cpp@41:4b20f909bbcb, 2019-05-08 (annotated)
- Committer:
- adambakerwa
- Date:
- Wed May 08 15:22:53 2019 +0000
- Revision:
- 41:4b20f909bbcb
- Parent:
- 40:f53c70793975
- Child:
- 42:0dad7c359fa5
final amendments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adambakerwa | 41:4b20f909bbcb | 1 | ///////// pre-processor directives //////// |
adambakerwa | 1:a3cce487dffe | 2 | #include "mbed.h" |
adambakerwa | 1:a3cce487dffe | 3 | #include "Gamepad.h" |
adambakerwa | 1:a3cce487dffe | 4 | #include "N5110.h" |
adambakerwa | 25:230e4163cb6b | 5 | #include "BlockheadEngine.h" |
adambakerwa | 36:6f452777b9ce | 6 | #include "Menu.h" |
adambakerwa | 1:a3cce487dffe | 7 | |
adambakerwa | 41:4b20f909bbcb | 8 | /////////////// objects /////////////// |
adambakerwa | 1:a3cce487dffe | 9 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
adambakerwa | 1:a3cce487dffe | 10 | Gamepad pad; |
adambakerwa | 25:230e4163cb6b | 11 | BlockheadEngine blockhead; |
adambakerwa | 13:35a459a33bbd | 12 | Ticker ticker; |
adambakerwa | 36:6f452777b9ce | 13 | Menu menu; |
adambakerwa | 13:35a459a33bbd | 14 | |
adambakerwa | 41:4b20f909bbcb | 15 | ///////////// prototypes /////////////// |
adambakerwa | 13:35a459a33bbd | 16 | void timer_isr(); |
adambakerwa | 31:bf765e3961f1 | 17 | void init(); |
adambakerwa | 37:23b6312c8315 | 18 | void intro(N5110 &lcd, Gamepad &pad); |
adambakerwa | 26:3319ddd5111d | 19 | void main_menu(N5110 &lcd, Gamepad &pad); |
adambakerwa | 36:6f452777b9ce | 20 | int select_menu_item(Gamepad &pad, N5110 &lcd); |
adambakerwa | 36:6f452777b9ce | 21 | void go_to_main_menu_item(N5110 &lcd, Gamepad &pad, int input); |
adambakerwa | 26:3319ddd5111d | 22 | void menu_playgame(N5110 &lcd, Gamepad &pad); |
adambakerwa | 36:6f452777b9ce | 23 | void menu_highscore(N5110 &lcd, Gamepad &pad); |
adambakerwa | 36:6f452777b9ce | 24 | void menu_settings(N5110 &lcd, Gamepad &pad); |
adambakerwa | 36:6f452777b9ce | 25 | void menu_quit(N5110 &lcd, Gamepad &pad); |
adambakerwa | 31:bf765e3961f1 | 26 | void select_continue_menu_item(N5110 &lcd, Gamepad &pad, int input); |
adambakerwa | 31:bf765e3961f1 | 27 | void menu_continue(N5110 &lcd, Gamepad &pad); |
adambakerwa | 31:bf765e3961f1 | 28 | |
adambakerwa | 41:4b20f909bbcb | 29 | volatile int timer_flag = 0; //sets timer_flag to 0 |
adambakerwa | 31:bf765e3961f1 | 30 | |
adambakerwa | 41:4b20f909bbcb | 31 | |
adambakerwa | 41:4b20f909bbcb | 32 | ///////////// functions //////////////// |
adambakerwa | 13:35a459a33bbd | 33 | int main() |
adambakerwa | 13:35a459a33bbd | 34 | { |
adambakerwa | 13:35a459a33bbd | 35 | |
adambakerwa | 41:4b20f909bbcb | 36 | int fps = 6; //sets fps to 6 |
adambakerwa | 31:bf765e3961f1 | 37 | init(); |
adambakerwa | 41:4b20f909bbcb | 38 | ticker.attach(&timer_isr,1.0f/fps); //creates a ticker and attaches it to timer_isr (ticks 6 * per second) |
adambakerwa | 36:6f452777b9ce | 39 | |
adambakerwa | 41:4b20f909bbcb | 40 | lcd.setContrast(0.55); //intialy sets contrast to 0.55 |
adambakerwa | 24:f5dd1f3db693 | 41 | |
adambakerwa | 41:4b20f909bbcb | 42 | intro(lcd, pad); //runs intro sequence |
adambakerwa | 37:23b6312c8315 | 43 | |
adambakerwa | 41:4b20f909bbcb | 44 | main_menu(lcd, pad); //runs main menu ( finite state machine, including "play game" "highscore" "settings" and "quit..." ) |
adambakerwa | 41:4b20f909bbcb | 45 | |
adambakerwa | 13:35a459a33bbd | 46 | } |
adambakerwa | 13:35a459a33bbd | 47 | |
adambakerwa | 37:23b6312c8315 | 48 | |
adambakerwa | 41:4b20f909bbcb | 49 | //sets the timer_flag to 1 |
adambakerwa | 41:4b20f909bbcb | 50 | void timer_isr() |
adambakerwa | 13:35a459a33bbd | 51 | { |
adambakerwa | 13:35a459a33bbd | 52 | |
adambakerwa | 41:4b20f909bbcb | 53 | timer_flag = 1; // set flag in ISR |
adambakerwa | 1:a3cce487dffe | 54 | } |
adambakerwa | 1:a3cce487dffe | 55 | |
adambakerwa | 41:4b20f909bbcb | 56 | |
adambakerwa | 41:4b20f909bbcb | 57 | //intialises variables of below classes |
adambakerwa | 41:4b20f909bbcb | 58 | void init() |
adambakerwa | 31:bf765e3961f1 | 59 | { |
adambakerwa | 41:4b20f909bbcb | 60 | pad.init(); //intialise Gamepad class variables |
adambakerwa | 41:4b20f909bbcb | 61 | lcd.init(); //intialise N5110 class variables |
adambakerwa | 41:4b20f909bbcb | 62 | menu.init(); //intialise menu variables |
adambakerwa | 31:bf765e3961f1 | 63 | } |
adambakerwa | 31:bf765e3961f1 | 64 | |
adambakerwa | 41:4b20f909bbcb | 65 | |
adambakerwa | 41:4b20f909bbcb | 66 | //runs the intro sequence |
adambakerwa | 41:4b20f909bbcb | 67 | void intro(N5110 &lcd, Gamepad &pad) |
adambakerwa | 37:23b6312c8315 | 68 | { |
adambakerwa | 41:4b20f909bbcb | 69 | menu.title_intro(lcd, pad); //runs 'BLOCK HEAD' intro sequence |
adambakerwa | 37:23b6312c8315 | 70 | int start = 0; |
adambakerwa | 37:23b6312c8315 | 71 | |
adambakerwa | 37:23b6312c8315 | 72 | do { |
adambakerwa | 41:4b20f909bbcb | 73 | if (timer_flag == true) { //only run when timer flag is true (6fps) |
adambakerwa | 37:23b6312c8315 | 74 | |
adambakerwa | 37:23b6312c8315 | 75 | timer_flag = 0; |
adambakerwa | 37:23b6312c8315 | 76 | |
adambakerwa | 41:4b20f909bbcb | 77 | start = menu.press_start(lcd, pad); //flashes start untill start button is pressed |
adambakerwa | 37:23b6312c8315 | 78 | |
adambakerwa | 37:23b6312c8315 | 79 | } else { |
adambakerwa | 37:23b6312c8315 | 80 | |
adambakerwa | 41:4b20f909bbcb | 81 | sleep(); //sleep when timer_flag not true in order to conserve energy |
adambakerwa | 37:23b6312c8315 | 82 | |
adambakerwa | 37:23b6312c8315 | 83 | } |
adambakerwa | 37:23b6312c8315 | 84 | |
adambakerwa | 37:23b6312c8315 | 85 | |
adambakerwa | 37:23b6312c8315 | 86 | } while (start == 0); |
adambakerwa | 37:23b6312c8315 | 87 | |
adambakerwa | 41:4b20f909bbcb | 88 | menu.init(); //intilises menu variables |
adambakerwa | 26:3319ddd5111d | 89 | } |
adambakerwa | 26:3319ddd5111d | 90 | |
adambakerwa | 26:3319ddd5111d | 91 | |
adambakerwa | 41:4b20f909bbcb | 92 | //main menu state |
adambakerwa | 41:4b20f909bbcb | 93 | void main_menu(N5110 &lcd, Gamepad &pad) |
adambakerwa | 41:4b20f909bbcb | 94 | { |
adambakerwa | 41:4b20f909bbcb | 95 | |
adambakerwa | 41:4b20f909bbcb | 96 | blockhead.init(); //intialise blockhead engine variables for new game |
adambakerwa | 41:4b20f909bbcb | 97 | int input = select_menu_item(pad, lcd); //reads input and prints the main menu |
adambakerwa | 41:4b20f909bbcb | 98 | go_to_main_menu_item(lcd, pad, input); //peforms what ever input is chosen |
adambakerwa | 41:4b20f909bbcb | 99 | } |
adambakerwa | 36:6f452777b9ce | 100 | |
adambakerwa | 26:3319ddd5111d | 101 | |
adambakerwa | 41:4b20f909bbcb | 102 | //reads input and prints main menu |
adambakerwa | 41:4b20f909bbcb | 103 | int select_menu_item(Gamepad &pad, N5110 &lcd) |
adambakerwa | 36:6f452777b9ce | 104 | { |
adambakerwa | 37:23b6312c8315 | 105 | int input = 0; |
adambakerwa | 37:23b6312c8315 | 106 | do { |
adambakerwa | 40:f53c70793975 | 107 | |
adambakerwa | 41:4b20f909bbcb | 108 | if (timer_flag == true) { //only run when timer flag is true (6fps) |
adambakerwa | 37:23b6312c8315 | 109 | |
adambakerwa | 40:f53c70793975 | 110 | timer_flag = 0; |
adambakerwa | 40:f53c70793975 | 111 | |
adambakerwa | 41:4b20f909bbcb | 112 | input = menu.select_input_main(pad, lcd); //runs until input is selected |
adambakerwa | 40:f53c70793975 | 113 | |
adambakerwa | 37:23b6312c8315 | 114 | } else { |
adambakerwa | 40:f53c70793975 | 115 | |
adambakerwa | 41:4b20f909bbcb | 116 | sleep(); //sleep when timer_flag not true in order to conserve energy |
adambakerwa | 41:4b20f909bbcb | 117 | |
adambakerwa | 40:f53c70793975 | 118 | } |
adambakerwa | 41:4b20f909bbcb | 119 | } while (input == 0); //repeats untill an input is chosen |
adambakerwa | 26:3319ddd5111d | 120 | |
adambakerwa | 25:230e4163cb6b | 121 | return input; |
adambakerwa | 25:230e4163cb6b | 122 | } |
adambakerwa | 25:230e4163cb6b | 123 | |
adambakerwa | 41:4b20f909bbcb | 124 | |
adambakerwa | 41:4b20f909bbcb | 125 | //depending on input, goes to following menu items |
adambakerwa | 41:4b20f909bbcb | 126 | void go_to_main_menu_item(N5110 &lcd, Gamepad &pad, int input) |
adambakerwa | 26:3319ddd5111d | 127 | { |
adambakerwa | 41:4b20f909bbcb | 128 | switch (input) { |
adambakerwa | 26:3319ddd5111d | 129 | case 1: |
adambakerwa | 26:3319ddd5111d | 130 | menu_playgame(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 131 | break; |
adambakerwa | 26:3319ddd5111d | 132 | case 2: |
adambakerwa | 36:6f452777b9ce | 133 | menu_highscore(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 134 | break; |
adambakerwa | 36:6f452777b9ce | 135 | case 3: |
adambakerwa | 36:6f452777b9ce | 136 | menu_settings(lcd, pad); |
adambakerwa | 36:6f452777b9ce | 137 | case 4: |
adambakerwa | 36:6f452777b9ce | 138 | menu_quit(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 139 | default: |
adambakerwa | 26:3319ddd5111d | 140 | exit(1); |
adambakerwa | 26:3319ddd5111d | 141 | break; |
adambakerwa | 26:3319ddd5111d | 142 | } |
adambakerwa | 25:230e4163cb6b | 143 | } |
adambakerwa | 25:230e4163cb6b | 144 | |
adambakerwa | 41:4b20f909bbcb | 145 | |
adambakerwa | 41:4b20f909bbcb | 146 | //runs playgame menu state |
adambakerwa | 41:4b20f909bbcb | 147 | void menu_playgame(N5110 &lcd, Gamepad &pad) |
adambakerwa | 26:3319ddd5111d | 148 | { |
adambakerwa | 26:3319ddd5111d | 149 | int gameover = 0; |
adambakerwa | 31:bf765e3961f1 | 150 | |
adambakerwa | 26:3319ddd5111d | 151 | do { |
adambakerwa | 41:4b20f909bbcb | 152 | if (timer_flag == true) { //only run when timer flag is true (6fps) |
adambakerwa | 26:3319ddd5111d | 153 | |
adambakerwa | 41:4b20f909bbcb | 154 | timer_flag = 0; //if it has, clear the flag |
adambakerwa | 26:3319ddd5111d | 155 | |
adambakerwa | 41:4b20f909bbcb | 156 | gameover = blockhead.playgame(lcd, pad); //run game untill gameover |
adambakerwa | 26:3319ddd5111d | 157 | |
adambakerwa | 26:3319ddd5111d | 158 | } else { |
adambakerwa | 26:3319ddd5111d | 159 | |
adambakerwa | 41:4b20f909bbcb | 160 | sleep(); //sleep when timer_flag not true in order to conserve energy |
adambakerwa | 26:3319ddd5111d | 161 | |
adambakerwa | 26:3319ddd5111d | 162 | } |
adambakerwa | 26:3319ddd5111d | 163 | } while (gameover == 0); |
adambakerwa | 26:3319ddd5111d | 164 | |
adambakerwa | 41:4b20f909bbcb | 165 | menu_continue(lcd, pad); //once gameover occurs, go to continue menu |
adambakerwa | 26:3319ddd5111d | 166 | |
adambakerwa | 26:3319ddd5111d | 167 | } |
adambakerwa | 26:3319ddd5111d | 168 | |
adambakerwa | 41:4b20f909bbcb | 169 | |
adambakerwa | 41:4b20f909bbcb | 170 | //runs highscore menu state |
adambakerwa | 41:4b20f909bbcb | 171 | void menu_highscore(N5110 &lcd, Gamepad &pad) |
adambakerwa | 25:230e4163cb6b | 172 | { |
adambakerwa | 25:230e4163cb6b | 173 | |
adambakerwa | 26:3319ddd5111d | 174 | int goback = 0; |
adambakerwa | 36:6f452777b9ce | 175 | |
adambakerwa | 26:3319ddd5111d | 176 | do { |
adambakerwa | 25:230e4163cb6b | 177 | |
adambakerwa | 41:4b20f909bbcb | 178 | if (timer_flag == true) { //only run when timer flag is true (6fps) |
adambakerwa | 26:3319ddd5111d | 179 | |
adambakerwa | 41:4b20f909bbcb | 180 | timer_flag = 0; //if it has, clear the flag |
adambakerwa | 26:3319ddd5111d | 181 | |
adambakerwa | 41:4b20f909bbcb | 182 | int highscore = blockhead.highscore(); //gets high score from blockhead engine |
adambakerwa | 36:6f452777b9ce | 183 | |
adambakerwa | 41:4b20f909bbcb | 184 | menu.print_highscore(lcd, highscore); //prints high score menu, with high score |
adambakerwa | 26:3319ddd5111d | 185 | |
adambakerwa | 41:4b20f909bbcb | 186 | if (pad.check_event(Gamepad::B_PRESSED) || pad.check_event(Gamepad::BACK_PRESSED)) { //B or Back button to go back to main menu |
adambakerwa | 26:3319ddd5111d | 187 | goback = 1; |
adambakerwa | 26:3319ddd5111d | 188 | } |
adambakerwa | 26:3319ddd5111d | 189 | |
adambakerwa | 26:3319ddd5111d | 190 | } else { |
adambakerwa | 26:3319ddd5111d | 191 | |
adambakerwa | 41:4b20f909bbcb | 192 | sleep(); //sleep when timer_flag not true in order to conserve energy |
adambakerwa | 26:3319ddd5111d | 193 | |
adambakerwa | 26:3319ddd5111d | 194 | } |
adambakerwa | 26:3319ddd5111d | 195 | } while (goback == 0); |
adambakerwa | 37:23b6312c8315 | 196 | |
adambakerwa | 41:4b20f909bbcb | 197 | main_menu(lcd, pad); //once b or back pressed, go to main menu |
adambakerwa | 36:6f452777b9ce | 198 | } |
adambakerwa | 26:3319ddd5111d | 199 | |
adambakerwa | 41:4b20f909bbcb | 200 | |
adambakerwa | 41:4b20f909bbcb | 201 | //runs settings menu state |
adambakerwa | 41:4b20f909bbcb | 202 | void menu_settings(N5110 &lcd, Gamepad &pad) |
adambakerwa | 36:6f452777b9ce | 203 | { |
adambakerwa | 36:6f452777b9ce | 204 | |
adambakerwa | 36:6f452777b9ce | 205 | int goback = 0; |
adambakerwa | 40:f53c70793975 | 206 | float contrast; |
adambakerwa | 36:6f452777b9ce | 207 | |
adambakerwa | 36:6f452777b9ce | 208 | do { |
adambakerwa | 36:6f452777b9ce | 209 | |
adambakerwa | 41:4b20f909bbcb | 210 | if (timer_flag == true) { //only run when timer flag is true (6fps) |
adambakerwa | 36:6f452777b9ce | 211 | |
adambakerwa | 41:4b20f909bbcb | 212 | timer_flag = 0; //if it has, clear the flag |
adambakerwa | 36:6f452777b9ce | 213 | |
adambakerwa | 41:4b20f909bbcb | 214 | contrast = menu.print_settings(pad, lcd); //run comtast menu and return contast |
adambakerwa | 41:4b20f909bbcb | 215 | lcd.setContrast(contrast); //update lcd contast |
adambakerwa | 40:f53c70793975 | 216 | |
adambakerwa | 41:4b20f909bbcb | 217 | if (pad.check_event(Gamepad::B_PRESSED || pad.check_event(Gamepad::BACK_PRESSED))) { |
adambakerwa | 36:6f452777b9ce | 218 | goback = 1; |
adambakerwa | 36:6f452777b9ce | 219 | } |
adambakerwa | 36:6f452777b9ce | 220 | |
adambakerwa | 36:6f452777b9ce | 221 | } else { |
adambakerwa | 36:6f452777b9ce | 222 | |
adambakerwa | 41:4b20f909bbcb | 223 | sleep(); //sleep when timer_flag not true in order to conserve energy |
adambakerwa | 36:6f452777b9ce | 224 | |
adambakerwa | 36:6f452777b9ce | 225 | } |
adambakerwa | 36:6f452777b9ce | 226 | } while (goback == 0); |
adambakerwa | 41:4b20f909bbcb | 227 | main_menu(lcd, pad); //go to main menu once b or back is pressed |
adambakerwa | 26:3319ddd5111d | 228 | } |
adambakerwa | 25:230e4163cb6b | 229 | |
adambakerwa | 36:6f452777b9ce | 230 | void menu_quit(N5110 &lcd, Gamepad &pad) |
adambakerwa | 36:6f452777b9ce | 231 | { |
adambakerwa | 36:6f452777b9ce | 232 | lcd.turnOff(); |
adambakerwa | 36:6f452777b9ce | 233 | sleep(); |
adambakerwa | 37:23b6312c8315 | 234 | |
adambakerwa | 36:6f452777b9ce | 235 | } |
adambakerwa | 36:6f452777b9ce | 236 | |
adambakerwa | 25:230e4163cb6b | 237 | |
adambakerwa | 41:4b20f909bbcb | 238 | //depending on input, goes to following continue menu items |
adambakerwa | 41:4b20f909bbcb | 239 | void select_continue_menu_item(N5110 &lcd, Gamepad &pad, int input) |
adambakerwa | 31:bf765e3961f1 | 240 | { |
adambakerwa | 31:bf765e3961f1 | 241 | switch (input) { |
adambakerwa | 31:bf765e3961f1 | 242 | case 1: |
adambakerwa | 41:4b20f909bbcb | 243 | menu_playgame(lcd, pad); //if play game, level will be the one you were just on |
adambakerwa | 31:bf765e3961f1 | 244 | break; |
adambakerwa | 31:bf765e3961f1 | 245 | case 2: |
adambakerwa | 41:4b20f909bbcb | 246 | main_menu(lcd, pad); //if menu, game resets |
adambakerwa | 31:bf765e3961f1 | 247 | break; |
adambakerwa | 31:bf765e3961f1 | 248 | default: |
adambakerwa | 31:bf765e3961f1 | 249 | exit(1); |
adambakerwa | 31:bf765e3961f1 | 250 | break; |
adambakerwa | 31:bf765e3961f1 | 251 | } |
adambakerwa | 31:bf765e3961f1 | 252 | } |
adambakerwa | 31:bf765e3961f1 | 253 | |
adambakerwa | 41:4b20f909bbcb | 254 | |
adambakerwa | 41:4b20f909bbcb | 255 | //runs continue game state |
adambakerwa | 41:4b20f909bbcb | 256 | void menu_continue(N5110 &lcd, Gamepad &pad) |
adambakerwa | 31:bf765e3961f1 | 257 | { |
adambakerwa | 41:4b20f909bbcb | 258 | blockhead.continueInit(); //intialised continue game variables (level does not change) |
adambakerwa | 40:f53c70793975 | 259 | menu.init(); |
adambakerwa | 37:23b6312c8315 | 260 | |
adambakerwa | 36:6f452777b9ce | 261 | int input = 0; |
adambakerwa | 37:23b6312c8315 | 262 | do { |
adambakerwa | 37:23b6312c8315 | 263 | |
adambakerwa | 41:4b20f909bbcb | 264 | if (timer_flag == true) { //only run when timer flag is true (6fps) |
adambakerwa | 41:4b20f909bbcb | 265 | |
adambakerwa | 41:4b20f909bbcb | 266 | timer_flag = 0; //if it has, clear the flag |
adambakerwa | 40:f53c70793975 | 267 | |
adambakerwa | 41:4b20f909bbcb | 268 | input = menu.select_input_continue(pad, lcd); //prints continue menu, and selects coninue menu item |
adambakerwa | 37:23b6312c8315 | 269 | |
adambakerwa | 41:4b20f909bbcb | 270 | } else { |
adambakerwa | 41:4b20f909bbcb | 271 | |
adambakerwa | 41:4b20f909bbcb | 272 | sleep(); //sleep when timer_flag not true in order to conserve energy |
adambakerwa | 40:f53c70793975 | 273 | |
adambakerwa | 37:23b6312c8315 | 274 | } |
adambakerwa | 40:f53c70793975 | 275 | } while (input == 0); |
adambakerwa | 41:4b20f909bbcb | 276 | select_continue_menu_item(lcd, pad, input); //go to what ever item has been selected |
adambakerwa | 31:bf765e3961f1 | 277 | } |
adambakerwa | 31:bf765e3961f1 | 278 |