Hugo Hu / Mbed 2 deprecated BRAVEHEART

Dependencies:   mbed N5110 ShiftReg PinDetect

Files at this revision

API Documentation at this revision

Comitter:
Siriagus
Date:
Fri May 01 18:10:59 2015 +0000
Parent:
6:edb48de563a9
Child:
8:9ac6a428fa26
Commit message:
Fixed bug in State.h: Not setting fsm variable. State classes can now request the state to be changed.

Changed in this revision

Game.cpp Show annotated file Show diff for this revision Revisions of this file
Game.h Show annotated file Show diff for this revision Revisions of this file
MainMenu.cpp Show annotated file Show diff for this revision Revisions of this file
MainMenu.h Show annotated file Show diff for this revision Revisions of this file
State.cpp Show annotated file Show diff for this revision Revisions of this file
State.h Show annotated file Show diff for this revision Revisions of this file
StateManager.h Show annotated file Show diff for this revision Revisions of this file
StateManger.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Game.cpp	Fri May 01 09:44:38 2015 +0000
+++ b/Game.cpp	Fri May 01 18:10:59 2015 +0000
@@ -0,0 +1,11 @@
+#include "Game.h"
+
+void Game::update(float dt)
+{
+    
+}
+
+void Game::render()
+{
+    lcd->printString("GAME_MODE", 15, 2);
+}
\ No newline at end of file
--- a/Game.h	Fri May 01 09:44:38 2015 +0000
+++ b/Game.h	Fri May 01 18:10:59 2015 +0000
@@ -1,14 +1,16 @@
 #ifndef GAME_H
 #define GAME_H
 
+#include "State.h"
+
 class Game : public State
 {
     public:
         Game(StateManager* fsm, N5110 *lcd, InputManager* input)
                 : State(fsm, lcd, input) {init();}
                 
-        virtual void update(time_t dt) {}
-        virtual void render() {}
+        virtual void update(float dt);
+        virtual void render();
     private:
         void init() {}
         
--- a/MainMenu.cpp	Fri May 01 09:44:38 2015 +0000
+++ b/MainMenu.cpp	Fri May 01 18:10:59 2015 +0000
@@ -37,7 +37,7 @@
     input->addBtnPressInterrupt(input->btnC, &btnCPress);
 }
 
-void MainMenu::update(time_t dt)
+void MainMenu::update(float dt)
 {
     
 }
@@ -83,6 +83,7 @@
         
         case LOAD_GAME:
             lcd->printString("Loading...", 10, 2);
+            requestStateChange(GAME);
         break;
         
         default:
--- a/MainMenu.h	Fri May 01 09:44:38 2015 +0000
+++ b/MainMenu.h	Fri May 01 18:10:59 2015 +0000
@@ -9,7 +9,7 @@
         MainMenu(StateManager* fsm, N5110 *lcd, InputManager* input)
                 : State(fsm, lcd, input) {init();}
                 
-        virtual void update(time_t dt);
+        virtual void update(float dt);
         virtual void render();
     
     private:
--- a/State.cpp	Fri May 01 09:44:38 2015 +0000
+++ b/State.cpp	Fri May 01 18:10:59 2015 +0000
@@ -2,8 +2,7 @@
 #include "StateManager.h"
 
 // Request the fsm to change to a new state
-void State::changeState(MainState newState)
+void State::requestStateChange(MainState newState)
 {
-    // Todo: Rather call a function in StateManager that postponed the change until every update is done
-    fsm->changeState(newState);
+    fsm->requestStateChange(newState);
 }
\ No newline at end of file
--- a/State.h	Fri May 01 09:44:38 2015 +0000
+++ b/State.h	Fri May 01 18:10:59 2015 +0000
@@ -8,7 +8,7 @@
 class StateManager;
 
 // List of main states in the game
-enum MainState {MAIN_MENU, GAME, SUBMIT_HIGHSCORE, GAME_OVER};
+enum MainState {MAIN_MENU, GAME, SUBMIT_HIGHSCORE, GAME_OVER, NO_STATE};
 
 // Each main state is a derived from the State class.
 
@@ -17,13 +17,13 @@
     public:
     
         State(StateManager* fsm, N5110 *lcd, InputManager* input)
-                :lcd(lcd), input(input){}
+                :lcd(lcd), input(input), fsm(fsm){}
                 
-        virtual void update(time_t dt) = 0;
+        virtual void update(float dt) = 0;
         virtual void render() = 0;
         
-    private:
-        void changeState(MainState newState);
+    protected:
+        void requestStateChange(MainState newState);
         
     protected:
         N5110 *lcd;
@@ -34,6 +34,4 @@
         
 };
 
-
-
 #endif
\ No newline at end of file
--- a/StateManager.h	Fri May 01 09:44:38 2015 +0000
+++ b/StateManager.h	Fri May 01 18:10:59 2015 +0000
@@ -3,25 +3,32 @@
 
 #include "State.h"
 #include "MainMenu.h"
