From 3ecf5bbc1c81908bfd3355999a37e8e96e15b8b5 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 22 Feb 2021 22:46:44 -0500 Subject: Generalized upper/lower layer map rendering Map layers can have a flag on them that specifies that they should be rendered as part of the upper set (rendered above the normal sprite layer but below the above sprite layer). Also added map connections between hallucination_interior and hallucination_cliff. And named the layers in the map files bc why not. --- src/map.cpp | 25 +++++++++++++++++--- src/map.h | 7 ++++-- src/renderer.cpp | 72 +++++++++++++++++++++++++++++++------------------------- src/renderer.h | 6 ++--- 4 files changed, 70 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/map.cpp b/src/map.cpp index d7c1e7e..4781231 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -50,7 +50,18 @@ Map::Map(std::string_view name) : name_(name) { tilesToStore.push_back(std::move(tile)); } - layers_.push_back(std::move(tilesToStore)); + bool above = false; + for (const tmx::Property& property : tileLayer.getProperties()) { + if (property.getName() == "above" && property.getBoolValue()) { + above = true; + } + } + + if (above) { + upperLayers_.push_back(std::move(tilesToStore)); + } else { + lowerLayers_.push_back(std::move(tilesToStore)); + } } else if (layer->getType() == tmx::Layer::Type::Object) { const auto& objectLayer = layer->getLayerAs(); @@ -137,9 +148,13 @@ Map::Map(std::string_view name) : name_(name) { } bool Map::isBlocked(int x, int y) const { + if (x < 0 || y < 0 || x >= mapSize_.w() || y >= mapSize_.h()) { + return false; + } + int i = x + y * mapSize_.w(); - for (const std::vector& layer : layers_) { + for (const std::vector& layer : lowerLayers_) { if (layer.at(i).blocked) { return true; } @@ -149,9 +164,13 @@ bool Map::isBlocked(int x, int y) const { } StepType Map::getStepType(int x, int y) const { + if (x < 0 || y < 0 || x >= mapSize_.w() || y >= mapSize_.h()) { + return StepType::none; + } + int i = x + y * mapSize_.w(); - for (const std::vector& layer : layers_) { + for (const std::vector& layer : lowerLayers_) { if (layer.at(i).step != StepType::none) { return layer.at(i).step; } diff --git a/src/map.h b/src/map.h index a311be1..9467d75 100644 --- a/src/map.h +++ b/src/map.h @@ -52,7 +52,9 @@ public: const vec2i& getTileSize() const { return tileSize_; } - const std::vector>& getLayers() const { return layers_; } + const std::vector>& getUpperLayers() const { return upperLayers_; } + + const std::vector>& getLowerLayers() const { return lowerLayers_; } const std::string& getTilesetFilename() const { return tilesetFilename_; } @@ -75,7 +77,8 @@ private: std::string name_; vec2i mapSize_; vec2i tileSize_; - std::vector> layers_; + std::vector> upperLayers_; + std::vector> lowerLayers_; std::string tilesetFilename_; int tilesetColumns_; std::vector prototypes_; diff --git a/src/renderer.cpp b/src/renderer.cpp index f8b2482..3b2bd21 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -36,7 +36,7 @@ Renderer::Renderer() { } } -texture_ptr Renderer::renderMapLayer(const Map& map, int layer) { +texture_ptr Renderer::renderMapLayer(const Map& map, bool above) { if (cachedTilesetName_ != map.getTilesetFilename()) { surface_ptr pfs(IMG_Load(map.getTilesetFilename().c_str())); if (!pfs) { @@ -64,32 +64,40 @@ texture_ptr Renderer::renderMapLayer(const Map& map, int layer) { SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 0); SDL_RenderClear(ren_.get()); - const std::vector& tiles = map.getLayers().at(layer); - for (int y = 0; y < map.getMapSize().h(); y++) { - for (int x = 0; x < map.getMapSize().w(); x++) { - const Tile& tile = tiles.at(x + y * map.getMapSize().w()); - - SDL_Rect srcRect { - static_cast((tile.id % map.getTilesetColumns()) * map.getTileSize().w()), - static_cast((tile.id / map.getTilesetColumns()) * map.getTileSize().h()), - map.getTileSize().w(), - map.getTileSize().h() }; - - SDL_Rect destRect { - x * map.getTileSize().w(), - y * map.getTileSize().h(), - map.getTileSize().w(), - map.getTileSize().h() }; - - SDL_RendererFlip flip = SDL_FLIP_NONE; - if (tile.flipHorizontal && tile.flipVertical) { - flip = static_cast(SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL); - } else if (tile.flipHorizontal) { - flip = SDL_FLIP_HORIZONTAL; - } else if (tile.flipVertical) { - flip = SDL_FLIP_VERTICAL; + const std::vector>* layers = nullptr; + if (above) { + layers = &map.getUpperLayers(); + } else { + layers = &map.getLowerLayers(); + } + + for (const std::vector& tiles : *layers) { + for (int y = 0; y < map.getMapSize().h(); y++) { + for (int x = 0; x < map.getMapSize().w(); x++) { + const Tile& tile = tiles.at(x + y * map.getMapSize().w()); + + SDL_Rect srcRect { + static_cast((tile.id % map.getTilesetColumns()) * map.getTileSize().w()), + static_cast((tile.id / map.getTilesetColumns()) * map.getTileSize().h()), + map.getTileSize().w(), + map.getTileSize().h() }; + + SDL_Rect destRect { + x * map.getTileSize().w(), + y * map.getTileSize().h(), + map.getTileSize().w(), + map.getTileSize().h() }; + + SDL_RendererFlip flip = SDL_FLIP_NONE; + if (tile.flipHorizontal && tile.flipVertical) { + flip = static_cast(SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL); + } else if (tile.flipHorizontal) { + flip = SDL_FLIP_HORIZONTAL; + } else if (tile.flipVertical) { + flip = SDL_FLIP_VERTICAL; + } + SDL_RenderCopyEx(ren_.get(), tilesetTex_.get(), &srcRect, &destRect, 0, nullptr, flip); } - SDL_RenderCopyEx(ren_.get(), tilesetTex_.get(), &srcRect, &destRect, 0, nullptr, flip); } } @@ -116,8 +124,8 @@ void Renderer::render(Game& game) { if (cachedMapName_ != game.getMap().getName()) { cachedMapName_ = game.getMap().getName(); - renLay1_ = renderMapLayer(game.getMap(), 0); - renLay0_ = renderMapLayer(game.getMap(), 1); + renLowerLayer_ = renderMapLayer(game.getMap(), false); + renUpperLayer_ = renderMapLayer(game.getMap(), true); vec2i mapBounds = game.getMap().getMapSize() * game.getMap().getTileSize(); mapSwapTex_.reset(SDL_CreateTexture( @@ -150,13 +158,13 @@ void Renderer::render(Game& game) { SDL_RenderClear(ren_.get()); // Render lower map layer - SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); + SDL_RenderCopy(ren_.get(), renLowerLayer_.get(), nullptr, nullptr); if (effects.isMapFaded()) { SDL_SetRenderTarget(ren_.get(), mapSwapTex_.get()); SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); SDL_RenderClear(ren_.get()); - SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); + SDL_RenderCopy(ren_.get(), renLowerLayer_.get(), nullptr, nullptr); SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_MOD); SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); @@ -173,13 +181,13 @@ void Renderer::render(Game& game) { } // Render upper map layer - SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr); + SDL_RenderCopy(ren_.get(), renUpperLayer_.get(), nullptr, nullptr); if (effects.isMapFaded()) { SDL_SetRenderTarget(ren_.get(), mapSwapTex_.get()); SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); SDL_RenderClear(ren_.get()); - SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr); + SDL_RenderCopy(ren_.get(), renUpperLayer_.get(), nullptr, nullptr); SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_MOD); SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); diff --git a/src/renderer.h b/src/renderer.h index 489a7ec..4c6ccc4 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -138,13 +138,13 @@ private: void renderSprite(const Sprite& sprite); // Map rendering - texture_ptr renderMapLayer(const Map& map, int layer); + texture_ptr renderMapLayer(const Map& map, bool above); std::string cachedMapName_; std::string cachedTilesetName_; texture_ptr tilesetTex_; - texture_ptr renLay0_; - texture_ptr renLay1_; + texture_ptr renLowerLayer_; + texture_ptr renUpperLayer_; texture_ptr mapSwapTex_; // Text rendering -- cgit 1.4.1