Skip to content

Commit

Permalink
Fix some conversion warnings from msvc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Mar 6, 2024
1 parent 77bf5bb commit 53298c7
Show file tree
Hide file tree
Showing 34 changed files with 189 additions and 104 deletions.
14 changes: 10 additions & 4 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,13 @@ end
disablewarnings {
"4100", -- '%var': unreferenced formal parameter
"4013", -- '$func' undefined, assuming extern return int
"4244", -- '=': conversion from '$type1' to '$type2', possible loss of data
"4245", -- '=': conversion from '$type1' to '$type2', signed/unsigned mismatch
"4389", -- '==': signed/unsigned mismatch
"4456", -- declaration of '$var' hides previous local declaration
"4457", -- declaration of '$var' hides function parameter
"4458", -- declaration of '$var' hides class member
"4459", -- declaration of '$var' hides global declaration
"4701", -- potentially uninitialized local variable '$var' used
"4703", -- potentially uninitialized local pointer variable '$var' used
"4996", -- '$func': The POSIX name of this item is deprecated. Instead, use the ISO C and C++ conformant name: $func2. See online help for details.

}

filter { "configurations:Debug" }
Expand Down Expand Up @@ -125,6 +121,10 @@ project "resinstaller"

links "SDL_flic"

filter { "toolset:msc*" }
disablewarnings {
"4244", -- '=': conversion from '$type1' to '$type2', possible loss of data
}
filter "action:not vs*"
if vorbis_headerPath then
includedirsafter { vorbis_headerPath }
Expand Down Expand Up @@ -232,6 +232,12 @@ project "SDL_flic"
files { "src/3rd/SDL_flic/**.c", "src/3rd/SDL_flic/**.h" }
vpaths { ["SDL_flic/*"] = "src/3rd/SDL_flic" }

filter { "toolset:msc*" }
disablewarnings {
"4244", -- '=': conversion from '$type1' to '$type2', possible loss of data
}
filter {}

if premake.action.supports("None") then
project "doctest" -- header only
kind "None"
Expand Down
5 changes: 3 additions & 2 deletions src/lib/SDLutility/drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "SDLutility/uniquesurface.h"
#include "utility/box.h"
#include "utility/color.h"
#include "utility/narrow_cast.h"
#include "utility/position.h"

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -179,11 +180,11 @@ void putPixel (SDL_Surface& surface, const cPosition& position, Uint32 pixel)
switch (bpp)
{
case 1:
*p = pixel;
*p = narrow_cast<Uint8> (pixel);
break;

case 2:
*(Uint16*) p = pixel;
*(Uint16*) p = narrow_cast<Uint16> (pixel);
break;

case 3:
Expand Down
6 changes: 3 additions & 3 deletions src/lib/crashreporter/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ void initCrashreporting()
info.uMiniDumpType = MiniDumpWithIndirectlyReferencedMemory;
//info.pszUrl
//info.uPriorities[CR_SFTP] = 1;
info.uPriorities[CR_SMAPI] = -1;
info.uPriorities[CR_SMTP] = -1;
info.uPriorities[CR_HTTP] = -1;
info.uPriorities[CR_SMAPI] = static_cast<UINT> (-1);
info.uPriorities[CR_SMTP] = static_cast<UINT> (-1);
info.uPriorities[CR_HTTP] = static_cast<UINT> (-1);
//info.dwFlags = CR_INST_DONT_SEND_REPORT;
//info.dwFlags = CR_INST_STORE_ZIP_ARCHIVES;
info.dwFlags = CR_INST_SHOW_ADDITIONAL_INFO_FIELDS;
Expand Down
11 changes: 6 additions & 5 deletions src/lib/game/data/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "utility/crc.h"
#include "utility/listhelpers.h"
#include "utility/log.h"
#include "utility/narrow_cast.h"
#include "utility/position.h"
#include "utility/ranges.h"
#include "utility/string/toString.h"
Expand Down Expand Up @@ -374,11 +375,11 @@ bool cStaticMap::loadMap (const std::filesystem::path& filename_)
}

// Generate new Map
this->size = std::max<int> (16, sWidth);
this->size = std::max<int> (16, narrow_cast<int> (sWidth));
Kacheln.resize (size * size, 0);
terrains.resize (iNumberOfTerrains);
terrains.resize (narrow_cast<std::size_t> (iNumberOfTerrains));

