Fixed the mapping from raw joystick values to the expected [SDL_JOYSTICK_AXIS_MIN, SDL_JOYSTICK_AXIS_MAX] range. (thanks Tas!)

The original code mapped incorrectly from [min, max] to [-32768, 32512], the upper bound being SDL_JOYSTICK_AXIS_MAX - 255 instead of SDL_JOYSTICK_AXIS_MAX.
This commit is contained in:
Sam Lantinga 2022-10-12 21:44:50 -07:00
parent eea9f638e2
commit fc73386f45
1 changed files with 4 additions and 12 deletions

View File

@ -677,9 +677,7 @@ BSD_JoystickUpdate(SDL_Joystick *joy)
xmin--; xmin--;
xmax++; xmax++;
} }
v = (Sint32) x; v = (((SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN) * ((Sint32)x - xmin) ) / (xmax - xmin)) + SDL_JOYSTICK_AXIS_MIN;
v -= (xmax + xmin + 1) / 2;
v *= 32768 / ((xmax - xmin + 1) / 2);
SDL_PrivateJoystickAxis(joy, 0, v); SDL_PrivateJoystickAxis(joy, 0, v);
} }
if (SDL_abs(y - gameport.y) > 8) { if (SDL_abs(y - gameport.y) > 8) {
@ -694,9 +692,7 @@ BSD_JoystickUpdate(SDL_Joystick *joy)
ymin--; ymin--;
ymax++; ymax++;
} }
v = (Sint32) y; v = (((SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN) * ((Sint32)y - ymin) ) / (ymax - ymin)) + SDL_JOYSTICK_AXIS_MIN;
v -= (ymax + ymin + 1) / 2;
v *= 32768 / ((ymax - ymin + 1) / 2);
SDL_PrivateJoystickAxis(joy, 1, v); SDL_PrivateJoystickAxis(joy, 1, v);
} }
SDL_PrivateJoystickButton(joy, 0, gameport.b1); SDL_PrivateJoystickButton(joy, 0, gameport.b1);
@ -731,11 +727,7 @@ BSD_JoystickUpdate(SDL_Joystick *joy)
naxe = joy->hwdata->axis_map[joyaxe]; naxe = joy->hwdata->axis_map[joyaxe];
/* scaleaxe */ /* scaleaxe */
v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem); v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
v -= (hitem.logical_maximum + v = (((SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN) * (v - hitem.logical_minimum) ) / (hitem.logical_maximum - hitem.logical_minimum)) + SDL_JOYSTICK_AXIS_MIN;
hitem.logical_minimum + 1) / 2;
v *= 32768 /
((hitem.logical_maximum -
hitem.logical_minimum + 1) / 2);
SDL_PrivateJoystickAxis(joy, naxe, v); SDL_PrivateJoystickAxis(joy, naxe, v);
} else if (usage == HUG_HAT_SWITCH) { } else if (usage == HUG_HAT_SWITCH) {
v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem); v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
@ -765,7 +757,7 @@ BSD_JoystickUpdate(SDL_Joystick *joy)
} }
case HUP_BUTTON: case HUP_BUTTON:
v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem); v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
nbutton = HID_USAGE(hitem.usage) - 1; /* SDL buttons are zero-based */ nbutton = HID_USAGE(hitem.usage) - 1; /* SDL buttons are zero-based */
SDL_PrivateJoystickButton(joy, nbutton, v); SDL_PrivateJoystickButton(joy, nbutton, v);
break; break;
default: default: