Adam Baker 201166301

Dependencies:   mbed Gamepad N5110

main.cpp

Committer:
adambakerwa
Date:
2019-05-05
Revision:
36:6f452777b9ce
Parent:
34:911d7d05a8d2
Child:
37:23b6312c8315

File content as of revision 36:6f452777b9ce:

#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "Runner.h"
#include "Levels.h"
#include "Animation.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 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 print_continue_menu(N5110 &lcd); //
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);

    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 main_menu(N5110 &lcd, Gamepad &pad)
{
    blockhead.init();                               //intialise blockhead engine variables for new game
                                                    //intialse menu variables 
    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 {                                            //ADD TRICKR AND ANIMATION
        input = menu.select_input(pad, lcd);        //prints menu and toggles cursor depending on input,
    } 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();

            lcd.clear();

            lcd.printString("  Highscore.. ", 0,1);

            char buffer[14];
            sprintf(buffer,"%i", highscore);
            lcd.printString(buffer,35,5);

            lcd.refresh();

            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;

    do {

        if (timer_flag == true) {

            timer_flag = 0;

            lcd.clear();

            lcd.printString("  settings ", 0,1);

            lcd.refresh();

            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 print_menu_continue(N5110 &lcd)
{
    lcd.clear();

    lcd.printString("Continue??",0,0);
    lcd.printString("A for hell yeh",0,1);
    lcd.printString("B for back",0,2);
    lcd.refresh();

}

void menu_continue(N5110 &lcd, Gamepad &pad)
{
    blockhead.continueInit();

    print_menu_continue(lcd);
    
    int input = 0;
    do{
        
        if (pad.check_event(Gamepad::A_PRESSED)) {
                input = 1;
            }
        
        if (pad.check_event(Gamepad::B_PRESSED)) {
                input = 2;
            }
        
        } 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)