summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Bratch <l_bratch@yahoo.co.uk>2010-11-26 16:15:18 +0000
committerLuke Bratch <l_bratch@yahoo.co.uk>2010-11-26 16:15:18 +0000
commitb5483c66aebec91d5354b859f365b8b60b1d4819 (patch)
treecf74a91fc7dcb97444a4a55bad56d2cad3eacfa2
parentde9f40dfbba90e7ccb0ee5883a150f4e129a5674 (diff)
Implement map reading and add tiles sprite sheet
-rw-r--r--Level.cpp51
-rw-r--r--Level.h8
-rw-r--r--Sprite.h3
-rw-r--r--intensemarcus.h3
-rw-r--r--tiles.pngbin0 -> 19919 bytes
5 files changed, 64 insertions, 1 deletions
diff --git a/Level.cpp b/Level.cpp
index c70181a..ac860b3 100644
--- a/Level.cpp
+++ b/Level.cpp
@@ -1,5 +1,6 @@
#include "Level.h"
#include <iostream>
+#include <fstream>
#include "Monster.h"
#include "Pickup.h"
@@ -36,6 +37,9 @@ void Level::changeLevel(int levelNo) {
player.setClip(ORIENT_BACK, MARCUS_WIDTH * 1, 0, MARCUS_WIDTH, MARCUS_HEIGHT);
player.setClip(ORIENT_RIGHT, MARCUS_WIDTH * 2, 0, MARCUS_WIDTH, MARCUS_HEIGHT);
player.setClip(ORIENT_LEFT, MARCUS_WIDTH * 3, 0, MARCUS_WIDTH, MARCUS_HEIGHT);
+
+ // Read the map file (all level/player init should be done here, not above)
+ loadMap(levelNo);
}
void Level::move() {
@@ -74,4 +78,51 @@ void Level::draw(Screen *screen) {
screen->blit(0, 0, background);
screen->blit(player.getX(), player.getY(), player.image, player.getClip());
+
+ // Draw each platform...
+ for (int i = 0; i < numPlatforms; i++) {
+ // ...if it is visible
+ if (platform[i].getX() + TILE_SIZE >= 0 && platform[i].getX() <= SCREEN_WIDTH) {
+ screen->blit(platform[i].getX(), platform[i].getY(),
+ platform[0].image, platform[i].getClip());
+ }
+ }
}
+
+void Level::loadMap(int LevelNo) {
+ numPlatforms = 0;
+ // TODO: Let each Platform have a reference a single tiles image
+ platform[0].setImage("tiles.png");
+
+ // Open map file
+ std::ifstream map("level01.map");
+
+ if (!map)
+ std::cout << "map is NULL.\n";
+
+ // Read each tile...
+ for (int i = 0; i < MAP_X * MAP_Y; i++) {
+ int type = -1;
+
+ map >> type;
+
+ if (map.fail()) {
+ std::cout << "map.fail().\n";
+ }
+
+ // ...and register it as a platform if it's a platform
+ if (type == 1 || type == 2) {
+ // Set x/y-coordinates
+ platform[numPlatforms].setX((i % MAP_X) * TILE_SIZE);
+ platform[numPlatforms].setY((i / MAP_X) * TILE_SIZE);
+
+ // Set the correct sprite sheet clip
+ platform[numPlatforms].setClip(0, TILE_SIZE * (type - 1), 0, TILE_SIZE, TILE_SIZE);
+
+ numPlatforms++;
+ }
+ }
+
+ // Close map file
+ map.close();
+} \ No newline at end of file
diff --git a/Level.h b/Level.h
index c65eab9..774164e 100644
--- a/Level.h
+++ b/Level.h
@@ -3,6 +3,7 @@
#include "Player.h"
#include "Screen.h"
+#include "Platform.h"
class Level {
public:
@@ -23,6 +24,13 @@ class Level {
private:
// Background image
SDL_Surface *background;
+
+ // Map platforms
+ int numPlatforms;
+ Platform platform[576];
+
+ // Reads in the map file
+ void loadMap(int levelNo);
};
#endif
diff --git a/Sprite.h b/Sprite.h
index d745376..72a5270 100644
--- a/Sprite.h
+++ b/Sprite.h
@@ -22,6 +22,7 @@ class Sprite {
// Load in this sprite's image
bool setImage(std::string path);
+ // TODO: Add method that can set reference to an already loaded image
// This sprite's image
SDL_Surface *image;
@@ -37,7 +38,7 @@ class Sprite {
bool isVisible;
// Sprite clip
- SDL_Rect clip[4];
+ SDL_Rect clip[5];
// Clip number
int clipNo;
};
diff --git a/intensemarcus.h b/intensemarcus.h
index 8a4dd5a..01d7268 100644
--- a/intensemarcus.h
+++ b/intensemarcus.h
@@ -17,6 +17,9 @@ const int ORIENT_RIGHT = 2;
const int ORIENT_LEFT = 3;
const int GRAVITY = 3;
const int JUMP_STRENGTH = 40;
+const int MAP_X = 48;
+const int MAP_Y = 12;
+const int TILE_SIZE = 50;
SDL_Surface *loadImage(std::string filename);
diff --git a/tiles.png b/tiles.png
new file mode 100644
index 0000000..a9175b7
--- /dev/null
+++ b/tiles.png
Binary files differ