From 8bbf7dfc46bbc271f1beb29a642092ce798041e1 Mon Sep 17 00:00:00 2001 From: LaurentGom Date: Tue, 5 Jan 2010 16:48:32 +0000 Subject: [PATCH] FS#143 - Increase the number of supported joysticks to 4 FS#142 - Fix joystick axes being sometimes ignored git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1318 4e206d99-4929-0410-ac5d-dfc041789085 --- build/vc2008/sfml-window.vcproj | 2 +- src/SFML/Window/Linux/Joystick.cpp | 83 ++++++++++++++++++++++++------ src/SFML/Window/Linux/Joystick.hpp | 18 ++++--- src/SFML/Window/Win32/Joystick.cpp | 31 +++++++---- src/SFML/Window/Win32/Joystick.hpp | 17 +++--- src/SFML/Window/WindowImpl.cpp | 23 +++++---- src/SFML/Window/WindowImpl.hpp | 2 +- 7 files changed, 125 insertions(+), 51 deletions(-) diff --git a/build/vc2008/sfml-window.vcproj b/build/vc2008/sfml-window.vcproj index 3b9af5d5..81225ab9 100644 --- a/build/vc2008/sfml-window.vcproj +++ b/build/vc2008/sfml-window.vcproj @@ -412,7 +412,7 @@ > JoystickState::MaxButtons) myNbButtons = JoystickState::MaxButtons; + myAxes[Joy::AxisX] = true; + myAxes[Joy::AxisY] = true; + myAxes[Joy::AxisZ] = (Caps.wCaps & JOYCAPS_HASZ) != 0; + myAxes[Joy::AxisR] = (Caps.wCaps & JOYCAPS_HASR) != 0; + myAxes[Joy::AxisU] = (Caps.wCaps & JOYCAPS_HASU) != 0; + myAxes[Joy::AxisV] = (Caps.wCaps & JOYCAPS_HASV) != 0; + myAxes[Joy::AxisPOV] = (Caps.wCaps & JOYCAPS_HASPOV) != 0; + myHasContinuousPOV = (Caps.wCaps & JOYCAPS_POVCTS) != 0; + return; } @@ -94,8 +104,9 @@ JoystickState Joystick::UpdateState() { // Get the current joystick state JOYINFOEX Pos; - Pos.dwFlags = JOY_RETURNALL; - Pos.dwSize = sizeof(JOYINFOEX); + Pos.dwFlags = JOY_RETURNX | JOY_RETURNY | JOY_RETURNZ | JOY_RETURNR | JOY_RETURNU | JOY_RETURNV | JOY_RETURNBUTTONS; + Pos.dwFlags |= myHasContinuousPOV ? JOY_RETURNPOVCTS : JOY_RETURNPOV; + Pos.dwSize = sizeof(JOYINFOEX); if (joyGetPosEx(myIndex, &Pos) == JOYERR_NOERROR) { // Axes @@ -121,11 +132,11 @@ JoystickState Joystick::UpdateState() //////////////////////////////////////////////////////////// -/// Get the number of axes supported by the joystick +/// Check if the joystick supports the given axis //////////////////////////////////////////////////////////// -unsigned int Joystick::GetAxesCount() const +bool Joystick::HasAxis(Joy::Axis Axis) const { - return myNbAxes; + return myAxes[Axis]; } diff --git a/src/SFML/Window/Win32/Joystick.hpp b/src/SFML/Window/Win32/Joystick.hpp index d2f10174..51666e5f 100644 --- a/src/SFML/Window/Win32/Joystick.hpp +++ b/src/SFML/Window/Win32/Joystick.hpp @@ -58,12 +58,14 @@ public : JoystickState UpdateState(); //////////////////////////////////////////////////////////// - /// Get the number of axes supported by the joystick + /// Check if the joystick supports the given axis /// - /// \return Number of axis + /// \param Axis : Axis to check + /// + /// \return True of the axis is supported, false otherwise /// //////////////////////////////////////////////////////////// - unsigned int GetAxesCount() const; + bool HasAxis(Joy::Axis Axis) const; //////////////////////////////////////////////////////////// /// Get the number of buttons supported by the joystick @@ -78,10 +80,11 @@ private : //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - bool myIsConnected; ///< Is there a joystick connected? - unsigned int myIndex; ///< Windows ID of the joystick - unsigned int myNbAxes; ///< Number of axis supported by the joystick - unsigned int myNbButtons; ///< Number of buttons supported by the joystick + bool myIsConnected; ///< Is there a joystick connected? + unsigned int myIndex; ///< Windows ID of the joystick + unsigned int myNbButtons; ///< Number of buttons supported by the joystick + bool myAxes[Joy::Count]; ///< Supported axes + bool myHasContinuousPOV; ///< True if the driver supports continuous values for the POV }; } // namespace priv diff --git a/src/SFML/Window/WindowImpl.cpp b/src/SFML/Window/WindowImpl.cpp index bc605436..782f8e3e 100644 --- a/src/SFML/Window/WindowImpl.cpp +++ b/src/SFML/Window/WindowImpl.cpp @@ -221,18 +221,21 @@ void WindowImpl::ProcessJoystickEvents() myJoyStates[i] = myJoysticks[i].UpdateState(); // Axis - for (unsigned int j = 0; j < myJoysticks[i].GetAxesCount(); ++j) + for (unsigned int j = 0; j < Joy::Count; ++j) { - float PrevPos = PreviousState.Axis[j]; - float CurrPos = myJoyStates[i].Axis[j]; - if (fabs(CurrPos - PrevPos) >= myJoyThreshold) + if (myJoysticks[i].HasAxis(static_cast(i))) { - Event Event; - Event.Type = Event::JoyMoved; - Event.JoyMove.JoystickId = i; - Event.JoyMove.Axis = static_cast(j); - Event.JoyMove.Position = CurrPos; - SendEvent(Event); + float PrevPos = PreviousState.Axis[j]; + float CurrPos = myJoyStates[i].Axis[j]; + if (fabs(CurrPos - PrevPos) >= myJoyThreshold) + { + Event Event; + Event.Type = Event::JoyMoved; + Event.JoyMove.JoystickId = i; + Event.JoyMove.Axis = static_cast(j); + Event.JoyMove.Position = CurrPos; + SendEvent(Event); + } } } diff --git a/src/SFML/Window/WindowImpl.hpp b/src/SFML/Window/WindowImpl.hpp index 15d29ecf..2e019809 100644 --- a/src/SFML/Window/WindowImpl.hpp +++ b/src/SFML/Window/WindowImpl.hpp @@ -293,7 +293,7 @@ private : //////////////////////////////////////////////////////////// // Total number of joysticks supported //////////////////////////////////////////////////////////// - enum {JoysticksCount = 2}; + enum {JoysticksCount = 4}; //////////////////////////////////////////////////////////// // Member data