CInputGenerator: Make use of std::array where applicable

Allows removing hardcoded array sizes.
This commit is contained in:
Lioncash 2020-03-17 23:23:13 -04:00
parent 6ddbd15cfc
commit e83d5caea6
3 changed files with 32 additions and 24 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <array>
#include <atomic> #include <atomic>
#include <mutex> #include <mutex>
@ -82,9 +83,9 @@ public:
* report thread. This class atomically exchanges that data to the * report thread. This class atomically exchanges that data to the
* game thread as needed */ * game thread as needed */
struct DolphinSmashAdapterCallback : boo::IDolphinSmashAdapterCallback { struct DolphinSmashAdapterCallback : boo::IDolphinSmashAdapterCallback {
std::atomic<EStatusChange> m_statusChanges[4]; std::array<std::atomic<EStatusChange>, 4> m_statusChanges;
bool m_connected[4] = {}; std::array<bool, 4> m_connected{};
boo::DolphinControllerState m_states[4]; std::array<boo::DolphinControllerState, 4> m_states;
std::mutex m_stateLock; std::mutex m_stateLock;
void controllerConnected(unsigned idx, boo::EDolphinControllerType) override { void controllerConnected(unsigned idx, boo::EDolphinControllerType) override {
/* Controller thread */ /* Controller thread */
@ -103,7 +104,7 @@ public:
m_states[idx] = state; m_states[idx] = state;
} }
CFinalInput m_lastUpdates[4]; std::array<CFinalInput, 4> m_lastUpdates;
const CFinalInput& getFinalInput(unsigned idx, float dt, float leftDiv, float rightDiv) { const CFinalInput& getFinalInput(unsigned idx, float dt, float leftDiv, float rightDiv) {
/* Game thread */ /* Game thread */
std::unique_lock<std::mutex> lk(m_stateLock); std::unique_lock<std::mutex> lk(m_stateLock);
@ -159,9 +160,9 @@ public:
} }
} }
} }
void ControlAllMotors(const EMotorState* states) { void ControlAllMotors(const std::array<EMotorState, 4>& states) {
if (smashAdapter) { if (smashAdapter) {
for (int i = 0; i < 4; ++i) { for (size_t i = 0; i < states.size(); ++i) {
switch (states[i]) { switch (states[i]) {
case EMotorState::Stop: case EMotorState::Stop:
smashAdapter->stopRumble(i); smashAdapter->stopRumble(i);

View File

@ -30,8 +30,8 @@ void CRumbleGenerator::Update(float dt) {
if (!xf0_24_disabled) { if (!xf0_24_disabled) {
bool updated = false; bool updated = false;
for (int i = 0; i < 4; ++i) { for (size_t i = 0; i < x0_voices.size(); ++i) {
float intensity = x0_voices[i].GetIntensity(); const float intensity = x0_voices[i].GetIntensity();
if (!x0_voices[i].Update(dt) || intensity <= 0.f) { if (!x0_voices[i].Update(dt) || intensity <= 0.f) {
xc0_periodTime[i] = 0.f; xc0_periodTime[i] = 0.f;
xd0_onTime[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() { void CRumbleGenerator::HardStopAll() {
for (int i = 0; i < 4; ++i) { static constexpr std::array HardStopCommands{
xc0_periodTime[i] = 0.f; EMotorState::StopHard,
xd0_onTime[i] = 0.f; EMotorState::StopHard,
xe0_commandArray[i] = EMotorState::Stop; EMotorState::StopHard,
x0_voices[i].HardReset(); 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); g_InputGenerator->ControlAllMotors(HardStopCommands);
} }
s16 CRumbleGenerator::Rumble(const SAdsrData& adsr, float gain, ERumblePriority prio, EIOPort port) { s16 CRumbleGenerator::Rumble(const SAdsrData& adsr, float gain, ERumblePriority prio, EIOPort port) {
CRumbleVoice& vox = x0_voices[int(port)]; CRumbleVoice& vox = x0_voices[size_t(port)];
s16 freeChan = vox.GetFreeChannel(); const s16 freeChan = vox.GetFreeChannel();
if (prio >= vox.GetPriority(freeChan)) { 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 vox.Activate(adsr, freeChan, gain, prio);
} }
return -1; return -1;
} }
void CRumbleGenerator::Stop(s16 id, EIOPort port) { void CRumbleGenerator::Stop(s16 id, EIOPort port) {
CRumbleVoice& vox = x0_voices[int(port)]; CRumbleVoice& vox = x0_voices[size_t(port)];
vox.Deactivate(id, false); vox.Deactivate(id, false);
} }

View File

@ -1,15 +1,17 @@
#pragma once #pragma once
#include <array>
#include "Runtime/GCNTypes.hpp" #include "Runtime/GCNTypes.hpp"
#include "Runtime/Input/CInputGenerator.hpp" #include "Runtime/Input/CInputGenerator.hpp"
#include "Runtime/Input/CRumbleVoice.hpp" #include "Runtime/Input/CRumbleVoice.hpp"
namespace urde { namespace urde {
class CRumbleGenerator { class CRumbleGenerator {
CRumbleVoice x0_voices[4]; std::array<CRumbleVoice, 4> x0_voices;
float xc0_periodTime[4]; std::array<float, 4> xc0_periodTime;
float xd0_onTime[4]; std::array<float, 4> xd0_onTime;
EMotorState xe0_commandArray[4]; std::array<EMotorState, 4> xe0_commandArray;
bool xf0_24_disabled : 1; bool xf0_24_disabled : 1;
public: public: