From 08ae390643a8b5d4772d673b469686ea294e2f75 Mon Sep 17 00:00:00 2001 From: ephphatha Date: Mon, 5 Jul 2021 19:51:58 +1000 Subject: [PATCH] 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. --- Source/DiabloUI/diabloui.cpp | 2 +- Source/controls/devices/game_controller.cpp | 8 +++---- Source/controls/plrctrls.cpp | 4 ++-- Source/controls/touch.cpp | 24 ++++++++++----------- Source/hwcursor.cpp | 4 ++-- Source/objects.cpp | 2 +- Source/palette.cpp | 6 +++--- Source/utils/display.h | 4 ++-- Source/utils/file_util.cpp | 3 ++- 9 files changed, 28 insertions(+), 29 deletions(-) diff --git a/Source/DiabloUI/diabloui.cpp b/Source/DiabloUI/diabloui.cpp index 629f73587..bec178d6a 100644 --- a/Source/DiabloUI/diabloui.cpp +++ b/Source/DiabloUI/diabloui.cpp @@ -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((SDL_GetTicks() - fadeTc) / 2.083); // 32 frames @ 60hz if (fadeValue > 256) { fadeValue = 256; fadeTc = 0; diff --git a/Source/controls/devices/game_controller.cpp b/Source/controls/devices/game_controller.cpp index 51981cd94..1faf94ba2 100644 --- a/Source/controls/devices/game_controller.cpp +++ b/Source/controls/devices/game_controller.cpp @@ -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(event.caxis.value); leftStickNeedsScaling = true; break; case SDL_CONTROLLER_AXIS_LEFTY: - leftStickYUnscaled = -event.caxis.value; + leftStickYUnscaled = static_cast(-event.caxis.value); leftStickNeedsScaling = true; break; case SDL_CONTROLLER_AXIS_RIGHTX: - rightStickXUnscaled = event.caxis.value; + rightStickXUnscaled = static_cast(event.caxis.value); rightStickNeedsScaling = true; break; case SDL_CONTROLLER_AXIS_RIGHTY: - rightStickYUnscaled = -event.caxis.value; + rightStickYUnscaled = static_cast(-event.caxis.value); rightStickNeedsScaling = true; break; default: diff --git a/Source/controls/plrctrls.cpp b/Source/controls/plrctrls.cpp index 64a80f04c..d5dd25ae9 100644 --- a/Source/controls/plrctrls.cpp +++ b/Source/controls/plrctrls.cpp @@ -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(hiresDX / slowdown); + const int dy = static_cast(hiresDY / slowdown); *x += dx; *y -= dy; lastTc = tc; diff --git a/Source/controls/touch.cpp b/Source/controls/touch.cpp index fba56f682..676f86ae6 100644 --- a/Source/controls/touch.cpp +++ b/Source/controls/touch.cpp @@ -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(event->tfinger.x * visible_width) + x_borderwidth; + y = static_cast(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(event->tfinger.x * visible_width) + x_borderwidth; + y = static_cast(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(event->tfinger.x * visible_width) + x_borderwidth; + y = static_cast(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(mouse_x + (event->tfinger.dx * speedFactor * devilution::GetOutputSurface()->w)); + y = static_cast(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++) { diff --git a/Source/hwcursor.cpp b/Source/hwcursor.cpp index 1e68c6d03..6aa68eac3 100644 --- a/Source/hwcursor.cpp +++ b/Source/hwcursor.cpp @@ -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(surface->w * scaleX); + const int scaledH = static_cast(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); diff --git a/Source/objects.cpp b/Source/objects.cpp index 0f7cea19c..9949a6bd7 100644 --- a/Source/objects.cpp +++ b/Source/objects.cpp @@ -4030,7 +4030,7 @@ bool OperateShrineGlowing(int pnum) int xpLoss = 0; if (playerXP > 5000) { magicGain = 5; - xpLoss = ((double)playerXP * 0.95); + xpLoss = static_cast(playerXP * 0.95); } ModifyPlrMag(myplr, magicGain); plr[myplr]._pExperience = xpLoss; diff --git a/Source/palette.cpp b/Source/palette.cpp index 36cf67c12..46b070a7c 100644 --- a/Source/palette.cpp +++ b/Source/palette.cpp @@ -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(pow(src[i].r / 256.0, g) * 256.0); + dst[i].g = static_cast(pow(src[i].g / 256.0, g) * 256.0); + dst[i].b = static_cast(pow(src[i].b / 256.0, g) * 256.0); } force_redraw = 255; } diff --git a/Source/utils/display.h b/Source/utils/display.h index 0057c3c1b..23972b5d8 100644 --- a/Source/utils/display.h +++ b/Source/utils/display.h @@ -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(*x / scaleX); + *y = static_cast(*y / scaleX); SDL_Rect view; SDL_RenderGetViewport(renderer, &view); diff --git a/Source/utils/file_util.cpp b/Source/utils/file_util.cpp index 35106ad31..f61072427 100644 --- a/Source/utils/file_util.cpp +++ b/Source/utils/file_util.cpp @@ -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(attr.nFileSizeHigh) << (sizeof(attr.nFileSizeHigh) * 8) | attr.nFileSizeLow; return true; #else struct ::stat statResult;