ELEC2645 (2017/18) / Mbed OS el16ajm

main.cpp

Committer:
Andrew_M
Date:
2018-05-07
Revision:
13:81573be8fac6
Parent:
12:d3eef5ea3f43
Child:
14:a57a40ff9430

File content as of revision 13:81573be8fac6:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name: Andrew Moore
Username: el16ajm
Student ID Number: 201042893
Date:
*/

#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "Engine.h"
#include "Menu.h"

/////////////// structs /////////////////
struct UserInput {
    Direction d;
};

/////////////// objects ///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
Engine gameEngine;
Menu mainMenu;

///////////// prototypes ///////////////
void init();
void update_game(UserInput input);
void renderGame();
void renderMenu();
void welcome();
void transition();
void reset();
float setDif();

///////////// functions ////////////////
int main()
{
    init();
    welcome();

    transition();

    // game loop - read input, update the game state and render the display
    while (1) {
        if (!mainMenu.started()) { //menu logic and rendering
            mainMenu.read_input(pad);
            mainMenu.update();
            gameEngine.setLvl(mainMenu.getLvl());
            renderMenu();
        } else if (gameEngine.getGameOver()) { //resets the game once a game over state has been reached
            gameEngine.gameOverScreen(lcd);
            reset();
        } else { //game logic and rendering
            gameEngine.read_input(pad);
            gameEngine.update(pad);
            renderGame();
        }
    }
}

void welcome()
{
    lcd.printString("     Snake!    ",0,1);
    lcd.printString("  Press Start ",0,4);
    lcd.refresh();

    // wait flashing LEDs until start button is pressed
    while ( pad.check_event(Gamepad::START_PRESSED) == false) {
        pad.leds_on();
        wait(0.1);
        pad.leds_off();
        wait(0.1);
    }

}

void init()
{
    // need to initialise LCD and Gamepad
    gameEngine.init();
    lcd.init();
    pad.init();
    mainMenu.init();

}

void reset()
{
    mainMenu.init();
    gameEngine.init();
}

void renderGame()
{
    // clear screen, re-draw and refresh
    lcd.clear();
    gameEngine.draw(lcd);
    lcd.refresh();
    wait(setDif());
}

void renderMenu()
{
    // clear screen, re-draw and refresh
    lcd.clear();
    mainMenu.draw(lcd);
    lcd.refresh();
    wait(1.0f/8);
}

void transition()
{
    //transition animation between modes to stop 'ghosting' of inputs
    for (int i = 0; i <= 11; i++) {
        for (int j = 0; j <= 21; j++) {
            lcd.drawRect(j*4,i*4,4,4,FILL_BLACK);
            wait(0.005);
            lcd.refresh();
        }
    }

}

float setDif()
{
    int _dif = mainMenu.getDif();

    if (_dif == 1) {    //Low difficulty
        return 1.0f/4;
    } else if (_dif == 2) { //Medium difficulty
        return 1.0f/8;
    } else if (_dif == 3) { //Hard difficulty
        return 1.0f/12;
    } else {    //Adaptive difficulty
        float _delta = gameEngine.getScore(); 
        if (_delta < 25) { //Limits FPS to 25
            return ((1.0f/(_delta + 1)));
        } else {
            return (1.0f/25);
        }
    }
}