ELEC2645 (2016/17) / Mbed 2 deprecated 2645_Project_el15as

Dependencies:   mbed

Committer:
el15as
Date:
Wed May 03 15:25:13 2017 +0000
Revision:
9:35f992fd407b
Parent:
8:c3cf8d1057bb
Main file cleaned up and thoroughly commented

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el15as 9:35f992fd407b 1 ///////// pre-processor directives ////////
el15as 0:9aa78a94ec93 2 #include "mbed.h"
el15as 0:9aa78a94ec93 3 #include "N5110.h"
el15as 0:9aa78a94ec93 4 #include "Gamepad.h"
el15as 2:8c5c47b2372d 5 #include "GameEngine.h"
el15as 8:c3cf8d1057bb 6 #include "MenuEngine.h"
el15as 0:9aa78a94ec93 7
el15as 9:35f992fd407b 8 /////////////// objects ///////////////
el15as 0:9aa78a94ec93 9 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
el15as 9:35f992fd407b 10 Serial pc(USBTX, USBRX);
el15as 0:9aa78a94ec93 11 Gamepad pad;
el15as 2:8c5c47b2372d 12 GameEngine game;
el15as 8:c3cf8d1057bb 13 MenuEngine menu;
el15as 0:9aa78a94ec93 14
el15as 9:35f992fd407b 15 ///////////// prototypes ///////////////
el15as 1:5e5791293388 16 void update_screen();
el15as 8:c3cf8d1057bb 17 void welcome();
el15as 9:35f992fd407b 18 void menu_loop();
el15as 9:35f992fd407b 19 void game_loop();
el15as 8:c3cf8d1057bb 20
el15as 9:35f992fd407b 21 ///////////// functions ////////////////
el15as 0:9aa78a94ec93 22 int main()
el15as 0:9aa78a94ec93 23 {
el15as 9:35f992fd407b 24 // Initialise the pad and the LCD screen
el15as 0:9aa78a94ec93 25 lcd.init();
el15as 1:5e5791293388 26 pad.init();
el15as 2:8c5c47b2372d 27
el15as 9:35f992fd407b 28 welcome();
el15as 3:e05d4de7690b 29
el15as 8:c3cf8d1057bb 30 while(1)
el15as 8:c3cf8d1057bb 31 {
el15as 9:35f992fd407b 32 menu_loop();
el15as 9:35f992fd407b 33 game_loop();
el15as 8:c3cf8d1057bb 34 }
el15as 8:c3cf8d1057bb 35 }
el15as 2:8c5c47b2372d 36
el15as 8:c3cf8d1057bb 37 void welcome()
el15as 8:c3cf8d1057bb 38 {
el15as 9:35f992fd407b 39 // Variable that determines whether "Press START" is displayed
el15as 9:35f992fd407b 40 bool showStart = true;
el15as 9:35f992fd407b 41
el15as 9:35f992fd407b 42 // Loop until player presses START
el15as 9:35f992fd407b 43 while (!pad.check_event(Gamepad::START_PRESSED))
el15as 8:c3cf8d1057bb 44 {
el15as 9:35f992fd407b 45 // Clear screen and print out the title
el15as 9:35f992fd407b 46 lcd.clear();
el15as 9:35f992fd407b 47 lcd.printString("Escape", 5, 0);
el15as 9:35f992fd407b 48 lcd.printString("is", 30, 1);
el15as 9:35f992fd407b 49 lcd.printString("an", 35, 2);
el15as 9:35f992fd407b 50 lcd.printString("Ellusion", 30, 3);
el15as 9:35f992fd407b 51
el15as 9:35f992fd407b 52 // Show and hide "Press START every 0.5 seconds"
el15as 9:35f992fd407b 53 if (showStart) {
el15as 9:35f992fd407b 54 lcd.printString("Press START", 10, 5);
el15as 9:35f992fd407b 55 showStart = false;
el15as 9:35f992fd407b 56 }
el15as 9:35f992fd407b 57 else {
el15as 9:35f992fd407b 58 showStart = true;
el15as 9:35f992fd407b 59 }
el15as 9:35f992fd407b 60
el15as 9:35f992fd407b 61 // Update screen and wait until next animation frame
el15as 9:35f992fd407b 62 lcd.refresh();
el15as 9:35f992fd407b 63 wait(0.5);
el15as 0:9aa78a94ec93 64 }
el15as 9:35f992fd407b 65 }
el15as 9:35f992fd407b 66
el15as 9:35f992fd407b 67 void menu_loop()
el15as 9:35f992fd407b 68 {
el15as 9:35f992fd407b 69 // Initialise the menu with the last level played
el15as 9:35f992fd407b 70 menu.init(game.get_level());
el15as 9:35f992fd407b 71
el15as 9:35f992fd407b 72 // Loop until player exits the menu
el15as 9:35f992fd407b 73 while (!menu.gameStarted)
el15as 8:c3cf8d1057bb 74 {
el15as 9:35f992fd407b 75 menu.read_input(pad, pc);
el15as 9:35f992fd407b 76 menu.update(pad, pc, lcd);
el15as 9:35f992fd407b 77 menu.draw(lcd,pc);
el15as 9:35f992fd407b 78 menu.check_finish(pad, lcd, pc);
el15as 9:35f992fd407b 79 wait(1.0f/10.0f); // 10 fps
el15as 8:c3cf8d1057bb 80 }
el15as 9:35f992fd407b 81 }
el15as 9:35f992fd407b 82
el15as 9:35f992fd407b 83 void game_loop()
el15as 9:35f992fd407b 84 {
el15as 9:35f992fd407b 85 // Initialise the game with the settings selected in the menu
el15as 9:35f992fd407b 86 game.init(menu.selectedLevel, menu.soundIsOn, pad);
el15as 9:35f992fd407b 87
el15as 9:35f992fd407b 88 // Loop until the player finishes or exits the game
el15as 9:35f992fd407b 89 while (game.isActive)
el15as 9:35f992fd407b 90 {
el15as 9:35f992fd407b 91 game.read_input(pad,pc);
el15as 9:35f992fd407b 92 game.update(pad, pc, lcd);
el15as 9:35f992fd407b 93 game.draw(lcd);
el15as 9:35f992fd407b 94 game.check_finish(pad, lcd);
el15as 9:35f992fd407b 95 wait(1.0f/10.0f); // 10 fps
el15as 9:35f992fd407b 96 }
el15as 3:e05d4de7690b 97 }