From db895b43b2739d6cb133396b6f79f0e017d66db3 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 27 Sep 2020 08:36:30 +0200 Subject: [PATCH] - copied constexpr declarations plus a few fixed in utility code and fixed a few places where this triggered a compile error now. --- src/common/engine/stringtable.cpp | 2 +- src/common/utility/m_fixed.h | 20 +++++++++++++++----- src/common/utility/palentry.h | 26 +++++++++++++------------- src/common/utility/tarray.h | 10 ++++++++++ src/common/utility/utf8.cpp | 2 +- src/common/utility/zstring.h | 4 ++++ src/d_main.cpp | 2 +- src/gamedata/d_dehacked.cpp | 2 +- src/intermission/intermission.cpp | 9 +++------ 9 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/common/engine/stringtable.cpp b/src/common/engine/stringtable.cpp index a6cf61e967..b0a0059305 100644 --- a/src/common/engine/stringtable.cpp +++ b/src/common/engine/stringtable.cpp @@ -450,7 +450,7 @@ void FStringTable::InsertString(int lumpnum, int langid, FName label, const FStr auto replace = allMacros.CheckKey(lookupname); for (int i = 0; i < 4; i++) { - const char *replacement = replace && replace->Replacements[i] ? replace->Replacements[i].GetChars() : ""; + const char *replacement = replace? replace->Replacements[i].GetChars() : ""; te.strings[i].Substitute(replacee, replacement); } } diff --git a/src/common/utility/m_fixed.h b/src/common/utility/m_fixed.h index e452242cee..b765f997ad 100644 --- a/src/common/utility/m_fixed.h +++ b/src/common/utility/m_fixed.h @@ -43,7 +43,7 @@ inline int32_t FixedDiv (int32_t a, int32_t b) return (int32_t)(((int64_t)a << 16) / b); } -__forceinline int32_t FixedMul(int32_t a, int32_t b) +__forceinline constexpr int32_t FixedMul(int32_t a, int32_t b) { return (int32_t)(((int64_t)a * b) >> 16); } @@ -53,9 +53,19 @@ inline fixed_t FloatToFixed(double f) return xs_Fix<16>::ToFix(f); } -inline double FixedToFloat(fixed_t f) +inline constexpr fixed_t IntToFixed(int32_t f) { - return f / 65536.; + return f << FRACBITS; +} + +inline constexpr double FixedToFloat(fixed_t f) +{ + return f * (1/65536.); +} + +inline constexpr int32_t FixedToInt(fixed_t f) +{ + return (f + FRACUNIT/2) >> FRACBITS; } inline unsigned FloatToAngle(double f) @@ -63,12 +73,12 @@ inline unsigned FloatToAngle(double f) return xs_CRoundToInt((f)* (0x40000000 / 90.)); } -inline double AngleToFloat(unsigned f) +inline constexpr double AngleToFloat(unsigned f) { return f * (90. / 0x40000000); } -inline double AngleToFloat(int f) +inline constexpr double AngleToFloat(int f) { return f * (90. / 0x40000000); } diff --git a/src/common/utility/palentry.h b/src/common/utility/palentry.h index 4064f4ac7b..6d6d5a86aa 100644 --- a/src/common/utility/palentry.h +++ b/src/common/utility/palentry.h @@ -12,7 +12,7 @@ struct PalEntry { PalEntry() = default; - PalEntry (uint32_t argb) { d = argb; } + constexpr PalEntry (uint32_t argb) : d(argb) { } operator uint32_t () const { return d; } void SetRGB(PalEntry other) { @@ -36,35 +36,35 @@ struct PalEntry return other; } } - int Luminance() const + constexpr int Luminance() const { return (r * 77 + g * 143 + b * 37) >> 8; } - int Amplitude() const + constexpr int Amplitude() const { return std::max(r, std::max(g, b)); } - void Decolorize() // this for 'nocoloredspritelighting' and not the same as desaturation. The normal formula results in a value that's too dark. + constexpr void Decolorize() // this for 'nocoloredspritelighting' and not the same as desaturation. The normal formula results in a value that's too dark. { int v = (r + g + b); r = g = b = ((255*3) + v + v) / 9; } - bool isBlack() const + constexpr bool isBlack() const { return (d & 0xffffff) == 0; } - bool isWhite() const + constexpr bool isWhite() const { return (d & 0xffffff) == 0xffffff; } PalEntry &operator= (const PalEntry &other) = default; - PalEntry &operator= (uint32_t other) { d = other; return *this; } - PalEntry InverseColor() const { PalEntry nc; nc.a = a; nc.r = 255 - r; nc.g = 255 - g; nc.b = 255 - b; return nc; } + constexpr PalEntry &operator= (uint32_t other) { d = other; return *this; } + constexpr PalEntry InverseColor() const { PalEntry nc(a, 255 - r, 255 - g, 255 - b); return nc; } #ifdef __BIG_ENDIAN__ - PalEntry (uint8_t ir, uint8_t ig, uint8_t ib) : a(0), r(ir), g(ig), b(ib) {} - PalEntry (uint8_t ia, uint8_t ir, uint8_t ig, uint8_t ib) : a(ia), r(ir), g(ig), b(ib) {} + constexpr PalEntry (uint8_t ir, uint8_t ig, uint8_t ib) : a(0), r(ir), g(ig), b(ib) {} + constexpr PalEntry (uint8_t ia, uint8_t ir, uint8_t ig, uint8_t ib) : a(ia), r(ir), g(ig), b(ib) {} union { struct @@ -74,8 +74,8 @@ struct PalEntry uint32_t d; }; #else - PalEntry (uint8_t ir, uint8_t ig, uint8_t ib) : b(ib), g(ig), r(ir), a(0) {} - PalEntry (uint8_t ia, uint8_t ir, uint8_t ig, uint8_t ib) : b(ib), g(ig), r(ir), a(ia) {} + constexpr PalEntry (uint8_t ir, uint8_t ig, uint8_t ib) : b(ib), g(ig), r(ir), a(0) {} + constexpr PalEntry (uint8_t ia, uint8_t ir, uint8_t ig, uint8_t ib) : b(ib), g(ig), r(ir), a(ia) {} union { struct @@ -87,7 +87,7 @@ struct PalEntry #endif }; -inline int Luminance(int r, int g, int b) +constexpr inline int Luminance(int r, int g, int b) { return (r * 77 + g * 143 + b * 37) >> 8; } diff --git a/src/common/utility/tarray.h b/src/common/utility/tarray.h index 7bafeb9ee3..ebe83f9844 100644 --- a/src/common/utility/tarray.h +++ b/src/common/utility/tarray.h @@ -1527,6 +1527,16 @@ public: { memset(&bytes[0], on ? -1 : 0, sizeof(bytes)); } + + // These are for utilities that need access to the raw storage. The serializer needs this to do its work, for example. + uint8_t* Storage() + { + return bytes; + } + unsigned StorageSize() const + { + return sizeof(bytes); + } }; // A wrapper to externally stored data. diff --git a/src/common/utility/utf8.cpp b/src/common/utility/utf8.cpp index 1eec781301..eb6dd6bf57 100644 --- a/src/common/utility/utf8.cpp +++ b/src/common/utility/utf8.cpp @@ -249,7 +249,7 @@ const char *MakeUTF8(const char *outline, int *numchars) UTF8String.Push(encode[i]); } } - if (numchars) *numchars++; + if (numchars) (*numchars)++; } UTF8String.Push(0); return UTF8String.Data(); diff --git a/src/common/utility/zstring.h b/src/common/utility/zstring.h index 23795d41a2..a30458c3c8 100644 --- a/src/common/utility/zstring.h +++ b/src/common/utility/zstring.h @@ -164,6 +164,10 @@ public: std::swap(Chars, other.Chars); } + // We do not want any implicit conversions from FString in conditionals. + explicit operator bool() = delete; // this is needed to render the operator const char * ineffective when used in boolean constructs. + bool operator !() = delete; + operator const char *() const { return Chars; } const char *GetChars() const { return Chars; } diff --git a/src/d_main.cpp b/src/d_main.cpp index 487b3b8ba3..b8c0675ea4 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -3805,7 +3805,7 @@ void I_UpdateWindowTitle() switch (I_FriendlyWindowTitle) { case 1: - if (level.LevelName && level.LevelName.GetChars()[0]) + if (level.LevelName.IsNotEmpty()) { titlestr.Format("%s - %s", level.LevelName.GetChars(), GameStartupInfo.Name.GetChars()); break; diff --git a/src/gamedata/d_dehacked.cpp b/src/gamedata/d_dehacked.cpp index 78005f62f4..7a84db83f2 100644 --- a/src/gamedata/d_dehacked.cpp +++ b/src/gamedata/d_dehacked.cpp @@ -2375,7 +2375,7 @@ static int DoInclude (int dummy) // Try looking for the included file in the same directory // as the patch before looking in the current file. - const char *lastSlash = savepatchname ? strrchr (savepatchname, '/') : NULL; + const char *lastSlash = strrchr(savepatchname, '/'); char *path = data; if (lastSlash != NULL) diff --git a/src/intermission/intermission.cpp b/src/intermission/intermission.cpp index ec8f7e394d..46bd7a8413 100644 --- a/src/intermission/intermission.cpp +++ b/src/intermission/intermission.cpp @@ -236,12 +236,9 @@ void DIntermissionScreen::Drawer () if (CheckOverlay(i)) DrawTexture(twod, TexMan.GetGameTexture(mOverlays[i].mPic), mOverlays[i].x, mOverlays[i].y, DTA_320x200, true, TAG_DONE); } - if (mSubtitle) - { - const char *sub = mSubtitle.GetChars(); - if (sub && *sub == '$') sub = GStrings[sub + 1]; - if (sub) DrawFullscreenSubtitle(sub); - } + const char *sub = mSubtitle.GetChars(); + if (sub && *sub == '$') sub = GStrings[sub + 1]; + if (sub) DrawFullscreenSubtitle(sub); } void DIntermissionScreen::OnDestroy()