From fe5456ee783a789958d030c208ac1e5de6e8ba68 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Wed, 16 Oct 2024 18:12:09 -0600 Subject: [PATCH] Use optionals to represent empty states --- src/SFML/Window/Win32/JoystickImpl.cpp | 46 ++++++++++++-------------- src/SFML/Window/Win32/JoystickImpl.hpp | 17 +++++----- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/SFML/Window/Win32/JoystickImpl.cpp b/src/SFML/Window/Win32/JoystickImpl.cpp index 85f077831..d130065d0 100644 --- a/src/SFML/Window/Win32/JoystickImpl.cpp +++ b/src/SFML/Window/Win32/JoystickImpl.cpp @@ -519,8 +519,8 @@ bool JoystickImpl::openDInput(unsigned int index) // Initialize DirectInput members m_device = nullptr; - m_axes.fill(-1); - m_buttons.fill(-1); + m_axes.fill(std::nullopt); + m_buttons.fill(std::nullopt); m_deviceCaps = {}; m_deviceCaps.dwSize = sizeof(DIDEVCAPS); @@ -711,9 +711,9 @@ bool JoystickImpl::openDInput(unsigned int index) } // Set device's axis mode to absolute if the device reports having at least one axis - for (const int axis : m_axes) + for (const std::optional axis : m_axes) { - if (axis != -1) + if (axis.has_value()) { property = {}; property.diph.dwSize = sizeof(property); @@ -847,17 +847,15 @@ JoystickCaps JoystickImpl::getCapabilitiesDInput() const // Count how many buttons have valid offsets caps.buttonCount = 0; - for (const int button : m_buttons) - { - if (button != -1) + for (const std::optional button : m_buttons) + if (button.has_value()) ++caps.buttonCount; - } // Check which axes have valid offsets for (unsigned int i = 0; i < Joystick::AxisCount; ++i) { const auto axis = static_cast(i); - caps.axes[axis] = (m_axes[axis] != -1); + caps.axes[axis] = m_axes[axis].has_value(); } return caps; @@ -911,7 +909,7 @@ JoystickState JoystickImpl::updateDInputBuffered() for (unsigned int j = 0; j < Joystick::AxisCount; ++j) { const auto axis = static_cast(j); - if (m_axes[axis] == static_cast(events[i].dwOfs)) + if (m_axes[axis] == events[i].dwOfs) { if ((axis == Joystick::Axis::PovX) || (axis == Joystick::Axis::PovY)) { @@ -945,11 +943,9 @@ JoystickState JoystickImpl::updateDInputBuffered() continue; // Get the current state of each button - for (unsigned int j = 0; j < Joystick::ButtonCount; ++j) - { - if (m_buttons[j] == static_cast(events[i].dwOfs)) + for (std::size_t j = 0; j < m_buttons.size(); ++j) + if (m_buttons[j] == events[i].dwOfs) m_state.buttons[j] = (events[i].dwData != 0); - } } m_state.connected = true; @@ -1001,12 +997,12 @@ JoystickState JoystickImpl::updateDInputPolled() for (unsigned int i = 0; i < Joystick::AxisCount; ++i) { const auto axis = static_cast(i); - if (m_axes[axis] != -1) + if (m_axes[axis].has_value()) { if ((axis == Joystick::Axis::PovX) || (axis == Joystick::Axis::PovY)) { const unsigned short value = LOWORD( - *reinterpret_cast(reinterpret_cast(&joystate) + m_axes[axis])); + *reinterpret_cast(reinterpret_cast(&joystate) + *m_axes[axis])); if (value != 0xFFFF) { @@ -1024,7 +1020,7 @@ JoystickState JoystickImpl::updateDInputPolled() else { state.axes[axis] = (static_cast(*reinterpret_cast( - reinterpret_cast(&joystate) + m_axes[axis])) + + reinterpret_cast(&joystate) + *m_axes[axis])) + 0.5f) * 100.f / 32767.5f; } @@ -1038,9 +1034,9 @@ JoystickState JoystickImpl::updateDInputPolled() // Get the current state of each button for (unsigned int i = 0; i < Joystick::ButtonCount; ++i) { - if (m_buttons[i] != -1) + if (m_buttons[i].has_value()) { - const BYTE value = *reinterpret_cast(reinterpret_cast(&joystate) + m_buttons[i]); + const BYTE value = *reinterpret_cast(reinterpret_cast(&joystate) + *m_buttons[i]); state.buttons[i] = ((value & 0x80) != 0); } @@ -1099,10 +1095,10 @@ BOOL CALLBACK JoystickImpl::deviceObjectEnumerationCallback(const DIDEVICEOBJECT joystick.m_axes[Joystick::Axis::V] = DIJOFS_RY; else if (deviceObjectInstance->guidType == guids::GUID_Slider) { - if (joystick.m_axes[Joystick::Axis::U] == -1) - joystick.m_axes[Joystick::Axis::U] = DIJOFS_SLIDER(0); + if (!joystick.m_axes[Joystick::Axis::U].has_value()) + joystick.m_axes[Joystick::Axis::U] = static_cast(DIJOFS_SLIDER(0)); else - joystick.m_axes[Joystick::Axis::V] = DIJOFS_SLIDER(1); + joystick.m_axes[Joystick::Axis::V] = static_cast(DIJOFS_SLIDER(1)); } else return DIENUM_CONTINUE; @@ -1128,7 +1124,7 @@ BOOL CALLBACK JoystickImpl::deviceObjectEnumerationCallback(const DIDEVICEOBJECT // POVs if (deviceObjectInstance->guidType == guids::GUID_POV) { - if (joystick.m_axes[Joystick::Axis::PovX] == -1) + if (!joystick.m_axes[Joystick::Axis::PovX].has_value()) { joystick.m_axes[Joystick::Axis::PovX] = DIJOFS_POV(0); joystick.m_axes[Joystick::Axis::PovY] = DIJOFS_POV(0); @@ -1142,9 +1138,9 @@ BOOL CALLBACK JoystickImpl::deviceObjectEnumerationCallback(const DIDEVICEOBJECT // Buttons for (unsigned int i = 0; i < Joystick::ButtonCount; ++i) { - if (joystick.m_buttons[i] == -1) + if (!joystick.m_buttons[i].has_value()) { - joystick.m_buttons[i] = DIJOFS_BUTTON(static_cast(i)); + *joystick.m_buttons[i] = DIJOFS_BUTTON(static_cast(i)); break; } } diff --git a/src/SFML/Window/Win32/JoystickImpl.hpp b/src/SFML/Window/Win32/JoystickImpl.hpp index 37e167b51..1a2cac22c 100644 --- a/src/SFML/Window/Win32/JoystickImpl.hpp +++ b/src/SFML/Window/Win32/JoystickImpl.hpp @@ -32,6 +32,7 @@ #include #include +#include namespace sf::priv @@ -213,14 +214,14 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - unsigned int m_index{}; //!< Index of the joystick - JOYCAPS m_caps{}; //!< Joystick capabilities - IDirectInputDevice8W* m_device{}; //!< DirectInput 8.x device - DIDEVCAPS m_deviceCaps{}; //!< DirectInput device capabilities - EnumArray m_axes{}; //!< Offsets to the bytes containing the axes states, -1 if not available - std::array m_buttons{}; //!< Offsets to the bytes containing the button states, -1 if not available - Joystick::Identification m_identification; //!< Joystick identification - JoystickState m_state; //!< Buffered joystick state + unsigned int m_index{}; //!< Index of the joystick + JOYCAPS m_caps{}; //!< Joystick capabilities + IDirectInputDevice8W* m_device{}; //!< DirectInput 8.x device + DIDEVCAPS m_deviceCaps{}; //!< DirectInput device capabilities + EnumArray, Joystick::AxisCount> m_axes{}; //!< Offsets to the bytes containing the axes states + std::array, Joystick::ButtonCount> m_buttons{}; //!< Offsets to the bytes containing the button states + Joystick::Identification m_identification; //!< Joystick identification + JoystickState m_state; //!< Buffered joystick state bool m_buffered{}; //!< `true` if the device uses buffering, `false` if the device uses polling };