Hugo Hu / Mbed 2 deprecated BRAVEHEART

Dependencies:   mbed N5110 ShiftReg PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers State.h Source File

State.h

Go to the documentation of this file.
00001 #ifndef STATE_H
00002 #define STATE_H
00003 
00004 #include "N5110.h"
00005 #include "PinDetect.h"
00006 #include "InputManager.h "
00007 #include "Music.h "
00008 #include "ShiftReg.h"
00009 
00010 /** @file State.h
00011 * @author Andreas Garmannslund
00012 * @date April 2015
00013 */
00014 
00015 class StateManager;
00016 
00017 /// States used in the finite state machine.
00018 enum MainState {MAIN_MENU, GAME, SUBMIT_HIGHSCORE, GAME_OVER, NO_STATE, TITLE_SCREEN};
00019 
00020 /// Abstract class for states in the program's main finite state machine. All state implementations is derived from this abstract class.
00021 class State
00022 {   
00023     public:
00024         
00025         /* Creates a new state object. Should be called from child's constructor.
00026         * @param fsm Pointer to finished state machine.
00027         * @param lcd Pointer to the N5110 lcd object.
00028         * @param input Pointer to the InputManager object, used for controlling user input.
00029         */
00030         State(StateManager* fsm, N5110 *lcd, InputManager* input, Sound* sound, ShiftReg* shiftreg)
00031                 :lcd(lcd), input(input), sound(sound), shiftreg(shiftreg), fsm(fsm) {}
00032                 
00033         /// Handle user input and update logic.
00034         virtual void update(float dt) = 0;
00035         
00036         /// Draw to screen.
00037         virtual void render() = 0;
00038         
00039     protected:
00040         /* Requests the finite state machine to switch to a new state when possible.
00041         * @param newState The state the fsm should switch to.
00042         **/
00043         void requestStateChange(MainState newState);
00044         
00045         /** Draws an image to the lcd
00046         * @param img Array with the same size as the display, where 1 is opaque, 0 is blank.
00047         */
00048         //void drawImage(const int img[BANKS][WIDTH]); // Draws an image from array
00049         
00050         
00051         /** Draws an image/sprite to the lcd
00052         *   Only the solid pixels are drawn. If two images overlap, the second image drawn will
00053         *   not clear pixels which are solid in the first image.
00054         *   @param img const int array where a solid pixel equals 1, and a blank pixel equals zero
00055         *   @param x Horizontal position of image (leftmost pixel)
00056         *   @param y Vertical position of image (uppermost pixel)
00057         *   @param Inverses images. Default value is false
00058         *   See seperate program for how this array can be generated from an image file using SFML!
00059         */
00060         template<class T, size_t rows, size_t cols>
00061         void drawImage(const T (&img)[rows][cols], int x = 0, int y = 0, bool inverse = false, bool flipX = false, bool flipY = false);
00062         
00063     protected:
00064         N5110 *lcd;
00065         InputManager *input;
00066         Sound *sound;
00067         ShiftReg *shiftreg;
00068         
00069     private:
00070         StateManager *fsm;
00071         
00072 };
00073 
00074 // Template functions needs to be declared in the header file
00075 // TODO: Add functions for inverse drawing
00076 template<class T, size_t rows, size_t cols>
00077 void State::drawImage(const T (&img)[rows][cols], int x, int y, bool inverse, bool flipX, bool flipY)
00078 {
00079     int targetX, targetY; // The position on the lcd we are writing to
00080     
00081     for (int i = 0; i < rows; ++i)
00082     {
00083         targetY = (flipY) ? (y + (rows-1) - i) : (y + i);
00084         
00085         // Skip if outside dimensions of LCD
00086         if (targetY < 0) continue;
00087         else if (targetY >= HEIGHT) continue;
00088         
00089         for (int j = 0; j < cols; ++j)
00090         {
00091             targetX = (flipX) ? (x + ((cols - 1) - j)) : (x + j);
00092             
00093             // Dimensions check. Draws left to right.
00094             if (targetX < 0) continue;
00095             else if (targetX >= WIDTH) continue;
00096             
00097             int solid = img[i][j]; 
00098             
00099             if ((solid && !inverse) || (!solid && inverse))
00100                 lcd->setPixel(targetX, targetY);
00101                 
00102         }
00103     }   
00104 }
00105 #endif