Unconnected joysticks are no longer updated (on Windows)

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1195 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2009-07-25 10:02:07 +00:00
parent 953c73c5a6
commit b5a17a832f
2 changed files with 32 additions and 26 deletions

View File

@ -42,9 +42,10 @@ namespace priv
void Joystick::Initialize(unsigned int Index)
{
// Reset state
myIndex = JOYSTICKID1;
myNbAxes = 0;
myNbButtons = 0;
myIndex = JOYSTICKID1;
myNbAxes = 0;
myNbButtons = 0;
myIsConnected = false;
// Get the Index-th connected joystick
MMRESULT Error;
@ -60,6 +61,7 @@ void Joystick::Initialize(unsigned int Index)
if (NbFound == Index)
{
// Ok : store its parameters and return
myIsConnected = true;
JOYCAPS Caps;
joyGetDevCaps(myIndex, &Caps, sizeof(Caps));
myNbAxes = Caps.wNumAxes;
@ -84,30 +86,33 @@ JoystickState Joystick::UpdateState()
{
JoystickState State = {0};
// Get the joystick caps (for range conversions)
JOYCAPS Caps;
if (joyGetDevCaps(myIndex, &Caps, sizeof(Caps)) == JOYERR_NOERROR)
if (myIsConnected)
{
// Get the current joystick state
JOYINFOEX Pos;
Pos.dwFlags = JOY_RETURNALL;
Pos.dwSize = sizeof(JOYINFOEX);
if (joyGetPosEx(myIndex, &Pos) == JOYERR_NOERROR)
// Get the joystick caps (for range conversions)
JOYCAPS Caps;
if (joyGetDevCaps(myIndex, &Caps, sizeof(Caps)) == JOYERR_NOERROR)
{
// Axes
State.Axis[Joy::AxisX] = (Pos.dwXpos - (Caps.wXmax + Caps.wXmin) / 2.f) * 200.f / (Caps.wXmax - Caps.wXmin);
State.Axis[Joy::AxisY] = (Pos.dwYpos - (Caps.wYmax + Caps.wYmin) / 2.f) * 200.f / (Caps.wYmax - Caps.wYmin);
State.Axis[Joy::AxisZ] = (Pos.dwZpos - (Caps.wZmax + Caps.wZmin) / 2.f) * 200.f / (Caps.wZmax - Caps.wZmin);
State.Axis[Joy::AxisR] = (Pos.dwRpos - (Caps.wRmax + Caps.wRmin) / 2.f) * 200.f / (Caps.wRmax - Caps.wRmin);
State.Axis[Joy::AxisU] = (Pos.dwUpos - (Caps.wUmax + Caps.wUmin) / 2.f) * 200.f / (Caps.wUmax - Caps.wUmin);
State.Axis[Joy::AxisV] = (Pos.dwVpos - (Caps.wVmax + Caps.wVmin) / 2.f) * 200.f / (Caps.wVmax - Caps.wVmin);
// Get the current joystick state
JOYINFOEX Pos;
Pos.dwFlags = JOY_RETURNALL;
Pos.dwSize = sizeof(JOYINFOEX);
if (joyGetPosEx(myIndex, &Pos) == JOYERR_NOERROR)
{
// Axes
State.Axis[Joy::AxisX] = (Pos.dwXpos - (Caps.wXmax + Caps.wXmin) / 2.f) * 200.f / (Caps.wXmax - Caps.wXmin);
State.Axis[Joy::AxisY] = (Pos.dwYpos - (Caps.wYmax + Caps.wYmin) / 2.f) * 200.f / (Caps.wYmax - Caps.wYmin);
State.Axis[Joy::AxisZ] = (Pos.dwZpos - (Caps.wZmax + Caps.wZmin) / 2.f) * 200.f / (Caps.wZmax - Caps.wZmin);
State.Axis[Joy::AxisR] = (Pos.dwRpos - (Caps.wRmax + Caps.wRmin) / 2.f) * 200.f / (Caps.wRmax - Caps.wRmin);
State.Axis[Joy::AxisU] = (Pos.dwUpos - (Caps.wUmax + Caps.wUmin) / 2.f) * 200.f / (Caps.wUmax - Caps.wUmin);
State.Axis[Joy::AxisV] = (Pos.dwVpos - (Caps.wVmax + Caps.wVmin) / 2.f) * 200.f / (Caps.wVmax - Caps.wVmin);
// POV
State.Axis[Joy::AxisPOV] = Pos.dwPOV / 100.f;
// POV
State.Axis[Joy::AxisPOV] = Pos.dwPOV / 100.f;
// Buttons
for (unsigned int i = 0; i < GetButtonsCount(); ++i)
State.Buttons[i] = (Pos.dwButtons & (1 << i)) != 0;
// Buttons
for (unsigned int i = 0; i < GetButtonsCount(); ++i)
State.Buttons[i] = (Pos.dwButtons & (1 << i)) != 0;
}
}
}

View File

@ -78,9 +78,10 @@ private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
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 myNbAxes; ///< Number of axis supported by the joystick
unsigned int myNbButtons; ///< Number of buttons supported by the joystick
};
} // namespace priv