Simplify gamepad axis code

Yes I intentionally spelled it JOYAXISES to not conflict with the old define
This commit is contained in:
GenericHeroGuy 2025-03-11 20:38:37 +01:00
parent 4d65f09eb7
commit f547af9992
4 changed files with 17 additions and 33 deletions

View file

@ -84,9 +84,9 @@ INT32 G_GetDevicePlayer(INT32 deviceID)
// converts ev_joystick axis to its corresponding key
INT32 G_AxisToKey(event_t *ev)
{
return KEY_AXIS1 + (ev->data1 >= JOYANALOGS*2
? ev->data1 + JOYANALOGS*2
: ev->data1*2 + (ev->data2 >= 0));
return KEY_AXIS1 + (ev->data1 >= JOYANALOGS
? ev->data1 + JOYANALOGS // triggers
: ev->data1*2 + (ev->data2 >= 0)); // analog sticks
}
//
@ -189,7 +189,7 @@ void G_MapEventsToControls(event_t *ev)
break;
}
if (ev->data1 >= JOYAXISSETS*2)
if (ev->data1 >= JOYAXISES)
{
#ifdef PARANOIA
CONS_Debug(DBG_GAMELOGIC, "Bad joystick axis event %d\n", ev->data1);
@ -197,36 +197,20 @@ void G_MapEventsToControls(event_t *ev)
break;
}
i = ev->data1;
if (G_GetDevicePlayer(ev->device) == 0)
{
if (CON_Ready() || chat_on)
break;
}
if (i >= 2 * JOYANALOGS)
i = G_AxisToKey(ev);
if (ev->data1 < JOYANALOGS)
{
// The trigger axes are handled specially.
i -= 2 * JOYANALOGS;
gamekeydown[ev->device][KEY_AXIS1 + (JOYANALOGS * 4) + i] = max(0, ev->data2);
}
else
{
i *= 2;
if (ev->data2 < 0)
{
// Left
gamekeydown[ev->device][KEY_AXIS1 + i] = abs(ev->data2);
gamekeydown[ev->device][KEY_AXIS1 + i + 1] = 0;
}
else
{
// Right
gamekeydown[ev->device][KEY_AXIS1 + i] = 0;
gamekeydown[ev->device][KEY_AXIS1 + i + 1] = abs(ev->data2);
}
// analog stick axes have a negative and positive keydown
i -= ev->data2 >= 0;
gamekeydown[ev->device][i++] = max(0, -ev->data2);
}
gamekeydown[ev->device][i] = max(0, ev->data2);
break;

View file

@ -29,10 +29,10 @@ extern "C" {
#define MOUSEBUTTONS 8
#define JOYBUTTONS 21 // 21 buttons, to match SDL_GameControllerButton
#define JOYANALOGS 2 // 2 sets of analog stick axes, with positive and negative each
#define JOYTRIGGERS 1 // 1 set of trigger axes, positive only
#define JOYAXISSETS (JOYANALOGS + JOYTRIGGERS)
#define JOYAXES ((4 * JOYANALOGS) + (2 * JOYTRIGGERS))
#define JOYANALOGS 4 // 4 analog stick axes (2 sticks * 2 axes)
#define JOYTRIGGERS 2 // 2 trigger axes, positive only
#define JOYAXISES (JOYANALOGS + JOYTRIGGERS)
#define JOYAXISKEYS ((2 * JOYANALOGS) + JOYTRIGGERS)
#define MAXINPUTMAPPING 4
@ -44,7 +44,7 @@ typedef enum
KEY_JOY1 = NUMKEYS,
KEY_HAT1 = KEY_JOY1 + 11, // macro for SDL_CONTROLLER_BUTTON_DPAD_UP
KEY_AXIS1 = KEY_JOY1 + JOYBUTTONS,
JOYINPUTEND = KEY_AXIS1 + JOYAXES,
JOYINPUTEND = KEY_AXIS1 + JOYAXISKEYS,
KEY_MOUSE1 = JOYINPUTEND,
KEY_MOUSEMOVE = KEY_MOUSE1 + MOUSEBUTTONS,

View file

@ -2609,7 +2609,7 @@ boolean M_Responder(event_t *ev)
{
if (ev->type == ev_joystick && deviceplayer == 0)
{
static INT32 lastjoy[JOYAXISSETS*2] = {0};
static INT32 lastjoy[JOYAXISES] = {0};
if (G_AxisInDeadzone(deviceplayer, ev))
{

View file

@ -1186,7 +1186,7 @@ void I_ShutdownJoystick(UINT8 index)
// reset joystick position
event.type = ev_joystick;
for (i = 0; i < JOYAXISSETS*2; i++)
for (i = 0; i < JOYAXISES; i++)
{
event.data1 = i;
D_PostEvent(&event);