ELEC2645 (2018/19) / Mbed 2 deprecated henririgby98

Dependencies:   mbed

main.cpp

Committer:
henririgby98
Date:
2019-05-08
Revision:
5:0da65740cd5e
Parent:
4:d744920089ea
Child:
6:e7ac87a41840

File content as of revision 5:0da65740cd5e:

///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "SpaceRebEngine.h"


#define SPACEINVADER_WIDTH 8
#define SPACEINVADER_HEIGHT 11
#define MISSILES_SIZE 2
#define MISSILES_SPEED 3

/////////////// structs /////////////////
struct UserInput {
    Direction d;
    float mag;
};
/////////////// objects ///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
SpaceRebEngine spacerebellion;

///////////// prototypes ///////////////
void init();
void update_game(UserInput input);
void render();
void welcome();

///////////// functions ////////////////
int main()
{
    int time =0;
    int fps = 8;  // frames per second

    init();     // initialise and then display welcome screen...
    welcome();  // waiting for the user to start
    
    render();  // first draw the initial frame 
    wait(1.0f/fps);  // and wait for one frame period


    // game loop - read input, update the game state and render the display
    while (1) {
        spacerebellion.read_input(pad);
        spacerebellion.update(pad);
        render();
        wait(1.0f/fps);
    }
}

// initialies all classes and libraries
void init()
{
    // need to initialise LCD and Gamepad 
    lcd.init();
    pad.init();
     
    // initialise the game with correct ball and paddle sizes
    spacerebellion.init(SPACEINVADER_WIDTH,SPACEINVADER_HEIGHT,MISSILES_SIZE,MISSILES_SPEED);

}

// this function draws each frame on the LCD
void render()
{
    // clear screen, re-draw and refresh
    lcd.clear();  
    spacerebellion.draw(lcd);
    lcd.refresh();
}

// simple splash screen displayed on start-up
void welcome() {
    
    lcd.printString("    Space    ",0,1);
    lcd.printString("  Rebellion! ",0,2);
    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);
    }
 
}