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,7 +997,8 @@ 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(); int nJoysticks = safe_pfnSDL_NumJoysticks();
if ( nJoysticks > 0 ) if ( nJoysticks > 0 )
{ {
@ -1030,7 +1030,9 @@ void IN_StartupJoystick (void)
{ {
gEngfuncs.Con_DPrintf ("joystick not found -- driver not present\n\n"); gEngfuncs.Con_DPrintf ("joystick not found -- driver not present\n\n");
} }
#elif XASH_WIN32 return;
}
#if 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,8 +1149,17 @@ void Joy_AdvancedUpdate_f (void)
{ {
dwAxisMap[i] = AxisNada; dwAxisMap[i] = AxisNada;
dwControlMap[i] = JOY_ABSOLUTE_AXIS; dwControlMap[i] = JOY_ABSOLUTE_AXIS;
if (UseSDL2Joystick())
{
pdwRawValue[i] = RawValuePointer(i); 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,6 +1201,8 @@ void Joy_AdvancedUpdate_f (void)
} }
#if XASH_WIN32 #if XASH_WIN32
if (!UseSDL2Joystick())
{
// compute the axes to collect from DirectInput // compute the axes to collect from DirectInput
joy_flags = JOY_RETURNCENTERED | JOY_RETURNBUTTONS | JOY_RETURNPOV; joy_flags = JOY_RETURNCENTERED | JOY_RETURNBUTTONS | JOY_RETURNPOV;
for (i = 0; i < JOY_MAX_AXES; i++) for (i = 0; i < JOY_MAX_AXES; i++)
@ -1195,9 +1212,14 @@ void Joy_AdvancedUpdate_f (void)
joy_flags |= dwAxisFlags[i]; joy_flags |= dwAxisFlags[i];
} }
} }
}
#endif #endif
} }
bool GoldSourceInput::UseSDL2Joystick()
{
return sdl2Lib != NULL;
}
/* /*
=========== ===========
@ -1217,7 +1239,8 @@ 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; buttonstate = 0;
for ( i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++ ) for ( i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++ )
{ {
@ -1231,8 +1254,12 @@ void GoldSourceInput::IN_Commands (void)
{ {
pdwRawValue[i] = RawValuePointer(i); pdwRawValue[i] = RawValuePointer(i);
} }
#else }
#if XASH_WIN32
else
{
buttonstate = ji.dwButtons; buttonstate = ji.dwButtons;
}
#endif #endif
for (i=0 ; i < (int)joy_numbuttons ; i++) for (i=0 ; i < (int)joy_numbuttons ; i++)
@ -1258,6 +1285,8 @@ 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 (!UseSDL2Joystick())
{
if(ji.dwPOV != JOY_POVCENTERED) if(ji.dwPOV != JOY_POVCENTERED)
{ {
if (ji.dwPOV == JOY_POVFORWARD) if (ji.dwPOV == JOY_POVFORWARD)
@ -1269,6 +1298,7 @@ void GoldSourceInput::IN_Commands (void)
if (ji.dwPOV == JOY_POVLEFT) if (ji.dwPOV == JOY_POVLEFT)
povstate |= 0x08; 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
for (i=0 ; i < 4 ; i++) for (i=0 ; i < 4 ; i++)
@ -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(); safe_pfnSDL_JoystickUpdate();
return 1; return 1;
#elif XASH_WIN32 }
#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]; fAxisValue = (float)pdwRawValue[i];
#elif XASH_WIN32 }
fAxisValue = (float) *pdwRawValue[i]; #if XASH_WIN32
else
{
fAxisValue = (float) *pdwRawValue_windows[i];
fAxisValue -= 32768.0; 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