engine: fix building with SDL older than 2.0.4

This commit is contained in:
Alibek Omarov 2021-03-01 21:48:31 +03:00
parent 14008703f6
commit ca84a1a10e
3 changed files with 29 additions and 11 deletions

View File

@ -90,6 +90,21 @@ GNU General Public License for more details.
#define SDL_GetScancodeName( x ) "unknown"
#endif
static qboolean SDLash_IsInstanceIDAGameController( SDL_JoystickID joyId )
{
#if !SDL_VERSION_ATLEAST( 2, 0, 4 )
// HACKHACK: if we're not initialized g_joy, then we're probably using gamecontroller api
// so return true
if( !g_joy )
return true;
return false;
#else
if( SDL_GameControllerFromInstanceID( joyId ) )
return true;
return false;
#endif
}
/*
=============
SDLash_KeyEvent
@ -432,23 +447,23 @@ static void SDLash_EventFilter( SDL_Event *event )
/* Joystick events */
case SDL_JOYAXISMOTION:
if ( SDL_GameControllerFromInstanceID( event->jaxis.which ) == NULL )
if ( !SDLash_IsInstanceIDAGameController( event->jaxis.which ))
Joy_AxisMotionEvent( event->jaxis.axis, event->jaxis.value );
break;
case SDL_JOYBALLMOTION:
if ( SDL_GameControllerFromInstanceID( event->jball.which ) == NULL )
if ( !SDLash_IsInstanceIDAGameController( event->jball.which ))
Joy_BallMotionEvent( event->jball.ball, event->jball.xrel, event->jball.yrel );
break;
case SDL_JOYHATMOTION:
if ( SDL_GameControllerFromInstanceID( event->jhat.which ) == NULL )
if ( !SDLash_IsInstanceIDAGameController( event->jhat.which ))
Joy_HatMotionEvent( event->jhat.hat, event->jhat.value );
break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
if ( SDL_GameControllerFromInstanceID( event->jbutton.which ) == NULL )
if ( !SDLash_IsInstanceIDAGameController( event->jbutton.which ))
Joy_ButtonEvent( event->jbutton.button, event->jbutton.state );
break;

View File

@ -30,5 +30,8 @@ qboolean GL_UpdateContext( void );
qboolean GL_DeleteContext( void );
void VID_SaveWindowSize( int width, int height );
// joystick events
extern SDL_Joystick *g_joy;
#endif // XASH_SDL
#endif // KEYWRAPPER_H

View File

@ -24,7 +24,7 @@ GNU General Public License for more details.
#include "sound.h"
#include "vid_common.h"
static SDL_Joystick *joy;
SDL_Joystick *g_joy = NULL;
#if !SDL_VERSION_ATLEAST( 2, 0, 0 )
#define SDL_WarpMouseInWindow( win, x, y ) SDL_WarpMouse( ( x ), ( y ) )
#endif
@ -143,9 +143,9 @@ static int SDLash_JoyInit_Old( int numjoy )
return 0;
}
if( joy )
if( g_joy )
{
SDL_JoystickClose( joy );
SDL_JoystickClose( g_joy );
}
num = SDL_NumJoysticks();
@ -165,9 +165,9 @@ static int SDLash_JoyInit_Old( int numjoy )
Con_Reportf( "Pass +set joy_index N to command line, where N is number, to select active joystick\n" );
joy = SDL_JoystickOpen( numjoy );
g_joy = SDL_JoystickOpen( numjoy );
if( !joy )
if( !g_joy )
{
Con_Reportf( "Failed to select joystick: %s\n", SDL_GetError( ) );
return 0;
@ -179,8 +179,8 @@ static int SDLash_JoyInit_Old( int numjoy )
"\tHats: %i\n"
"\tButtons: %i\n"
"\tBalls: %i\n",
SDL_JoystickName( joy ), SDL_JoystickNumAxes( joy ), SDL_JoystickNumHats( joy ),
SDL_JoystickNumButtons( joy ), SDL_JoystickNumBalls( joy ) );
SDL_JoystickName( g_joy ), SDL_JoystickNumAxes( g_joy ), SDL_JoystickNumHats( g_joy ),
SDL_JoystickNumButtons( g_joy ), SDL_JoystickNumBalls( g_joy ) );
SDL_GameControllerEventState( SDL_DISABLE );
#endif // SDL_VERSION_ATLEAST( 2, 0, 0 )