PDA

View Full Version : SDL_JoystickGetAxis doesn't return a correct value



0xABC123
November 26th, 2009, 05:45 PM
I have an old xbox gamepad. It has 2 joysticks (2 axis per joystick), a d-pad, 2 triggers and some buttons.

For some odd reason, SDL_JoystickGetAxis() always returns positive value, no matter if the real value is negative or positive.

jscalibrator shows correct values, negative when the joystick points at left and positive when it points at right, so the problem is not in my xbox gamepad.

What am I doing wrong?

0xABC123
November 26th, 2009, 07:46 PM
The problem seems to be here:


static int get_joystick_axis( int js, int axis )
{
Sint16 raw;

ASSERT_JOYSTICK_NUM( js );
raw = SDL_JoystickGetAxis( joysticks[js], axis );

printf( "%d\n", raw ); // raw is correctly something between -32767 and +32767
int tmp = (signed int) raw; // conversion fails and tmp is something between 0 and 65404

return tmp;
}
int is 4 bytes and Sint16 is 2 bytes. Why the conversion messes it up?

EDIT: Solution:

static int get_joystick_axis( int js, int axis )
{
Sint16 raw;

ASSERT_JOYSTICK_NUM( js );
raw = SDL_JoystickGetAxis( joysticks[js], axis );

int tmp = (int) raw;

if ( tmp > 32767 )
{
tmp -= 32767;
tmp = ( 32767 - tmp ) * -1;
}

return tmp;
} This works but how/why does it work?