Allow using SDL2 joystick on Windows

This commit is contained in:
FreeSlave 2024-04-08 02:44:20 +03:00
parent 97c7cda257
commit 372fed374d
3 changed files with 124 additions and 73 deletions

View File

@ -236,10 +236,9 @@ DWORD dwAxisFlags[JOY_MAX_AXES] =
DWORD dwAxisMap[ JOY_MAX_AXES ]; DWORD dwAxisMap[ JOY_MAX_AXES ];
DWORD dwControlMap[ JOY_MAX_AXES ]; DWORD dwControlMap[ JOY_MAX_AXES ];
#if !XASH_WIN32
int pdwRawValue[ JOY_MAX_AXES ]; int pdwRawValue[ JOY_MAX_AXES ];
#else #if XASH_WIN32
PDWORD pdwRawValue[ JOY_MAX_AXES ]; PDWORD pdwRawValue_windows[ JOY_MAX_AXES ];
#endif #endif
DWORD joy_oldbuttonstate, joy_oldpovstate; DWORD joy_oldbuttonstate, joy_oldpovstate;
@ -990,7 +989,7 @@ void GoldSourceInput::IN_ClearStates (void)
IN_StartupJoystick IN_StartupJoystick
=============== ===============
*/ */
void IN_StartupJoystick (void) void GoldSourceInput::IN_StartupJoystick (void)
{ {
// abort startup if user requests no joystick // abort startup if user requests no joystick
if ( gEngfuncs.CheckParm ("-nojoy", NULL ) ) if ( gEngfuncs.CheckParm ("-nojoy", NULL ) )
@ -998,39 +997,42 @@ void IN_StartupJoystick (void)
// assume no joystick // assume no joystick
joy_avail = 0; joy_avail = 0;
#if !XASH_WIN32 if (UseSDL2Joystick())
int nJoysticks = safe_pfnSDL_NumJoysticks();
if ( nJoysticks > 0 )
{ {
for ( int i = 0; i < nJoysticks; i++ ) int nJoysticks = safe_pfnSDL_NumJoysticks();
if ( nJoysticks > 0 )
{ {
if ( safe_pfnSDL_IsGameController( i ) ) for ( int i = 0; i < nJoysticks; i++ )
{ {
s_pJoystick = safe_pfnSDL_GameControllerOpen( i ); if ( safe_pfnSDL_IsGameController( i ) )
if ( s_pJoystick )
{ {
//save the joystick's number of buttons and POV status s_pJoystick = safe_pfnSDL_GameControllerOpen( i );
joy_numbuttons = SDL_CONTROLLER_BUTTON_MAX; if ( s_pJoystick )
joy_haspov = 0; {
//save the joystick's number of buttons and POV status
joy_numbuttons = SDL_CONTROLLER_BUTTON_MAX;
joy_haspov = 0;
// old button and POV states default to no buttons pressed // old button and POV states default to no buttons pressed
joy_oldbuttonstate = joy_oldpovstate = 0; joy_oldbuttonstate = joy_oldpovstate = 0;
// mark the joystick as available and advanced initialization not completed // mark the joystick as available and advanced initialization not completed
// this is needed as cvars are not available during initialization // this is needed as cvars are not available during initialization
gEngfuncs.Con_Printf ("joystick found\n\n", safe_pfnSDL_GameControllerName(s_pJoystick)); gEngfuncs.Con_Printf ("joystick found\n\n", safe_pfnSDL_GameControllerName(s_pJoystick));
joy_avail = 1; joy_avail = 1;
joy_advancedinit = 0; joy_advancedinit = 0;
break; break;
}
} }
} }
} }
else
{
gEngfuncs.Con_DPrintf ("joystick not found -- driver not present\n\n");
}
return;
} }
else #if XASH_WIN32
{
gEngfuncs.Con_DPrintf ("joystick not found -- driver not present\n\n");
}
#elif XASH_WIN32
int numdevs; int numdevs;
JOYCAPS jc; JOYCAPS jc;
MMRESULT mmr; MMRESULT mmr;
@ -1085,7 +1087,6 @@ void IN_StartupJoystick (void)
#endif #endif
} }
#if !XASH_WIN32
int RawValuePointer (int axis) int RawValuePointer (int axis)
{ {
switch (axis) switch (axis)
@ -1102,8 +1103,8 @@ int RawValuePointer (int axis)
} }
} }
#else #if XASH_WIN32
PDWORD RawValuePointer (int axis) PDWORD RawValuePointer_windows(int axis)
{ {
switch (axis) switch (axis)
{ {
@ -1130,7 +1131,12 @@ PDWORD RawValuePointer (int axis)
Joy_AdvancedUpdate_f Joy_AdvancedUpdate_f
=========== ===========
*/ */
void Joy_AdvancedUpdate_f (void) void Joy_AdvancedUpdate_f(void)
{
CurrentMouseInput()->Joy_AdvancedUpdate();
}
void GoldSourceInput::Joy_AdvancedUpdate(void)
{ {
// called once by IN_ReadJoystick and by user whenever an update is needed // called once by IN_ReadJoystick and by user whenever an update is needed
@ -1143,7 +1149,16 @@ void Joy_AdvancedUpdate_f (void)
{ {
dwAxisMap[i] = AxisNada; dwAxisMap[i] = AxisNada;
dwControlMap[i] = JOY_ABSOLUTE_AXIS; dwControlMap[i] = JOY_ABSOLUTE_AXIS;
pdwRawValue[i] = RawValuePointer(i); if (UseSDL2Joystick())
{
pdwRawValue[i] = RawValuePointer(i);
}
#if XASH_WIN32
else
{
pdwRawValue_windows[i] = RawValuePointer_windows(i);
}
#endif
} }
if( joy_advanced->value == 0.0) if( joy_advanced->value == 0.0)
@ -1186,18 +1201,25 @@ void Joy_AdvancedUpdate_f (void)
} }
#if XASH_WIN32 #if XASH_WIN32
// compute the axes to collect from DirectInput if (!UseSDL2Joystick())
joy_flags = JOY_RETURNCENTERED | JOY_RETURNBUTTONS | JOY_RETURNPOV;
for (i = 0; i < JOY_MAX_AXES; i++)
{ {
if (dwAxisMap[i] != AxisNada) // compute the axes to collect from DirectInput
joy_flags = JOY_RETURNCENTERED | JOY_RETURNBUTTONS | JOY_RETURNPOV;
for (i = 0; i < JOY_MAX_AXES; i++)
{ {
joy_flags |= dwAxisFlags[i]; if (dwAxisMap[i] != AxisNada)
{
joy_flags |= dwAxisFlags[i];
}
} }
} }
#endif #endif
} }
bool GoldSourceInput::UseSDL2Joystick()
{
return sdl2Lib != NULL;
}
/* /*
=========== ===========
@ -1217,22 +1239,27 @@ void GoldSourceInput::IN_Commands (void)
// loop through the joystick buttons // loop through the joystick buttons
// key a joystick event or auxillary event for higher number buttons for each state change // key a joystick event or auxillary event for higher number buttons for each state change
#if !XASH_WIN32 if (UseSDL2Joystick())
buttonstate = 0; {
for ( i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++ ) buttonstate = 0;
{ for ( i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++ )
if ( safe_pfnSDL_GameControllerGetButton( s_pJoystick, (SDL_GameControllerButton)i ) ) {
{ if ( safe_pfnSDL_GameControllerGetButton( s_pJoystick, (SDL_GameControllerButton)i ) )
buttonstate |= 1<<i; {
} buttonstate |= 1<<i;
} }
}
for (i = 0; i < JOY_MAX_AXES; i++) for (i = 0; i < JOY_MAX_AXES; i++)
{ {
pdwRawValue[i] = RawValuePointer(i); pdwRawValue[i] = RawValuePointer(i);
} }
#else }
buttonstate = ji.dwButtons; #if XASH_WIN32
else
{
buttonstate = ji.dwButtons;
}
#endif #endif
for (i=0 ; i < (int)joy_numbuttons ; i++) for (i=0 ; i < (int)joy_numbuttons ; i++)
@ -1258,16 +1285,19 @@ void GoldSourceInput::IN_Commands (void)
// direction to another without going through the center position // direction to another without going through the center position
povstate = 0; povstate = 0;
#if XASH_WIN32 #if XASH_WIN32
if(ji.dwPOV != JOY_POVCENTERED) if (!UseSDL2Joystick())
{ {
if (ji.dwPOV == JOY_POVFORWARD) if(ji.dwPOV != JOY_POVCENTERED)
povstate |= 0x01; {
if (ji.dwPOV == JOY_POVRIGHT) if (ji.dwPOV == JOY_POVFORWARD)
povstate |= 0x02; povstate |= 0x01;
if (ji.dwPOV == JOY_POVBACKWARD) if (ji.dwPOV == JOY_POVRIGHT)
povstate |= 0x04; povstate |= 0x02;
if (ji.dwPOV == JOY_POVLEFT) if (ji.dwPOV == JOY_POVBACKWARD)
povstate |= 0x08; povstate |= 0x04;
if (ji.dwPOV == JOY_POVLEFT)
povstate |= 0x08;
}
} }
#endif #endif
// determine which bits have changed and key an auxillary event for each change // determine which bits have changed and key an auxillary event for each change
@ -1293,12 +1323,14 @@ void GoldSourceInput::IN_Commands (void)
IN_ReadJoystick IN_ReadJoystick
=============== ===============
*/ */
int IN_ReadJoystick (void) int GoldSourceInput::IN_ReadJoystick (void)
{ {
#if !XASH_WIN32 if (UseSDL2Joystick())
safe_pfnSDL_JoystickUpdate(); {
return 1; safe_pfnSDL_JoystickUpdate();
#elif XASH_WIN32 return 1;
}
#if XASH_WIN32
memset (&ji, 0, sizeof(ji)); memset (&ji, 0, sizeof(ji));
ji.dwSize = sizeof(ji); ji.dwSize = sizeof(ji);
ji.dwFlags = joy_flags; ji.dwFlags = joy_flags;
@ -1334,7 +1366,7 @@ int IN_ReadJoystick (void)
IN_JoyMove IN_JoyMove
=========== ===========
*/ */
void IN_JoyMove ( float frametime, usercmd_t *cmd ) void GoldSourceInput::IN_JoyMove ( float frametime, usercmd_t *cmd )
{ {
float speed, aspeed; float speed, aspeed;
float fAxisValue, fTemp; float fAxisValue, fTemp;
@ -1348,7 +1380,7 @@ void IN_JoyMove ( float frametime, usercmd_t *cmd )
// this is needed as cvars are not available at initialization time // this is needed as cvars are not available at initialization time
if( joy_advancedinit != 1 ) if( joy_advancedinit != 1 )
{ {
Joy_AdvancedUpdate_f(); Joy_AdvancedUpdate();
joy_advancedinit = 1; joy_advancedinit = 1;
} }
@ -1375,11 +1407,16 @@ void IN_JoyMove ( float frametime, usercmd_t *cmd )
for (i = 0; i < JOY_MAX_AXES; i++) for (i = 0; i < JOY_MAX_AXES; i++)
{ {
// get the floating point zero-centered, potentially-inverted data for the current axis // get the floating point zero-centered, potentially-inverted data for the current axis
#if !XASH_WIN32 if (UseSDL2Joystick())
fAxisValue = (float)pdwRawValue[i]; {
#elif XASH_WIN32 fAxisValue = (float)pdwRawValue[i];
fAxisValue = (float) *pdwRawValue[i]; }
fAxisValue -= 32768.0; #if XASH_WIN32
else
{
fAxisValue = (float) *pdwRawValue_windows[i];
fAxisValue -= 32768.0;
}
#endif #endif
if (joy_wwhack2->value != 0.0) if (joy_wwhack2->value != 0.0)

View File

@ -86,3 +86,8 @@ void IN_ResetMouse()
{ {
currentInput->IN_ResetMouse(); currentInput->IN_ResetMouse();
} }
AbstractInput* CurrentMouseInput()
{
return currentInput;
}

View File

@ -20,6 +20,7 @@ public:
virtual void IN_Shutdown( void ) = 0; virtual void IN_Shutdown( void ) = 0;
virtual void IN_Init( void ) = 0; virtual void IN_Init( void ) = 0;
virtual void IN_ResetMouse( void ) = 0; virtual void IN_ResetMouse( void ) = 0;
virtual void Joy_AdvancedUpdate( void ) = 0;
}; };
class FWGSInput : public AbstractInput class FWGSInput : public AbstractInput
@ -37,6 +38,7 @@ public:
virtual void IN_Shutdown( void ); virtual void IN_Shutdown( void );
virtual void IN_Init( void ); virtual void IN_Init( void );
virtual void IN_ResetMouse( void ) {} virtual void IN_ResetMouse( void ) {}
virtual void Joy_AdvancedUpdate( void ) {}
protected: protected:
float ac_forwardmove; float ac_forwardmove;
@ -79,11 +81,16 @@ public:
virtual void IN_Shutdown( void ); virtual void IN_Shutdown( void );
virtual void IN_Init( void ); virtual void IN_Init( void );
virtual void IN_ResetMouse( void ); virtual void IN_ResetMouse( void );
virtual void Joy_AdvancedUpdate( void );
protected: protected:
void IN_GetMouseDelta( int *pOutX, int *pOutY); void IN_GetMouseDelta( int *pOutX, int *pOutY);
void IN_MouseMove ( float frametime, usercmd_t *cmd); void IN_MouseMove ( float frametime, usercmd_t *cmd);
void IN_StartupMouse (void); void IN_StartupMouse (void);
void IN_StartupJoystick (void);
int IN_ReadJoystick (void);
void IN_JoyMove ( float frametime, usercmd_t *cmd );
bool UseSDL2Joystick();
int mouse_buttons; int mouse_buttons;
int mouse_oldbuttonstate; int mouse_oldbuttonstate;
@ -94,4 +101,6 @@ protected:
}; };
#endif #endif
AbstractInput* CurrentMouseInput();
#endif #endif