SDL: Fixed Visual Studio warning 4244

From 308bcbbe7687f59253c9c648d465f72c2d064821 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 30 Mar 2023 14:04:32 -0700
Subject: [PATCH] Fixed Visual Studio warning 4244

---
 src/audio/SDL_audio.c                      | 10 ++++--
 src/audio/SDL_mixer.c                      | 20 ++++++------
 src/haptic/SDL_haptic.c                    |  2 +-
 src/haptic/windows/SDL_dinputhaptic.c      |  2 +-
 src/haptic/windows/SDL_xinputhaptic.c      |  2 +-
 src/hidapi/windows/hid.c                   |  2 +-
 src/joystick/SDL_joystick.c                |  2 +-
 src/joystick/hidapi/SDL_hidapi_luna.c      |  4 +--
 src/joystick/hidapi/SDL_hidapi_ps3.c       |  6 ++--
 src/joystick/hidapi/SDL_hidapi_steam.c     | 26 +++++++--------
 src/joystick/hidapi/SDL_hidapi_switch.c    |  5 ++-
 src/joystick/hidapi/SDL_hidapi_wii.c       |  8 ++---
 src/joystick/hidapi/SDL_hidapi_xboxone.c   | 14 ++++----
 src/joystick/virtual/SDL_virtualjoystick.c |  6 ++--
 src/joystick/windows/SDL_dinputjoystick.c  | 14 ++++----
 src/render/SDL_yuv_sw_c.h                  |  2 +-
 src/render/software/SDL_rotate.c           |  8 ++---
 src/render/software/SDL_triangle.c         | 18 +++++-----
 src/stdlib/SDL_string.c                    | 28 ++++++++--------
 src/video/SDL_RLEaccel.c                   | 38 +++++++++++-----------
 src/video/SDL_blit.h                       |  7 ++--
 src/video/SDL_blit_1.c                     |  2 +-
 src/video/SDL_blit_A.c                     |  6 ++--
 src/video/SDL_blit_N.c                     | 12 +++----
 src/video/SDL_pixels.c                     | 12 +++----
 src/video/SDL_stretch.c                    | 18 +++++-----
 src/video/SDL_surface.c                    |  2 +-
 src/video/windows/SDL_windowsmessagebox.c  | 16 ++++-----
 src/video/windows/SDL_windowsopengl.c      | 23 +++++++------
 29 files changed, 160 insertions(+), 155 deletions(-)

diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index f7c3ec81b30c..8ae372ed536b 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -1162,7 +1162,7 @@ GetDefaultSamplesFromFreq(int freq)
 {
     /* Pick a default of ~46 ms at desired frequency */
     /* !!! FIXME: remove this when the non-Po2 resampling is in. */
-    const Uint16 max_sample = (freq / 1000) * 46;
+    const Uint16 max_sample = (Uint16)((freq / 1000) * 46);
     Uint16 current_sample = 1;
     while (current_sample < max_sample) {
         current_sample *= 2;
@@ -1373,7 +1373,13 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
      * value we got from 'desired' and round up to the nearest value
      */
     if (!current_audio.impl.SupportsNonPow2Samples && device->spec.samples > 0) {
-        device->spec.samples = SDL_powerof2(device->spec.samples);
+        int samples = SDL_powerof2(device->spec.samples);
+        if (samples <= SDL_MAX_UINT16) {
+            device->spec.samples = (Uint16)samples;
+        } else {
+            SDL_SetError("Couldn't hold sample count %d\n", samples);
+            return 0;
+        }
     }
 
     if (current_audio.impl.OpenDevice(device, devname) < 0) {
diff --git a/src/audio/SDL_mixer.c b/src/audio/SDL_mixer.c
index 337083cac7b6..e1d257d999c2 100644
--- a/src/audio/SDL_mixer.c
+++ b/src/audio/SDL_mixer.c
@@ -78,8 +78,8 @@ static const Uint8 mix8[] = {
 };
 
 /* The volume ranges from 0 - 128 */
-#define ADJUST_VOLUME(s, v)     ((s) = ((s) * (v)) / SDL_MIX_MAXVOLUME)
-#define ADJUST_VOLUME_U8(s, v)  ((s) = ((((s) - 128) * (v)) / SDL_MIX_MAXVOLUME) + 128)
+#define ADJUST_VOLUME(type, s, v) ((s) = (type)(((s) * (v)) / SDL_MIX_MAXVOLUME))
+#define ADJUST_VOLUME_U8(s, v)    ((s) = (Uint8)(((((s) - 128) * (v)) / SDL_MIX_MAXVOLUME) + 128))
 
 int SDL_MixAudioFormat(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format,
                         Uint32 len, int volume)
@@ -115,14 +115,14 @@ int SDL_MixAudioFormat(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format,
         dst8 = (Sint8 *)dst;
         while (len--) {
             src_sample = *src8;
-            ADJUST_VOLUME(src_sample, volume);
+            ADJUST_VOLUME(Sint8, src_sample, volume);
             dst_sample = *dst8 + src_sample;
             if (dst_sample > max_audioval) {
                 dst_sample = max_audioval;
             } else if (dst_sample < min_audioval) {
                 dst_sample = min_audioval;
             }
-            *dst8 = dst_sample;
+            *dst8 = (Sint8)dst_sample;
             ++dst8;
             ++src8;
         }
@@ -138,7 +138,7 @@ int SDL_MixAudioFormat(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format,
         len /= 2;
         while (len--) {
             src1 = SDL_SwapLE16(*(Sint16 *)src);
-            ADJUST_VOLUME(src1, volume);
+            ADJUST_VOLUME(Sint16, src1, volume);
             src2 = SDL_SwapLE16(*(Sint16 *)dst);
             src += 2;
             dst_sample = src1 + src2;
@@ -147,7 +147,7 @@ int SDL_MixAudioFormat(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format,
             } else if (dst_sample < min_audioval) {
                 dst_sample = min_audioval;
             }
-            *(Sint16 *)dst = SDL_SwapLE16(dst_sample);
+            *(Sint16 *)dst = SDL_SwapLE16((Sint16)dst_sample);
             dst += 2;
         }
     } break;
@@ -162,7 +162,7 @@ int SDL_MixAudioFormat(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format,
         len /= 2;
         while (len--) {
             src1 = SDL_SwapBE16(*(Sint16 *)src);
-            ADJUST_VOLUME(src1, volume);
+            ADJUST_VOLUME(Sint16, src1, volume);
             src2 = SDL_SwapBE16(*(Sint16 *)dst);
             src += 2;
             dst_sample = src1 + src2;
@@ -171,7 +171,7 @@ int SDL_MixAudioFormat(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format,
             } else if (dst_sample < min_audioval) {
                 dst_sample = min_audioval;
             }
-            *(Sint16 *)dst = SDL_SwapBE16(dst_sample);
+            *(Sint16 *)dst = SDL_SwapBE16((Sint16)dst_sample);
             dst += 2;
         }
     } break;
@@ -189,7 +189,7 @@ int SDL_MixAudioFormat(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format,
         while (len--) {
             src1 = (Sint64)((Sint32)SDL_SwapLE32(*src32));
             src32++;
-            ADJUST_VOLUME(src1, volume);
+            ADJUST_VOLUME(Sint64, src1, volume);
             src2 = (Sint64)((Sint32)SDL_SwapLE32(*dst32));
             dst_sample = src1 + src2;
             if (dst_sample > max_audioval) {
@@ -214,7 +214,7 @@ int SDL_MixAudioFormat(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format,
         while (len--) {
             src1 = (Sint64)((Sint32)SDL_SwapBE32(*src32));
             src32++;
-            ADJUST_VOLUME(src1, volume);
+            ADJUST_VOLUME(Sint64, src1, volume);
             src2 = (Sint64)((Sint32)SDL_SwapBE32(*dst32));
             dst_sample = src1 + src2;
             if (dst_sample > max_audioval) {
diff --git a/src/haptic/SDL_haptic.c b/src/haptic/SDL_haptic.c
index 72dc78899b00..b12fafb625ae 100644
--- a/src/haptic/SDL_haptic.c
+++ b/src/haptic/SDL_haptic.c
@@ -134,7 +134,7 @@ SDL_HapticOpen(int device_index)
     /* Initialize the haptic device */
     SDL_memset(haptic, 0, sizeof(*haptic));
     haptic->rumble_id = -1;
-    haptic->index = device_index;
+    haptic->index = (Uint8)device_index;
     if (SDL_SYS_HapticOpen(haptic) < 0) {
         SDL_free(haptic);
         return NULL;
diff --git a/src/haptic/windows/SDL_dinputhaptic.c b/src/haptic/windows/SDL_dinputhaptic.c
index 2acf6fd4fda9..20901a165410 100644
--- a/src/haptic/windows/SDL_dinputhaptic.c
+++ b/src/haptic/windows/SDL_dinputhaptic.c
@@ -463,7 +463,7 @@ int SDL_DINPUT_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
 int SDL_DINPUT_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
 {
     SDL_hapticlist_item *item;
-    int index = 0;
+    Uint8 index = 0;
     HRESULT ret;
     DIDEVICEINSTANCE joy_instance;
 
diff --git a/src/haptic/windows/SDL_xinputhaptic.c b/src/haptic/windows/SDL_xinputhaptic.c
index fce0033c9975..681b4571ea9f 100644
--- a/src/haptic/windows/SDL_xinputhaptic.c
+++ b/src/haptic/windows/SDL_xinputhaptic.c
@@ -226,7 +226,7 @@ int SDL_XINPUT_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
 int SDL_XINPUT_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
 {
     SDL_hapticlist_item *item;
-    int index = 0;
+    Uint8 index = 0;
 
     /* Since it comes from a joystick we have to try to match it with a haptic device on our haptic list. */
     for (item = SDL_hapticlist; item != NULL; item = item->next) {
diff --git a/src/hidapi/windows/hid.c b/src/hidapi/windows/hid.c
index 9f22521e70ab..858ce2a1fafe 100644
--- a/src/hidapi/windows/hid.c
+++ b/src/hidapi/windows/hid.c
@@ -603,7 +603,7 @@ static int hid_get_bluetooth_info(const char *path, struct hid_device_info* dev)
 	for (id = compatible_ids; *id; id += wcslen(id) + 1) {
 		/* Normalize to upper case */
 		wchar_t* p = id;
-		for (; *p; ++p) *p = towupper(*p);
+		for (; *p; ++p) *p = (wchar_t)towupper(*p);
 
 		/* USB devices
 		   https://docs.microsoft.com/windows-hardware/drivers/hid/plug-and-play-support
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index a35bd56adaf9..709cda80c179 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -1421,7 +1421,7 @@ void SDL_PrivateJoystickAdded(SDL_JoystickID instance_id)
 
 void SDL_PrivateJoystickForceRecentering(SDL_Joystick *joystick)
 {
-    int i, j;
+    Uint8 i, j;
     Uint64 timestamp = SDL_GetTicksNS();
 
     SDL_AssertJoysticksLocked();
diff --git a/src/joystick/hidapi/SDL_hidapi_luna.c b/src/joystick/hidapi/SDL_hidapi_luna.c
index 905de9c55f43..1b28b99f09df 100644
--- a/src/joystick/hidapi/SDL_hidapi_luna.c
+++ b/src/joystick/hidapi/SDL_hidapi_luna.c
@@ -117,8 +117,8 @@ static int HIDAPI_DriverLuna_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joyst
         Uint8 rumble_packet[] = { 0x03, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xEB };
 
         /* Magnitude is 1..100 so scale the 16-bit input here */
-        rumble_packet[4] = low_frequency_rumble / 655;
-        rumble_packet[5] = high_frequency_rumble / 655;
+        rumble_packet[4] = (Uint8)(low_frequency_rumble / 655);
+        rumble_packet[5] = (Uint8)(high_frequency_rumble / 655);
 
         if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
             return SDL_SetError("Couldn't send rumble packet");
diff --git a/src/joystick/hidapi/SDL_hidapi_ps3.c b/src/joystick/hidapi/SDL_hidapi_ps3.c
index 882f9e4e41c6..498a87215139 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps3.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps3.c
@@ -447,7 +447,7 @@ static void HIDAPI_DriverPS3_HandleStatePacket(SDL_Joystick *joystick, SDL_Drive
             17, /* SDL_GAMEPAD_BUTTON_DPAD_LEFT */
             15, /* SDL_GAMEPAD_BUTTON_DPAD_RIGHT */
         };
-        int i, axis_index = 6;
+        Uint8 i, axis_index = 6;
 
         for (i = 0; i < SDL_arraysize(button_axis_offsets); ++i) {
             int offset = button_axis_offsets[i];
@@ -772,7 +772,7 @@ static void HIDAPI_DriverPS3ThirdParty_HandleStatePacket18(SDL_Joystick *joystic
             7,  /* SDL_GAMEPAD_BUTTON_DPAD_LEFT */
             6,  /* SDL_GAMEPAD_BUTTON_DPAD_RIGHT */
         };
-        int i, axis_index = 6;
+        Uint8 i, axis_index = 6;
 
         for (i = 0; i < SDL_arraysize(button_axis_offsets); ++i) {
             int offset = button_axis_offsets[i];
@@ -888,7 +888,7 @@ static void HIDAPI_DriverPS3ThirdParty_HandleStatePacket19(SDL_Joystick *joystic
             8,  /* SDL_GAMEPAD_BUTTON_DPAD_LEFT */
             7,  /* SDL_GAMEPAD_BUTTON_DPAD_RIGHT */
         };
-        int i, axis_index = 6;
+        Uint8 i, axis_index = 6;
 
         for (i = 0; i < SDL_arraysize(button_axis_offsets); ++i) {
             int offset = button_axis_offsets[i];
diff --git a/src/joystick/hidapi/SDL_hidapi_steam.c b/src/joystick/hidapi/SDL_hidapi_steam.c
index 54d3e40f3251..9fc0874908fd 100644
--- a/src/joystick/hidapi/SDL_hidapi_steam.c
+++ b/src/joystick/hidapi/SDL_hidapi_steam.c
@@ -516,7 +516,7 @@ static bool ResetSteamController(SDL_hid_device *dev, bool bSuppressErrorSpew, u
     ADD_SETTING(SETTING_RIGHT_TRACKPAD_MODE, TRACKPAD_NONE);
     ADD_SETTING(SETTING_SMOOTH_ABSOLUTE_MOUSE, 0);
 #endif
-    buf[2] = nSettings * 3;
+    buf[2] = (unsigned char)(nSettings * 3);
 
     res = SetFeatureReport(dev, buf, 3 + nSettings * 3);
     if (res < 0) {
@@ -616,7 +616,7 @@ static void CloseSteamController(SDL_hid_device *dev)
     SDL_memset(buf, 0, 65);
     buf[1] = ID_SET_SETTINGS_VALUES;
     ADD_SETTING(SETTING_RIGHT_TRACKPAD_MODE, TRACKPAD_ABSOLUTE_MOUSE);
-    buf[2] = nSettings * 3;
+    buf[2] = (unsigned char)(nSettings * 3);
     SetFeatureReport(dev, buf, 3 + nSettings * 3);
 }
 
@@ -640,14 +640,14 @@ static float RemapValClamped(float val, float A, float B, float C, float D)
 //---------------------------------------------------------------------------
 static void RotatePad(int *pX, int *pY, float flAngleInRad)
 {
-    short int origX = *pX, origY = *pY;
+    int origX = *pX, origY = *pY;
 
     *pX = (int)(SDL_cosf(flAngleInRad) * origX - SDL_sinf(flAngleInRad) * origY);
     *pY = (int)(SDL_sinf(flAngleInRad) * origX + SDL_cosf(flAngleInRad) * origY);
 }
 static void RotatePadShort(short *pX, short *pY, float flAngleInRad)
 {
-    short int origX = *pX, origY = *pY;
+    int origX = *pX, origY = *pY;
 
     *pX = (short)(SDL_cosf(flAngleInRad) * origX - SDL_sinf(flAngleInRad) * origY);
     *pY = (short)(SDL_sinf(flAngleInRad) * origX + SDL_cosf(flAngleInRad) * origY);
@@ -753,8 +753,8 @@ static void FormatStatePacketUntilGyro(SteamControllerStateInternal_t *pState, V
         nPadOffset = 0;
     }
 
-    pState->sLeftPadX = clamp(nLeftPadX + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
-    pState->sLeftPadY = clamp(nLeftPadY + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
+    pState->sLeftPadX = (short)clamp(nLeftPadX + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
+    pState->sLeftPadY = (short)clamp(nLeftPadY + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
 
     nPadOffset = 0;
     if (pState->ulButtons & STEAM_RIGHTPAD_FINGERDOWN_MASK) {
@@ -763,8 +763,8 @@ static void FormatStatePacketUntilGyro(SteamControllerStateInternal_t *pState, V
         nPadOffset = 0;
     }
 
-    pState->sRightPadX = clamp(nRightPadX + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
-    pState->sRightPadY = clamp(nRightPadY + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
+    pState->sRightPadX = (short)clamp(nRightPadX + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
+    pState->sRightPadY = (short)clamp(nRightPadY + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
 
     pState->sTriggerL = (unsigned short)RemapValClamped((float)((pStatePacket->ButtonTriggerData.Triggers.nLeft << 7) | pStatePacket->ButtonTriggerData.Triggers.nLeft), 0, STEAMCONTROLLER_TRIGGER_MAX_ANALOG, 0, SDL_MAX_SINT16);
     pState->sTriggerR = (unsigned short)RemapValClamped((float)((pStatePacket->ButtonTriggerData.Triggers.nRight << 7) | pStatePacket->ButtonTriggerData.Triggers.nRight), 0, STEAMCONTROLLER_TRIGGER_MAX_ANALOG, 0, SDL_MAX_SINT16);
@@ -815,8 +815,8 @@ static bool UpdateBLESteamControllerState(const uint8_t *pData, int nDataSize, S
         }
 
         RotatePadShort(&pState->sLeftPadX, &pState->sLeftPadY, -flRotationAngle);
-        pState->sLeftPadX = clamp(pState->sLeftPadX + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
-        pState->sLeftPadY = clamp(pState->sLeftPadY + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
+        pState->sLeftPadX = (short)clamp(pState->sLeftPadX + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
+        pState->sLeftPadY = (short)clamp(pState->sLeftPadY + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
         pData += nLength;
     }
     if (ucOptionDataMask & k_EBLERightTrackpadChunk) {
@@ -832,8 +832,8 @@ static bool UpdateBLESteamControllerState(const uint8_t *pData, int nDataSize, S
         }
 
         RotatePadShort(&pState->sRightPadX, &pState->sRightPadY, flRotationAngle);
-        pState->sRightPadX = clamp(pState->sRightPadX + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
-        pState->sRightPadY = clamp(pState->sRightPadY + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
+        pState->sRightPadX = (short)clamp(pState->sRightPadX + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
+        pState->sRightPadY = (short)clamp(pState->sRightPadY + nPadOffset, SDL_MIN_SINT16, SDL_MAX_SINT16);
         pData += nLength;
     }
     if (ucOptionDataMask & k_EBLEIMUAccelChunk) {
@@ -1074,7 +1074,7 @@ static int HIDAPI_DriverSteam_SetSensorsEnabled(SDL_HIDAPI_Device *device, SDL_J
     } else {
         ADD_SETTING(SETTING_GYRO_MODE, 0x00 /* SETTING_GYRO_MODE_OFF */);
     }
-    buf[2] = nSettings * 3;
+    buf[2] = (unsigned char)(nSettings * 3);
     if (SetFeatureReport(device->dev, buf, 3 + nSettings * 3) < 0) {
         return SDL_SetError("Couldn't write feature report");
     }
diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c
index e03c3d66eab5..ea2b5f9e2c3b 100644
--- a/src/joystick/hidapi/SDL_hidapi_switch.c
+++ b/src/joystick/hidapi/SDL_hidapi_switch.c
@@ -681,12 +681,15 @@ static void SDLCALL SDL_HomeLEDHintChanged(void *userdata, const char *name, con
 
         if (SDL_strchr(hint, '.') != NULL) {
             value = (int)(100.0f * SDL_atof(hint));
+            if (value > 255) {
+                value = 255;
+            }
         } else if (SDL_GetStringBoolean(hint, SDL_TRUE)) {
             value = 100;
         } else {
             value = 0;
         }
-        SetHomeLED(ctx, value);
+        SetHomeLED(ctx, (Uint8)value);
     }
 }
 
diff --git a/src/joystick/hidapi/SDL_hidapi_wii.c b/src/joystick/hidapi/SDL_hidapi_wii.c
index 4d45345a4b4d..8b6fa2a8614c 100644
--- a/src/joystick/hidapi/SDL_hidapi_wii.c
+++ b/src/joystick/hidapi/SDL_hidapi_wii.c
@@ -256,11 +256,11 @@ static SDL_bool WriteRegister(SDL_DriverWii_Context *ctx, Uint32 address, const
 
     SDL_zeroa(writeRequest);
     writeRequest[0] = k_eWiiOutputReportIDs_WriteMemory;
-    writeRequest[1] = 0x04 | ctx->m_bRumbleActive;
+    writeRequest[1] = (Uint8)(0x04 | ctx->m_bRumbleActive);
     writeRequest[2] = (address >> 16) & 0xff;
     writeRequest[3] = (address >> 8) & 0xff;
     writeRequest[4] = address & 0xff;
-    writeRequest[5] = size;
+    writeRequest[5] = (Uint8)size;
     SDL_assert(size > 0 && size <= 16);
     SDL_memcpy(writeRequest + 6, data, size);
 
@@ -285,7 +285,7 @@ static SDL_bool ReadRegister(SDL_DriverWii_Context *ctx, Uint32 address, int siz
     Uint8 readRequest[7];
 
     readRequest[0] = k_eWiiOutputReportIDs_ReadMemory;
-    readRequest[1] = 0x04 | ctx->m_bRumbleActive;
+    readRequest[1] = (Uint8)(0x04 | ctx->m_bRumbleActive);
     readRequest[2] = (address >> 16) & 0xff;
     readRequest[3] = (address >> 8) & 0xff;
     readRequest[4] = address & 0xff;
@@ -1400,7 +1400,7 @@ static void GetExtensionData(WiiButtonData *dst, const Uint8 *src, int size)
     }
     if (valid_data) {
         SDL_memcpy(dst->rgucExtension, src, size);
-        dst->ucNExtensionBytes = size;
+        dst->ucNExtensionBytes = (Uint8)size;
     }
 }
 
diff --git a/src/joystick/hidapi/SDL_hidapi_xboxone.c b/src/joystick/hidapi/SDL_hidapi_xboxone.c
index aa0b32363650..0dd31db9794b 100644
--- a/src/joystick/hidapi/SDL_hidapi_xboxone.c
+++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c
@@ -514,8 +514,8 @@ static int HIDAPI_DriverXboxOne_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Jo
     SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
 
     /* Magnitude is 1..100 so scale the 16-bit input here */
-    ctx->low_frequency_rumble = low_frequency_rumble / 655;
-    ctx->high_frequency_rumble = high_frequency_rumble / 655;
+    ctx->low_frequency_rumble = (Uint8)(low_frequency_rumble / 655);
+    ctx->high_frequency_rumble = (Uint8)(high_frequency_rumble / 655);
     ctx->rumble_pending = SDL_TRUE;
 
     return HIDAPI_DriverXboxOne_UpdateRumble(device);
@@ -530,8 +530,8 @@ static int HIDAPI_DriverXboxOne_RumbleJoystickTriggers(SDL_HIDAPI_Device *device
     }
 
     /* Magnitude is 1..100 so scale the 16-bit input here */
-    ctx->left_trigger_rumble = left_rumble / 655;
-    ctx->right_trigger_rumble = right_rumble / 655;
+    ctx->left_trigger_rumble = (Uint8)(left_rumble / 655);
+    ctx->right_trigger_rumble = (Uint8)(right_rumble / 655);
     ctx->rumble_pending = SDL_TRUE;
 
     return HIDAPI_DriverXboxOne_UpdateRumble(device);
@@ -638,7 +638,7 @@ static void HIDAPI_DriverXboxOne_HandleUnmappedStatePacket(SDL_Joystick *joystic
     }
 
     if (ctx->last_paddle_state != data[paddle_index]) {
-        int nButton = SDL_GAMEPAD_BUTTON_MISC1 + ctx->has_share_button; /* Next available button */
+        Uint8 nButton = (Uint8)(SDL_GAMEPAD_BUTTON_MISC1 + ctx->has_share_button); /* Next available button */
         SDL_SendJoystickButton(timestamp, joystick, nButton++, (data[paddle_index] & button1_bit) ? SDL_PRESSED : SDL_RELEASED);
         SDL_SendJoystickButton(timestamp, joystick, nButton++, (data[paddle_index] & button2_bit) ? SDL_PRESSED : SDL_RELEASED);
         SDL_SendJoystickButton(timestamp, joystick, nButton++, (data[paddle_index] & button3_bit) ? SDL_PRESSED : SDL_RELEASED);
@@ -781,7 +781,7 @@ static void HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, SDL_D
         }
 
         if (ctx->last_paddle_state != data[paddle_index]) {
-            int nButton = SDL_GAMEPAD_BUTTON_MISC1 + ctx->has_share_button; /* Next available button */
+            Uint8 nButton = (Uint8)(SDL_GAMEPAD_BUTTON_MISC1 + ctx->has_share_button); /* Next available button */
             SDL_SendJoystickButton(timestamp, joystick, nButton++, (data[paddle_index] & button1_bit) ? SDL_PRESSED : SDL_RELEASED);
             SDL_SendJoystickButton(timestamp, joystick, nButton++, (data[paddle_index] & button2_bit) ? SDL_PRESSED : SDL_RELEASED);
             SDL_SendJoystickButton(timestamp, joystick, nButton++, (data[paddle_index] & button3_bit) ? SDL_PRESSED : SDL_RELEASED);
@@ -948,7 +948,7 @@ static void HIDAPI_DriverXboxOneBluetooth_HandleButtons(Uint64 timestamp, SDL_Jo
         }
 
         if (ctx->last_paddle_state != data[paddle_index]) {
-            int nButton = SDL_GAMEPAD_BUTTON_MISC1; /* Next available button */
+            Uint8 nButton = SDL_GAMEPAD_BUTTON_MISC1; /* Next available button */
             SDL_SendJoystickButton(timestamp, joystick, nButton++, (data[paddle_index] & button1_bit) ? SDL_PRESSED : SDL_RELEASED);
             SDL_SendJoystickButton(timestamp, joystick, nButton++, (data[paddle_index] & button2_bit) ? SDL_PRESSED : SDL_RELEASED);
             SDL_SendJoystickButton(timestamp, joystick, nButton++, (data[paddle_index] & button3_bit) ? SDL_PRESSED : SDL_RELEASED);
diff --git a/src/joystick/virtual/SDL_virtualjoystick.c b/src/joystick/virtual/SDL_virtualjoystick.c
index b221b29edc9a..7ac3bb233821 100644
--- a/src/joystick/virtual/SDL_virtualjoystick.c
+++ b/src/joystick/virtual/SDL_virtualjoystick.c
@@ -530,7 +530,7 @@ static int VIRTUAL_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool en
 static void VIRTUAL_JoystickUpdate(SDL_Joystick *joystick)
 {
     joystick_hwdata *hwdata;
-    int i;
+    Uint8 i;
     Uint64 timestamp = SDL_GetTicksNS();
 
     SDL_AssertJoysticksLocked();
@@ -582,8 +582,8 @@ static void VIRTUAL_JoystickQuit(void)
 static SDL_bool VIRTUAL_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
 {
     joystick_hwdata *hwdata = VIRTUAL_HWDataForIndex(device_index);
-    int current_button = 0;
-    int current_axis = 0;
+    Uint8 current_button = 0;
+    Uint8 current_axis = 0;
 
     if (hwdata->desc.type != SDL_JOYSTICK_TYPE_GAMEPAD) {
         return SDL_FALSE;
diff --git a/src/joystick/windows/SDL_dinputjoystick.c b/src/joystick/windows/SDL_dinputjoystick.c
index 3e14b470ddef..3d4ce62344de 100644
--- a/src/joystick/windows/SDL_dinputjoystick.c
+++ b/src/joystick/windows/SDL_dinputjoystick.c
@@ -607,12 +607,12 @@ static BOOL CALLBACK EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE pDeviceObj
 
     if (pDeviceObject->dwType & DIDFT_BUTTON) {
         in->type = BUTTON;
-        in->num = joystick->nbuttons;
+        in->num = (Uint8)joystick->nbuttons;
         in->ofs = DIJOFS_BUTTON(in->num);
         joystick->nbuttons++;
     } else if (pDeviceObject->dwType & DIDFT_POV) {
         in->type = HAT;
-        in->num = joystick->nhats;
+        in->num = (Uint8)joystick->nhats;
         in->ofs = DIJOFS_POV(in->num);
         joystick->nhats++;
     } else if (pDeviceObject->dwType & DIDFT_AXIS) {
@@ -620,7 +620,7 @@ static BOOL CALLBACK EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE pDeviceObj
         DIPROPDWORD dilong;
 
         in->type = AXIS;
-        in->num = joystick->naxes;
+        in->num = (Uint8)joystick->naxes;
         if (SDL_memcmp(&pDeviceObject->guidType, &GUID_XAxis, sizeof(pDeviceObject->guidType)) == 0) {
             in->ofs = DIJOFS_X;
         } else if (SDL_memcmp(&pDeviceObject->guidType, &GUID_YAxis, sizeof(pDeviceObject->guidType)) == 0) {
@@ -703,9 +703,9 @@ static int SDLCALL SortDevFunc(const void *a, const void *b)
 static void SortDevObjects(SDL_Joystick *joystick)
 {
     input_t *inputs = joystick->hwdata->Inputs;
-    int nButtons = 0;
-    int nHats = 0;
-    int nAxis = 0;
+    Uint8 nButtons = 0;
+    Uint8 nHats = 0;
+    Uint8 nAxis = 0;
     int n;
 
     SDL_qsort(inputs, joystick->hwdata->NumInputs, sizeof(input_t), SortDevFunc);
@@ -949,7 +949,7 @@ SDL_DINPUT_JoystickGetCapabilities(SDL_Joystick *joystick)
 
 static Uint8 TranslatePOV(DWORD value)
 {
-    const int HAT_VALS[] = {
+    const Uint8 HAT_VALS[] = {
         SDL_HAT_UP,
         SDL_HAT_UP | SDL_HAT_RIGHT,
         SDL_HAT_RIGHT,
diff --git a/src/render/SDL_yuv_sw_c.h b/src/render/SDL_yuv_sw_c.h
index 8b674aade504..85d67f045a1b 100644
--- a/src/render/SDL_yuv_sw_c.h
+++ b/src/render/SDL_yuv_sw_c.h
@@ -34,7 +34,7 @@ struct SDL_SW_YUVTexture
     Uint8 *pixels;
 
     /* These are just so we don't have to allocate them separately */
-    Uint16 pitches[3];
+    int pitches[3];
     Uint8 *planes[3];
 
     /* This is a temporary surface in case we have to stretch copy */
diff --git a/src/render/software/SDL_rotate.c b/src/render/software/SDL_rotate.c
index 7c2e075c8368..8bdc8188a3c6 100644
--- a/src/render/software/SDL_rotate.c
+++ b/src/render/software/SDL_rotate.c
@@ -343,16 +343,16 @@ static void transformSurfaceRGBA(SDL_Surface *src, SDL_Surface *dst, int isin, i
                     ey = (sdy & 0xffff);
                     t1 = ((((c01.r - c00.r) * ex) >> 16) + c00.r) & 0xff;
                     t2 = ((((c11.r - c10.r) * ex) >> 16) + c10.r) & 0xff;
-                    pc->r = (((t2 - t1) * ey) >> 16) + t1;
+                    pc->r = (Uint8)((((t2 - t1) * ey) >> 16) + t1);
                     t1 = ((((c01.g - c00.g) * ex) >> 16) + c00.g) & 0xff;
                     t2 = ((((c11.g - c10.g) * ex) >> 16) + c10.g) & 0xff;
-                    pc->g = (((t2 - t1) * ey) >> 16) + t1;
+                    pc->g = (Uint8)((((t2 - t1) * ey) >> 16) + t1);
                     t1 = ((((c01.b - c00.b) * ex) >> 16) + c00.b) & 0xff;
                     t2 = ((((c11.b - c10.b) * ex) >> 16) + c10.b) & 0xff;
-                    pc->b = (((t2 - t1) * ey) >> 16) + t1;
+                    pc->b = (Uint8)((((t2 - t1) * ey) >> 16) + t1);
                     t1 = ((((c01.a - c00.a) * ex) >> 16) + c00.a) & 0xff;
                     t2 = ((((c11.a - c10.a) * ex) >> 16) + c10.a) & 0xff;
-                    pc->a = (((t2 - t1) * ey) >> 16) + t1;
+                    pc->a = (Uint8)((((t2 - t1) * ey) >> 16) + t1);
                 }
                 sdx += icos;
                 sdy += isin;
diff --git a/src/render/software/SDL_triangle.c b/src/render/software/SDL_triangle.c
index d2e8dd8eebff..2896ddcb47a1 100644
--- a/src/render/software/SDL_triangle.c
+++ b/src/render/software/SDL_triangle.c
@@ -171,11 +171,11 @@ static void bounding_rect(const SDL_Point *a, const SDL_Point *b, const SDL_Poin
     int srcy = (int)(((Sint64)w0 * s2s0_y + (Sint64)w1 * s2s1_y + s2_x_area.y) / area);
 
 #define TRIANGLE_GET_MAPPED_COLOR                                                      \
-    int r = (int)(((Sint64)w0 * c0.r + (Sint64)w1 * c1.r + (Sint64)w2 * c2.r) / area); \
-    int g = (int)(((Sint64)w0 * c0.g + (Sint64)w1 * c1.g + (Sint64)w2 * c2.g) / area); \
-    int b = (int)(((Sint64)w0 * c0.b + (Sint64)w1 * c1.b + (Sint64)w2 * c2.b) / area); \
-    int a = (int)(((Sint64)w0 * c0.a + (Sint64)w1 * c1.a + (Sint64)w2 * c2.a) / area); \
-    int color = SDL_MapRGBA(format, r, g, b, a);
+    Uint8 r = (Uint8)(((Sint64)w0 * c0.r + (Sint64)w1 * c1.r + (Sint64)w2 * c2.r) / area); \
+    Uint8 g = (Uint8)(((Sint64)w0 * c0.g + (Sint64)w1 * c1.g + (Sint64)w2 * c2.g) / area); \
+    Uint8 b = (Uint8)(((Sint64)w0 * c0.b + (Sint64)w1 * c1.b + (Sint64)w2 * c2.b) / area); \
+    Uint8 a = (Uint8)(((Sint64)w0 * c0.a + (Sint64)w1 * c1.a + (Sint64)w2 * c2.a) / area); \
+    Uint32 color = SDL_MapRGBA(format, r, g, b, a);
 
 #define TRIANGLE_GET_COLOR                                                             \
     int r = (int)(((Sint64)w0 * c0.r + (Sint64)w1 * c1.r + (Sint64)w2 * c2.r) / area); \
@@ -366,13 +366,13 @@ int SDL_SW_FillTriangle(SDL_Surface *dst, SDL_Point *d0, SDL_Point *d1, SDL_Poin
         } else if (dstbpp == 2) {
             TRIANGLE_BEGIN_LOOP
             {
-                *(Uint16 *)dptr = color;
+                *(Uint16 *)dptr = (Uint16)color;
             }
             TRIANGLE_END_LOOP
         } else if (dstbpp == 1) {
             TRIANGLE_BEGIN_LOOP
             {
-                *dptr = color;
+                *dptr = (Uint8)color;
             }
             TRIANGLE_END_LOOP
         }
@@ -402,14 +402,14 @@ int SDL_SW_FillTriangle(SDL_Surface *dst, SDL_Point *d0, SDL_Point *d1, SDL_Poin
             TRIANGLE_BEGIN_LOOP
             {
                 TRIANGLE_GET_MAPPED_COLOR
-                *(Uint16 *)dptr = color;
+                *(Uint16 *)dptr = (Uint16)color;
             }
             TRIANGLE_END_LOOP
         } else if (dstbpp == 1) {
             TRIANGLE_BEGIN_LOOP
             {
                 TRIANGLE_GET_MAPPED_COLOR
-                *dptr = color;
+                *dptr = (Uint8)color;
             }
             TRIANGLE_END_LOOP
         }
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index 1229c3798a57..e68ae47a75c9 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -479,8 +479,8 @@ int SDL_wcscasecmp(const wchar_t *str1, const wchar_t *str2)
             a = *str1;
             b = *str2;
         } else {
-            a = SDL_toupper((unsigned char)*str1);
-            b = SDL_toupper((unsigned char)*str2);
+            a = (wchar_t)SDL_toupper((unsigned char)*str1);
+            b = (wchar_t)SDL_toupper((unsigned char)*str2);
         }
         if (a != b) {
             break;
@@ -494,8 +494,8 @@ int SDL_wcscasecmp(const wchar_t *str1, const wchar_t *str2)
         a = *str1;
         b = *str2;
     } else {
-        a = SDL_toupper((unsigned char)*str1);
-        b = SDL_toupper((unsigned char)*str2);
+        a = (wchar_t)SDL_toupper((unsigned char)*str1);
+        b = (wchar_t)SDL_toupper((unsigned char)*str2);
     }
     return (int)((unsigned int)a - (unsigned int)b);
 #endif /* HAVE__WCSICMP */
@@ -516,8 +516,8 @@ int SDL_wcsncasecmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen)
             a = *str1

(Patch may be truncated, please check the link at the top of this post.)