From 41d2e4a40fddfafe9d8fa316c8361d7c39afcf1b Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Thu, 23 May 2024 12:51:20 -0400 Subject: [PATCH 1/3] libretro: Replace std::clamp() with clamp() --- Common/Data/Color/RGBAUtil.h | 2 ++ libretro/libretro.cpp | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Common/Data/Color/RGBAUtil.h b/Common/Data/Color/RGBAUtil.h index 3ea03f328a..905cca56dc 100644 --- a/Common/Data/Color/RGBAUtil.h +++ b/Common/Data/Color/RGBAUtil.h @@ -2,6 +2,8 @@ #include +template +static T clamp(T f, T low, T high); uint32_t whiteAlpha(float alpha); uint32_t blackAlpha(float alpha); uint32_t colorAlpha(uint32_t color, float alpha); diff --git a/libretro/libretro.cpp b/libretro/libretro.cpp index 0a19b55135..fb621bbedf 100644 --- a/libretro/libretro.cpp +++ b/libretro/libretro.cpp @@ -8,6 +8,7 @@ #include #include "Common/CPUDetect.h" +#include "Common/Data/Color/RGBAUtil.h" #include "Common/Log.h" #include "Common/LogManager.h" #include "Common/System/Display.h" @@ -1430,8 +1431,8 @@ static void retro_input(void) } float mappedNorm = norm; - x_left = std::clamp(x_left / norm * mappedNorm, -1.0f, 1.0f); - y_left = std::clamp(y_left / norm * mappedNorm, -1.0f, 1.0f); + x_left = clamp(x_left / norm * mappedNorm, -1.0f, 1.0f); + y_left = clamp(y_left / norm * mappedNorm, -1.0f, 1.0f); __CtrlSetAnalogXY(CTRL_STICK_LEFT, x_left, y_left); __CtrlSetAnalogXY(CTRL_STICK_RIGHT, x_right, y_right); From 5c23305e09d468fb917817ad923cd2c4486c1cf9 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Thu, 23 May 2024 13:14:23 -0400 Subject: [PATCH 2/3] libretro: Remove static keyword from clamp() --- Common/Data/Color/RGBAUtil.cpp | 2 +- Common/Data/Color/RGBAUtil.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Common/Data/Color/RGBAUtil.cpp b/Common/Data/Color/RGBAUtil.cpp index 8b93a0fc96..c0497f49d2 100644 --- a/Common/Data/Color/RGBAUtil.cpp +++ b/Common/Data/Color/RGBAUtil.cpp @@ -1,7 +1,7 @@ #include "Common/Data/Color/RGBAUtil.h" template -static T clamp(T f, T low, T high) { +T clamp(T f, T low, T high) { if (f < low) return low; if (f > high) diff --git a/Common/Data/Color/RGBAUtil.h b/Common/Data/Color/RGBAUtil.h index 905cca56dc..df15f545f6 100644 --- a/Common/Data/Color/RGBAUtil.h +++ b/Common/Data/Color/RGBAUtil.h @@ -3,7 +3,7 @@ #include template -static T clamp(T f, T low, T high); +T clamp(T f, T low, T high); uint32_t whiteAlpha(float alpha); uint32_t blackAlpha(float alpha); uint32_t colorAlpha(uint32_t color, float alpha); From 6763ace946d7acc8f1ce3d47a47f6c67bdda84a7 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Fri, 24 May 2024 08:38:04 -0400 Subject: [PATCH 3/3] libretro: Add a local clamp() instead of using the color conv one --- Common/Data/Color/RGBAUtil.cpp | 2 +- Common/Data/Color/RGBAUtil.h | 2 -- libretro/libretro.cpp | 24 +++++++++++++++++++++--- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Common/Data/Color/RGBAUtil.cpp b/Common/Data/Color/RGBAUtil.cpp index c0497f49d2..8b93a0fc96 100644 --- a/Common/Data/Color/RGBAUtil.cpp +++ b/Common/Data/Color/RGBAUtil.cpp @@ -1,7 +1,7 @@ #include "Common/Data/Color/RGBAUtil.h" template -T clamp(T f, T low, T high) { +static T clamp(T f, T low, T high) { if (f < low) return low; if (f > high) diff --git a/Common/Data/Color/RGBAUtil.h b/Common/Data/Color/RGBAUtil.h index df15f545f6..3ea03f328a 100644 --- a/Common/Data/Color/RGBAUtil.h +++ b/Common/Data/Color/RGBAUtil.h @@ -2,8 +2,6 @@ #include -template -T clamp(T f, T low, T high); uint32_t whiteAlpha(float alpha); uint32_t blackAlpha(float alpha); uint32_t colorAlpha(uint32_t color, float alpha); diff --git a/libretro/libretro.cpp b/libretro/libretro.cpp index fb621bbedf..bed538a66b 100644 --- a/libretro/libretro.cpp +++ b/libretro/libretro.cpp @@ -8,7 +8,6 @@ #include #include "Common/CPUDetect.h" -#include "Common/Data/Color/RGBAUtil.h" #include "Common/Log.h" #include "Common/LogManager.h" #include "Common/System/Display.h" @@ -111,6 +110,25 @@ namespace Libretro static float runSpeed = 0.0f; static s64 runTicksLast = 0; + /** + * Clamp a value to a given range. + * + * This implementation was taken from `RGBAUtil.cpp` to allow building when `std::clamp()` is unavailable. + * + * @param f The value to clamp. + * @param low The lower bound of the range. + * @param high The upper bound of the range. + * @return The clamped value. + */ + template + static T clamp(T f, T low, T high) { + if (f < low) + return low; + if (f > high) + return high; + return f; + } + static void VsyncSwapIntervalReset() { expectedTimeUsPerRun = (s64)(1000000.0f / (60.0f / 1.001f)); @@ -1431,8 +1449,8 @@ static void retro_input(void) } float mappedNorm = norm; - x_left = clamp(x_left / norm * mappedNorm, -1.0f, 1.0f); - y_left = clamp(y_left / norm * mappedNorm, -1.0f, 1.0f); + x_left = Libretro::clamp(x_left / norm * mappedNorm, -1.0f, 1.0f); + y_left = Libretro::clamp(y_left / norm * mappedNorm, -1.0f, 1.0f); __CtrlSetAnalogXY(CTRL_STICK_LEFT, x_left, y_left); __CtrlSetAnalogXY(CTRL_STICK_RIGHT, x_right, y_right);