From b5483c66aebec91d5354b859f365b8b60b1d4819 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Fri, 26 Nov 2010 16:15:18 +0000 Subject: Implement map reading and add tiles sprite sheet --- Level.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ Level.h | 8 ++++++++ Sprite.h | 3 ++- intensemarcus.h | 3 +++ tiles.png | Bin 0 -> 19919 bytes 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tiles.png 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 +#include #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 Binary files /dev/null and b/tiles.png differ -- cgit v1.2.3