Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed N5110 ShiftReg PinDetect
State.h@19:89c3eeb3761b, 2021-03-25 (annotated)
- Committer:
- hugohu
- Date:
- Thu Mar 25 03:43:10 2021 +0000
- Revision:
- 19:89c3eeb3761b
- Parent:
- 17:d6a3b29cab31
A more complex game in developing.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Siriagus | 1:0cfe2255092a | 1 | #ifndef STATE_H |
Siriagus | 1:0cfe2255092a | 2 | #define STATE_H |
Siriagus | 1:0cfe2255092a | 3 | |
Siriagus | 4:d6661b976359 | 4 | #include "N5110.h" |
Siriagus | 4:d6661b976359 | 5 | #include "PinDetect.h" |
Siriagus | 4:d6661b976359 | 6 | #include "InputManager.h" |
hugohu | 19:89c3eeb3761b | 7 | #include "Music.h" |
hugohu | 19:89c3eeb3761b | 8 | #include "ShiftReg.h" |
Siriagus | 4:d6661b976359 | 9 | |
Siriagus | 10:f2488a0ecab7 | 10 | /** @file State.h |
Siriagus | 10:f2488a0ecab7 | 11 | * @author Andreas Garmannslund |
Siriagus | 10:f2488a0ecab7 | 12 | * @date April 2015 |
Siriagus | 10:f2488a0ecab7 | 13 | */ |
Siriagus | 10:f2488a0ecab7 | 14 | |
Siriagus | 4:d6661b976359 | 15 | class StateManager; |
Siriagus | 4:d6661b976359 | 16 | |
Siriagus | 10:f2488a0ecab7 | 17 | /// States used in the finite state machine. |
Siriagus | 8:9ac6a428fa26 | 18 | enum MainState {MAIN_MENU, GAME, SUBMIT_HIGHSCORE, GAME_OVER, NO_STATE, TITLE_SCREEN}; |
Siriagus | 4:d6661b976359 | 19 | |
Siriagus | 10:f2488a0ecab7 | 20 | /// Abstract class for states in the program's main finite state machine. All state implementations is derived from this abstract class. |
Siriagus | 1:0cfe2255092a | 21 | class State |
Siriagus | 5:100d960fc6d5 | 22 | { |
Siriagus | 1:0cfe2255092a | 23 | public: |
Siriagus | 10:f2488a0ecab7 | 24 | |
Siriagus | 10:f2488a0ecab7 | 25 | /* Creates a new state object. Should be called from child's constructor. |
Siriagus | 10:f2488a0ecab7 | 26 | * @param fsm Pointer to finished state machine. |
Siriagus | 10:f2488a0ecab7 | 27 | * @param lcd Pointer to the N5110 lcd object. |
Siriagus | 10:f2488a0ecab7 | 28 | * @param input Pointer to the InputManager object, used for controlling user input. |
Siriagus | 10:f2488a0ecab7 | 29 | */ |
hugohu | 19:89c3eeb3761b | 30 | State(StateManager* fsm, N5110 *lcd, InputManager* input, Sound* sound, ShiftReg* shiftreg) |
hugohu | 19:89c3eeb3761b | 31 | :lcd(lcd), input(input), sound(sound), shiftreg(shiftreg), fsm(fsm) {} |
Siriagus | 4:d6661b976359 | 32 | |
Siriagus | 10:f2488a0ecab7 | 33 | /// Handle user input and update logic. |
Siriagus | 7:678873947b29 | 34 | virtual void update(float dt) = 0; |
Siriagus | 10:f2488a0ecab7 | 35 | |
Siriagus | 10:f2488a0ecab7 | 36 | /// Draw to screen. |
Siriagus | 4:d6661b976359 | 37 | virtual void render() = 0; |
Siriagus | 4:d6661b976359 | 38 | |
Siriagus | 7:678873947b29 | 39 | protected: |
Siriagus | 10:f2488a0ecab7 | 40 | /* Requests the finite state machine to switch to a new state when possible. |
Siriagus | 10:f2488a0ecab7 | 41 | * @param newState The state the fsm should switch to. |
Siriagus | 10:f2488a0ecab7 | 42 | **/ |
Siriagus | 7:678873947b29 | 43 | void requestStateChange(MainState newState); |
Siriagus | 10:f2488a0ecab7 | 44 | |
Siriagus | 10:f2488a0ecab7 | 45 | /** Draws an image to the lcd |
Siriagus | 10:f2488a0ecab7 | 46 | * @param img Array with the same size as the display, where 1 is opaque, 0 is blank. |
Siriagus | 10:f2488a0ecab7 | 47 | */ |
Siriagus | 12:8178fad5e660 | 48 | //void drawImage(const int img[BANKS][WIDTH]); // Draws an image from array |
Siriagus | 12:8178fad5e660 | 49 | |
Siriagus | 12:8178fad5e660 | 50 | |
Siriagus | 12:8178fad5e660 | 51 | /** Draws an image/sprite to the lcd |
Siriagus | 13:7ab71c7c311b | 52 | * Only the solid pixels are drawn. If two images overlap, the second image drawn will |
Siriagus | 13:7ab71c7c311b | 53 | * not clear pixels which are solid in the first image. |
Siriagus | 12:8178fad5e660 | 54 | * @param img const int array where a solid pixel equals 1, and a blank pixel equals zero |
Siriagus | 12:8178fad5e660 | 55 | * @param x Horizontal position of image (leftmost pixel) |
Siriagus | 12:8178fad5e660 | 56 | * @param y Vertical position of image (uppermost pixel) |
Siriagus | 13:7ab71c7c311b | 57 | * @param Inverses images. Default value is false |
Siriagus | 12:8178fad5e660 | 58 | * See seperate program for how this array can be generated from an image file using SFML! |
Siriagus | 12:8178fad5e660 | 59 | */ |
hugohu | 19:89c3eeb3761b | 60 | template<class T, size_t rows, size_t cols> |
hugohu | 19:89c3eeb3761b | 61 | void drawImage(const T (&img)[rows][cols], int x = 0, int y = 0, bool inverse = false, bool flipX = false, bool flipY = false); |
Siriagus | 4:d6661b976359 | 62 | |
Siriagus | 4:d6661b976359 | 63 | protected: |
Siriagus | 4:d6661b976359 | 64 | N5110 *lcd; |
Siriagus | 4:d6661b976359 | 65 | InputManager *input; |
Siriagus | 17:d6a3b29cab31 | 66 | Sound *sound; |
hugohu | 19:89c3eeb3761b | 67 | ShiftReg *shiftreg; |
Siriagus | 4:d6661b976359 | 68 | |
Siriagus | 4:d6661b976359 | 69 | private: |
Siriagus | 4:d6661b976359 | 70 | StateManager *fsm; |
Siriagus | 4:d6661b976359 | 71 | |
Siriagus | 1:0cfe2255092a | 72 | }; |
Siriagus | 1:0cfe2255092a | 73 | |
Siriagus | 12:8178fad5e660 | 74 | // Template functions needs to be declared in the header file |
Siriagus | 12:8178fad5e660 | 75 | // TODO: Add functions for inverse drawing |
hugohu | 19:89c3eeb3761b | 76 | template<class T, size_t rows, size_t cols> |
hugohu | 19:89c3eeb3761b | 77 | void State::drawImage(const T (&img)[rows][cols], int x, int y, bool inverse, bool flipX, bool flipY) |
Siriagus | 12:8178fad5e660 | 78 | { |
Siriagus | 13:7ab71c7c311b | 79 | int targetX, targetY; // The position on the lcd we are writing to |
Siriagus | 13:7ab71c7c311b | 80 | |
Siriagus | 12:8178fad5e660 | 81 | for (int i = 0; i < rows; ++i) |
Siriagus | 12:8178fad5e660 | 82 | { |
Siriagus | 13:7ab71c7c311b | 83 | targetY = (flipY) ? (y + (rows-1) - i) : (y + i); |
Siriagus | 13:7ab71c7c311b | 84 | |
Siriagus | 12:8178fad5e660 | 85 | // Skip if outside dimensions of LCD |
Siriagus | 13:7ab71c7c311b | 86 | if (targetY < 0) continue; |
Siriagus | 13:7ab71c7c311b | 87 | else if (targetY >= HEIGHT) continue; |
Siriagus | 12:8178fad5e660 | 88 | |
Siriagus | 12:8178fad5e660 | 89 | for (int j = 0; j < cols; ++j) |
Siriagus | 12:8178fad5e660 | 90 | { |
Siriagus | 13:7ab71c7c311b | 91 | targetX = (flipX) ? (x + ((cols - 1) - j)) : (x + j); |
Siriagus | 13:7ab71c7c311b | 92 | |
Siriagus | 12:8178fad5e660 | 93 | // Dimensions check. Draws left to right. |
Siriagus | 13:7ab71c7c311b | 94 | if (targetX < 0) continue; |
Siriagus | 13:7ab71c7c311b | 95 | else if (targetX >= WIDTH) continue; |
Siriagus | 12:8178fad5e660 | 96 | |
Siriagus | 13:7ab71c7c311b | 97 | int solid = img[i][j]; |
Siriagus | 13:7ab71c7c311b | 98 | |
Siriagus | 13:7ab71c7c311b | 99 | if ((solid && !inverse) || (!solid && inverse)) |
Siriagus | 13:7ab71c7c311b | 100 | lcd->setPixel(targetX, targetY); |
Siriagus | 12:8178fad5e660 | 101 | |
Siriagus | 12:8178fad5e660 | 102 | } |
Siriagus | 12:8178fad5e660 | 103 | } |
Siriagus | 12:8178fad5e660 | 104 | } |
Siriagus | 1:0cfe2255092a | 105 | #endif |