From e7ef1e0b5e0f2c15218dcd3a975901292ab127a8 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 16 Apr 2020 19:42:18 -0400 Subject: [PATCH 1/4] CGameOptions: Make TuneScreenBrightness() a const member function This member function doesn't modify internal class state. --- Runtime/CGameOptions.cpp | 2 +- Runtime/CGameOptions.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Runtime/CGameOptions.cpp b/Runtime/CGameOptions.cpp index 35e96fdd7..283f0fd08 100644 --- a/Runtime/CGameOptions.cpp +++ b/Runtime/CGameOptions.cpp @@ -266,7 +266,7 @@ CGameOptions::CGameOptions() InitSoundMode(); } -float CGameOptions::TuneScreenBrightness() { return (0.375f * 1.f) + (float(x48_screenBrightness) * 0.25f); } +float CGameOptions::TuneScreenBrightness() const { return (0.375f * 1.f) + (float(x48_screenBrightness) * 0.25f); } void CGameOptions::InitSoundMode() { /* If system is mono, force x44 to mono, otherwise honor user preference */ } diff --git a/Runtime/CGameOptions.hpp b/Runtime/CGameOptions.hpp index fc4e7bdb5..cefa673ab 100644 --- a/Runtime/CGameOptions.hpp +++ b/Runtime/CGameOptions.hpp @@ -134,7 +134,7 @@ public: void EnsureSettings(); void PutTo(CBitStreamWriter& writer) const; - float TuneScreenBrightness(); + float TuneScreenBrightness() const; void SetScreenBrightness(s32, bool); s32 GetScreenBrightness() const { return x48_screenBrightness; } void ApplyGamma(); From f844f2028b4a700caf85f2987983ce7f59ad0553 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 16 Apr 2020 19:49:33 -0400 Subject: [PATCH 2/4] CGameOptions: Give all function prototype parameters a name Makes the interface more self-documenting and also allows better IDE introspection. --- Runtime/CGameOptions.cpp | 60 ++++++++++++++++++++++++---------------- Runtime/CGameOptions.hpp | 44 ++++++++++++++--------------- 2 files changed, 58 insertions(+), 46 deletions(-) diff --git a/Runtime/CGameOptions.cpp b/Runtime/CGameOptions.cpp index 283f0fd08..1f662ef9d 100644 --- a/Runtime/CGameOptions.cpp +++ b/Runtime/CGameOptions.cpp @@ -271,11 +271,14 @@ float CGameOptions::TuneScreenBrightness() const { return (0.375f * 1.f) + (floa void CGameOptions::InitSoundMode() { /* If system is mono, force x44 to mono, otherwise honor user preference */ } static float BrightnessCopyFilter = 0.f; -void CGameOptions::SetScreenBrightness(s32 val, bool apply) { - x48_screenBrightness = zeus::clamp(0, val, 8); +void CGameOptions::SetScreenBrightness(s32 value, bool apply) { + x48_screenBrightness = zeus::clamp(0, value, 8); - if (apply) - BrightnessCopyFilter = TuneScreenBrightness(); + if (!apply) { + return; + } + + BrightnessCopyFilter = TuneScreenBrightness(); } void CGameOptions::ApplyGamma() { @@ -287,51 +290,60 @@ void CGameOptions::ApplyGamma() { CGraphics::g_BooFactory->setDisplayGamma(gammaT); } -void CGameOptions::SetGamma(s32 val, bool apply) { - m_gamma = zeus::clamp(-100, val, 100); +void CGameOptions::SetGamma(s32 value, bool apply) { + m_gamma = zeus::clamp(-100, value, 100); - if (apply) - ApplyGamma(); + if (!apply) { + return; + } + + ApplyGamma(); } -void CGameOptions::SetScreenPositionX(s32 pos, bool apply) { - x4c_screenXOffset = zeus::clamp(-30, pos, 30); +void CGameOptions::SetScreenPositionX(s32 position, bool apply) { + x4c_screenXOffset = zeus::clamp(-30, position, 30); if (apply) { /* TOOD: CGraphics related funcs */ } } -void CGameOptions::SetScreenPositionY(s32 pos, bool apply) { - x50_screenYOffset = zeus::clamp(-30, pos, 30); +void CGameOptions::SetScreenPositionY(s32 position, bool apply) { + x50_screenYOffset = zeus::clamp(-30, position, 30); if (apply) { /* TOOD: CGraphics related funcs */ } } -void CGameOptions::SetScreenStretch(s32 st, bool apply) { - x54_screenStretch = zeus::clamp(-10, st, 10); +void CGameOptions::SetScreenStretch(s32 stretch, bool apply) { + x54_screenStretch = zeus::clamp(-10, stretch, 10); if (apply) { /* TOOD: CGraphics related funcs */ } } -void CGameOptions::SetSfxVolume(s32 vol, bool apply) { - x58_sfxVol = zeus::clamp(0, vol, 0x7f); +void CGameOptions::SetSfxVolume(s32 volume, bool apply) { + x58_sfxVol = zeus::clamp(0, volume, 0x7f); - if (apply) { - CAudioSys::SysSetSfxVolume(x58_sfxVol, 1, true, true); - CStreamAudioManager::SetSfxVolume(x58_sfxVol); - CMoviePlayer::SetSfxVolume(x58_sfxVol); + if (!apply) { + return; } + + CAudioSys::SysSetSfxVolume(x58_sfxVol, 1, true, true); + CStreamAudioManager::SetSfxVolume(x58_sfxVol); + CMoviePlayer::SetSfxVolume(x58_sfxVol); } -void CGameOptions::SetMusicVolume(s32 vol, bool apply) { - x5c_musicVol = zeus::clamp(0, vol, 0x7f); - if (apply) - CStreamAudioManager::SetMusicVolume(x5c_musicVol); +void CGameOptions::SetMusicVolume(s32 volume, bool apply) { + x5c_musicVol = zeus::clamp(0, volume, 0x7f); + + if (!apply) { + return; + } + + CStreamAudioManager::SetMusicVolume(x5c_musicVol); } void CGameOptions::SetHUDAlpha(u32 alpha) { x60_hudAlpha = alpha; } diff --git a/Runtime/CGameOptions.hpp b/Runtime/CGameOptions.hpp index cefa673ab..5d6325b34 100644 --- a/Runtime/CGameOptions.hpp +++ b/Runtime/CGameOptions.hpp @@ -77,21 +77,21 @@ public: bool GetCinematicState(CAssetId mlvlId, TEditorId cineId) const; void SetCinematicState(CAssetId mlvlId, TEditorId cineId, bool state); u32 GetAutoMapperKeyState() const { return xbc_autoMapperKeyState; } - void SetAutoMapperKeyState(u32 s) { xbc_autoMapperKeyState = s; } + void SetAutoMapperKeyState(u32 state) { xbc_autoMapperKeyState = state; } bool GetPlayerLinkedFusion() const { return xd0_24_fusionLinked; } - void SetPlayerLinkedFusion(bool v) { xd0_24_fusionLinked = v; } + void SetPlayerLinkedFusion(bool fusionLinked) { xd0_24_fusionLinked = fusionLinked; } bool GetPlayerBeatNormalMode() const { return xd0_25_normalModeBeat; } - void SetPlayerBeatNormalMode(bool v) { xd0_25_normalModeBeat = v; } + void SetPlayerBeatNormalMode(bool normalModeBeat) { xd0_25_normalModeBeat = normalModeBeat; } bool GetPlayerBeatHardMode() const { return xd0_26_hardModeBeat; } - void SetPlayerBeatHardMode(bool v) { xd0_26_hardModeBeat = v; } + void SetPlayerBeatHardMode(bool hardModeBeat) { xd0_26_hardModeBeat = hardModeBeat; } bool GetPlayerBeatFusion() const { return xd0_27_fusionBeat; } - void SetPlayerBeatFusion(bool v) { xd0_27_fusionBeat = v; } + void SetPlayerBeatFusion(bool fusionBeat) { xd0_27_fusionBeat = fusionBeat; } bool GetPlayerFusionSuitActive() const { return xd0_28_fusionSuitActive; } - void SetPlayerFusionSuitActive(bool v) { xd0_28_fusionSuitActive = v; } + void SetPlayerFusionSuitActive(bool fusionSuitActive) { xd0_28_fusionSuitActive = fusionSuitActive; } bool GetAllItemsCollected() const { return xd0_29_allItemsCollected; } - void SetAllItemsCollected(bool v) { xd0_29_allItemsCollected = v; } + void SetAllItemsCollected(bool allItemsCollected) { xd0_29_allItemsCollected = allItemsCollected; } u32 GetLogScanPercent() const { return xcc_logScanPercent; } - void SetLogScanPercent(u32 v) { xcc_logScanPercent = v; } + void SetLogScanPercent(u32 percent) { xcc_logScanPercent = percent; } void IncrementFrozenFpsCount() { xc0_frozenFpsCount = std::min(int(xc0_frozenFpsCount + 1), 3); } bool GetShowFrozenFpsMessage() const { return xc0_frozenFpsCount != 3; } void IncrementFrozenBallCount() { xc4_frozenBallCount = std::min(int(xc4_frozenBallCount + 1), 3); } @@ -135,36 +135,36 @@ public: void PutTo(CBitStreamWriter& writer) const; float TuneScreenBrightness() const; - void SetScreenBrightness(s32, bool); + void SetScreenBrightness(s32 value, bool apply); s32 GetScreenBrightness() const { return x48_screenBrightness; } void ApplyGamma(); - void SetGamma(s32, bool); + void SetGamma(s32 value, bool apply); s32 GetGamma() const { return m_gamma; } - void SetScreenPositionX(s32, bool); + void SetScreenPositionX(s32 position, bool apply); s32 GetScreenPositionX() const { return x4c_screenXOffset; } - void SetScreenPositionY(s32, bool); + void SetScreenPositionY(s32 position, bool apply); s32 GetScreenPositionY() const { return x50_screenYOffset; } - void SetScreenStretch(s32, bool); + void SetScreenStretch(s32 stretch, bool apply); s32 GetScreenStretch() const { return x54_screenStretch; } - void SetSfxVolume(s32, bool); + void SetSfxVolume(s32 volume, bool apply); s32 GetSfxVolume() const { return x58_sfxVol; } - void SetMusicVolume(s32, bool); + void SetMusicVolume(s32 volume, bool apply); s32 GetMusicVolume() const { return x5c_musicVol; } - void SetHUDAlpha(u32); + void SetHUDAlpha(u32 alpha); u32 GetHUDAlpha() const { return x60_hudAlpha; } - void SetHelmetAlpha(u32); + void SetHelmetAlpha(u32 alpha); u32 GetHelmetAlpha() const { return x64_helmetAlpha; } - void SetHUDLag(bool); + void SetHUDLag(bool lag); bool GetHUDLag() const { return x68_24_hudLag; } void SetSurroundMode(int mode, bool apply); CAudioSys::ESurroundModes GetSurroundMode() const; - void SetInvertYAxis(bool); + void SetInvertYAxis(bool invert); bool GetInvertYAxis() const { return x68_25_invertY; } - void SetIsRumbleEnabled(bool); + void SetIsRumbleEnabled(bool rumble); bool GetIsRumbleEnabled() const { return x68_26_rumble; } - void SetSwapBeamControls(bool); + void SetSwapBeamControls(bool swap); bool GetSwapBeamControls() const { return x68_27_swapBeamsControls; } - void SetIsHintSystemEnabled(bool); + void SetIsHintSystemEnabled(bool hints); bool GetIsHintSystemEnabled() const { return x68_28_hintSystem; } void SetControls(int controls); void ResetControllerAssets(int controls); From a551dbc21da583ceaaaedaccbc2247ca3f0545e8 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 16 Apr 2020 19:55:59 -0400 Subject: [PATCH 3/4] CGameState: Silence implicit truncation warnings Ensures boolean conditions are used where applicable. --- Runtime/CGameState.cpp | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/Runtime/CGameState.cpp b/Runtime/CGameState.cpp index ca4d7e469..0289bdc44 100644 --- a/Runtime/CGameState.cpp +++ b/Runtime/CGameState.cpp @@ -24,15 +24,16 @@ union BitsToDouble { }; CWorldLayerState::CWorldLayerState(CBitStreamReader& reader, const CSaveWorld& saveWorld) { - u32 bitCount = reader.ReadEncoded(10); + const u32 bitCount = reader.ReadEncoded(10); x10_saveLayers.reserve(bitCount); for (u32 i = 0; i < bitCount; ++i) { - bool bit = reader.ReadEncoded(1); - if (bit) + const bool bit = reader.ReadEncoded(1) != 0; + if (bit) { x10_saveLayers.setBit(i); - else + } else { x10_saveLayers.unsetBit(i); + } } } @@ -53,11 +54,14 @@ void CWorldLayerState::PutTo(CBitStreamWriter& writer) const { } void CWorldLayerState::InitializeWorldLayers(const std::vector& layers) { - if (x0_areaLayers.size()) + if (!x0_areaLayers.empty()) { return; + } + x0_areaLayers = layers; - if (x10_saveLayers.getBitCount() == 0) + if (x10_saveLayers.getBitCount() == 0) { return; + } u32 a = 0; u32 b = 0; @@ -102,9 +106,9 @@ CGameState::GameFileStateInfo CGameState::LoadGameFileState(const u8* data) { } ret.x14_timestamp = stream.ReadEncoded(32); - ret.x20_hardMode = stream.ReadEncoded(1); + ret.x20_hardMode = stream.ReadEncoded(1) != 0; stream.ReadEncoded(1); - CAssetId origMLVL = u32(stream.ReadEncoded(32)); + const CAssetId origMLVL = u32(stream.ReadEncoded(32)); ret.x8_mlvlId = origMLVL; BitsToDouble conv; @@ -139,8 +143,10 @@ CGameState::CGameState() { x9c_transManager = std::make_shared(); x228_24_hardMode = false; x228_25_initPowerupsAtFirstSpawn = true; - if (g_MemoryCardSys) + + if (g_MemoryCardSys != nullptr) { InitializeMemoryStates(); + } } CGameState::CGameState(CBitStreamReader& stream, u32 saveIdx) : x20c_saveFileIdx(saveIdx) { @@ -153,8 +159,8 @@ CGameState::CGameState(CBitStreamReader& stream, u32 saveIdx) : x20c_saveFileIdx } stream.ReadEncoded(32); - x228_24_hardMode = stream.ReadEncoded(1); - x228_25_initPowerupsAtFirstSpawn = stream.ReadEncoded(1); + x228_24_hardMode = stream.ReadEncoded(1) != 0; + x228_25_initPowerupsAtFirstSpawn = stream.ReadEncoded(1) != 0; x84_mlvlId = u32(stream.ReadEncoded(32)); MP1::CMain::EnsureWorldPakReady(x84_mlvlId); From 20e5595042be4671008dd2acf3ab5d0701556f30 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 16 Apr 2020 19:59:19 -0400 Subject: [PATCH 4/4] CGameState: Always initialize x80_ Provides a deterministic initial state. --- Runtime/CGameState.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/CGameState.hpp b/Runtime/CGameState.hpp index ad4146e6d..ab63a1a28 100644 --- a/Runtime/CGameState.hpp +++ b/Runtime/CGameState.hpp @@ -69,7 +69,7 @@ class CGameState { friend class CStateManager; std::array x0_{}; - u32 x80_; + u32 x80_ = 0; CAssetId x84_mlvlId; std::vector x88_worldStates; std::shared_ptr x98_playerState;