From 537722d57a1fdd2fed24ccbab84179eb97271162 Mon Sep 17 00:00:00 2001 From: David Chavez Date: Wed, 6 Jul 2022 05:53:42 +0200 Subject: [PATCH] Fix portability use of std::clamp (#596) --- libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp | 7 ++++--- libultraship/libultraship/Utils.cpp | 7 +++++++ libultraship/libultraship/Utils.h | 8 ++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp index ba60c1f1b..b53c2550f 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp @@ -33,6 +33,7 @@ #include "../../Environment.h" #include "../../GameVersions.h" #include "../../ResourceMgr.h" +#include "../../Utils.h" // OTRTODO: fix header files for these extern "C" { @@ -1062,8 +1063,8 @@ static void gfx_sp_vertex(size_t n_vertices, size_t dest_index, const Vtx *verti dotx /= 127.0f; doty /= 127.0f; - std::clamp(dotx, -1.0f, 1.0f); - std::clamp(doty, -1.0f, 1.0f); + dotx = math::clamp(dotx, -1.0f, 1.0f); + doty = math::clamp(doty, -1.0f, 1.0f); if (rsp.geometry_mode & G_TEXTURE_GEN_LINEAR) { // Not sure exactly what formula we should use to get accurate values @@ -1115,7 +1116,7 @@ static void gfx_sp_vertex(size_t n_vertices, size_t dest_index, const Vtx *verti if (winv < 0.0f) winv = std::numeric_limits::max(); float fog_z = z * winv * rsp.fog_mul + rsp.fog_offset; - std::clamp(fog_z, 0.0f, 255.0f); + fog_z = math::clamp(fog_z, 0.0f, 255.0f); d->color.a = fog_z; // Use alpha variable to store fog factor } else { d->color.a = v->cn[3]; diff --git a/libultraship/libultraship/Utils.cpp b/libultraship/libultraship/Utils.cpp index d54952c77..2b813aee7 100644 --- a/libultraship/libultraship/Utils.cpp +++ b/libultraship/libultraship/Utils.cpp @@ -5,6 +5,13 @@ #define strdup _strdup #endif +namespace math { + float clamp(float d, float min, float max) { + const float t = d < min ? min : d; + return t > max ? max : t; + } +} + namespace Utils { std::vector SplitText(const std::string text, char separator = ' ', bool keep_quotes = false) { std::vector args; diff --git a/libultraship/libultraship/Utils.h b/libultraship/libultraship/Utils.h index 32ceb6e6d..25c6dd64b 100644 --- a/libultraship/libultraship/Utils.h +++ b/libultraship/libultraship/Utils.h @@ -3,6 +3,10 @@ #include #include -namespace Utils { - std::vector SplitText(const std::string& text, char separator, bool keep_quotes); +namespace math { + float clamp(float d, float min, float max); +} + +namespace Utils { + std::vector SplitText(const std::string& text, char separator, bool keep_quotes); }