Andreas Garmannslund / Mbed 2 deprecated SimplePlatformGame

Dependencies:   N5110 PinDetect PowerControl mbed

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