Skip to content

Commit

Permalink
Remove some C-cast.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Mar 6, 2024
1 parent 53298c7 commit ce6200a
Show file tree
Hide file tree
Showing 29 changed files with 144 additions and 138 deletions.
3 changes: 2 additions & 1 deletion src/lib/game/data/player/clans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "game/data/units/unitdata.h"
#include "utility/log.h"
#include "utility/narrow_cast.h"

cClanData ClanDataGlobal;

Expand Down Expand Up @@ -93,6 +94,6 @@ void cClan::setDefaultName (const std::string& newName)
//------------------------------------------------------------------------------
cClan& cClanData::addClan()
{
clans.emplace_back ((int) clans.size());
clans.emplace_back (narrow_cast<int> (clans.size()));
return clans.back();
}
29 changes: 15 additions & 14 deletions src/lib/game/data/units/building.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "utility/crc.h"
#include "utility/listhelpers.h"
#include "utility/mathtools.h"
#include "utility/narrow_cast.h"
#include "utility/random.h"

#include <cmath>
Expand Down Expand Up @@ -519,34 +520,34 @@ void cBuilding::calcTurboBuild (std::array<int, 3>& turboBuildRounds, std::array
{
case 0:
WorkedRounds = (turboBuildCosts[0] - remainingMetal) / (1.f * staticData->needsMetal);
turboBuildCosts[0] -= (int) (1 * 1 * staticData->needsMetal * WorkedRounds);
turboBuildCosts[1] -= (int) (0.5f * 4 * staticData->needsMetal * WorkedRounds);
turboBuildCosts[2] -= (int) (0.25f * 12 * staticData->needsMetal * WorkedRounds);
turboBuildCosts[0] -= narrow_cast<int> (1 * 1 * staticData->needsMetal * WorkedRounds);
turboBuildCosts[1] -= narrow_cast<int> (0.5f * 4 * staticData->needsMetal * WorkedRounds);
turboBuildCosts[2] -= narrow_cast<int> (0.25f * 12 * staticData->needsMetal * WorkedRounds);
break;

case 1:
WorkedRounds = (turboBuildCosts[1] - remainingMetal) / (float) (4 * staticData->needsMetal);
turboBuildCosts[0] -= (int) (2 * 1 * staticData->needsMetal * WorkedRounds);
turboBuildCosts[1] -= (int) (1 * 4 * staticData->needsMetal * WorkedRounds);
turboBuildCosts[2] -= (int) (0.5f * 12 * staticData->needsMetal * WorkedRounds);
WorkedRounds = (turboBuildCosts[1] - remainingMetal) / (4.f * staticData->needsMetal);
turboBuildCosts[0] -= narrow_cast<int> (2 * 1 * staticData->needsMetal * WorkedRounds);
turboBuildCosts[1] -= narrow_cast<int> (1 * 4 * staticData->needsMetal * WorkedRounds);
turboBuildCosts[2] -= narrow_cast<int> (0.5f * 12 * staticData->needsMetal * WorkedRounds);
break;

case 2:
WorkedRounds = (turboBuildCosts[2] - remainingMetal) / (float) (12 * staticData->needsMetal);
turboBuildCosts[0] -= (int) (4 * 1 * staticData->needsMetal * WorkedRounds);
turboBuildCosts[1] -= (int) (2 * 4 * staticData->needsMetal * WorkedRounds);
turboBuildCosts[2] -= (int) (1 * 12 * staticData->needsMetal * WorkedRounds);
WorkedRounds = (turboBuildCosts[2] - remainingMetal) / (12.f * staticData->needsMetal);
turboBuildCosts[0] -= narrow_cast<int> (4 * 1 * staticData->needsMetal * WorkedRounds);
turboBuildCosts[1] -= narrow_cast<int> (2 * 4 * staticData->needsMetal * WorkedRounds);
turboBuildCosts[2] -= narrow_cast<int> (1 * 12 * staticData->needsMetal * WorkedRounds);
break;
}
}

// calc needed turns
turboBuildRounds[0] = (int) ceilf (turboBuildCosts[0] / (1.f * staticData->needsMetal));
turboBuildRounds[0] = turboBuildCosts[0] / staticData->needsMetal;

