diff --git a/src/Map.cpp b/src/Map.cpp index 58fd035..bd81064 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -185,23 +185,22 @@ void Map::shift(Point& pointToShift, Direction direction) const void Map::tagAreaAsChanged(Point leftUpper, Point rightLower) { - const int tileSize{Config::getInstance().getTileSize()}; TilePosition position{screenPointToTilePosition(leftUpper)}; if (point_utils::isValidPoint(leftUpper)) changedTiles_[position.x_][position.y_] = tileChanged_; - position = - screenPointToTilePosition({leftUpper.x_, leftUpper.y_ + tileSize}); - if (point_utils::isValidPoint({leftUpper.x_, leftUpper.y_ + tileSize})) + Point point{leftUpper.x_, rightLower.y_}; + position = screenPointToTilePosition(point); + if (point_utils::isValidPoint(point)) changedTiles_[position.x_][position.y_] = tileChanged_; position = screenPointToTilePosition(rightLower); if (point_utils::isValidPoint(rightLower)) changedTiles_[position.x_][position.y_] = tileChanged_; - position = screenPointToTilePosition( - Point{rightLower.x_, rightLower.y_ - tileSize}); - if (point_utils::isValidPoint({rightLower.x_, rightLower.y_ - tileSize})) + point = {rightLower.x_, leftUpper.y_}; + position = screenPointToTilePosition(point); + if (point_utils::isValidPoint(point)) changedTiles_[position.x_][position.y_] = tileChanged_; } diff --git a/test/FakeDisplay.h b/test/FakeDisplay.h index d55e79b..109f044 100644 --- a/test/FakeDisplay.h +++ b/test/FakeDisplay.h @@ -1,10 +1,19 @@ #pragma once +#include + #include class FakeDisplay : public Display { public: + struct ChangedArea + { + ResourceType type_; + int x_; + int y_; + }; + bool init() override { return true; }; void drawText([[maybe_unused]] int x, [[maybe_unused]] int y, @@ -20,7 +29,10 @@ class FakeDisplay : public Display void drawScaledBitmap([[maybe_unused]] ResourceType resourceType, [[maybe_unused]] int x, [[maybe_unused]] int y, [[maybe_unused]] int width, - [[maybe_unused]] int height) const override {}; + [[maybe_unused]] int height) const override + { + changedAreas_.push_back({resourceType, x, y}); + }; void drawScaledBitmapWithRotation( [[maybe_unused]] ResourceType resourceType, [[maybe_unused]] int x, @@ -54,7 +66,13 @@ class FakeDisplay : public Display void useWindowedMode() override {}; + std::vector getChangedAreas() const { return changedAreas_; } + + void resetChangedAreas() { changedAreas_.clear(); } + private: int resourceWidth_; int resourceHeight_; + + mutable std::vector changedAreas_; }; diff --git a/test/MapTest.cpp b/test/MapTest.cpp index 9f107f5..a54a270 100644 --- a/test/MapTest.cpp +++ b/test/MapTest.cpp @@ -9,6 +9,8 @@ #include #include +#include "FakeDisplay.h" + // 0 - plain // 1 - brick // 2 - water @@ -306,3 +308,84 @@ TEST_CASE("shift", "[map]") REQUIRE(pointToShift == expectedShiftedPoint); } } + +TEST_CASE("changed area", "[map]") +{ + const int tileSize{Config::getInstance().getTileSize()}; + + Map map(tileCount); + std::stringstream stream(getTestMap()); + map.loadMap(stream); + + FakeDisplay display; + map.drawBackground(display); + map.drawForeground(display); + + SECTION("First draw on display") + { + REQUIRE(display.getChangedAreas().size() == (25 + 1)); + } + + display.resetChangedAreas(); + + SECTION("change one tile and draw background") + { + map.tagAreaAsChanged({0, 0}, {0, 0}); + map.drawBackground(display); + auto changed{display.getChangedAreas()}; + REQUIRE(display.getChangedAreas().size() == 1); + } + + SECTION("change 2 tiles in line and draw background") + { + map.tagAreaAsChanged({0, 0}, {tileSize, 0}); + map.drawBackground(display); + REQUIRE(display.getChangedAreas().size() == 2); + } + + SECTION("change 2 tiles in column and draw background") + { + map.tagAreaAsChanged({0, 0}, {0, tileSize}); + map.drawBackground(display); + REQUIRE(display.getChangedAreas().size() == 2); + } + + SECTION("change 4 tiles and draw background") + { + map.tagAreaAsChanged({0, 0}, {tileSize, tileSize}); + map.drawBackground(display); + REQUIRE(display.getChangedAreas().size() == 4); + } + + SECTION("change one tile and draw foreground") + { + map.tagAreaAsChanged({3 * tileSize, 3 * tileSize}, + {3 * tileSize, 3 * tileSize}); + map.drawForeground(display); + REQUIRE(display.getChangedAreas().size() == 1); + } + + SECTION("change 2 tiles in line and draw foreground") + { + map.tagAreaAsChanged({3 * tileSize, 3 * tileSize}, + {4 * tileSize, 3 * tileSize}); + map.drawForeground(display); + REQUIRE(display.getChangedAreas().size() == 1); + } + + SECTION("change 2 tiles in column and draw foreground") + { + map.tagAreaAsChanged({3 * tileSize, 3 * tileSize}, + {3 * tileSize, 4 * tileSize}); + map.drawForeground(display); + REQUIRE(display.getChangedAreas().size() == 1); + } + + SECTION("change 4 tiles and draw foreground") + { + map.tagAreaAsChanged({3 * tileSize, 3 * tileSize}, + {4 * tileSize, 4 * tileSize}); + map.drawForeground(display); + REQUIRE(display.getChangedAreas().size() == 1); + } +}