Skip to content

Commit

Permalink
Remove some unsignedness from ints.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Sep 13, 2024
1 parent 652742b commit d4c76e9
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 78 deletions.
30 changes: 15 additions & 15 deletions src/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#include "map/TierUp.h"
#include "map/Water.h"

Map::Map(unsigned int mapDimension)
Map::Map(int mapDimension)
: plainTile_{std::make_unique<Plain>(Point{0, 0})},
mapDimension_(mapDimension)
mapDimension_{static_cast<std::size_t>(mapDimension)}
{
board_.resize(mapDimension_);
for (auto& item : board_)
Expand Down Expand Up @@ -49,8 +49,8 @@ std::list<Tank> Map::loadMap(std::iostream& stream)
{
char sign{};
std::list<Tank> tanks;
for (unsigned int y = 0; y < mapDimension_; ++y)
for (unsigned int x = 0; x < mapDimension_; ++x)
for (std::size_t y = 0; y < mapDimension_; ++y)
for (std::size_t x = 0; x < mapDimension_; ++x)
{
stream >> std::noskipws >> sign;

Expand Down Expand Up @@ -135,7 +135,7 @@ bool Map::canFly(Point point) const
return getTile(screenPointToTile(point))->canFly();
}

void Map::hit(Point point, unsigned int power)
void Map::hit(Point point, int power)
{
auto [x, y]{screenPointToTile(point)};
auto& tile{getTile({x, y})};
Expand All @@ -151,7 +151,7 @@ void Map::hit(Point point, unsigned int power)

void Map::shift(Point& pointToShift, Direction direction) const
{
const unsigned int tileSize{Config::getInstance().getTileSize()};
const int tileSize{Config::getInstance().getTileSize()};
const Point leftUpper{pointToShift};
const Point leftLower{leftUpper.x_, (leftUpper.y_ + tileSize) - 1};
const Point rightUpper{(leftUpper.x_ + tileSize) - 1, leftUpper.y_};
Expand Down Expand Up @@ -185,7 +185,7 @@ void Map::shift(Point& pointToShift, Direction direction) const

void Map::tagAreaAsChanged(Point leftUpper, Point rightLower)
{
const unsigned int tileSize{Config::getInstance().getTileSize()};
const int tileSize{Config::getInstance().getTileSize()};
Point point{screenPointToTile(leftUpper)};
if (point_utils::isValidPoint(leftUpper))
changedTiles_[point.x_][point.y_] = true;
Expand Down Expand Up @@ -215,30 +215,30 @@ Point Map::tileToScreenPoint(Point point)
point.y_ * Config::getInstance().getTileSize()};
}

void Map::shiftRight(Point& point, unsigned int tileSize)
void Map::shiftRight(Point& point, int tileSize)
{
point.x_ = ((point.x_ / tileSize) + 1) * tileSize;
}

void Map::shiftLeft(Point& point, unsigned int tileSize)
void Map::shiftLeft(Point& point, int tileSize)
{
point.x_ = (point.x_ / tileSize) * tileSize;
}

void Map::shiftUp(Point& point, unsigned int tileSize)
void Map::shiftUp(Point& point, int tileSize)
{
point.y_ = (point.y_ / tileSize) * tileSize;
}

void Map::shiftDown(Point& point, unsigned int tileSize)
void Map::shiftDown(Point& point, int tileSize)
{
point.y_ = (point.y_ / tileSize + 1) * tileSize;
}

void Map::drawBackground(const Screen& screen)
{
for (unsigned int x = 0; x < mapDimension_; ++x)
for (unsigned int y = 0; y < mapDimension_; ++y)
for (std::size_t x = 0; x < mapDimension_; ++x)
for (std::size_t y = 0; y < mapDimension_; ++y)
{
if (!changedTiles_[x][y])
continue;
Expand All @@ -260,8 +260,8 @@ void Map::drawBackground(const Screen& screen)

void Map::drawForeground(const Screen& screen)
{
for (unsigned int x = 0; x < mapDimension_; ++x)
for (unsigned int y = 0; y < mapDimension_; ++y)
for (std::size_t x = 0; x < mapDimension_; ++x)
for (std::size_t y = 0; y < mapDimension_; ++y)
{
if (!changedTiles_[x][y])
continue;
Expand Down
14 changes: 7 additions & 7 deletions src/Map.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Screen;
class Map
{
public:
explicit Map(unsigned int mapDimension);
explicit Map(int mapDimension);

std::list<Tank> loadMap(std::iostream& stream);

Expand All @@ -28,7 +28,7 @@ class Map

bool isBaseDestroyed() const;

void hit(Point point, unsigned int power);
void hit(Point point, int power);

void shift(Point& pointToShift, Direction direction) const;

Expand All @@ -48,10 +48,10 @@ class Map
static Point screenPointToTile(Point location);
static Point tileToScreenPoint(Point point);

static void shiftRight(Point& point, unsigned int tileSize);
static void shiftLeft(Point& point, unsigned int tileSize);
static void shiftUp(Point& point, unsigned int tileSize);
static void shiftDown(Point& point, unsigned int tileSize);
static void shiftRight(Point& point, int tileSize);
static void shiftLeft(Point& point, int tileSize);
static void shiftUp(Point& point, int tileSize);
static void shiftDown(Point& point, int tileSize);

std::vector<std::vector<std::unique_ptr<Tile>>> board_{};

Expand All @@ -60,5 +60,5 @@ class Map
bool baseDestroyed_{false};

std::unique_ptr<Tile> plainTile_;
const unsigned int mapDimension_;
const std::size_t mapDimension_;
};
4 changes: 2 additions & 2 deletions src/MapUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace map_utils
{
std::vector<Point> getMovePoints(Point leftUpperCorner, Direction direction,
unsigned int tileSize)
int tileSize)
{
const unsigned int oneThirdOfTank{tileSize / 3};
const int oneThirdOfTank{tileSize / 3};
switch (direction)
{
case Direction::UP:
Expand Down
2 changes: 1 addition & 1 deletion src/MapUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ class Map;
namespace map_utils
{
std::vector<Point> getMovePoints(Point leftUpperCorner, Direction direction,
unsigned int tileSize);
int tileSize);
} // namespace map_utils
50 changes: 22 additions & 28 deletions src/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ std::pair<bool, Level> Menu::playGame()
Menu::UserChoice Menu::getUserChoice()
{
Input input;
for (unsigned int currentItem{0};;)
for (int currentItem{0};;)
{
const InputAction action{input.getMenuAction()};

Expand All @@ -106,10 +106,10 @@ Menu::UserChoice Menu::getUserChoice()
}
}

void Menu::drawMenuItems(unsigned int currentItem)
void Menu::drawMenuItems(int currentItem)
{
const auto itemHeight{static_cast<float>(getItemHeight())};
for (unsigned int item = 0; item < items_.size(); ++item)
for (int item = 0; item < items_.size(); ++item)
{
ResourceType itemResource{ResourceType::MENU_ITEM};
if (item == currentItem)
Expand All @@ -118,53 +118,49 @@ void Menu::drawMenuItems(unsigned int currentItem)
const auto [itemX, itemY]{getItemPosition(item)};
screen_.drawScaledBitmap(itemResource, itemX, itemY, getItemWidth(),
getItemHeight());
const auto itemMiddleY{itemY +
static_cast<unsigned int>(itemHeight / 2.F)};
const auto itemMiddleY{itemY + static_cast<int>(itemHeight / 2.F)};
screen_.drawText(screen_.getCenterX(), itemMiddleY, items_[item].first);
}
}

unsigned int Menu::getCurrentItem(
std::pair<unsigned int, unsigned int> mousePosition, InputAction action,
unsigned int currentItem) const
int Menu::getCurrentItem(std::pair<int, int> mousePosition, InputAction action,
int currentItem) const
{
if ((action == InputAction::UP) && (currentItem > 0))
return currentItem - 1;

if ((action == InputAction::DOWN) &&
(currentItem < static_cast<unsigned int>(items_.size()) - 1))
if ((action == InputAction::DOWN) && (currentItem < (items_.size() - 1)))
return currentItem + 1;

if (action == InputAction::MOUSE_MOVE)
{
const unsigned int firstItem{getLocationOfFirstItem()};
const int firstItem{getLocationOfFirstItem()};
const auto [mouseX, mouseY] = mousePosition;
const unsigned int itemWidth{getItemWidth()};
const unsigned int itemHeight{getItemHeight()};
for (unsigned int i = 0; i < items_.size(); ++i)
const int itemWidth{getItemWidth()};
const int itemHeight{getItemHeight()};
for (int i = 0; i < items_.size(); ++i)
{
if ((mouseX > screen_.getCenterX() - itemWidth / 2) &&
(mouseX < screen_.getCenterX() + itemWidth / 2) &&
(mouseY > firstItem + itemHeight * i) &&
(mouseY < firstItem + itemHeight + itemHeight * i))
if ((mouseX > (screen_.getCenterX() - (itemWidth / 2))) &&
(mouseX < (screen_.getCenterX() + (itemWidth / 2))) &&
(mouseY > (firstItem + (itemHeight * i))) &&
(mouseY < (firstItem + itemHeight + (itemHeight * i))))
return i;
}
}

return currentItem;
}

void Menu::redraw(unsigned int currentItem)
void Menu::redraw(int currentItem)
{
screen_.drawBackground(ResourceType::BACKGROUND);
drawMenuItems(currentItem);
Screen::refresh();
}

unsigned int Menu::getLocationOfFirstItem() const
int Menu::getLocationOfFirstItem() const
{
return screen_.getCenterY() -
static_cast<unsigned int>(items_.size()) * getItemHeight() / 2;
return screen_.getCenterY() - (items_.size() * getItemHeight() / 2);
}

int Menu::getItemWidth() const
Expand All @@ -179,12 +175,10 @@ int Menu::getItemHeight() const
screen_.getBitmapHeight(ResourceType::MENU_ITEM));
}

std::pair<unsigned int, unsigned int> Menu::getItemPosition(
unsigned int item) const
std::pair<int, int> Menu::getItemPosition(int item) const
{
const unsigned int itemWidth{getItemWidth()};
const unsigned int itemX{screen_.getCenterX() - itemWidth / 2};
const unsigned int itemY{getLocationOfFirstItem() +
(getItemHeight() * item)};
const int itemWidth{getItemWidth()};
const int itemX{screen_.getCenterX() - itemWidth / 2};
const int itemY{getLocationOfFirstItem() + (getItemHeight() * item)};
return {itemX, itemY};
}
14 changes: 6 additions & 8 deletions src/Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ class Menu final
EXIT
};

void drawMenuItems(unsigned int currentItem);
void drawMenuItems(int currentItem);

unsigned int getCurrentItem(
std::pair<unsigned int, unsigned int> mousePosition, InputAction action,
unsigned int currentItem) const;
int getCurrentItem(std::pair<int, int> mousePosition, InputAction action,
int currentItem) const;

void redraw(unsigned int currentItem);
void redraw(int currentItem);

unsigned int getLocationOfFirstItem() const;
int getLocationOfFirstItem() const;

int getItemWidth() const;

Expand All @@ -54,8 +53,7 @@ class Menu final
static std::vector<std::pair<std::string, Menu::UserChoice>>
getOptionsMenu();

std::pair<unsigned int, unsigned int> getItemPosition(
unsigned int item) const;
std::pair<int, int> getItemPosition(int item) const;

Screen& screen_;

Expand Down
5 changes: 1 addition & 4 deletions src/map/PowerUp.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "PowerUp.h"

PowerUp::PowerUp(unsigned int armor, Point point)
: Tile(armor, point)
{
}
PowerUp::PowerUp(int armor, Point point) : Tile(armor, point) {}

bool PowerUp::canFly() { return true; }

Expand Down
2 changes: 1 addition & 1 deletion src/map/PowerUp.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class PowerUp : public Tile
{
public:
PowerUp(unsigned int armor, Point point);
PowerUp(int armor, Point point);

bool canFly() override;
bool canDrive() override;
Expand Down
8 changes: 4 additions & 4 deletions src/map/Tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include "../Config.h"
#include "../Screen.h"

Tile::Tile(unsigned int armor, Point point) : Drawable(point), armor_(armor) {}
Tile::Tile(int armor, Point point) : Drawable(point), armor_(armor) {}

bool Tile::isPartOfBackground() { return true; }

bool Tile::hit(unsigned int power)
bool Tile::hit(int power)
{
if (power > armor_)
armor_ = 0;
Expand All @@ -27,12 +27,12 @@ bool Tile::isPowerUp() const

void Tile::draw(const Screen& screen) const
{
const unsigned int tileSize{Config::getInstance().getTileSize()};
const int tileSize{Config::getInstance().getTileSize()};
screen.drawScaledSquareBitmap(getResourceType(), getX(), getY(), tileSize);
}

Point Tile::getCenter() const
{
const unsigned int middle{Config::getInstance().getTileSize()};
const int middle{Config::getInstance().getTileSize()};
return {getX() + middle, getY() + middle};
}
16 changes: 8 additions & 8 deletions src/map/Tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@
class Tile : public Drawable
{
public:
Tile(unsigned int armor, Point point);
Tile(int armor, Point point);

virtual bool canFly() = 0;
virtual bool canDrive() = 0;

virtual bool isPartOfBackground();

bool hit(unsigned int power);
bool hit(int power);

bool isPowerUp() const;

void draw(const Screen& screen) const override;
Point getCenter() const override;

protected:
static const unsigned int MAX_ARMOR{999};
static const unsigned int HIGH_ARMOR{100};
static const unsigned int MEDIUM_ARMOR{10};
static const unsigned int LOW_ARMOR{2};
static const unsigned int NO_ARMOR{0};
static const int MAX_ARMOR{999};
static const int HIGH_ARMOR{100};
static const int MEDIUM_ARMOR{10};
static const int LOW_ARMOR{2};
static const int NO_ARMOR{0};

private:
unsigned int armor_;
int armor_;
};

0 comments on commit d4c76e9

Please sign in to comment.