![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Adam Baker 201166301
Dependencies: mbed Gamepad N5110
main.cpp
- Committer:
- adambakerwa
- Date:
- 2019-05-07
- Revision:
- 40:f53c70793975
- Parent:
- 38:51ed5820ffe5
- Child:
- 41:4b20f909bbcb
File content as of revision 40:f53c70793975:
#include "mbed.h" #include "Gamepad.h" #include "N5110.h" #include "BlockheadEngine.h" #include "Menu.h" N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); Gamepad pad; BlockheadEngine blockhead; Ticker ticker; Menu menu; volatile int timer_flag = 0; void timer_isr(); void init(); void intro(N5110 &lcd, Gamepad &pad); void main_menu(N5110 &lcd, Gamepad &pad); int select_menu_item(Gamepad &pad, N5110 &lcd); void go_to_main_menu_item(N5110 &lcd, Gamepad &pad, int input); void menu_playgame(N5110 &lcd, Gamepad &pad); void menu_highscore(N5110 &lcd, Gamepad &pad); void menu_settings(N5110 &lcd, Gamepad &pad); void menu_quit(N5110 &lcd, Gamepad &pad); void select_continue_menu_item(N5110 &lcd, Gamepad &pad, int input); void menu_continue(N5110 &lcd, Gamepad &pad); int main() { int fps = 6; //6 init(); ticker.attach(&timer_isr,1.0f/fps); lcd.setContrast(0.55); intro(lcd, pad); main_menu(lcd, pad); } void timer_isr() { timer_flag = 1; // set flag in ISR } void init() { pad.init(); //intialise Gamepad class variables lcd.init(); menu.init(); //intialise N5110 class variables } void intro(N5110 &lcd, Gamepad &pad) { menu.title_intro(lcd, pad); int start = 0; do { if (timer_flag == true) { //only run when timer flag is true (6fps) timer_flag = 0; start = menu.press_start(lcd, pad); } else { sleep(); //sleep when timer_flag not true in order to conserve energy } } while (start == 0); menu.init(); } void main_menu(N5110 &lcd, Gamepad &pad) { blockhead.init(); //intialise blockhead engine variables for new game int input = select_menu_item(pad, lcd); //reads input and prints the main menu go_to_main_menu_item(lcd, pad, input); //peforms what ever input is chosen } int select_menu_item(Gamepad &pad, N5110 &lcd) { int input = 0; do { if (timer_flag == true) { //only run when timer flag is true (6fps) timer_flag = 0; input = menu.select_input_main(pad, lcd); } else { sleep(); } } while (input == 0); //repeats untill an input is chosen return input; } void go_to_main_menu_item(N5110 &lcd, Gamepad &pad, int input) { switch (input) { //depending on input, performes following menu items case 1: menu_playgame(lcd, pad); break; case 2: menu_highscore(lcd, pad); break; case 3: menu_settings(lcd, pad); case 4: menu_quit(lcd, pad); default: exit(1); break; } } void menu_playgame(N5110 &lcd, Gamepad &pad) { int gameover = 0; do { if (timer_flag == true) { //only run when timer flag is true (6fps) timer_flag = 0; //if it has, clear the flag gameover = blockhead.playgame(lcd, pad); //run game untill gameover } else { sleep(); //sleep when timer_flag not true in order to conserve energy } } while (gameover == 0); menu_continue(lcd, pad); //once gameover occurs, go to continue menu (LETS TRY GET LIVES INVOLVED MAYBE!!!))) } void menu_highscore(N5110 &lcd, Gamepad &pad) { int goback = 0; do { if (timer_flag == true) { timer_flag = 0; int highscore = blockhead.highscore(); menu.print_highscore(lcd, highscore); if (pad.check_event(Gamepad::B_PRESSED)) { goback = 1; } } else { sleep(); } } while (goback == 0); main_menu(lcd, pad); } void menu_settings(N5110 &lcd, Gamepad &pad) { int goback = 0; float contrast; do { if (timer_flag == true) { timer_flag = 0; contrast = menu.print_settings(pad, lcd); lcd.setContrast(contrast); if (pad.check_event(Gamepad::B_PRESSED)) { goback = 1; } } else { sleep(); } } while (goback == 0); main_menu(lcd, pad); } void menu_quit(N5110 &lcd, Gamepad &pad) { lcd.turnOff(); sleep(); } void select_continue_menu_item(N5110 &lcd, Gamepad &pad, int input) { switch (input) { case 1: menu_playgame(lcd, pad); break; case 2: main_menu(lcd, pad); break; default: exit(1); break; } } void menu_continue(N5110 &lcd, Gamepad &pad) { blockhead.continueInit(); menu.init(); int input = 0; do { if (timer_flag == true) { timer_flag = 0; input = menu.select_input_continue(pad, lcd); } } while (input == 0); select_continue_menu_item(lcd, pad, input); } //rtos /** char buffer[14]; sprintf(buffer,"%i", gameover); lcd.printString(buffer,0,0); */ //NEED TO RESET POS CORDS EACH NEW LEVEL TO PREVENT UNNECERSARRY GAME OVER (have fun x)