Skip to content

Commit

Permalink
Add tests for Map::tagAreaAsChanged.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Sep 30, 2024
1 parent 38f421b commit ab336cf
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 8 deletions.
13 changes: 6 additions & 7 deletions src/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
}

Expand Down
20 changes: 19 additions & 1 deletion test/FakeDisplay.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
#pragma once

#include <vector>

#include <src/Display.h>

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,
Expand All @@ -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,
Expand Down Expand Up @@ -54,7 +66,13 @@ class FakeDisplay : public Display

void useWindowedMode() override {};

std::vector<ChangedArea> getChangedAreas() const { return changedAreas_; }

void resetChangedAreas() { changedAreas_.clear(); }

private:
int resourceWidth_;
int resourceHeight_;

mutable std::vector<ChangedArea> changedAreas_;
};
83 changes: 83 additions & 0 deletions test/MapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <src/Tank.h>
#include <src/Utils.h>

#include "FakeDisplay.h"

// 0 - plain
// 1 - brick
// 2 - water
Expand Down Expand Up @@ -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);
}
}

0 comments on commit ab336cf

Please sign in to comment.