if (getStaticData().maxBuildFactor > 1)
{
turboBuildRounds[1] = (int) ceilf (turboBuildCosts[1] / (4.f * staticData->needsMetal));
turboBuildRounds[2] = (int) ceilf (turboBuildCosts[2] / (12.f * staticData->needsMetal));
turboBuildRounds[1] = turboBuildCosts[1] / (4 * staticData->needsMetal);
turboBuildRounds[2] = turboBuildCosts[2] / (12 * staticData->needsMetal);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/game/data/units/commandodata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
// to get to the next ranking
for (std::uint32_t i = 0; i != numberOfSuccess; ++i)
{
rank = rank + 1.f / (((int) rank + 5) / 5);
rank = rank + 1.f / ((narrow_cast<int> (rank) + 5) / 5);
}
return narrow_cast<int> (rank);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/game/data/units/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void cVehicle::calcTurboBuild (std::array<int, 3>& turboBuildTurns, std::array<i
turboBuildCosts[0] = buildCosts;
// prevent division by zero
const auto needsMetal = staticData->needsMetal == 0 ? 1 : staticData->needsMetal;
turboBuildTurns[0] = (int) ceilf (turboBuildCosts[0] / (float) (needsMetal));
turboBuildTurns[0] = turboBuildCosts[0] / needsMetal;
}

// step 2x
Expand Down
2 changes: 1 addition & 1 deletion src/lib/game/logic/attackjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void cAttackJob::fire (cModel& model)
aggressor->data.setShots (aggressor->data.getShots() - 1);
aggressor->data.setAmmo (aggressor->data.getAmmo() - 1);
if (aggressor->isAVehicle() && aggressor->getStaticUnitData().vehicleData.canDriveAndFire == false)
aggressor->data.setSpeed (aggressor->data.getSpeed() - (int) (((float) aggressor->data.getSpeedMax()) / aggressor->data.getShotsMax()));
aggressor->data.setSpeed (aggressor->data.getSpeed() - (aggressor->data.getSpeedMax() / aggressor->data.getShotsMax()));

auto muzzle = createMuzzleFx (*aggressor);
if (muzzle)
Expand Down
13 changes: 7 additions & 6 deletions src/lib/game/logic/pathcalculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "game/data/units/building.h"
#include "game/data/units/vehicle.h"
#include "utility/log.h"
#include "utility/narrow_cast.h"
#include "utility/position.h"

#include <forward_list>
Expand Down Expand Up @@ -151,33 +152,33 @@ int cPathCalculator::calcNextCost (const cPosition& source, const cPosition& des
const auto& unitData = vehicle->getStaticUnitData();
if (unitData.factorAir > 0)
{
costs = (int) (4 * unitData.factorAir);
costs = narrow_cast<int> (4 * unitData.factorAir);
}
else if (map->isWater (destination) && !(map->getField (destination).hasBridgeOrPlattform() && unitData.factorGround > 0))
{
costs = (int) (4 * unitData.factorSea);
costs = narrow_cast<int> (4 * unitData.factorSea);
}
else if (map->isCoast (destination) && !(map->getField (destination).hasBridgeOrPlattform() && unitData.factorGround > 0))
{
costs = (int) (4 * unitData.factorCoast);
costs = narrow_cast<int> (4 * unitData.factorCoast);
}
else
{
costs = (int) (4 * unitData.factorGround);
costs = narrow_cast<int> (4 * unitData.factorGround);
}

// moving on a road is cheaper
// assuming, only speed of ground units can be modified
const cBuilding* building = map->getField (destination).getBaseBuilding();
if (building && building->getStaticData().modifiesSpeed != 0 && unitData.factorGround > 0)
{
costs = (int) (costs * building->getStaticData().modifiesSpeed);
costs = narrow_cast<int> (costs * building->getStaticData().modifiesSpeed);
}

// multiply with the factor 1.5 for diagonal movements
if (source.x() != destination.x() && source.y() != destination.y())
{
costs = (int) (costs * 1.5f);
costs = 3 * costs / 2;
}

return costs;
Expand Down
5 changes: 3 additions & 2 deletions src/lib/game/logic/upgradecalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "utility/crc.h"
#include "utility/log.h"
#include "utility/mathtools.h"
#include "utility/narrow_cast.h"

#include <cassert>
#include <sstream>
Expand Down Expand Up @@ -844,9 +845,9 @@ int cUpgradeCalculator::getMaterialCostForUpgrading (int unitCost) const
int cUpgradeCalculator::getNearestPossibleCost (float realCost, int costDifference) const
{
if (costDifference <= 0)
return (int) realCost;
return narrow_cast<int> (realCost);

int intCost = (int) realCost;
int intCost = narrow_cast<int> (realCost);
int nearestLowerCost = intCost - (intCost % costDifference);
int result;
if (realCost - nearestLowerCost < (costDifference / 2.0f))
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mapdownloader/mapdownload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ uint32_t MapDownload::calculateCheckSum (const std::filesystem::path& mapFilenam
}
if (file.is_open())
{
const int mapSize = (int) file.tellg();
const int mapSize = narrow_cast<int> (file.tellg());
std::vector<char> data (mapSize);
file.seekg (0, std::ios::beg);

Expand Down
5 changes: 3 additions & 2 deletions src/lib/output/video/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "SDLutility/uniquesurface.h"
#include "settings.h"
#include "utility/narrow_cast.h"
#include "utility/signal/signal.h"
#include "utility/signal/signalconnectionmanager.h"

Expand Down Expand Up @@ -238,8 +239,8 @@ SDL_Surface* scaleSurface (SDL_Surface* scr, SDL_Surface* dest, int width, int h
* and scales it if necessary */
inline void CHECK_SCALING (SDL_Surface& surface, SDL_Surface& surface_org, float factor)
{
if (!cSettings::getInstance().shouldDoPrescale() && (surface.w != (int) (surface_org.w * factor) || surface.h != (int) (surface_org.h * (factor))))
scaleSurface (&surface_org, &surface, (int) (surface_org.w * factor), (int) (surface_org.h * factor));
if (!cSettings::getInstance().shouldDoPrescale() && (surface.w != narrow_cast<int> (surface_org.w * factor) || surface.h != narrow_cast<int> (surface_org.h * factor)))
scaleSurface (&surface_org, &surface, narrow_cast<int> (surface_org.w * factor), narrow_cast<int> (surface_org.h * factor));
}

UniqueSurface CreatePfeil (int p1x, int p1y, int p2x, int p2y, int p3x, int p3y, unsigned int color, int size);
Expand Down
14 changes: 7 additions & 7 deletions src/lib/resources/buildinguidata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ namespace
if (building.getIsBig())
{
if (!UnitsUiData.rubbleBig->img) return;
src.w = src.h = (int) (UnitsUiData.rubbleBig->img_org->h * zoomFactor);
src.w = src.h = narrow_cast<int> (UnitsUiData.rubbleBig->img_org->h * zoomFactor);
}
else
{
if (!UnitsUiData.rubbleSmall->img) return;
src.w = src.h = (int) (UnitsUiData.rubbleSmall->img_org->h * zoomFactor);
src.w = src.h = narrow_cast<int> (UnitsUiData.rubbleSmall->img_org->h * zoomFactor);
}

src.x = src.w * building.rubbleTyp;
Expand Down Expand Up @@ -133,8 +133,8 @@ void sBuildingUIData::render_simple (SDL_Surface& surface, const SDL_Rect& dest,
}
else
{
src.w = (int) (img_org->w * zoomFactor);
src.h = (int) (img_org->h * zoomFactor);
src.w = narrow_cast<int> (img_org->w * zoomFactor);
src.h = narrow_cast<int> (img_org->h * zoomFactor);
}

// blit the players color and building graphic
Expand All @@ -157,11 +157,11 @@ void sBuildingUIData::render_simple (SDL_Surface& surface, const SDL_Rect& dest,
CHECK_SCALING (*img, *img_org, zoomFactor);
src.x = 0;
src.y = 0;
src.w = (int) (128 * zoomFactor);
src.h = (int) (128 * zoomFactor);
src.w = narrow_cast<int> (128 * zoomFactor);
src.h = narrow_cast<int> (128 * zoomFactor);
// select clan image
if (clan != -1)
src.x = (int) ((clan + 1) * 128 * zoomFactor);
src.x = narrow_cast<int> ((clan + 1) * 128 * zoomFactor);
SDL_BlitSurface (img.get(), &src, GraphicsData.gfx_tmp.get(), nullptr);
}
else
Expand Down
19 changes: 10 additions & 9 deletions src/lib/resources/playercolor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "SDLutility/tosdl.h"
#include "utility/comparison.h"
#include "utility/narrow_cast.h"
#include "utility/random.h"

/* static */ std::map<cRgbColor, UniqueSurface, sLessRgbColor> cPlayerColor::textures{};
Expand Down Expand Up @@ -84,20 +85,20 @@ namespace
unsigned short changedH;
unsigned char changedS, changedV;

if ((int) (hsvColor.h) + hChange >= 360 || ((int) (hsvColor.h) - hChange >= 0 && randomBernoulli()))
changedH = hsvColor.h - (unsigned short) hChange;
if (narrow_cast<int> (hsvColor.h) + hChange >= 360 || (narrow_cast<int> (hsvColor.h) - hChange >= 0 && randomBernoulli()))
changedH = hsvColor.h - narrow_cast<unsigned short> (hChange);
else
changedH = hsvColor.h + (unsigned short) hChange;
changedH = hsvColor.h + narrow_cast<unsigned short> (hChange);

if ((int) (hsvColor.s) + sChange > 100 || ((int) (hsvColor.s) - sChange >= 0 && randomBernoulli()))
changedS = hsvColor.s - (unsigned char) sChange;
if (narrow_cast<int> (hsvColor.s) + sChange > 100 || (narrow_cast<int> (hsvColor.s) - sChange >= 0 && randomBernoulli()))
changedS = hsvColor.s - narrow_cast<unsigned char> (sChange);
else
changedS = hsvColor.s + (unsigned char) sChange;
changedS = hsvColor.s + narrow_cast<unsigned char> (sChange);

if ((int) (hsvColor.v) + vChange > 100 || ((int) (hsvColor.v) - vChange >= 0 && randomBernoulli()))
changedV = hsvColor.v - (unsigned char) vChange;
if (narrow_cast<int> (hsvColor.v) + vChange > 100 || (narrow_cast<int> (hsvColor.v) - vChange >= 0 && randomBernoulli()))
changedV = hsvColor.v - narrow_cast<unsigned char> (vChange);
else
changedV = hsvColor.v + (unsigned char) vChange;
changedV = hsvColor.v + narrow_cast<unsigned char> (vChange);

randomColors[i] = cHsvColor (changedH, changedS, changedV).toRgb();
} while (randomColors[i] == cRgbColor (0xFF, 0, 0xFF)); // 0xFF00FF is our "transparent color". Hence we do not want to select this color as player color.
Expand Down
14 changes: 7 additions & 7 deletions src/lib/resources/vehicleuidata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace
{
if (!cSettings::getInstance().shouldDoPrescale())
{
const int height = (int) (org_src.h * factor);
const int height = narrow_cast<int> (org_src.h * factor);
const int width = (frames > 1) ? height * frames : narrow_cast<int> (org_src.w * factor);
if (src.w != width || src.h != height)
{
Expand Down Expand Up @@ -69,7 +69,7 @@ void sVehicleUIData::render_shadow (const cVehicle& vehicle, const cMapView& map
// draw shadow
if (vehicle.getFlightHeight() > 0)
{
int high = ((int) (Round (shw_org[vehicle.dir]->w * zoomFactor) * (vehicle.getFlightHeight() / 64.0f)));
const int high = Round (shw_org[vehicle.dir]->w * zoomFactor) * vehicle.getFlightHeight() / 64;
tmp.x += high;
tmp.y += high;

Expand Down Expand Up @@ -97,7 +97,7 @@ void sVehicleUIData::render_smallClearing (const cVehicle& vehicle, unsigned lon
// draw player color
SDL_Rect src;
src.y = 0;
src.h = src.w = (int) (clear_small_org->h * zoomFactor);
src.h = src.w = narrow_cast<int> (clear_small_org->h * zoomFactor);
src.x = (animationTime % 4) * src.w;
if (vehicle.getOwner())
{
Expand Down Expand Up @@ -133,7 +133,7 @@ void sVehicleUIData::render_BuildingOrBigClearing (const cVehicle& vehicle, cons
// draw player color
SDL_Rect src;
src.y = 0;
src.h = src.w = (int) (build_org->h * zoomFactor);
src.h = src.w = narrow_cast<int> (build_org->h * zoomFactor);
src.x = (animationTime % 4) * src.w;
SDL_BlitSurface (cPlayerColor::getTexture (vehicle.getOwner()->getColor()), nullptr, GraphicsData.gfx_tmp.get(), nullptr);
blitWithPreScale (*build_org, *build, &src, *GraphicsData.gfx_tmp, nullptr, zoomFactor, 4);
Expand Down Expand Up @@ -165,13 +165,13 @@ void sVehicleUIData::render_simple (SDL_Surface& surface, const SDL_Rect& dest,

// read the size:
SDL_Rect src;
src.w = (int) (img_org[dir]->w * zoomFactor);
src.h = (int) (img_org[dir]->h * zoomFactor);
src.w = narrow_cast<int> (img_org[dir]->w * zoomFactor);
src.h = narrow_cast<int> (img_org[dir]->h * zoomFactor);

if (vehicleData.animationMovement)
{
SDL_Rect tmp;
src.w = src.h = tmp.h = tmp.w = (int) (img_org[dir]->h * zoomFactor);
src.w = src.h = tmp.h = tmp.w = narrow_cast<int> (img_org[dir]->h * zoomFactor);
tmp.x = walkFrame * tmp.w;
tmp.y = 0;
blitWithPreScale (*img_org[dir], *img[dir], &tmp, *GraphicsData.gfx_tmp, nullptr, zoomFactor);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utility/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ cRgbColor cHsvColor::toRgb() const
}
else
{
const auto hh = (double) h / 60;
const auto i = (int) hh;
const auto hh = (double) h / 60.;
const auto i = narrow_cast<int> (hh);

const auto f = hh - i;

Expand Down
6 changes: 3 additions & 3 deletions src/ui/graphical/game/drawfxeffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace
CHECK_SCALING (*images[1], *images[0], zoom);

SDL_Rect src;
src.x = (int) (images[0]->w * zoom * fx.getDir() / 8);
src.x = narrow_cast<int> (images[0]->w * zoom * fx.getDir() / 8);
src.y = 0;
src.w = images[1]->w / 8;
src.h = images[1]->h;
Expand All @@ -51,7 +51,7 @@ namespace
const int frame = fx.getTick() * frames / fx.getLength();

SDL_Rect src;
src.x = (int) (images[0]->w * zoom * frame / frames);
src.x = narrow_cast<int> (images[0]->w * zoom * frame / frames);
src.y = 0;
src.w = images[1]->w / frames;
src.h = images[1]->h;
Expand Down Expand Up @@ -106,7 +106,7 @@ namespace
const int frame = tick * frames / fx.getLength();

SDL_Rect src;
src.x = (int) (images[0]->w * zoom * frame / frames);
src.x = narrow_cast<int> (images[0]->w * zoom * frame / frames);
src.y = 0;
src.w = images[1]->w / frames;
src.h = images[1]->h;
Expand Down
5 changes: 3 additions & 2 deletions src/ui/graphical/game/gamegui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "utility/indexiterator.h"
#include "utility/language.h"
#include "utility/log.h"
#include "utility/narrow_cast.h"
#include "utility/random.h"

#include <iomanip>
Expand Down Expand Up @@ -657,11 +658,11 @@ bool cGameGui::handleMouseWheelMoved (cApplication& application, cMouse& mouse,

const auto oldScreenPixelX = gameMap->getSize().x() / oldZoomFactor;
const auto newScreenPixelX = gameMap->getSize().x() / newZoomFactor;
scrollOffset.x() = (int) ((oldScreenPixelX - newScreenPixelX) * (mouse.getPosition().x() - gameMap->getPosition().x()) / gameMap->getSize().x() - (oldScreenPixelX - newScreenPixelX) / 2);
scrollOffset.x() = narrow_cast<int> ((oldScreenPixelX - newScreenPixelX) * (mouse.getPosition().x() - gameMap->getPosition().x()) / gameMap->getSize().x() - (oldScreenPixelX - newScreenPixelX) / 2);

const auto oldScreenPixelY = gameMap->getSize().y() / oldZoomFactor;
const auto newScreenPixelY = gameMap->getSize().y() / newZoomFactor;
scrollOffset.y() = (int) ((oldScreenPixelY - newScreenPixelY) * (mouse.getPosition().y() - gameMap->getPosition().y()) / gameMap->getSize().y() - (oldScreenPixelY - newScreenPixelY) / 2);
scrollOffset.y() = narrow_cast<int> ((oldScreenPixelY - newScreenPixelY) * (mouse.getPosition().y() - gameMap->getPosition().y()) / gameMap->getSize().y() - (oldScreenPixelY - newScreenPixelY) / 2);

gameMap->scroll (scrollOffset);
}
Expand Down
Loading

0 comments on commit ce6200a

Please sign in to comment.