From 372fed374d76f0e5376755b2ecf97e49b4da21b5 Mon Sep 17 00:00:00 2001 From: FreeSlave Date: Mon, 8 Apr 2024 02:44:20 +0300 Subject: [PATCH] Allow using SDL2 joystick on Windows --- cl_dll/input_goldsource.cpp | 183 ++++++++++++++++++++++-------------- cl_dll/input_mouse.cpp | 5 + cl_dll/input_mouse.h | 9 ++ 3 files changed, 124 insertions(+), 73 deletions(-) diff --git a/cl_dll/input_goldsource.cpp b/cl_dll/input_goldsource.cpp index 4efe2779..a28ff600 100644 --- a/cl_dll/input_goldsource.cpp +++ b/cl_dll/input_goldsource.cpp @@ -236,10 +236,9 @@ DWORD dwAxisFlags[JOY_MAX_AXES] = DWORD dwAxisMap[ JOY_MAX_AXES ]; DWORD dwControlMap[ JOY_MAX_AXES ]; -#if !XASH_WIN32 int pdwRawValue[ JOY_MAX_AXES ]; -#else -PDWORD pdwRawValue[ JOY_MAX_AXES ]; +#if XASH_WIN32 +PDWORD pdwRawValue_windows[ JOY_MAX_AXES ]; #endif DWORD joy_oldbuttonstate, joy_oldpovstate; @@ -990,7 +989,7 @@ void GoldSourceInput::IN_ClearStates (void) IN_StartupJoystick =============== */ -void IN_StartupJoystick (void) +void GoldSourceInput::IN_StartupJoystick (void) { // abort startup if user requests no joystick if ( gEngfuncs.CheckParm ("-nojoy", NULL ) ) @@ -998,39 +997,42 @@ void IN_StartupJoystick (void) // assume no joystick joy_avail = 0; -#if !XASH_WIN32 - int nJoysticks = safe_pfnSDL_NumJoysticks(); - if ( nJoysticks > 0 ) + if (UseSDL2Joystick()) { - 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 ( s_pJoystick ) + if ( safe_pfnSDL_IsGameController( i ) ) { - //save the joystick's number of buttons and POV status - joy_numbuttons = SDL_CONTROLLER_BUTTON_MAX; - joy_haspov = 0; + s_pJoystick = safe_pfnSDL_GameControllerOpen( i ); + if ( s_pJoystick ) + { + //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 - joy_oldbuttonstate = joy_oldpovstate = 0; + // old button and POV states default to no buttons pressed + joy_oldbuttonstate = joy_oldpovstate = 0; - // mark the joystick as available and advanced initialization not completed - // this is needed as cvars are not available during initialization - gEngfuncs.Con_Printf ("joystick found\n\n", safe_pfnSDL_GameControllerName(s_pJoystick)); - joy_avail = 1; - joy_advancedinit = 0; - break; + // mark the joystick as available and advanced initialization not completed + // this is needed as cvars are not available during initialization + gEngfuncs.Con_Printf ("joystick found\n\n", safe_pfnSDL_GameControllerName(s_pJoystick)); + joy_avail = 1; + joy_advancedinit = 0; + break; + } } } } + else + { + gEngfuncs.Con_DPrintf ("joystick not found -- driver not present\n\n"); + } + return; } - else - { - gEngfuncs.Con_DPrintf ("joystick not found -- driver not present\n\n"); - } -#elif XASH_WIN32 +#if XASH_WIN32 int numdevs; JOYCAPS jc; MMRESULT mmr; @@ -1085,7 +1087,6 @@ void IN_StartupJoystick (void) #endif } -#if !XASH_WIN32 int RawValuePointer (int axis) { switch (axis) @@ -1102,8 +1103,8 @@ int RawValuePointer (int axis) } } -#else -PDWORD RawValuePointer (int axis) +#if XASH_WIN32 +PDWORD RawValuePointer_windows(int axis) { switch (axis) { @@ -1130,7 +1131,12 @@ PDWORD RawValuePointer (int axis) 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 @@ -1143,7 +1149,16 @@ void Joy_AdvancedUpdate_f (void) { dwAxisMap[i] = AxisNada; 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) @@ -1186,18 +1201,25 @@ void Joy_AdvancedUpdate_f (void) } #if XASH_WIN32 - // compute the axes to collect from DirectInput - joy_flags = JOY_RETURNCENTERED | JOY_RETURNBUTTONS | JOY_RETURNPOV; - for (i = 0; i < JOY_MAX_AXES; i++) + if (!UseSDL2Joystick()) { - 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 } +bool GoldSourceInput::UseSDL2Joystick() +{ + return sdl2Lib != NULL; +} /* =========== @@ -1217,22 +1239,27 @@ void GoldSourceInput::IN_Commands (void) // loop through the joystick buttons // key a joystick event or auxillary event for higher number buttons for each state change -#if !XASH_WIN32 - buttonstate = 0; - for ( i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++ ) - { - if ( safe_pfnSDL_GameControllerGetButton( s_pJoystick, (SDL_GameControllerButton)i ) ) - { - buttonstate |= 1<value != 0.0) diff --git a/cl_dll/input_mouse.cpp b/cl_dll/input_mouse.cpp index 38d2c7dd..e4f6c305 100644 --- a/cl_dll/input_mouse.cpp +++ b/cl_dll/input_mouse.cpp @@ -86,3 +86,8 @@ void IN_ResetMouse() { currentInput->IN_ResetMouse(); } + +AbstractInput* CurrentMouseInput() +{ + return currentInput; +} diff --git a/cl_dll/input_mouse.h b/cl_dll/input_mouse.h index aecb5958..186bf5ba 100644 --- a/cl_dll/input_mouse.h +++ b/cl_dll/input_mouse.h @@ -20,6 +20,7 @@ public: virtual void IN_Shutdown( void ) = 0; virtual void IN_Init( void ) = 0; virtual void IN_ResetMouse( void ) = 0; + virtual void Joy_AdvancedUpdate( void ) = 0; }; class FWGSInput : public AbstractInput @@ -37,6 +38,7 @@ public: virtual void IN_Shutdown( void ); virtual void IN_Init( void ); virtual void IN_ResetMouse( void ) {} + virtual void Joy_AdvancedUpdate( void ) {} protected: float ac_forwardmove; @@ -79,11 +81,16 @@ public: virtual void IN_Shutdown( void ); virtual void IN_Init( void ); virtual void IN_ResetMouse( void ); + virtual void Joy_AdvancedUpdate( void ); protected: void IN_GetMouseDelta( int *pOutX, int *pOutY); void IN_MouseMove ( float frametime, usercmd_t *cmd); 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_oldbuttonstate; @@ -94,4 +101,6 @@ protected: }; #endif +AbstractInput* CurrentMouseInput(); + #endif