Add explicit casts for some implicit conversions

Some of these were triggering multiple warnings due to casts applied at times which forced potentially truncating operations.
This commit is contained in:
ephphatha 2021-07-05 19:51:58 +10:00 committed by Anders Jenbo
commit 08ae390643
9 changed files with 28 additions and 29 deletions

View file

@ -646,7 +646,7 @@ void UiFadeIn()
if (fadeValue == 0 && fadeTc == 0)
fadeTc = SDL_GetTicks();
const int prevFadeValue = fadeValue;
fadeValue = (SDL_GetTicks() - fadeTc) / 2.083; // 32 frames @ 60hz
fadeValue = static_cast<int>((SDL_GetTicks() - fadeTc) / 2.083); // 32 frames @ 60hz
if (fadeValue > 256) {
fadeValue = 256;
fadeTc = 0;

View file

@ -132,19 +132,19 @@ bool GameController::ProcessAxisMotion(const SDL_Event &event)
return false;
switch (event.caxis.axis) {
case SDL_CONTROLLER_AXIS_LEFTX:
leftStickXUnscaled = event.caxis.value;
leftStickXUnscaled = static_cast<float>(event.caxis.value);
leftStickNeedsScaling = true;
break;
case SDL_CONTROLLER_AXIS_LEFTY:
leftStickYUnscaled = -event.caxis.value;
leftStickYUnscaled = static_cast<float>(-event.caxis.value);
leftStickNeedsScaling = true;
break;
case SDL_CONTROLLER_AXIS_RIGHTX:
rightStickXUnscaled = event.caxis.value;
rightStickXUnscaled = static_cast<float>(event.caxis.value);
rightStickNeedsScaling = true;
break;
case SDL_CONTROLLER_AXIS_RIGHTY:
rightStickYUnscaled = -event.caxis.value;
rightStickYUnscaled = static_cast<float>(-event.caxis.value);
rightStickNeedsScaling = true;
break;
default:

View file

@ -1175,8 +1175,8 @@ struct RightStickAccumulator {
const int dtc = tc - lastTc;
hiresDX += rightStickX * dtc;
hiresDY += rightStickY * dtc;
const int dx = hiresDX / slowdown;
const int dy = hiresDY / slowdown;
const int dx = static_cast<int>(hiresDX / slowdown);
const int dy = static_cast<int>(hiresDY / slowdown);
*x += dx;
*y -= dy;
lastTc = tc;

View file

@ -148,8 +148,8 @@ static void PreprocessFingerDown(SDL_Event *event)
int y = mouse_y;
if (direct_touch) {
x = event->tfinger.x * visible_width + x_borderwidth;
y = event->tfinger.y * visible_height + y_borderwidth;
x = static_cast<int>(event->tfinger.x * visible_width) + x_borderwidth;
y = static_cast<int>(event->tfinger.y * visible_height) + y_borderwidth;
devilution::OutputToLogical(&x, &y);
}
@ -269,8 +269,8 @@ static void PreprocessFingerUp(SDL_Event *event)
// need to raise the button later
simulated_click_start_time[port][0] = event->tfinger.timestamp;
if (direct_touch) {
x = event->tfinger.x * visible_width + x_borderwidth;
y = event->tfinger.y * visible_height + y_borderwidth;
x = static_cast<int>(event->tfinger.x * visible_width) + x_borderwidth;
y = static_cast<int>(event->tfinger.y * visible_height) + y_borderwidth;
devilution::OutputToLogical(&x, &y);
}
}
@ -311,27 +311,25 @@ static void PreprocessFingerMotion(SDL_Event *event)
if (numFingersDown >= 1) {
int x = mouse_x;
int y = mouse_y;
int xrel = 0;
int yrel = 0;
if (direct_touch) {
x = event->tfinger.x * visible_width + x_borderwidth;
y = event->tfinger.y * visible_height + y_borderwidth;
x = static_cast<int>(event->tfinger.x * visible_width) + x_borderwidth;
y = static_cast<int>(event->tfinger.y * visible_height) + y_borderwidth;
devilution::OutputToLogical(&x, &y);
} else {
// for relative mode, use the pointer speed setting
float speedFactor = 1.0;
constexpr float speedFactor = 1.25F;
// convert touch events to relative mouse pointer events
// Whenever an SDL_event involving the mouse is processed,
x = (mouse_x + (event->tfinger.dx * 1.25 * speedFactor * devilution::GetOutputSurface()->w));
y = (mouse_y + (event->tfinger.dy * 1.25 * speedFactor * devilution::GetOutputSurface()->h));
x = static_cast<int>(mouse_x + (event->tfinger.dx * speedFactor * devilution::GetOutputSurface()->w));
y = static_cast<int>(mouse_y + (event->tfinger.dy * speedFactor * devilution::GetOutputSurface()->h));
}
x = clip(x, 0, devilution::GetOutputSurface()->w);
y = clip(y, 0, devilution::GetOutputSurface()->h);
xrel = x - mouse_x;
yrel = y - mouse_y;
int xrel = x - mouse_x;
int yrel = y - mouse_y;
// update the current finger's coordinates so we can track it later
for (int i = 0; i < MaxNumFingers; i++) {

View file

@ -56,8 +56,8 @@ bool SetHardwareCursor(SDL_Surface *surface, HotpointPosition hotpointPosition)
// SDL does not support BlitScaled from 8-bit to RGBA.
SDLSurfaceUniquePtr converted { SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0) };
const int scaledW = surface->w * scaleX; // NOLINT(bugprone-narrowing-conversions)
const int scaledH = surface->h * scaleY; // NOLINT(bugprone-narrowing-conversions)
const int scaledW = static_cast<int>(surface->w * scaleX);
const int scaledH = static_cast<int>(surface->h * scaleY);
SDLSurfaceUniquePtr scaledSurface { SDL_CreateRGBSurfaceWithFormat(0, scaledW, scaledH, 32, SDL_PIXELFORMAT_ARGB8888) };
SDL_BlitScaled(converted.get(), nullptr, scaledSurface.get(), nullptr);
const Point hotpoint = GetHotpointPosition(*scaledSurface, hotpointPosition);

View file

@ -4030,7 +4030,7 @@ bool OperateShrineGlowing(int pnum)
int xpLoss = 0;
if (playerXP > 5000) {
magicGain = 5;
xpLoss = ((double)playerXP * 0.95);
xpLoss = static_cast<int>(playerXP * 0.95);
}
ModifyPlrMag(myplr, magicGain);
plr[myplr]._pExperience = xpLoss;

View file

@ -41,9 +41,9 @@ void ApplyGamma(SDL_Color *dst, const SDL_Color *src, int n)
g = sgOptions.Graphics.nGammaCorrection / 100.0;
for (i = 0; i < n; i++) {
dst[i].r = pow(src[i].r / 256.0, g) * 256.0;
dst[i].g = pow(src[i].g / 256.0, g) * 256.0;
dst[i].b = pow(src[i].b / 256.0, g) * 256.0;
dst[i].r = static_cast<Uint8>(pow(src[i].r / 256.0, g) * 256.0);
dst[i].g = static_cast<Uint8>(pow(src[i].g / 256.0, g) * 256.0);
dst[i].b = static_cast<Uint8>(pow(src[i].b / 256.0, g) * 256.0);
}
force_redraw = 255;
}

View file

@ -58,8 +58,8 @@ void OutputToLogical(T *x, T *y)
return;
float scaleX;
SDL_RenderGetScale(renderer, &scaleX, NULL);
*x /= scaleX;
*y /= scaleX;
*x = static_cast<T>(*x / scaleX);
*y = static_cast<T>(*y / scaleX);
SDL_Rect view;
SDL_RenderGetViewport(renderer, &view);

View file

@ -99,7 +99,8 @@ bool GetFileSize(const char *path, std::uintmax_t *size)
if (!GetFileAttributesExW(&pathUtf16[0], GetFileExInfoStandard, &attr)) {
return false;
}
*size = (attr.nFileSizeHigh) << (sizeof(attr.nFileSizeHigh) * 8) | attr.nFileSizeLow;
// C4293 in msvc when shifting a 32 bit type by 32 bits.
*size = static_cast<std::uintmax_t>(attr.nFileSizeHigh) << (sizeof(attr.nFileSizeHigh) * 8) | attr.nFileSizeLow;
return true;
#else
struct ::stat statResult;