From d4c76e9c19ec17b9dfa68d51a016e9d2c9482087 Mon Sep 17 00:00:00 2001 From: przemek83 <4788832+przemek83@users.noreply.github.com> Date: Fri, 13 Sep 2024 09:41:00 +0200 Subject: [PATCH] Remove some unsignedness from ints. --- src/Map.cpp | 30 +++++++++++++-------------- src/Map.h | 14 ++++++------- src/MapUtils.cpp | 4 ++-- src/MapUtils.h | 2 +- src/Menu.cpp | 50 ++++++++++++++++++++------------------------- src/Menu.h | 14 ++++++------- src/map/PowerUp.cpp | 5 +---- src/map/PowerUp.h | 2 +- src/map/Tile.cpp | 8 ++++---- src/map/Tile.h | 16 +++++++-------- 10 files changed, 67 insertions(+), 78 deletions(-) diff --git a/src/Map.cpp b/src/Map.cpp index b37258b..2c4edaf 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -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(Point{0, 0})}, - mapDimension_(mapDimension) + mapDimension_{static_cast(mapDimension)} { board_.resize(mapDimension_); for (auto& item : board_) @@ -49,8 +49,8 @@ std::list Map::loadMap(std::iostream& stream) { char sign{}; std::list 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; @@ -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})}; @@ -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_}; @@ -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; @@ -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; @@ -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; diff --git a/src/Map.h b/src/Map.h index 48aaa3a..07f7770 100644 --- a/src/Map.h +++ b/src/Map.h @@ -14,7 +14,7 @@ class Screen; class Map { public: - explicit Map(unsigned int mapDimension); + explicit Map(int mapDimension); std::list loadMap(std::iostream& stream); @@ -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; @@ -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>> board_{}; @@ -60,5 +60,5 @@ class Map bool baseDestroyed_{false}; std::unique_ptr plainTile_; - const unsigned int mapDimension_; + const std::size_t mapDimension_; }; diff --git a/src/MapUtils.cpp b/src/MapUtils.cpp index 281c45d..cd706ad 100644 --- a/src/MapUtils.cpp +++ b/src/MapUtils.cpp @@ -3,9 +3,9 @@ namespace map_utils { std::vector 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: diff --git a/src/MapUtils.h b/src/MapUtils.h index 9f74946..fe52070 100644 --- a/src/MapUtils.h +++ b/src/MapUtils.h @@ -10,5 +10,5 @@ class Map; namespace map_utils { std::vector getMovePoints(Point leftUpperCorner, Direction direction, - unsigned int tileSize); + int tileSize); } // namespace map_utils diff --git a/src/Menu.cpp b/src/Menu.cpp index c1148b0..63b1f7c 100644 --- a/src/Menu.cpp +++ b/src/Menu.cpp @@ -85,7 +85,7 @@ std::pair Menu::playGame() Menu::UserChoice Menu::getUserChoice() { Input input; - for (unsigned int currentItem{0};;) + for (int currentItem{0};;) { const InputAction action{input.getMenuAction()}; @@ -106,10 +106,10 @@ Menu::UserChoice Menu::getUserChoice() } } -void Menu::drawMenuItems(unsigned int currentItem) +void Menu::drawMenuItems(int currentItem) { const auto itemHeight{static_cast(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) @@ -118,35 +118,32 @@ 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(itemHeight / 2.F)}; + const auto itemMiddleY{itemY + static_cast(itemHeight / 2.F)}; screen_.drawText(screen_.getCenterX(), itemMiddleY, items_[item].first); } } -unsigned int Menu::getCurrentItem( - std::pair mousePosition, InputAction action, - unsigned int currentItem) const +int Menu::getCurrentItem(std::pair mousePosition, InputAction action, + int currentItem) const { if ((action == InputAction::UP) && (currentItem > 0)) return currentItem - 1; - if ((action == InputAction::DOWN) && - (currentItem < static_cast(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; } } @@ -154,17 +151,16 @@ unsigned int Menu::getCurrentItem( 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(items_.size()) * getItemHeight() / 2; + return screen_.getCenterY() - (items_.size() * getItemHeight() / 2); } int Menu::getItemWidth() const @@ -179,12 +175,10 @@ int Menu::getItemHeight() const screen_.getBitmapHeight(ResourceType::MENU_ITEM)); } -std::pair Menu::getItemPosition( - unsigned int item) const +std::pair 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}; } diff --git a/src/Menu.h b/src/Menu.h index bfd432c..aded2db 100644 --- a/src/Menu.h +++ b/src/Menu.h @@ -30,15 +30,14 @@ class Menu final EXIT }; - void drawMenuItems(unsigned int currentItem); + void drawMenuItems(int currentItem); - unsigned int getCurrentItem( - std::pair mousePosition, InputAction action, - unsigned int currentItem) const; + int getCurrentItem(std::pair 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; @@ -54,8 +53,7 @@ class Menu final static std::vector> getOptionsMenu(); - std::pair getItemPosition( - unsigned int item) const; + std::pair getItemPosition(int item) const; Screen& screen_; diff --git a/src/map/PowerUp.cpp b/src/map/PowerUp.cpp index 61c8547..98f985d 100644 --- a/src/map/PowerUp.cpp +++ b/src/map/PowerUp.cpp @@ -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; } diff --git a/src/map/PowerUp.h b/src/map/PowerUp.h index 840ee13..8b4ba0c 100644 --- a/src/map/PowerUp.h +++ b/src/map/PowerUp.h @@ -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; diff --git a/src/map/Tile.cpp b/src/map/Tile.cpp index c4800cc..4562ddc 100644 --- a/src/map/Tile.cpp +++ b/src/map/Tile.cpp @@ -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; @@ -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}; } diff --git a/src/map/Tile.h b/src/map/Tile.h index e1ec1bb..81880f3 100644 --- a/src/map/Tile.h +++ b/src/map/Tile.h @@ -5,14 +5,14 @@ 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; @@ -20,12 +20,12 @@ class Tile : public Drawable 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_; };