From e83d5caea62a22eeec42ec8209f636ae5a53ba3a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 17 Mar 2020 23:23:13 -0400 Subject: [PATCH] CInputGenerator: Make use of std::array where applicable Allows removing hardcoded array sizes. --- Runtime/Input/CInputGenerator.hpp | 13 ++++++------ Runtime/Input/CRumbleGenerator.cpp | 33 +++++++++++++++++------------- Runtime/Input/CRumbleGenerator.hpp | 10 +++++---- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/Runtime/Input/CInputGenerator.hpp b/Runtime/Input/CInputGenerator.hpp index 0126102d0..dd444dc1e 100644 --- a/Runtime/Input/CInputGenerator.hpp +++ b/Runtime/Input/CInputGenerator.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -82,9 +83,9 @@ public: * report thread. This class atomically exchanges that data to the * game thread as needed */ struct DolphinSmashAdapterCallback : boo::IDolphinSmashAdapterCallback { - std::atomic m_statusChanges[4]; - bool m_connected[4] = {}; - boo::DolphinControllerState m_states[4]; + std::array, 4> m_statusChanges; + std::array m_connected{}; + std::array m_states; std::mutex m_stateLock; void controllerConnected(unsigned idx, boo::EDolphinControllerType) override { /* Controller thread */ @@ -103,7 +104,7 @@ public: m_states[idx] = state; } - CFinalInput m_lastUpdates[4]; + std::array m_lastUpdates; const CFinalInput& getFinalInput(unsigned idx, float dt, float leftDiv, float rightDiv) { /* Game thread */ std::unique_lock lk(m_stateLock); @@ -159,9 +160,9 @@ public: } } } - void ControlAllMotors(const EMotorState* states) { + void ControlAllMotors(const std::array& states) { if (smashAdapter) { - for (int i = 0; i < 4; ++i) { + for (size_t i = 0; i < states.size(); ++i) { switch (states[i]) { case EMotorState::Stop: smashAdapter->stopRumble(i); diff --git a/Runtime/Input/CRumbleGenerator.cpp b/Runtime/Input/CRumbleGenerator.cpp index f39c24212..7887c405d 100644 --- a/Runtime/Input/CRumbleGenerator.cpp +++ b/Runtime/Input/CRumbleGenerator.cpp @@ -30,8 +30,8 @@ void CRumbleGenerator::Update(float dt) { if (!xf0_24_disabled) { bool updated = false; - for (int i = 0; i < 4; ++i) { - float intensity = x0_voices[i].GetIntensity(); + for (size_t i = 0; i < x0_voices.size(); ++i) { + const float intensity = x0_voices[i].GetIntensity(); if (!x0_voices[i].Update(dt) || intensity <= 0.f) { xc0_periodTime[i] = 0.f; xd0_onTime[i] = 0.f; @@ -76,31 +76,36 @@ void CRumbleGenerator::Update(float dt) { } } -static const EMotorState HardStopCommands[] = {EMotorState::StopHard, EMotorState::StopHard, EMotorState::StopHard, - EMotorState::StopHard}; - void CRumbleGenerator::HardStopAll() { - for (int i = 0; i < 4; ++i) { - xc0_periodTime[i] = 0.f; - xd0_onTime[i] = 0.f; - xe0_commandArray[i] = EMotorState::Stop; - x0_voices[i].HardReset(); + static constexpr std::array HardStopCommands{ + EMotorState::StopHard, + EMotorState::StopHard, + EMotorState::StopHard, + EMotorState::StopHard, + }; + + xc0_periodTime.fill(0.0f); + xd0_onTime.fill(0.0f); + xe0_commandArray.fill(EMotorState::Stop); + for (auto& voice : x0_voices) { + voice.HardReset(); } + g_InputGenerator->ControlAllMotors(HardStopCommands); } s16 CRumbleGenerator::Rumble(const SAdsrData& adsr, float gain, ERumblePriority prio, EIOPort port) { - CRumbleVoice& vox = x0_voices[int(port)]; - s16 freeChan = vox.GetFreeChannel(); + CRumbleVoice& vox = x0_voices[size_t(port)]; + const s16 freeChan = vox.GetFreeChannel(); if (prio >= vox.GetPriority(freeChan)) { - xc0_periodTime[int(port)] = 0.f; + xc0_periodTime[size_t(port)] = 0.f; return vox.Activate(adsr, freeChan, gain, prio); } return -1; } void CRumbleGenerator::Stop(s16 id, EIOPort port) { - CRumbleVoice& vox = x0_voices[int(port)]; + CRumbleVoice& vox = x0_voices[size_t(port)]; vox.Deactivate(id, false); } diff --git a/Runtime/Input/CRumbleGenerator.hpp b/Runtime/Input/CRumbleGenerator.hpp index d280e0d6c..51818fa1d 100644 --- a/Runtime/Input/CRumbleGenerator.hpp +++ b/Runtime/Input/CRumbleGenerator.hpp @@ -1,15 +1,17 @@ #pragma once +#include + #include "Runtime/GCNTypes.hpp" #include "Runtime/Input/CInputGenerator.hpp" #include "Runtime/Input/CRumbleVoice.hpp" namespace urde { class CRumbleGenerator { - CRumbleVoice x0_voices[4]; - float xc0_periodTime[4]; - float xd0_onTime[4]; - EMotorState xe0_commandArray[4]; + std::array x0_voices; + std::array xc0_periodTime; + std::array xd0_onTime; + std::array xe0_commandArray; bool xf0_24_disabled : 1; public: