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
Revision 12:8178fad5e660, committed 2015-05-08
- Comitter:
- Siriagus
- Date:
- Fri May 08 23:51:26 2015 +0000
- Parent:
- 11:adb68da98262
- Child:
- 13:7ab71c7c311b
- Commit message:
- Added template drawImage function which can draw images of any dimension.
Changed in this revision
--- a/Entity.h Fri May 08 22:10:54 2015 +0000
+++ b/Entity.h Fri May 08 23:51:26 2015 +0000
@@ -1,19 +1,24 @@
#ifndef ENTITY_H
#define ENTITY_H
+/** An entity represents a movable character, such as the player, enemies etc.
+* Note that the entity class does not contain the sprite (image) of the entity.
+* Different sprites are given as 2D const int arrays in
+* OBS! The entity's dimensions should be the same as the width and height or else this will lead to undefined behaviour!
+*/
class Entity
{
public:
Entity() {x = y = width = height = vx = vy = 0; facingLeft = true; onGround = false;}
Entity(int x, int y, int w, int h) : x(x), y(y), width(w), height(h) {vx = vy = 0; facingLeft = true; onGround = false;}
- int x, y;
- int vx, vy; // Velocity x and y
- int width;
- int height;
+ int x, y; /// Position of entity
+ int vx, vy; /// Velocity of entity
+ int width; /// Width of entity
+ int height; /// Height of entity
bool facingLeft; /// True if the entity is facing left
- bool onGround; /// True if entity is on the ground on the map
+ bool onGround; /// True if entity is standing on the ground.
};
#endif
\ No newline at end of file
--- a/Game.cpp Fri May 08 22:10:54 2015 +0000
+++ b/Game.cpp Fri May 08 23:51:26 2015 +0000
@@ -4,11 +4,11 @@
void Game::init()
{
- // Player
+ // Set initial values for the player
player.x = 40;
player.y = 5;
player.width = player.height = 5;
- onGround = false;
+ player.onGround = false;
}
// Functions
@@ -48,10 +48,10 @@
player.vy += 1;
// Check if player is trying to jump. Player can only jump if it's on the ground
- if (input->read(Input::ButtonA) && onGround)
+ if (input->read(Input::ButtonA) && player.onGround)
{
player.vy = -4;
- onGround = false;
+ player.onGround = false;
}
// Terminal velocity 3 px/update
@@ -142,7 +142,7 @@
if (collision) // If collision
{
player.vy = 0; // Set vertical velocity to 0 (playe
- onGround = true; // Player has hit ground
+ player.onGround = true; // Player has hit ground
break; // Skip the while loop as the player can not move further downwards
}
else // Can safely move player without collision
@@ -219,6 +219,10 @@
// Draw map
drawImage(map);
+ // Draw player
+ drawImage(Image::Player, player.x, player.y);
+
+ /*
// Draw player - TODO: Make this a part of sprite class (so they can draw themselves)
int x0, x1, y0, y1;
x0 = (player.x < 0) ? 0 : player.x; // x0 = max(0,x);
@@ -236,7 +240,7 @@
if (Image::Player[i-y0][xIndex])
lcd->setPixel(j,i);
}
- }
+ }*/
// Render bullets
for (std::vector<Point*>::iterator it = bullets.begin(); it != bullets.end(); ++it)
--- a/Game.h Fri May 08 22:10:54 2015 +0000
+++ b/Game.h Fri May 08 23:51:26 2015 +0000
@@ -1,12 +1,17 @@
#ifndef GAME_H
#define GAME_H
+/** @file Game.h
+* @author Andreas Garmannslund
+*/
+
#include "State.h"
#include "Resources.h" // TODO: Move to State.h ?
#include "Entity.h"
#include "map.h"
#include <vector>
+/// Simple objects with a position (x,y) and velocity (vx, vy)
struct Point
{
int x;
@@ -14,24 +19,30 @@
int vx, vy;
};
+/// State: Game
class Game : public State
{
public:
+ /// Creates the Game state
Game(StateManager* fsm, N5110 *lcd, InputManager* input)
: State(fsm, lcd, input) {init();}
-
+
+ /// Handle input and update logic
virtual void update(float dt);
+
+ /// Draw state to lcd
virtual void render();
- void init();
private:
- Entity player;
- bool onGround; // true if player is on ground
+
- bool releasedBtnB;
+ void init(); /// Sets some initial values
+ Entity player; /// Player object
- std::vector<Point*> bullets;
+ bool releasedBtnB; /// True if button B has been released after being pressed down
+
+ std::vector<Point*> bullets; /// Container for bullets
};
#endif
--- a/MainMenu.cpp Fri May 08 22:10:54 2015 +0000
+++ b/MainMenu.cpp Fri May 08 23:51:26 2015 +0000
@@ -44,6 +44,8 @@
void MainMenu::render()
{
+ drawImage(Border::DotsColumn, 0, 0);
+
switch (currentState)
{
case SELECT_PLAY:
--- a/MainMenu.h Fri May 08 22:10:54 2015 +0000 +++ b/MainMenu.h Fri May 08 23:51:26 2015 +0000 @@ -1,7 +1,13 @@ #ifndef MAIN_MENU_H #define MAIN_MENU_H +/** @file MainMenu.h +* @author Andreas Garmannslund +* @date April 2015 +*/ + #include "State.h" +#include "Resources.h" /// State: Main Menu class MainMenu : public State
--- a/Resources.h Fri May 08 22:10:54 2015 +0000
+++ b/Resources.h Fri May 08 23:51:26 2015 +0000
@@ -5,7 +5,7 @@
/// Images: Arrays consisting of 1 (opaque) or 0 (blank).
namespace Image
-{
+{
const int Player[5][5] = {
{1, 1, 1, 1, 1},
{1, 0, 0, 0, 1},
@@ -24,9 +24,64 @@
};*/
}
-
+namespace Border
+{
+ const int DotsColumn[48][3] = {
+{0,0,0},
+{0,1,0},
+{0,0,0},
+{1,1,1},
+{1,0,1},
+{1,1,1},
+{0,0,0},
+{0,1,0},
+{0,0,0},
+{1,1,1},
+{1,0,1},
+{1,1,1},
+{0,0,0},
+{0,1,0},
+{0,0,0},
+{1,1,1},
+{1,0,1},
+{1,1,1},
+{0,0,0},
+{0,1,0},
+{0,0,0},
+{1,1,1},
+{1,0,1},
+{1,1,1},
+{0,0,0},
+{0,1,0},
+{0,0,0},
+{1,1,1},
+{1,0,1},
+{1,1,1},
+{0,0,0},
+{0,1,0},
+{0,0,0},
+{1,1,1},
+{1,0,1},
+{1,1,1},
+{0,0,0},
+{0,1,0},
+{0,0,0},
+{1,1,1},
+{1,0,1},
+{1,1,1},
+{0,0,0},
+{0,1,0},
+{0,0,0},
+{1,1,1},
+{1,0,1},
+{1,1,1}
+};
-
+
+
+
+
+}
--- a/State.cpp Fri May 08 22:10:54 2015 +0000
+++ b/State.cpp Fri May 08 23:51:26 2015 +0000
@@ -5,16 +5,4 @@
void State::requestStateChange(MainState newState)
{
fsm->requestStateChange(newState);
-}
-
-void State::drawImage(const int img[HEIGHT][WIDTH])
-{
- for (int i = 0; i < HEIGHT; ++i)
- {
- for (int j = 0; j < WIDTH; ++j)
- {
- if (img[i][j])
- lcd->setPixel(j,i);
- }
- }
}
\ No newline at end of file
--- a/State.h Fri May 08 22:10:54 2015 +0000
+++ b/State.h Fri May 08 23:51:26 2015 +0000
@@ -43,7 +43,23 @@
/** Draws an image to the lcd
* @param img Array with the same size as the display, where 1 is opaque, 0 is blank.
*/
- void drawImage(const int img[BANKS][WIDTH]); // Draws an image from array
+ //void drawImage(const int img[BANKS][WIDTH]); // Draws an image from array
+
+
+ /** Draws an image/sprite to the lcd
+ * @param img const int array where a solid pixel equals 1, and a blank pixel equals zero
+ * @param x Horizontal position of image (leftmost pixel)
+ * @param y Vertical position of image (uppermost pixel)
+ * See seperate program for how this array can be generated from an image file using SFML!
+ */
+ template<size_t rows, size_t cols>
+ void drawImage(const int (&img)[rows][cols], int x, int y);
+
+ /** Draws an image/sprite to the lcd with origin in upper-left corner of the lcd
+ * @param img Array where 1 corresponds to opaque and 0 corresponds to blank
+ */
+ template <size_t rows, size_t cols>
+ void drawImage(const int (&img)[rows][cols]) { drawImage(img, 0, 0); }
protected:
N5110 *lcd;
@@ -54,4 +70,27 @@
};
+// Template functions needs to be declared in the header file
+// TODO: Add functions for inverse drawing
+template<size_t rows, size_t cols>
+void State::drawImage(const int (&img)[rows][cols], int x, int y)
+{
+ for (int i = 0; i < rows; ++i)
+ {
+ // Skip if outside dimensions of LCD
+ if (y + i < 0) continue;
+ else if (y + i >= HEIGHT) break; // Drawing top to bottom, so all succeding points will also be outside
+
+ for (int j = 0; j < cols; ++j)
+ {
+ // Dimensions check. Draws left to right.
+ if (x + j < 0) continue;
+ else if (x + j >= WIDTH) break;
+
+ if (img[i][j])
+ lcd->setPixel(x+j,y+i);
+
+ }
+ }
+}
#endif
\ No newline at end of file