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

Dependencies:   mbed

Revision:
9:35f992fd407b
Parent:
8:c3cf8d1057bb
--- a/main.cpp	Wed May 03 14:25:55 2017 +0000
+++ b/main.cpp	Wed May 03 15:25:13 2017 +0000
@@ -1,71 +1,97 @@
+///////// pre-processor directives ////////
 #include "mbed.h"
 #include "N5110.h"
 #include "Gamepad.h"
 #include "GameEngine.h"
 #include "MenuEngine.h"
 
+/////////////// objects ///////////////
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
-Serial pc(USBTX, USBRX); // tx, rx
+Serial pc(USBTX, USBRX);
 Gamepad pad;
 GameEngine game;
 MenuEngine menu;
 
+///////////// prototypes ///////////////
 void update_screen();
 void welcome();
+void menu_loop();
+void game_loop();
 
-bool showStart = true;
-
+///////////// functions ////////////////
 int main()
 {
+    // Initialise the pad and the LCD screen
     lcd.init();
     pad.init();
 
-    lcd.normalMode();
-    lcd.setBrightness(1);
-
-    while (!pad.check_event(Gamepad::START_PRESSED)) {
-        welcome();
-    }
+    welcome();
 
     while(1)
     {
-        menu.init();
-        while (!menu.gameStarted)
-        {
-            menu.read_input(pad, pc);
-            menu.update(pad, pc, lcd);
-            menu.draw(lcd,pc);
-            menu.check_finish(pad, lcd, pc);
-            wait(1.0f/10);
-        }
-        game.init(menu.selectedLevel, menu.soundIsOn, pad);
-        while (game.isActive)
-        {
-            game.read_input(pad,pc);
-            game.update(pad, pc, lcd);
-            game.draw(lcd);
-            game.check_finish(pad, lcd);
-            wait(1.0f/10); // 10 fps
-        }
+        menu_loop();
+        game_loop();
     }
 }
 
 void welcome()
 {
-    lcd.clear();
-    lcd.printString("Escape", 5, 0);
-    lcd.printString("is", 30, 1);
-    lcd.printString("an", 35, 2);
-    lcd.printString("Ellusion", 30, 3);
-    if (showStart)
+    // Variable that determines whether "Press START" is displayed
+    bool showStart = true;
+
+    // Loop until player presses START
+    while (!pad.check_event(Gamepad::START_PRESSED))
     {
-        lcd.printString("Press START", 10, 5);
-        showStart = false;
+        // Clear screen and print out the title
+        lcd.clear();
+        lcd.printString("Escape", 5, 0);
+        lcd.printString("is", 30, 1);
+        lcd.printString("an", 35, 2);
+        lcd.printString("Ellusion", 30, 3);
+
+        // Show and hide "Press START every 0.5 seconds"
+        if (showStart) {
+            lcd.printString("Press START", 10, 5);
+            showStart = false;
+        }
+        else {
+            showStart = true;
+        }
+
+        // Update screen and wait until next animation frame
+        lcd.refresh();
+        wait(0.5);
     }
-    else
+}
+
+void menu_loop()
+{
+    // Initialise the menu with the last level played
+    menu.init(game.get_level());
+
+    // Loop until player exits the menu
+    while (!menu.gameStarted)
     {
-        showStart = true;
+        menu.read_input(pad, pc);
+        menu.update(pad, pc, lcd);
+        menu.draw(lcd,pc);
+        menu.check_finish(pad, lcd, pc);
+        wait(1.0f/10.0f); // 10 fps
     }
-    lcd.refresh();
-    wait(0.5);
+}
+
+void game_loop()
+{
+    // Initialise the game with the settings selected in the menu
+    game.init(menu.selectedLevel, menu.soundIsOn, pad);
+
+    // Loop until the player finishes or exits the game
+    while (game.isActive)
+    {
+        game.read_input(pad,pc);
+        game.update(pad, pc, lcd);
+        game.draw(lcd);
+        game.check_finish(pad, lcd);
+        wait(1.0f/10.0f); // 10 fps
+    }
 }
\ No newline at end of file