Ellis Blackford Stroud 201155309

Dependencies:   mbed FATFileSystem

Revision:
14:08ac9aaa34c3
Parent:
13:681192091568
Child:
15:d855e8c666e7
--- a/main.cpp	Mon May 06 10:00:57 2019 +0000
+++ b/main.cpp	Wed May 08 14:54:19 2019 +0000
@@ -24,61 +24,56 @@
 Ticker ticker_frame;
 
 //prototypes
+void frame_isr();
 void init();
-void inverseFlash();
-void frame_isr();
+
 
 //global variables
-volatile int g_frame_flag = 0; //flag set in isr to control running frequency/frame rate of game
+volatile int g_frame_flag = 0; //flag set in isr to control running frame rate of game
 
 //functions 
 int main()
 {
-    init(); //initialises pad, lcd and menu objects
-    lcd.clear();
-    menu.print_welcome(lcd);  //Prints welcome message
-    lcd.clear();
-    bool start_game = false; 
-    
-    while(start_game == false) {   //navigates menu via a series of loops until user chooses to start game
-    
-        start_game = menu.menu_loop(pad, lcd, sd); //series of loop functions used to navigate the menu
-    }  
-    
-    lcd.clear();
-    golf.printLevel(lcd);  //Indicating start of game
-    lcd.refresh();
-    wait(1);
-    
-    int frame_rate = menu.get_frame_rate(); //initialises frame rate variable and sets to frame rate chosen in settings
-    golf.init(frame_rate); //initialises golf objects, sets variables for game and sets frame rate
-    ticker_frame.attach(&frame_isr,1.0f/frame_rate); //sets up ticker to call fram_isr with interval of the time period of the fps 
-
-    while(1){ 
+    init(); //Initialises pad, lcd and menu objects. Sets up ticker
+    menu.welcome_loop(pad, lcd); //Prints welcome screen and loops until start pressed
+    while(1) {   
+        //Menu Loop - Naviagates series of loops for menu screens
+        if(menu.get_start_game_flag() == false) { //if start flag false stay in menu loop
+            menu.menu_loop(pad, lcd); //main menu screen - loops until user chooses next screen
+                switch (menu.get_screen()) { //uses user choice to enter next loop in menu
+                    case START:
+                        menu.start_loop(pad, lcd); //loops until a pressed then game begins
+                        break;
+                    case HIGHSCORES:
+                        menu.highscores_loop(pad, lcd, sd); //loops until back pressed then returns to main menu screen
+                        break;
+                    case SETTINGS:
+                        menu.settings_loop(pad, lcd); //loops until back pressed then returns to main menu screen
+                        break;
+                } //when user exits sub-menu loop check game_start_flag - if false then return to main menu loop
+        } else { //Game Loop - If start flag = true
+            
+            menu.reset_start_game_flag(); //resets start flag to return to menu after course complete 
+            golf.init(menu.get_frame_rate()); //initialises golf objects, sets variables for game and sets frame rate
 
-        //Game Loop - 
-        if(g_frame_flag) { //Causes game loop code to run at frame rate 
-        
-            g_frame_flag = 0; //reset flag
-        
-            if(golf.get_hole_flag() == false) { //if ball not in hole run game loop
+            lcd.clear();
+            golf.printLevel(lcd);  //Indicating start of game
+            lcd.refresh();
+            wait(3.0f); 
+            golf.reset_game_over_flag();
+            while(golf.get_game_over_flag() == false){ //Main Game Loop
             
-                lcd.clear();
-                golf.read_input(pad); //reads input from gamepad 
-                golf.update_ball(pad); //moves ball and checks for bounces/shots/holes
-                golf.drawGame(lcd, pad); //draws ball, walls etc.
-                lcd.refresh(); //updates lc display
-                sleep(); //sleeps mcu until ticker wakes it up
-            
-            } else if(golf.get_hole_flag() == true) { //if ball in hole then end level 
-            
-                inverseFlash(); //flashes screen pixels on and off to indicate end of level
-                inverseFlash();
-                lcd.clear();
-                golf.new_level(lcd, sd); //increments level count, prints level for user, checks if all levels complete
-                golf.reset_hole_flag(); //so that the game loop can continue
-                lcd.refresh();
-                wait(1);
+                if(g_frame_flag) { //If ticker calls isr and ball not in hole
+        
+                    g_frame_flag = 0; //reset flag
+                    lcd.clear(); //clears lcd buffer
+                    golf.read_input(pad); //reads input from gamepad 
+                    golf.drawGame(lcd); //draws ball, walls etc. - draws game from previous frame 
+                    golf.update_ball(pad, lcd); //moves ball and checks for bounces/shots/holes/game over
+                    golf.check_end_level(lcd, pad, sd); //checks if level comleted and if game over
+                    lcd.refresh(); //updates lcd display
+                    sleep(); //sleeps mcu until ticker wakes it up
+                }
             }   
         }
     }
@@ -94,21 +89,6 @@
     lcd.init();
     pad.init();
     menu.init();
+    ticker_frame.attach(&frame_isr,1.0f/menu.get_frame_rate()); //sets up ticker used to control running speed/frame rate
     lcd.setContrast(0.5f); //can alter this in settings
-}
-
-void inverseFlash() // at end of level to indicate ball in hole
-{ 
-    lcd.inverseMode();
-    lcd.refresh();
-    wait(0.25);
-    lcd.normalMode();
-    lcd.refresh();
-    wait(0.25);
-    lcd.inverseMode();
-    lcd.refresh();
-    wait(0.25);
-    lcd.normalMode();
-    lcd.refresh();
-    wait(0.25);
-}
+}
\ No newline at end of file