graphic.loadPalette (fpMapFile, iPalettePos, iNumberOfTerrains);
graphic.loadPalette (fpMapFile, narrow_cast<std::size_t> (iPalettePos), narrow_cast<std::size_t> (iNumberOfTerrains));
// Load necessary Terrain Graphics
for (int iNum = 0; iNum < iNumberOfTerrains; iNum++)
{
Expand Down Expand Up @@ -407,7 +408,7 @@ bool cStaticMap::loadMap (const std::filesystem::path& filename_)
//SDL_RWclose (fpMapFile);
//return false;
}
if (!graphic.loadTile (fpMapFile, iGraphicsPos, iNum))
if (!graphic.loadTile (fpMapFile, narrow_cast<std::size_t> (iGraphicsPos), iNum))
{
clear();
return false;
Expand Down Expand Up @@ -494,7 +495,7 @@ std::string cMap::resourcesToString() const
str.reserve (4 * Resources.size() + 1);
for (size_t i = 0; i != Resources.size(); ++i)
{
str += getHexValue (static_cast<int> (Resources[i].typ));
str += getHexValue (narrow_cast<unsigned char> (Resources[i].typ));
str += getHexValue (Resources[i].value);
}
return str;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/game/data/rangemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "rangemap.h"

#include "utility/crc.h"
#include "utility/narrow_cast.h"

#include <cassert>

Expand Down Expand Up @@ -223,7 +224,7 @@ void cRangeMap::subtract (const std::vector<uint16_t>& data)
for (size_t i = 0; i < data.size(); i++)
{
auto oldValue = map[i];
map[i] = std::max (oldValue - data[i], 0);
map[i] = narrow_cast<std::uint16_t> (std::max (oldValue - data[i], 0));
if (map[i] == 0 && oldValue > 0)
{
positions.push_back (cPosition (i % size.x(), i / size.x()));
Expand Down
3 changes: 2 additions & 1 deletion src/lib/game/data/units/commandodata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "game/data/units/unit.h"
#include "game/data/units/vehicle.h"
#include "utility/crc.h"
#include "utility/narrow_cast.h"
#include "utility/position.h"

#include <cstdint>
Expand Down Expand Up @@ -91,7 +92,7 @@
{
rank = rank + 1.f / (((int) rank + 5) / 5);
}
return rank;
return narrow_cast<int> (rank);
}

//------------------------------------------------------------------------------
Expand Down
12 changes: 6 additions & 6 deletions src/lib/game/logic/action/actioninitnewgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ namespace
for (std::size_t j = 0; j < i; j++)
{
const float maxDist = 40.f;
float dist = (resSpots[i] - resSpots[j]).l2Norm();
float dist = narrow_cast<float> ((resSpots[i] - resSpots[j]).l2Norm());
if (dist < maxDist) amount[resSpotTypes[j]] += 1 - sqrtf (dist / maxDist);
}

Expand Down Expand Up @@ -283,13 +283,13 @@ namespace
res.typ = type;
if (i >= playerCount)
{
res.value = 1 + model.randomGenerator.get (2 + getResourceAmountFactor (frequencies[type]) * 2);
if (p == pos) res.value += 3 + model.randomGenerator.get (4 + getResourceAmountFactor (frequencies[type]) * 2);
res.value = narrow_cast<char> (1 + model.randomGenerator.get (2 + getResourceAmountFactor (frequencies[type]) * 2));
if (p == pos) res.value += narrow_cast<char> (3 + model.randomGenerator.get (4 + getResourceAmountFactor (frequencies[type]) * 2));
}
else
{
res.value = 1 + 4 + getResourceAmountFactor (frequencies[type]);
if (p == pos) res.value += 3 + 2 + getResourceAmountFactor (frequencies[type]);
res.value = narrow_cast<char> (1 + 4 + getResourceAmountFactor (frequencies[type]));
if (p == pos) res.value += narrow_cast<char> (3 + 2 + getResourceAmountFactor (frequencies[type]));
}
res.value = std::min<unsigned char> (16, res.value);
model.getMap()->setResource (p, res);
Expand Down Expand Up @@ -360,7 +360,7 @@ void cActionInitNewGame::execute (cModel& model) const

const auto playerReadyCount = ranges::count_if (model.getPlayerList(), [] (const auto& player) { return player->getLandingPos() != cPosition{-1, -1}; });

if (playerReadyCount == model.getPlayerList().size())
if (narrow_cast<std::size_t> (playerReadyCount) == model.getPlayerList().size())
{
placeInitialResources (model);
if (model.getGameSettings()->bridgeheadType == eGameSettingsBridgeheadType::Definite) placeMiningStations (model);
Expand Down
3 changes: 2 additions & 1 deletion src/lib/game/logic/attackjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "utility/crc.h"
#include "utility/listhelpers.h"
#include "utility/log.h"
#include "utility/narrow_cast.h"

#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -120,7 +121,7 @@ bool cAttackJob::finished() const
//------------------------------------------------------------------------------
void cAttackJob::onRemoveUnit (const cUnit& unit)
{
if (aggressorId == unit.getId())
if (narrow_cast<unsigned> (aggressorId) == unit.getId())
{
aggressorId = -1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/game/logic/gametimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ cGameTimer::cGameTimer() :
mutex()
{
eventCounter = 0;
maxEventQueueSize = -1;
maxEventQueueSize = static_cast<unsigned>(-1);
timerID = 0;
}

Expand Down
5 changes: 3 additions & 2 deletions src/lib/game/logic/jobs/jobcontainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "job.h"
#include "utility/crc.h"
#include "utility/log.h"
#include "utility/narrow_cast.h"
#include "utility/ranges.h"

#include <algorithm>
Expand Down Expand Up @@ -78,7 +79,7 @@ std::vector<std::unique_ptr<cJob>>::iterator cJobContainer::releaseJob (const cM
if (unit)
{
auto nr = ranges::count_if (jobs, [&] (const auto& x) {
return x->unitId == unit->getId();
return narrow_cast<unsigned> (x->unitId) == unit->getId();
});
if (nr <= 1)
{
Expand All @@ -93,7 +94,7 @@ void cJobContainer::onRemoveUnit (const cUnit& unit)
{
for (auto& job : jobs)
{
if (job->unitId == unit.getId())
if (narrow_cast<unsigned> (job->unitId) == unit.getId())
{
job->unitId = -1;
job->finished = true;
Expand Down
5 changes: 3 additions & 2 deletions src/lib/game/logic/movejob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "game/data/units/vehicle.h"
#include "game/logic/gametimer.h"
#include "game/logic/pathcalculator.h"
#include "utility/narrow_cast.h"
#include "utility/ranges.h"

// N, NE, E, SE, S, SW, W, NW
Expand Down Expand Up @@ -373,7 +374,7 @@ void cMoveJob::updateSpeed (cVehicle& vehicle, const cMap& map)

if (path.empty() || state == eMoveJobState::Stopping || cPathCalculator::calcNextCost (vehicle.getPosition(), path.front(), &vehicle, &map) > vehicle.data.getSpeed())
{
int maxSpeedBreaking = 100 * sqrt (2 * MOVE_ACCELERATION * vehicle.getMovementOffset().l2Norm());
int maxSpeedBreaking = narrow_cast<int> (100 * sqrt (2 * MOVE_ACCELERATION * vehicle.getMovementOffset().l2Norm()));
maxSpeed = std::min (maxSpeed, maxSpeedBreaking);

//don't break to zero before movejob is stopped
Expand All @@ -382,7 +383,7 @@ void cMoveJob::updateSpeed (cVehicle& vehicle, const cMap& map)

if (currentSpeed < maxSpeed)
{
currentSpeed += 100 * MOVE_ACCELERATION;
currentSpeed += narrow_cast<std::size_t> (100 * MOVE_ACCELERATION);
}
if (currentSpeed > maxSpeed)
{
Expand Down
3 changes: 2 additions & 1 deletion src/lib/game/logic/pathcalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "utility/listhelpers.h"
#include "utility/log.h"
#include "utility/mathtools.h"
#include "utility/narrow_cast.h"

#include <cassert>
#include <forward_list>
Expand Down Expand Up @@ -69,7 +70,7 @@ int cPathDestHandler::heuristicCost (const cPosition& source) const
case ePathDestinationType::Attack:
default:
{
return Round ((destination - source).l2Norm());
return Round (narrow_cast<float> ((destination - source).l2Norm()));
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/game/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "game/protocol/netmessage.h"
#include "utility/listhelpers.h"
#include "utility/log.h"
#include "utility/narrow_cast.h"

namespace
{
Expand Down Expand Up @@ -119,7 +120,7 @@ int cNetwork::openServer (int port)
NetLog.debug ("Network: Open server on port: " + std::to_string (port));

IPaddress ipaddr;
if (SDLNet_ResolveHost (&ipaddr, nullptr, port) == -1)
if (SDLNet_ResolveHost (&ipaddr, nullptr, narrow_cast<Uint16> (port)) == -1)
{
return -1;
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/mapdownloader/mapdownload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "settings.h"
#include "utility/crc.h"
#include "utility/log.h"
#include "utility/narrow_cast.h"
#include "utility/string/tolower.h"

#include <filesystem>
Expand Down Expand Up @@ -249,7 +250,7 @@ bool cMapSender::getMapFileContent()
Log.warn (std::string ("MapSender: could not read the map \"") + filename.u8string() + "\" into memory.");
return false;
}
const std::size_t mapSize = file.tellg();
const std::size_t mapSize = narrow_cast<std::size_t> (file.tellg());
sendBuffer.resize (mapSize);
file.seekg (0, std::ios::beg);
file.read (sendBuffer.data(), mapSize);
Expand Down
7 changes: 4 additions & 3 deletions src/lib/output/video/unifonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "settings.h"
#include "utility/color.h"
#include "utility/log.h"
#include "utility/narrow_cast.h"
#include "utility/position.h"
#include "utility/string/trim.h"
#include "utility/string/utf-8.h"
Expand Down Expand Up @@ -600,7 +601,7 @@ void cUnicodeFont::showText (int x, int y, const std::string& text, eUnicodeFont
case eUnicodeFontType::LatinSmallWhite:
case eUnicodeFontType::LatinSmallYellow:
for (char& c : sText)
c = toupper (c);
c = narrow_cast<char> (toupper (narrow_cast<int> (narrow_cast<unsigned char> (c))));
iSpace = 1;
break;
case eUnicodeFontType::LatinNormal:
Expand Down Expand Up @@ -814,7 +815,7 @@ SDL_Rect cUnicodeFont::getTextSize (const std::string& text, eUnicodeFontType fo
case eUnicodeFontType::LatinSmallWhite:
case eUnicodeFontType::LatinSmallYellow:
for (char& c : sText)
c = toupper (c);
c = narrow_cast<char> (toupper (narrow_cast<int> (narrow_cast<unsigned char> (c))));
iSpace = 1;
break;
case eUnicodeFontType::LatinNormal:
Expand Down Expand Up @@ -923,7 +924,7 @@ int cUnicodeFont::getUnicodeCharacterWidth (Uint16 unicodeCharacter, eUnicodeFon
case eUnicodeFontType::LatinSmallRed:
case eUnicodeFontType::LatinSmallWhite:
case eUnicodeFontType::LatinSmallYellow:
unicodeCharacter = toupper (unicodeCharacter);
unicodeCharacter = narrow_cast<Uint16> (toupper (narrow_cast<int> (unicodeCharacter)));
space = 1;
break;
default:
Expand Down
3 changes: 2 additions & 1 deletion src/lib/output/video/video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "utility/os.h"
#include "SDLutility/tosdl.h"
#include "SDLutility/uniquesurface.h"
#include "utility/narrow_cast.h"
#include "utility/thread/ismainthread.h"

#include <SDL.h>
Expand Down Expand Up @@ -515,7 +516,7 @@ void blittPerSurfaceAlphaToAlphaChannel (SDL_Surface* src, SDL_Rect* srcrect, SD
g = (((dcolor & dgmask) * (255 - srcAlpha) * dalpha) >> 8) + g * srcAlpha;
b = (((dcolor & dbmask) * (255 - srcAlpha) * dalpha) >> 8) + b * srcAlpha;

const Uint8 a = srcAlpha + dalpha - (srcAlpha * dalpha) / 255;
const Uint8 a = narrow_cast<Uint8> (srcAlpha + dalpha - (srcAlpha * dalpha) / 255);

if (a > 0)
{
Expand Down
Loading

0 comments on commit 53298c7

Please sign in to comment.