From 433fc9fa8dad30955276dc72587acd8d231a2968 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Sat, 24 Apr 2021 08:26:35 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=9A=20Packport=20std::clamp=20for=20pr?= =?UTF-8?q?e=20GCC=207.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/automap.cpp | 8 ++++---- Source/engine.h | 10 ++++++++++ Source/items.cpp | 10 +++++----- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Source/automap.cpp b/Source/automap.cpp index 3147beeac..609dba2c3 100644 --- a/Source/automap.cpp +++ b/Source/automap.cpp @@ -257,11 +257,11 @@ void SearchAutomapItem(const CelOutputBuffer &out) y++; } - const int startX = std::clamp(x - 8, 0, MAXDUNX); - const int startY = std::clamp(y - 8, 0, MAXDUNY); + const int startX = clamp(x - 8, 0, MAXDUNX); + const int startY = clamp(y - 8, 0, MAXDUNY); - const int endX = std::clamp(x + 8, 0, MAXDUNX); - const int endY = std::clamp(y + 8, 0, MAXDUNY); + const int endX = clamp(x + 8, 0, MAXDUNX); + const int endY = clamp(y + 8, 0, MAXDUNY); for (int i = startX; i < endX; i++) { for (int j = startY; j < endY; j++) { diff --git a/Source/engine.h b/Source/engine.h index 818e070d8..c75043c82 100644 --- a/Source/engine.h +++ b/Source/engine.h @@ -27,6 +27,16 @@ namespace devilution { +#if defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 7 || (__GNUC__ == 7 && __GNUC_MINOR__ < 0) +using std::clamp; +#else +template +constexpr const T &clamp(const T &x, const T &lower, const T &upper) +{ + return std::min(std::max(x, lower), upper); +} +#endif + #define MemFreeDbg(p) \ { \ void *p__p; \ diff --git a/Source/items.cpp b/Source/items.cpp index 70ce940ab..7eefec76a 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -668,7 +668,7 @@ void CalcPlrItemVals(int p, bool Loadgfx) plr[p]._pIBonusDamMod = dmod; plr[p]._pIGetHit = ghit; - lrad = std::clamp(lrad, 2, 15); + lrad = clamp(lrad, 2, 15); if (plr[p]._pLightRad != lrad && p == myplr) { ChangeLightRadius(plr[p]._plid, lrad); @@ -752,9 +752,9 @@ void CalcPlrItemVals(int p, bool Loadgfx) lr = 0; } - plr[p]._pMagResist = std::clamp(mr, 0, MAXRESIST); - plr[p]._pFireResist = std::clamp(fr, 0, MAXRESIST); - plr[p]._pLghtResist = std::clamp(lr, 0, MAXRESIST); + plr[p]._pMagResist = clamp(mr, 0, MAXRESIST); + plr[p]._pFireResist = clamp(fr, 0, MAXRESIST); + plr[p]._pLghtResist = clamp(lr, 0, MAXRESIST); if (plr[p]._pClass == HeroClass::Warrior) { vadd *= 2; @@ -4383,7 +4383,7 @@ static void SpawnOnePremium(int i, int plvl, int myplr) dexterity *= 1.2; magic *= 1.2; - plvl = std::clamp(plvl, 1, 30); + plvl = clamp(plvl, 1, 30); int count = 0;