Browse Source

SDL2 GameController: Map axes to engine explicitly

pull/2/head
Gleb Mazovetskiy 4 years ago committed by Alibek Omarov
parent
commit
4daa7c23a0
  1. 18
      engine/client/in_joy.c
  2. 12
      engine/client/input.h
  3. 23
      engine/platform/sdl/events.c

18
engine/client/in_joy.c

@ -24,17 +24,6 @@ GNU General Public License for more details. @@ -24,17 +24,6 @@ GNU General Public License for more details.
#define SHRT_MAX 0x7FFF
#endif
typedef enum engineAxis_e
{
JOY_AXIS_SIDE = 0,
JOY_AXIS_FWD,
JOY_AXIS_PITCH,
JOY_AXIS_YAW,
JOY_AXIS_RT,
JOY_AXIS_LT,
JOY_AXIS_NULL
} engineAxis_t;
#define MAX_AXES JOY_AXIS_NULL
// index - axis num come from event
@ -246,8 +235,6 @@ Axis events @@ -246,8 +235,6 @@ Axis events
*/
void Joy_AxisMotionEvent( byte axis, short value )
{
byte engineAxis;
if( !joy_found->value )
return;
@ -257,8 +244,11 @@ void Joy_AxisMotionEvent( byte axis, short value ) @@ -257,8 +244,11 @@ void Joy_AxisMotionEvent( byte axis, short value )
return;
}
engineAxis = joyaxesmap[axis]; // convert to engine inner axis control
return Joy_KnownAxisMotionEvent( joyaxesmap[axis], value );
}
void Joy_KnownAxisMotionEvent( engineAxis_t engineAxis, short value )
{
if( engineAxis == JOY_AXIS_NULL )
return;

12
engine/client/input.h

@ -93,9 +93,21 @@ enum @@ -93,9 +93,21 @@ enum
JOY_HAT_LEFTDOWN = JOY_HAT_LEFT | JOY_HAT_DOWN
};
typedef enum engineAxis_e
{
JOY_AXIS_SIDE = 0,
JOY_AXIS_FWD,
JOY_AXIS_PITCH,
JOY_AXIS_YAW,
JOY_AXIS_RT,
JOY_AXIS_LT,
JOY_AXIS_NULL
} engineAxis_t;
qboolean Joy_IsActive( void );
void Joy_HatMotionEvent( byte hat, byte value );
void Joy_AxisMotionEvent( byte axis, short value );
void Joy_KnownAxisMotionEvent( engineAxis_t engineAxis, short value );
void Joy_BallMotionEvent( byte ball, short xrel, short yrel );
void Joy_ButtonEvent( byte button, byte down );
void Joy_AddEvent( void );

23
engine/platform/sdl/events.c

@ -532,18 +532,21 @@ static void SDLash_EventFilter( SDL_Event *event ) @@ -532,18 +532,21 @@ static void SDLash_EventFilter( SDL_Event *event )
/* GameController API */
case SDL_CONTROLLERAXISMOTION:
if( event->caxis.axis == (Uint8)SDL_CONTROLLER_AXIS_INVALID )
break;
{
// Swap axis to follow default axis binding:
// LeftX, LeftY, RightX, RightY, TriggerRight, TriggerLeft
if( event->caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT )
event->caxis.axis = SDL_CONTROLLER_AXIS_TRIGGERRIGHT;
else if( event->caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT )
event->caxis.axis = SDL_CONTROLLER_AXIS_TRIGGERLEFT;
Joy_AxisMotionEvent( event->caxis.axis, event->caxis.value );
static int sdlControllerAxisToEngine[] = {
JOY_AXIS_SIDE, // SDL_CONTROLLER_AXIS_LEFTX,
JOY_AXIS_FWD, // SDL_CONTROLLER_AXIS_LEFTY,
JOY_AXIS_PITCH, // SDL_CONTROLLER_AXIS_RIGHTX,
JOY_AXIS_YAW, // SDL_CONTROLLER_AXIS_RIGHTY,
JOY_AXIS_LT, // SDL_CONTROLLER_AXIS_TRIGGERLEFT,
JOY_AXIS_RT, // SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
};
if( Joy_IsActive() && event->caxis.axis != (Uint8)SDL_CONTROLLER_AXIS_INVALID )
Joy_KnownAxisMotionEvent( sdlControllerAxisToEngine[event->caxis.axis], event->caxis.value );
break;
}
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
@ -558,7 +561,7 @@ static void SDLash_EventFilter( SDL_Event *event ) @@ -558,7 +561,7 @@ static void SDLash_EventFilter( SDL_Event *event )
};
// TODO: Use joyinput funcs, for future multiple gamepads support
if( Joy_IsActive() && event->cbutton.button != SDL_CONTROLLER_BUTTON_INVALID )
if( Joy_IsActive() && event->cbutton.button != (Uint8)SDL_CONTROLLER_BUTTON_INVALID )
Key_Event( sdlControllerButtonToEngine[event->cbutton.button], event->cbutton.state );
break;
}

Loading…
Cancel
Save