diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/map.cpp | 25 | ||||
-rw-r--r-- | src/map.h | 7 | ||||
-rw-r--r-- | src/renderer.cpp | 72 | ||||
-rw-r--r-- | src/renderer.h | 6 |
4 files changed, 70 insertions, 40 deletions
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) { | |||
50 | tilesToStore.push_back(std::move(tile)); | 50 | tilesToStore.push_back(std::move(tile)); |
51 | } | 51 | } |
52 | 52 | ||
53 | layers_.push_back(std::move(tilesToStore)); | 53 | bool above = false; |
54 | for (const tmx::Property& property : tileLayer.getProperties()) { | ||
55 | if (property.getName() == "above" && property.getBoolValue()) { | ||
56 | above = true; | ||
57 | } | ||
58 | } | ||
59 | |||
60 | if (above) { | ||
61 | upperLayers_.push_back(std::move(tilesToStore)); | ||
62 | } else { | ||
63 | lowerLayers_.push_back(std::move(tilesToStore)); | ||
64 | } | ||
54 | } else if (layer->getType() == tmx::Layer::Type::Object) { | 65 | } else if (layer->getType() == tmx::Layer::Type::Object) { |
55 | const auto& objectLayer = layer->getLayerAs<tmx::ObjectGroup>(); | 66 | const auto& objectLayer = layer->getLayerAs<tmx::ObjectGroup>(); |
56 | 67 | ||
@@ -137,9 +148,13 @@ Map::Map(std::string_view name) : name_(name) { | |||
137 | } | 148 | } |
138 | 149 | ||
139 | bool Map::isBlocked(int x, int y) const { | 150 | bool Map::isBlocked(int x, int y) const { |
151 | if (x < 0 || y < 0 || x >= mapSize_.w() || y >= mapSize_.h()) { | ||
152 | return false; | ||
153 | } | ||
154 | |||
140 | int i = x + y * mapSize_.w(); | 155 | int i = x + y * mapSize_.w(); |
141 | 156 | ||
142 | for (const std::vector<Tile>& layer : layers_) { | 157 | for (const std::vector<Tile>& layer : lowerLayers_) { |
143 | if (layer.at(i).blocked) { | 158 | if (layer.at(i).blocked) { |
144 | return true; | 159 | return true; |
145 | } | 160 | } |
@@ -149,9 +164,13 @@ bool Map::isBlocked(int x, int y) const { | |||
149 | } | 164 | } |
150 | 165 | ||
151 | StepType Map::getStepType(int x, int y) const { | 166 | StepType Map::getStepType(int x, int y) const { |
167 | if (x < 0 || y < 0 || x >= mapSize_.w() || y >= mapSize_.h()) { | ||
168 | return StepType::none; | ||
169 | } | ||
170 | |||
152 | int i = x + y * mapSize_.w(); | 171 | int i = x + y * mapSize_.w(); |
153 | 172 | ||
154 | for (const std::vector<Tile>& layer : layers_) { | 173 | for (const std::vector<Tile>& layer : lowerLayers_) { |
155 | if (layer.at(i).step != StepType::none) { | 174 | if (layer.at(i).step != StepType::none) { |
156 | return layer.at(i).step; | 175 | return layer.at(i).step; |
157 | } | 176 | } |
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: | |||
52 | 52 | ||
53 | const vec2i& getTileSize() const { return tileSize_; } | 53 | const vec2i& getTileSize() const { return tileSize_; } |
54 | 54 | ||
55 | const std::vector<std::vector<Tile>>& getLayers() const { return layers_; } | 55 | const std::vector<std::vector<Tile>>& getUpperLayers() const { return upperLayers_; } |
56 | |||
57 | const std::vector<std::vector<Tile>>& getLowerLayers() const { return lowerLayers_; } | ||
56 | 58 | ||
57 | const std::string& getTilesetFilename() const { return tilesetFilename_; } | 59 | const std::string& getTilesetFilename() const { return tilesetFilename_; } |
58 | 60 | ||
@@ -75,7 +77,8 @@ private: | |||
75 | std::string name_; | 77 | std::string name_; |
76 | vec2i mapSize_; | 78 | vec2i mapSize_; |
77 | vec2i tileSize_; | 79 | vec2i tileSize_; |
78 | std::vector<std::vector<Tile>> layers_; | 80 | std::vector<std::vector<Tile>> upperLayers_; |
81 | std::vector<std::vector<Tile>> lowerLayers_; | ||
79 | std::string tilesetFilename_; | 82 | std::string tilesetFilename_; |
80 | int tilesetColumns_; | 83 | int tilesetColumns_; |
81 | std::vector<Prototype> prototypes_; | 84 | std::vector<Prototype> 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() { | |||
36 | } | 36 | } |
37 | } | 37 | } |
38 | 38 | ||
39 | texture_ptr Renderer::renderMapLayer(const Map& map, int layer) { | 39 | texture_ptr Renderer::renderMapLayer(const Map& map, bool above) { |
40 | if (cachedTilesetName_ != map.getTilesetFilename()) { | 40 | if (cachedTilesetName_ != map.getTilesetFilename()) { |
41 | surface_ptr pfs(IMG_Load(map.getTilesetFilename().c_str())); | 41 | surface_ptr pfs(IMG_Load(map.getTilesetFilename().c_str())); |
42 | if (!pfs) { | 42 | if (!pfs) { |
@@ -64,32 +64,40 @@ texture_ptr Renderer::renderMapLayer(const Map& map, int layer) { | |||
64 | SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 0); | 64 | SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 0); |
65 | SDL_RenderClear(ren_.get()); | 65 | SDL_RenderClear(ren_.get()); |
66 | 66 | ||
67 | const std::vector<Tile>& tiles = map.getLayers().at(layer); | 67 | const std::vector<std::vector<Tile>>* layers = nullptr; |
68 | for (int y = 0; y < map.getMapSize().h(); y++) { | 68 | if (above) { |
69 | for (int x = 0; x < map.getMapSize().w(); x++) { | 69 | layers = &map.getUpperLayers(); |
70 | const Tile& tile = tiles.at(x + y * map.getMapSize().w()); | 70 | } else { |
71 | 71 | layers = &map.getLowerLayers(); | |
72 | SDL_Rect srcRect { | 72 | } |
73 | static_cast<int>((tile.id % map.getTilesetColumns()) * map.getTileSize().w()), | 73 | |
74 | static_cast<int>((tile.id / map.getTilesetColumns()) * map.getTileSize().h()), | 74 | for (const std::vector<Tile>& tiles : *layers) { |
75 | map.getTileSize().w(), | 75 | for (int y = 0; y < map.getMapSize().h(); y++) { |
76 | map.getTileSize().h() }; | 76 | for (int x = 0; x < map.getMapSize().w(); x++) { |
77 | 77 | const Tile& tile = tiles.at(x + y * map.getMapSize().w()); | |
78 | SDL_Rect destRect { | 78 | |
79 | x * map.getTileSize().w(), | 79 | SDL_Rect srcRect { |
80 | y * map.getTileSize().h(), | 80 | static_cast<int>((tile.id % map.getTilesetColumns()) * map.getTileSize().w()), |
81 | map.getTileSize().w(), | 81 | static_cast<int>((tile.id / map.getTilesetColumns()) * map.getTileSize().h()), |
82 | map.getTileSize().h() }; | 82 | map.getTileSize().w(), |
83 | 83 | map.getTileSize().h() }; | |
84 | SDL_RendererFlip flip = SDL_FLIP_NONE; | 84 | |
85 | if (tile.flipHorizontal && tile.flipVertical) { | 85 | SDL_Rect destRect { |
86 | flip = static_cast<SDL_RendererFlip>(SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL); | 86 | x * map.getTileSize().w(), |
87 | } else if (tile.flipHorizontal) { | 87 | y * map.getTileSize().h(), |
88 | flip = SDL_FLIP_HORIZONTAL; | 88 | map.getTileSize().w(), |
89 | } else if (tile.flipVertical) { | 89 | map.getTileSize().h() }; |
90 | flip = SDL_FLIP_VERTICAL; | 90 | |
91 | SDL_RendererFlip flip = SDL_FLIP_NONE; | ||
92 | if (tile.flipHorizontal && tile.flipVertical) { | ||
93 | flip = static_cast<SDL_RendererFlip>(SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL); | ||
94 | } else if (tile.flipHorizontal) { | ||
95 | flip = SDL_FLIP_HORIZONTAL; | ||
96 | } else if (tile.flipVertical) { | ||
97 | flip = SDL_FLIP_VERTICAL; | ||
98 | } | ||
99 | SDL_RenderCopyEx(ren_.get(), tilesetTex_.get(), &srcRect, &destRect, 0, nullptr, flip); | ||
91 | } | 100 | } |
92 | SDL_RenderCopyEx(ren_.get(), tilesetTex_.get(), &srcRect, &destRect, 0, nullptr, flip); | ||
93 | } | 101 | } |
94 | } | 102 | } |
95 | 103 | ||
@@ -116,8 +124,8 @@ void Renderer::render(Game& game) { | |||
116 | 124 | ||
117 | if (cachedMapName_ != game.getMap().getName()) { | 125 | if (cachedMapName_ != game.getMap().getName()) { |
118 | cachedMapName_ = game.getMap().getName(); | 126 | cachedMapName_ = game.getMap().getName(); |
119 | renLay1_ = renderMapLayer(game.getMap(), 0); | 127 | renLowerLayer_ = renderMapLayer(game.getMap(), false); |
120 | renLay0_ = renderMapLayer(game.getMap(), 1); | 128 | renUpperLayer_ = renderMapLayer(game.getMap(), true); |
121 | 129 | ||
122 | vec2i mapBounds = game.getMap().getMapSize() * game.getMap().getTileSize(); | 130 | vec2i mapBounds = game.getMap().getMapSize() * game.getMap().getTileSize(); |
123 | mapSwapTex_.reset(SDL_CreateTexture( | 131 | mapSwapTex_.reset(SDL_CreateTexture( |
@@ -150,13 +158,13 @@ void Renderer::render(Game& game) { | |||
150 | SDL_RenderClear(ren_.get()); | 158 | SDL_RenderClear(ren_.get()); |
151 | 159 | ||
152 | // Render lower map layer | 160 | // Render lower map layer |
153 | SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); | 161 | SDL_RenderCopy(ren_.get(), renLowerLayer_.get(), nullptr, nullptr); |
154 | 162 | ||
155 | if (effects.isMapFaded()) { | 163 | if (effects.isMapFaded()) { |
156 | SDL_SetRenderTarget(ren_.get(), mapSwapTex_.get()); | 164 | SDL_SetRenderTarget(ren_.get(), mapSwapTex_.get()); |
157 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); | 165 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); |
158 | SDL_RenderClear(ren_.get()); | 166 | SDL_RenderClear(ren_.get()); |
159 | SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); | 167 | SDL_RenderCopy(ren_.get(), renLowerLayer_.get(), nullptr, nullptr); |
160 | 168 | ||
161 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_MOD); | 169 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_MOD); |
162 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); | 170 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); |
@@ -173,13 +181,13 @@ void Renderer::render(Game& game) { | |||
173 | } | 181 | } |
174 | 182 | ||
175 | // Render upper map layer | 183 | // Render upper map layer |
176 | SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr); | 184 | SDL_RenderCopy(ren_.get(), renUpperLayer_.get(), nullptr, nullptr); |
177 | 185 | ||
178 | if (effects.isMapFaded()) { | 186 | if (effects.isMapFaded()) { |
179 | SDL_SetRenderTarget(ren_.get(), mapSwapTex_.get()); | 187 | SDL_SetRenderTarget(ren_.get(), mapSwapTex_.get()); |
180 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); | 188 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); |
181 | SDL_RenderClear(ren_.get()); | 189 | SDL_RenderClear(ren_.get()); |
182 | SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr); | 190 | SDL_RenderCopy(ren_.get(), renUpperLayer_.get(), nullptr, nullptr); |
183 | 191 | ||
184 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_MOD); | 192 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_MOD); |
185 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); | 193 | 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: | |||
138 | void renderSprite(const Sprite& sprite); | 138 | void renderSprite(const Sprite& sprite); |
139 | 139 | ||
140 | // Map rendering | 140 | // Map rendering |
141 | texture_ptr renderMapLayer(const Map& map, int layer); | 141 | texture_ptr renderMapLayer(const Map& map, bool above); |
142 | 142 | ||
143 | std::string cachedMapName_; | 143 | std::string cachedMapName_; |
144 | std::string cachedTilesetName_; | 144 | std::string cachedTilesetName_; |
145 | texture_ptr tilesetTex_; | 145 | texture_ptr tilesetTex_; |
146 | texture_ptr renLay0_; | 146 | texture_ptr renLowerLayer_; |
147 | texture_ptr renLay1_; | 147 | texture_ptr renUpperLayer_; |
148 | texture_ptr mapSwapTex_; | 148 | texture_ptr mapSwapTex_; |
149 | 149 | ||
150 | // Text rendering | 150 | // Text rendering |