FINAL VERSION

Dependencies:   mbed

main.cpp

Committer:
jamesheavey
Date:
2019-04-21
Revision:
9:f6f0f39538c7
Parent:
8:1ab6d90c4d60
Child:
10:9f445faa892c

File content as of revision 9:f6f0f39538c7:

///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "PongEngine.h"
#include "Bitmap.h"
#include<iostream>
#include "Sprites.h"

#ifdef WITH_TESTING
# include "tests.h"
#endif

#define PADDLE_WIDTH 15
#define PADDLE_HEIGHT 2
#define BALL_SIZE 2
#define BALL_SPEED 3

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

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

///////////// functions ////////////////
int main()
{
#ifdef WITH_TESTING
    int number_of_failures = run_all_tests();

    if(number_of_failures > 0) return number_of_failures;
#endif

    int fps = 8;  // frames per second
    bool pause = false;
    init();     // initialise and then display welcome screen...
    welcome();  // waiting for the user to start
    
    Bitmap three(three_data, 48, 84);
    Bitmap two(two_data, 48, 84);
    Bitmap one(one_data, 48, 84);
    
    lcd.clear();
    three.render(lcd, 0, 0);
    lcd.refresh();
    wait(1);
    
    lcd.clear();
    two.render(lcd, 0, 0);
    lcd.refresh();
    wait(1);
    
    lcd.clear();
    one.render(lcd, 0, 0);
    lcd.refresh();
    wait(1);
    
    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) {
        pong.read_input(pad);
        pong.update(pad);
        render();
        if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
            pause = !pause;
        }
        while (pause == true) {
            lcd.clear();
            lcd.printString("    PAUSED ",0,2);
            lcd.refresh();
            if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
                pause = !pause;
            }
            wait(1.0f/fps);
        }
        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
    pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED);

}

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

// simple splash screen displayed on start-up
void welcome() {
    Bitmap breakwhite(breakwhite_data, 48, 84);
    Bitmap breakblack(breakblack_data, 48, 84);
     
    // wait flashing LEDs until start button is pressed 
    while (pad.check_event(Gamepad::START_PRESSED) == false) { 
        lcd.clear();
        
        breakwhite.render(lcd, 0, 0);
        lcd.printString(" PRESS START ",2,5);
        lcd.refresh();
        pad.leds_on();
        wait(0.5);
        
        lcd.clear();
        
        breakblack.render(lcd, 0, 0);
        lcd.printString(" PRESS START ",2,5);
        lcd.refresh();
        pad.leds_off();
        wait(0.5);
        
    }
 
}