From 4c30b9e14dfbebe6a0c1131e3a5a19410b2dcdcd Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 13 Jul 2018 10:35:35 -0400 Subject: [PATCH] ControlGroup/Force: Return state data by value Ensures that an array of sufficient size is always used and doesn't put the responsibility on the caller. It also allows for direct assignment. --- Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp | 44 +++++++++---------- .../ControllerEmu/ControlGroup/Force.cpp | 11 +++-- .../ControllerEmu/ControlGroup/Force.h | 6 ++- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp b/Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp index 06f98533bd..f6a465113c 100644 --- a/Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp @@ -5,8 +5,10 @@ #include "Core/HW/WiimoteEmu/WiimoteEmu.h" #include +#include #include #include +#include #include #include @@ -210,25 +212,24 @@ void EmulateTilt(AccelData* const accel, ControllerEmu::Tilt* const tilt_group, void EmulateSwing(AccelData* const accel, ControllerEmu::Force* const swing_group, const double intensity, const bool sideways, const bool upright) { - ControlState swing[3]; - swing_group->GetState(swing); + const ControllerEmu::Force::StateData swing = swing_group->GetState(); - s8 g_dir[3] = {-1, -1, -1}; - u8 axis_map[3]; + // Determine which axis is which direction + const std::array axis_map{{ + upright ? (sideways ? 0 : 1) : 2, // up/down + sideways, // left/right + upright ? 2 : (sideways ? 0 : 1), // forward/backward + }}; - // determine which axis is which direction - axis_map[0] = upright ? (sideways ? 0 : 1) : 2; // up/down - axis_map[1] = sideways; // left|right - axis_map[2] = upright ? 2 : (sideways ? 0 : 1); // forward/backward - - // some orientations have up as positive, some as negative + // Some orientations have up as positive, some as negative // same with forward + std::array g_dir{{-1, -1, -1}}; if (sideways && !upright) g_dir[axis_map[2]] *= -1; if (!sideways && upright) g_dir[axis_map[0]] *= -1; - for (unsigned int i = 0; i < 3; ++i) + for (std::size_t i = 0; i < swing.size(); ++i) (&accel->x)[axis_map[i]] += swing[i] * g_dir[i] * intensity; } @@ -237,25 +238,24 @@ void EmulateDynamicSwing(AccelData* const accel, DynamicData& dynamic_data, const DynamicConfiguration& config, const bool sideways, const bool upright) { - ControlState swing[3]; - swing_group->GetState(swing); + const ControllerEmu::Force::StateData swing = swing_group->GetState(); - s8 g_dir[3] = {-1, -1, -1}; - u8 axis_map[3]; + // Determine which axis is which direction + const std::array axis_map{{ + upright ? (sideways ? 0 : 1) : 2, // up/down + sideways, // left/right + upright ? 2 : (sideways ? 0 : 1), // forward/backward + }}; - // determine which axis is which direction - axis_map[0] = upright ? (sideways ? 0 : 1) : 2; // up/down - axis_map[1] = sideways; // left|right - axis_map[2] = upright ? 2 : (sideways ? 0 : 1); // forward/backward - - // some orientations have up as positive, some as negative + // Some orientations have up as positive, some as negative // same with forward + std::array g_dir{{-1, -1, -1}}; if (sideways && !upright) g_dir[axis_map[2]] *= -1; if (!sideways && upright) g_dir[axis_map[0]] *= -1; - for (unsigned int i = 0; i < 3; ++i) + for (std::size_t i = 0; i < swing.size(); ++i) { if (swing[i] > 0 && dynamic_data.executing_frames_left[i] == 0) { diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.cpp index 0c27e30298..c2178ea370 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.cpp @@ -29,18 +29,23 @@ Force::Force(const std::string& name_) : ControlGroup(name_, GroupType::Force) numeric_settings.emplace_back(std::make_unique(_trans("Dead Zone"), 0, 0, 50)); } -void Force::GetState(ControlState* axis) +Force::StateData Force::GetState() { + StateData state_data; const ControlState deadzone = numeric_settings[0]->GetValue(); for (u32 i = 0; i < 6; i += 2) { - ControlState tmpf = 0; const ControlState state = controls[i + 1]->control_ref->State() - controls[i]->control_ref->State(); + + ControlState tmpf = 0; if (fabs(state) > deadzone) tmpf = ((state - (deadzone * sign(state))) / (1 - deadzone)); - *axis++ = tmpf; + + state_data[i / 2] = tmpf; } + + return state_data; } } // namespace ControllerEmu diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.h index 1630a948b4..03dc07ecbd 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.h +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.h @@ -14,11 +14,13 @@ namespace ControllerEmu class Force : public ControlGroup { public: + using StateData = std::array; + explicit Force(const std::string& name); - void GetState(ControlState* axis); + StateData GetState(); private: - std::array m_swing{}; + StateData m_swing{}; }; } // namespace ControllerEmu