Fixed bug: SDL2 Xinput joystick axis jumps from positive to negative
authorSam Lantinga <slouken@libsdl.org>
Thu, 27 Jun 2013 11:21:37 -0700
changeset 732146de847fef38
parent 7320 314c488ab516
child 7322 5b94da2650a6
Fixed bug: SDL2 Xinput joystick axis jumps from positive to negative

Franz Schrober

Attached is my patch. It ensures that the values are correctly limitted between -32767 and 32767 (otherwise the negator - and the conversion to sint16 would corrupt the result)

I am using Motioninjoy (Dualshock 3 Sixaxxis controller on Windows 7) together with a recent SDL2 (post rc1) and noticed with the testjoystick binary that the axis 3 (left analog up/down) jumps when going in down direction from 32257 to -32768. This seems obviously wrong and I have never seen this before. In my games the people are now going backwards before they start to sprint forward when the player actually wants to run as fast as possible backwards. This also happens on the axis 2 (right analog stick up/down)

This problem doesn't happen in DX mode
src/joystick/windows/SDL_dxjoystick.c
     1.1 --- a/src/joystick/windows/SDL_dxjoystick.c	Thu Jun 27 11:17:48 2013 -0700
     1.2 +++ b/src/joystick/windows/SDL_dxjoystick.c	Thu Jun 27 11:21:37 2013 -0700
     1.3 @@ -1507,9 +1507,9 @@
     1.4          XINPUT_STATE_EX *pXInputStatePrev = &joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot ^ 1];
     1.5  
     1.6          SDL_PrivateJoystickAxis( joystick, 0, (Sint16)pXInputState->Gamepad.sThumbLX );
     1.7 -        SDL_PrivateJoystickAxis( joystick, 1, (Sint16)(-pXInputState->Gamepad.sThumbLY) );
     1.8 +        SDL_PrivateJoystickAxis( joystick, 1, (Sint16)(-SDL_max(-32767, pXInputState->Gamepad.sThumbLY)) );
     1.9          SDL_PrivateJoystickAxis( joystick, 2, (Sint16)pXInputState->Gamepad.sThumbRX );
    1.10 -        SDL_PrivateJoystickAxis( joystick, 3, (Sint16)(-pXInputState->Gamepad.sThumbRY) );
    1.11 +        SDL_PrivateJoystickAxis( joystick, 3, (Sint16)(-SDL_max(-32767, pXInputState->Gamepad.sThumbRY)) );
    1.12          SDL_PrivateJoystickAxis( joystick, 4, (Sint16)(((int)pXInputState->Gamepad.bLeftTrigger*65535/255) - 32768));
    1.13          SDL_PrivateJoystickAxis( joystick, 5, (Sint16)(((int)pXInputState->Gamepad.bRightTrigger*65535/255) - 32768));
    1.14