From d05f490caaacf66008bd1409e61de37b1ac6c32b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 13 Jul 2018 11:19:08 -0400 Subject: [PATCH] ControlGroup/AnalogStick: Return state data by value Makes it less error-prone to get state data from analog sticks (no need to pass any locals), and also allows direct assignment, letting the retrieved data be const. --- Source/Core/Core/HW/GCPadEmu.cpp | 27 +++++++++-------- .../Core/HW/WiimoteEmu/Attachment/Classic.cpp | 30 +++++++++---------- .../Core/HW/WiimoteEmu/Attachment/Drums.cpp | 8 ++--- .../Core/HW/WiimoteEmu/Attachment/Guitar.cpp | 9 +++--- .../Core/HW/WiimoteEmu/Attachment/Nunchuk.cpp | 8 ++--- .../HW/WiimoteEmu/Attachment/Turntable.cpp | 7 ++--- .../ControlGroup/AnalogStick.cpp | 27 ++++++++--------- .../ControllerEmu/ControlGroup/AnalogStick.h | 8 ++++- 8 files changed, 63 insertions(+), 61 deletions(-) diff --git a/Source/Core/Core/HW/GCPadEmu.cpp b/Source/Core/Core/HW/GCPadEmu.cpp index 35f6850761..8f94fb7519 100644 --- a/Source/Core/Core/HW/GCPadEmu.cpp +++ b/Source/Core/Core/HW/GCPadEmu.cpp @@ -4,6 +4,8 @@ #include "Core/HW/GCPadEmu.h" +#include + #include "Common/Common.h" #include "Common/CommonTypes.h" @@ -133,8 +135,6 @@ ControllerEmu::ControlGroup* GCPad::GetGroup(PadGroup group) GCPadStatus GCPad::GetInput() const { const auto lock = GetStateLock(); - - ControlState x, y, triggers[2]; GCPadStatus pad = {}; if (!(m_always_connected->GetValue() || IsDefaultDeviceConnected())) @@ -156,20 +156,21 @@ GCPadStatus GCPad::GetInput() const m_dpad->GetState(&pad.button, dpad_bitmasks); // sticks - m_main_stick->GetState(&x, &y); - pad.stickX = - static_cast(GCPadStatus::MAIN_STICK_CENTER_X + (x * GCPadStatus::MAIN_STICK_RADIUS)); - pad.stickY = - static_cast(GCPadStatus::MAIN_STICK_CENTER_Y + (y * GCPadStatus::MAIN_STICK_RADIUS)); + const ControllerEmu::AnalogStick::StateData main_stick_state = m_main_stick->GetState(); + pad.stickX = static_cast(GCPadStatus::MAIN_STICK_CENTER_X + + (main_stick_state.x * GCPadStatus::MAIN_STICK_RADIUS)); + pad.stickY = static_cast(GCPadStatus::MAIN_STICK_CENTER_Y + + (main_stick_state.y * GCPadStatus::MAIN_STICK_RADIUS)); - m_c_stick->GetState(&x, &y); - pad.substickX = - static_cast(GCPadStatus::C_STICK_CENTER_X + (x * GCPadStatus::C_STICK_RADIUS)); - pad.substickY = - static_cast(GCPadStatus::C_STICK_CENTER_Y + (y * GCPadStatus::C_STICK_RADIUS)); + const ControllerEmu::AnalogStick::StateData c_stick_state = m_c_stick->GetState(); + pad.substickX = static_cast(GCPadStatus::C_STICK_CENTER_X + + (c_stick_state.x * GCPadStatus::C_STICK_RADIUS)); + pad.substickY = static_cast(GCPadStatus::C_STICK_CENTER_Y + + (c_stick_state.y * GCPadStatus::C_STICK_RADIUS)); // triggers - m_triggers->GetState(&pad.button, trigger_bitmasks, triggers); + std::array triggers; + m_triggers->GetState(&pad.button, trigger_bitmasks, triggers.data()); pad.triggerLeft = static_cast(triggers[0] * 0xFF); pad.triggerRight = static_cast(triggers[1] * 0xFF); diff --git a/Source/Core/Core/HW/WiimoteEmu/Attachment/Classic.cpp b/Source/Core/Core/HW/WiimoteEmu/Attachment/Classic.cpp index 654c7a0cec..0e9b592ce1 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Attachment/Classic.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Attachment/Classic.cpp @@ -137,29 +137,27 @@ void Classic::GetState(u8* const data) // left stick { - ControlState x, y; - m_left_stick->GetState(&x, &y); + const ControllerEmu::AnalogStick::StateData left_stick_state = m_left_stick->GetState(); - classic_data.regular_data.lx = - static_cast(Classic::LEFT_STICK_CENTER_X + (x * Classic::LEFT_STICK_RADIUS)); - classic_data.regular_data.ly = - static_cast(Classic::LEFT_STICK_CENTER_Y + (y * Classic::LEFT_STICK_RADIUS)); + classic_data.regular_data.lx = static_cast( + Classic::LEFT_STICK_CENTER_X + (left_stick_state.x * Classic::LEFT_STICK_RADIUS)); + classic_data.regular_data.ly = static_cast( + Classic::LEFT_STICK_CENTER_Y + (left_stick_state.y * Classic::LEFT_STICK_RADIUS)); } // right stick { - ControlState x, y; - m_right_stick->GetState(&x, &y); + const ControllerEmu::AnalogStick::StateData right_stick_data = m_right_stick->GetState(); - const u8 x_ = - static_cast(Classic::RIGHT_STICK_CENTER_X + (x * Classic::RIGHT_STICK_RADIUS)); - const u8 y_ = - static_cast(Classic::RIGHT_STICK_CENTER_Y + (y * Classic::RIGHT_STICK_RADIUS)); + const u8 x = static_cast(Classic::RIGHT_STICK_CENTER_X + + (right_stick_data.x * Classic::RIGHT_STICK_RADIUS)); + const u8 y = static_cast(Classic::RIGHT_STICK_CENTER_Y + + (right_stick_data.y * Classic::RIGHT_STICK_RADIUS)); - classic_data.rx1 = x_; - classic_data.rx2 = x_ >> 1; - classic_data.rx3 = x_ >> 3; - classic_data.ry = y_; + classic_data.rx1 = x; + classic_data.rx2 = x >> 1; + classic_data.rx3 = x >> 3; + classic_data.ry = y; } // triggers diff --git a/Source/Core/Core/HW/WiimoteEmu/Attachment/Drums.cpp b/Source/Core/Core/HW/WiimoteEmu/Attachment/Drums.cpp index ac4600b184..726a20739b 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Attachment/Drums.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Attachment/Drums.cpp @@ -74,11 +74,10 @@ void Drums::GetState(u8* const data) // stick { - ControlState x, y; - m_stick->GetState(&x, &y); + const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState(); - drum_data.sx = static_cast((x * 0x1F) + 0x20); - drum_data.sy = static_cast((y * 0x1F) + 0x20); + drum_data.sx = static_cast((stick_state.x * 0x1F) + 0x20); + drum_data.sy = static_cast((stick_state.y * 0x1F) + 0x20); } // TODO: softness maybe @@ -87,6 +86,7 @@ void Drums::GetState(u8* const data) // buttons m_buttons->GetState(&drum_data.bt, drum_button_bitmasks.data()); + // pads m_pads->GetState(&drum_data.bt, drum_pad_bitmasks.data()); diff --git a/Source/Core/Core/HW/WiimoteEmu/Attachment/Guitar.cpp b/Source/Core/Core/HW/WiimoteEmu/Attachment/Guitar.cpp index d096257e74..05873126d8 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Attachment/Guitar.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Attachment/Guitar.cpp @@ -106,11 +106,10 @@ void Guitar::GetState(u8* const data) // stick { - ControlState x, y; - m_stick->GetState(&x, &y); + const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState(); - guitar_data.sx = static_cast((x * 0x1F) + 0x20); - guitar_data.sy = static_cast((y * 0x1F) + 0x20); + guitar_data.sx = static_cast((stick_state.x * 0x1F) + 0x20); + guitar_data.sy = static_cast((stick_state.y * 0x1F) + 0x20); } // slider bar @@ -133,8 +132,10 @@ void Guitar::GetState(u8* const data) // buttons m_buttons->GetState(&guitar_data.bt, guitar_button_bitmasks.data()); + // frets m_frets->GetState(&guitar_data.bt, guitar_fret_bitmasks.data()); + // strum m_strum->GetState(&guitar_data.bt, guitar_strum_bitmasks.data()); diff --git a/Source/Core/Core/HW/WiimoteEmu/Attachment/Nunchuk.cpp b/Source/Core/Core/HW/WiimoteEmu/Attachment/Nunchuk.cpp index b2a6f04ccb..6bd4bd6c1f 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Attachment/Nunchuk.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Attachment/Nunchuk.cpp @@ -76,11 +76,9 @@ void Nunchuk::GetState(u8* const data) wm_nc nc_data = {}; // stick - double jx, jy; - m_stick->GetState(&jx, &jy); - - nc_data.jx = u8(STICK_CENTER + jx * STICK_RADIUS); - nc_data.jy = u8(STICK_CENTER + jy * STICK_RADIUS); + const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState(); + nc_data.jx = u8(STICK_CENTER + stick_state.x * STICK_RADIUS); + nc_data.jy = u8(STICK_CENTER + stick_state.y * STICK_RADIUS); // Some terribly coded games check whether to move with a check like // diff --git a/Source/Core/Core/HW/WiimoteEmu/Attachment/Turntable.cpp b/Source/Core/Core/HW/WiimoteEmu/Attachment/Turntable.cpp index 94c66a46bb..1323d4550f 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Attachment/Turntable.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Attachment/Turntable.cpp @@ -90,11 +90,10 @@ void Turntable::GetState(u8* const data) // stick { - ControlState x, y; - m_stick->GetState(&x, &y); + const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState(); - tt_data.sx = static_cast((x * 0x1F) + 0x20); - tt_data.sy = static_cast((y * 0x1F) + 0x20); + tt_data.sx = static_cast((stick_state.x * 0x1F) + 0x20); + tt_data.sy = static_cast((stick_state.y * 0x1F) + 0x20); } // left table diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp index 3317da427c..c38e5f1d3a 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp @@ -36,20 +36,20 @@ AnalogStick::AnalogStick(const char* const name_, const char* const ui_name_, numeric_settings.emplace_back(std::make_unique(_trans("Dead Zone"), 0, 0, 50)); } -void AnalogStick::GetState(ControlState* const x, ControlState* const y) +AnalogStick::StateData AnalogStick::GetState() { - ControlState yy = controls[0]->control_ref->State() - controls[1]->control_ref->State(); - ControlState xx = controls[3]->control_ref->State() - controls[2]->control_ref->State(); + ControlState y = controls[0]->control_ref->State() - controls[1]->control_ref->State(); + ControlState x = controls[3]->control_ref->State() - controls[2]->control_ref->State(); - ControlState radius = numeric_settings[SETTING_RADIUS]->GetValue(); - ControlState deadzone = numeric_settings[SETTING_DEADZONE]->GetValue(); - ControlState m = controls[4]->control_ref->State(); + const ControlState radius = numeric_settings[SETTING_RADIUS]->GetValue(); + const ControlState deadzone = numeric_settings[SETTING_DEADZONE]->GetValue(); + const ControlState m = controls[4]->control_ref->State(); - ControlState ang = atan2(yy, xx); - ControlState ang_sin = sin(ang); - ControlState ang_cos = cos(ang); + const ControlState ang = atan2(y, x); + const ControlState ang_sin = sin(ang); + const ControlState ang_cos = cos(ang); - ControlState dist = sqrt(xx * xx + yy * yy); + ControlState dist = sqrt(x * x + y * y); // dead zone code dist = std::max(0.0, dist - deadzone); @@ -63,10 +63,9 @@ void AnalogStick::GetState(ControlState* const x, ControlState* const y) if (m) dist *= 0.5; - yy = std::max(-1.0, std::min(1.0, ang_sin * dist)); - xx = std::max(-1.0, std::min(1.0, ang_cos * dist)); + y = std::max(-1.0, std::min(1.0, ang_sin * dist)); + x = std::max(-1.0, std::min(1.0, ang_cos * dist)); - *y = yy; - *x = xx; + return {x, y}; } } // namespace ControllerEmu diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h index a598202ba7..d8d8e4ddea 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h @@ -18,10 +18,16 @@ public: SETTING_DEADZONE, }; + struct StateData + { + ControlState x{}; + ControlState y{}; + }; + // The GameCube controller and Wiimote attachments have a different default radius AnalogStick(const char* name, ControlState default_radius); AnalogStick(const char* name, const char* ui_name, ControlState default_radius); - void GetState(ControlState* x, ControlState* y); + StateData GetState(); }; } // namespace ControllerEmu