From 6c8cfcb0296671f503525fd8fc5a15489e959994 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 28 Mar 2021 22:12:27 +0200 Subject: [PATCH] ControllerEmu: Round input floats instead of casting, part 2 Like 3bc4968 but for Wii Remote extensions. I'm doing this to ensure that TAS input values will still roundtrip after the next commit. --- .../Core/Core/HW/WiimoteEmu/Extension/Classic.cpp | 14 +++++++------- Source/Core/Core/HW/WiimoteEmu/Extension/Classic.h | 7 ++++--- .../HW/WiimoteEmu/Extension/DrawsomeTablet.cpp | 10 +++++----- Source/Core/Core/HW/WiimoteEmu/Extension/Drums.cpp | 4 ++-- .../Core/Core/HW/WiimoteEmu/Extension/Guitar.cpp | 6 +++--- Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.h | 1 + .../Core/Core/HW/WiimoteEmu/Extension/Nunchuk.cpp | 4 ++-- Source/Core/Core/HW/WiimoteEmu/Extension/Nunchuk.h | 1 + .../Core/HW/WiimoteEmu/Extension/Shinkansen.cpp | 7 +++++-- .../Core/HW/WiimoteEmu/Extension/Turntable.cpp | 12 ++++++------ .../Core/Core/HW/WiimoteEmu/Extension/Turntable.h | 5 +++-- 11 files changed, 39 insertions(+), 32 deletions(-) diff --git a/Source/Core/Core/HW/WiimoteEmu/Extension/Classic.cpp b/Source/Core/Core/HW/WiimoteEmu/Extension/Classic.cpp index 48d56d898d3e..ba4f58fb39b2 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Extension/Classic.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Extension/Classic.cpp @@ -84,7 +84,7 @@ Classic::Classic() : Extension1stParty("Classic", _trans("Classic Controller")) } // sticks - constexpr auto gate_radius = ControlState(STICK_GATE_RADIUS) / CAL_STICK_RANGE; + constexpr auto gate_radius = ControlState(STICK_GATE_RADIUS) / CAL_STICK_RADIUS; groups.emplace_back(m_left_stick = new ControllerEmu::OctagonAnalogStick(_trans("Left Stick"), gate_radius)); groups.emplace_back( @@ -114,8 +114,8 @@ void Classic::Update() const ControllerEmu::AnalogStick::StateData left_stick_state = m_left_stick->GetState(m_input_override_function); - const u8 x = static_cast(LEFT_STICK_CENTER + (left_stick_state.x * LEFT_STICK_RADIUS)); - const u8 y = static_cast(LEFT_STICK_CENTER + (left_stick_state.y * LEFT_STICK_RADIUS)); + const u8 x = MapFloat(left_stick_state.x, LEFT_STICK_CENTER, 0, LEFT_STICK_RANGE); + const u8 y = MapFloat(left_stick_state.y, LEFT_STICK_CENTER, 0, LEFT_STICK_RANGE); classic_data.SetLeftStick({x, y}); } @@ -125,8 +125,8 @@ void Classic::Update() const ControllerEmu::AnalogStick::StateData right_stick_data = m_right_stick->GetState(m_input_override_function); - const u8 x = static_cast(RIGHT_STICK_CENTER + (right_stick_data.x * RIGHT_STICK_RADIUS)); - const u8 y = static_cast(RIGHT_STICK_CENTER + (right_stick_data.y * RIGHT_STICK_RADIUS)); + const u8 x = MapFloat(right_stick_data.x, RIGHT_STICK_CENTER, 0, RIGHT_STICK_RANGE); + const u8 y = MapFloat(right_stick_data.y, RIGHT_STICK_CENTER, 0, RIGHT_STICK_RANGE); classic_data.SetRightStick({x, y}); } @@ -139,8 +139,8 @@ void Classic::Update() m_triggers->GetState(&buttons, classic_trigger_bitmasks.data(), triggers, m_input_override_function); - const u8 lt = static_cast(triggers[0] * TRIGGER_RANGE); - const u8 rt = static_cast(triggers[1] * TRIGGER_RANGE); + const u8 lt = MapFloat(triggers[0], 0, 0, TRIGGER_RANGE); + const u8 rt = MapFloat(triggers[1], 0, 0, TRIGGER_RANGE); classic_data.SetLeftTrigger(lt); classic_data.SetRightTrigger(rt); diff --git a/Source/Core/Core/HW/WiimoteEmu/Extension/Classic.h b/Source/Core/Core/HW/WiimoteEmu/Extension/Classic.h index 36bc2a7f8b4f..8944b92f7226 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Extension/Classic.h +++ b/Source/Core/Core/HW/WiimoteEmu/Extension/Classic.h @@ -204,13 +204,14 @@ class Classic : public Extension1stParty static constexpr u8 STICK_GATE_RADIUS = 0x61; static constexpr u8 CAL_STICK_CENTER = 0x80; - static constexpr u8 CAL_STICK_RANGE = 0x7f; + static constexpr u8 CAL_STICK_RADIUS = 0x7f; + static constexpr u8 CAL_STICK_RANGE = 0xff; static constexpr u8 LEFT_STICK_CENTER = CAL_STICK_CENTER >> (CAL_STICK_BITS - LEFT_STICK_BITS); - static constexpr u8 LEFT_STICK_RADIUS = CAL_STICK_RANGE >> (CAL_STICK_BITS - LEFT_STICK_BITS); + static constexpr u8 LEFT_STICK_RANGE = CAL_STICK_RANGE >> (CAL_STICK_BITS - LEFT_STICK_BITS); static constexpr u8 RIGHT_STICK_CENTER = CAL_STICK_CENTER >> (CAL_STICK_BITS - RIGHT_STICK_BITS); - static constexpr u8 RIGHT_STICK_RADIUS = CAL_STICK_RANGE >> (CAL_STICK_BITS - RIGHT_STICK_BITS); + static constexpr u8 RIGHT_STICK_RANGE = CAL_STICK_RANGE >> (CAL_STICK_BITS - RIGHT_STICK_BITS); static constexpr u8 TRIGGER_RANGE = 0x1F; diff --git a/Source/Core/Core/HW/WiimoteEmu/Extension/DrawsomeTablet.cpp b/Source/Core/Core/HW/WiimoteEmu/Extension/DrawsomeTablet.cpp index d92f23e8836c..ed89dbbdf7cf 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Extension/DrawsomeTablet.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Extension/DrawsomeTablet.cpp @@ -42,12 +42,12 @@ void DrawsomeTablet::Update() // the "Drawsome" game expects you to go "off screen" a bit to access some menu items. constexpr u16 MIN_Y = 0x15ff + 0x100; constexpr u16 MAX_Y = 0x00; - constexpr double CENTER_X = (MAX_X + MIN_X) / 2.0; - constexpr double CENTER_Y = (MAX_Y + MIN_Y) / 2.0; + constexpr u16 CENTER_X = (MAX_X + MIN_X + 1) / 2; + constexpr u16 CENTER_Y = (MAX_Y + MIN_Y + 1) / 2; const auto stylus_state = m_stylus->GetState(m_input_override_function); - const auto stylus_x = u16(std::lround(CENTER_X + stylus_state.x * (MAX_X - CENTER_X))); - const auto stylus_y = u16(std::lround(CENTER_Y + stylus_state.y * (MAX_Y - CENTER_Y))); + const u16 stylus_x = MapFloat(stylus_state.x, CENTER_X, MIN_X, MAX_X); + const u16 stylus_y = MapFloat(-stylus_state.y, CENTER_Y, MAX_Y, MIN_Y); tablet_data.stylus_x1 = u8(stylus_x); tablet_data.stylus_x2 = u8(stylus_x >> 8); @@ -73,7 +73,7 @@ void DrawsomeTablet::Update() constexpr u16 MAX_PRESSURE = 0x7ff; const auto touch_state = m_touch->GetState(m_input_override_function); - const auto pressure = u16(std::lround(touch_state.data[0] * MAX_PRESSURE)); + const u16 pressure = MapFloat(touch_state.data[0], 0, 0, MAX_PRESSURE); tablet_data.pressure1 = u8(pressure); tablet_data.pressure2 = u8(pressure >> 8); diff --git a/Source/Core/Core/HW/WiimoteEmu/Extension/Drums.cpp b/Source/Core/Core/HW/WiimoteEmu/Extension/Drums.cpp index 52b2fdc7c662..3548b3081c17 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Extension/Drums.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Extension/Drums.cpp @@ -99,8 +99,8 @@ void Drums::Update() const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState(m_input_override_function); - drum_data.stick_x = MapFloat(stick_state.x, STICK_CENTER, STICK_MIN, STICK_MAX); - drum_data.stick_y = MapFloat(stick_state.y, STICK_CENTER, STICK_MIN, STICK_MAX); + drum_data.stick_x = MapFloat(stick_state.x, STICK_CENTER, STICK_MIN, STICK_MAX); + drum_data.stick_y = MapFloat(stick_state.y, STICK_CENTER, STICK_MIN, STICK_MAX); } // Buttons. diff --git a/Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.cpp b/Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.cpp index 67bf9365381e..f9a3a77b75b6 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.cpp @@ -102,8 +102,8 @@ void Guitar::Update() const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState(m_input_override_function); - guitar_data.sx = static_cast((stick_state.x * STICK_RADIUS) + STICK_CENTER); - guitar_data.sy = static_cast((stick_state.y * STICK_RADIUS) + STICK_CENTER); + guitar_data.sx = MapFloat(stick_state.x, STICK_CENTER, 0, STICK_RANGE); + guitar_data.sy = MapFloat(stick_state.y, STICK_CENTER, 0, STICK_RANGE); } // slider bar @@ -124,7 +124,7 @@ void Guitar::Update() // whammy bar const ControllerEmu::Triggers::StateData whammy_state = m_whammy->GetState(m_input_override_function); - guitar_data.whammy = static_cast(whammy_state.data[0] * 0x1F); + guitar_data.whammy = MapFloat(whammy_state.data[0], 0, 0, 0x1F); // buttons m_buttons->GetState(&guitar_data.bt, guitar_button_bitmasks.data(), m_input_override_function); diff --git a/Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.h b/Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.h index 04630c428ee6..45814b6373a1 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.h +++ b/Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.h @@ -68,6 +68,7 @@ class Guitar : public Extension1stParty static const u8 STICK_CENTER = 0x20; static const u8 STICK_RADIUS = 0x1f; + static const u8 STICK_RANGE = 0x3f; // TODO: Test real hardware. Is this accurate? static const u8 STICK_GATE_RADIUS = 0x16; diff --git a/Source/Core/Core/HW/WiimoteEmu/Extension/Nunchuk.cpp b/Source/Core/Core/HW/WiimoteEmu/Extension/Nunchuk.cpp index adfb22a1b2ac..5e804fd0932b 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Extension/Nunchuk.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Extension/Nunchuk.cpp @@ -68,8 +68,8 @@ void Nunchuk::Update() bool override_occurred = false; const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState(m_input_override_function, &override_occurred); - nc_data.jx = u8(STICK_CENTER + stick_state.x * STICK_RADIUS); - nc_data.jy = u8(STICK_CENTER + stick_state.y * STICK_RADIUS); + nc_data.jx = MapFloat(stick_state.x, STICK_CENTER, 0, STICK_RANGE); + nc_data.jy = MapFloat(stick_state.y, STICK_CENTER, 0, STICK_RANGE); if (!override_occurred) { diff --git a/Source/Core/Core/HW/WiimoteEmu/Extension/Nunchuk.h b/Source/Core/Core/HW/WiimoteEmu/Extension/Nunchuk.h index 704de14d4b62..c3ef1d477eb7 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Extension/Nunchuk.h +++ b/Source/Core/Core/HW/WiimoteEmu/Extension/Nunchuk.h @@ -165,6 +165,7 @@ class Nunchuk : public Extension1stParty static constexpr u8 STICK_CENTER = 0x80; static constexpr u8 STICK_RADIUS = 0x7F; + static constexpr u8 STICK_RANGE = 0xFF; void LoadDefaults(const ControllerInterface& ciface) override; diff --git a/Source/Core/Core/HW/WiimoteEmu/Extension/Shinkansen.cpp b/Source/Core/Core/HW/WiimoteEmu/Extension/Shinkansen.cpp index 81e4a52070f5..88ec8d30a769 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Extension/Shinkansen.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Extension/Shinkansen.cpp @@ -62,8 +62,11 @@ void Shinkansen::Update() // guesses). const u8 brake_values[] = {0, 53, 79, 105, 132, 159, 187, 217, 250}; const u8 power_values[] = {255, 229, 208, 189, 170, 153, 135, 118, 101, 85, 68, 51, 35, 17}; - ext_data.brake = brake_values[size_t(analog[0] * (sizeof(brake_values) - 1))]; - ext_data.power = power_values[size_t(analog[1] * (sizeof(power_values) - 1))]; + // Not casting from size_t would trigger a static assert in MapFloat due to its use of llround + ext_data.brake = + brake_values[MapFloat(analog[0], 0, 0, static_cast(sizeof(brake_values) - 1))]; + ext_data.power = + power_values[MapFloat(analog[1], 0, 0, static_cast(sizeof(power_values) - 1))]; // Note: This currently assumes a little-endian host. const u16 button_bitmasks[] = { diff --git a/Source/Core/Core/HW/WiimoteEmu/Extension/Turntable.cpp b/Source/Core/Core/HW/WiimoteEmu/Extension/Turntable.cpp index 32238901a625..9d359eb6e27c 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Extension/Turntable.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Extension/Turntable.cpp @@ -87,14 +87,14 @@ void Turntable::Update() const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState(m_input_override_function); - tt_data.sx = static_cast((stick_state.x * STICK_RADIUS) + STICK_CENTER); - tt_data.sy = static_cast((stick_state.y * STICK_RADIUS) + STICK_CENTER); + tt_data.sx = MapFloat(stick_state.x, STICK_CENTER, 0, STICK_RANGE); + tt_data.sy = MapFloat(stick_state.y, STICK_CENTER, 0, STICK_RANGE); } // left table { const ControllerEmu::Slider::StateData lt = m_left_table->GetState(m_input_override_function); - const s8 tt = static_cast(lt.value * TABLE_RANGE); + const s8 tt = MapFloat(lt.value, 0, 0, TABLE_RANGE); tt_data.ltable1 = tt; tt_data.ltable2 = tt >> 5; @@ -103,7 +103,7 @@ void Turntable::Update() // right table { const ControllerEmu::Slider::StateData rt = m_right_table->GetState(m_input_override_function); - const s8 tt = static_cast(rt.value * TABLE_RANGE); + const s8 tt = MapFloat(rt.value, 0, 0, TABLE_RANGE); tt_data.rtable1 = tt; tt_data.rtable2 = tt >> 1; @@ -114,7 +114,7 @@ void Turntable::Update() // effect dial { const auto dial_state = m_effect_dial->GetState(m_input_override_function); - const u8 dial = static_cast(dial_state.value * EFFECT_DIAL_RANGE) + EFFECT_DIAL_CENTER; + const u8 dial = MapFloat(dial_state.value, EFFECT_DIAL_CENTER, 0, EFFECT_DIAL_RANGE); tt_data.dial1 = dial; tt_data.dial2 = dial >> 3; @@ -124,7 +124,7 @@ void Turntable::Update() { const ControllerEmu::Slider::StateData cfs = m_crossfade->GetState(m_input_override_function); - tt_data.slider = static_cast((cfs.value * CROSSFADE_RANGE) + CROSSFADE_CENTER); + tt_data.slider = MapFloat(cfs.value, CROSSFADE_CENTER, 0, CROSSFADE_RANGE); } // buttons diff --git a/Source/Core/Core/HW/WiimoteEmu/Extension/Turntable.h b/Source/Core/Core/HW/WiimoteEmu/Extension/Turntable.h index 539f5ce10c0d..db04dc8f57f0 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Extension/Turntable.h +++ b/Source/Core/Core/HW/WiimoteEmu/Extension/Turntable.h @@ -77,6 +77,7 @@ class Turntable : public Extension1stParty static constexpr int STICK_BIT_COUNT = 6; static constexpr u8 STICK_CENTER = (1 << STICK_BIT_COUNT) / 2; static constexpr u8 STICK_RADIUS = STICK_CENTER - 1; + static constexpr u8 STICK_RANGE = (1 << STICK_BIT_COUNT) - 1; // TODO: Test real hardware. Is this accurate? static constexpr u8 STICK_GATE_RADIUS = 0x16; @@ -85,11 +86,11 @@ class Turntable : public Extension1stParty static constexpr int EFFECT_DIAL_BIT_COUNT = 5; static constexpr u8 EFFECT_DIAL_CENTER = (1 << EFFECT_DIAL_BIT_COUNT) / 2; - static constexpr u8 EFFECT_DIAL_RANGE = EFFECT_DIAL_CENTER - 1; + static constexpr u8 EFFECT_DIAL_RANGE = (1 << EFFECT_DIAL_BIT_COUNT) - 1; static constexpr int CROSSFADE_BIT_COUNT = 4; static constexpr u8 CROSSFADE_CENTER = (1 << CROSSFADE_BIT_COUNT) / 2; - static constexpr u8 CROSSFADE_RANGE = CROSSFADE_CENTER - 1; + static constexpr u8 CROSSFADE_RANGE = (1 << CROSSFADE_BIT_COUNT) - 1; private: ControllerEmu::Buttons* m_buttons;