-#include <map>
+#include "Game.h"
 
 // Main Finite State Machine - controls the flow between the main states
 class StateManager
 {
     public:
-        StateManager(N5110 *lcd, InputManager* input)
-                    : lcd(lcd),  input(input){currentState = 0;}
+        StateManager(N5110 *lcd, InputManager* input, MainState firstState)
+                    : lcd(lcd),  input(input){currentState = 0; nextState = NO_STATE; changeState(firstState);}
         ~StateManager() {if (currentState != 0) delete currentState;}
         
-        void update(time_t dt);
+        void update(float dt);
         void render();
         
+        void requestStateChange(MainState newState);
+        
+        void processRequest(); // See if any request to change the state have been made
+        
+    private:
         void changeState(MainState newState); // Deletes the current main state and creates a new one
-                
+    
+    // Variables    
     private:
         N5110 *lcd;
         InputManager *input;
         State* currentState; // Current state object
+        MainState nextState;
 };
 
 #endif
\ No newline at end of file
--- a/StateManger.cpp	Fri May 01 09:44:38 2015 +0000
+++ b/StateManger.cpp	Fri May 01 18:10:59 2015 +0000
@@ -5,7 +5,7 @@
 {
     if (currentState != 0)      // if a state exist
         delete currentState;    // delete the old state
-        
+
     // Create new state
     switch (newState)
     {
@@ -13,10 +13,11 @@
             currentState = new MainMenu(this, lcd, input);
         break;
         
-        // TODO: Make classes for the other states, currently all  than MAIN_MENU will give error
         case GAME:
-        //break;
+            currentState = new Game(this, lcd, input);
+        break;
         
+        // TODO: Make classes for the other states.
         case SUBMIT_HIGHSCORE:
         //break;
         
@@ -28,12 +29,30 @@
     }
 }
 
-void StateManager::update(time_t dt)
+void StateManager::requestStateChange(MainState requestedState)
 {
+    if (nextState != NO_STATE)
+        error("Invalid - can't request new state before the first request has been processed!");
+        
+    nextState = requestedState;
+}
+
+void StateManager::update(float dt)
+{       
     currentState->update(dt);
 }
 
 void StateManager::render()
 {
     currentState->render();
+}
+
+void StateManager::processRequest()
+{
+    // Check if there has been a request to change the state
+    if (nextState != NO_STATE)
+    {
+        changeState(nextState);
+        nextState = NO_STATE;       // reset, state has been changed
+    }
 }
\ No newline at end of file
--- a/main.cpp	Fri May 01 09:44:38 2015 +0000
+++ b/main.cpp	Fri May 01 18:10:59 2015 +0000
@@ -9,6 +9,7 @@
 #include "PowerControl.h"
 #include "PinDetect.h"
 #include <string>
+#include <sstream>
 #include <ctime>
 
 #include "Joystick.h"
@@ -40,7 +41,6 @@
 AnalogIn ledPot(LED_POT);
 
 // Debugging
-Serial pc(USBTX, USBRX);
 BusOut leds(LED1, LED2, LED3, LED4);
 
 // @brief Clears the screen and fill it with the image in the argument.
@@ -70,23 +70,26 @@
 {
     init();
     
-    time_t dt = time(NULL);
+    Timer timer;
+    timer.start();
     
     while(true)
-    {   
-        
-        input->joystick->update();
+    {
+        // update
         lcd->setBrightness(1.0 - ledPot); // Update brightness of screen
-        
-        // update
-        fsm->update(dt);
+        fsm->update(timer.read());
+        input->joystick->update();
         
         // render
         lcd->clear();
-        fsm->render();
+        fsm->render(); 
         lcd->refresh();
         
-        Sleep();
+        fsm->processRequest(); // Change the state if requested.
+        
+        timer.reset();
+        
+        Sleep(); // Sleep for 50 ms until InputManager reads the 
     }
     
     cleanUp();
@@ -94,25 +97,6 @@
     return 0;
 }
 
-void ledToggle()
-{
-    leds[0] = !leds[0];
-}
-
-int ctr = 0;
-void ledToggle4() {leds[3] = !leds[3];}
-void ledToggle2()
-{
-    leds[1] = !leds[1];
-    
-    if (++ctr > 4)
-        input->addBtnPressInterrupt(input->btnB, ledToggle4);
-        
-}
-
-
-void ledToggle3() {leds[2] = !leds[2];}
-
 void init()
 {
     // Init LCD
@@ -123,12 +107,8 @@
     
     // Input
     input = new InputManager(BUTTON_A, BUTTON_B, BUTTON_C, JOY_V, JOY_H, JOY_BTN);
-    //input->addBtnPressInterrupt(input->btnA, ledToggle);
-   // input->addBtnPressInterrupt(input->btnB, ledToggle2);
-    //input->addBtnPressInterrupt(input->btnC, ledToggle3);
     
-    fsm = new StateManager(lcd, input);
-    fsm->changeState(MAIN_MENU);
+    fsm = new StateManager(lcd, input, MAIN_MENU);
 }
 
 void cleanUp()