Browse Source

engine: client: rework mouse input again, do what GoldSrc does, minimize SDL mouse calls

pull/2/head
Alibek Omarov 3 years ago committed by a1batross
parent
commit
d0a39ef492
  1. 8
      engine/client/in_touch.c
  2. 232
      engine/client/input.c
  3. 2
      engine/client/input.h
  4. 7
      engine/platform/sdl/events.c
  5. 2
      engine/platform/sdl/vid_sdl.c

8
engine/client/in_touch.c

@ -503,20 +503,14 @@ void Touch_SetClientOnly( byte state )
touch.move_finger = touch.look_finger = -1; touch.move_finger = touch.look_finger = -1;
touch.forward = touch.side = 0; touch.forward = touch.side = 0;
/// TODO: touch sdl platform
#if 0
if( state ) if( state )
{ {
SDL_SetRelativeMouseMode( SDL_FALSE );
SDL_ShowCursor( true );
IN_DeactivateMouse(); IN_DeactivateMouse();
} }
else else
{ {
SDL_ShowCursor( false ); IN_ActivateMouse();
SDL_GetRelativeMouseState( 0, 0 );
} }
#endif
} }
static void Touch_SetClientOnly_f( void ) static void Touch_SetClientOnly_f( void )

232
engine/client/input.c

@ -50,6 +50,8 @@ convar_t *cl_backspeed;
convar_t *look_filter; convar_t *look_filter;
convar_t *m_rawinput; convar_t *m_rawinput;
static qboolean s_bRawInput, s_bMouseGrab;
/* /*
================ ================
IN_CollectInputDevices IN_CollectInputDevices
@ -129,21 +131,9 @@ void IN_StartupMouse( void )
in_mouseinitialized = true; in_mouseinitialized = true;
} }
static void IN_ActivateCursor( void )
{
if( cls.key_dest == key_menu )
{
#if XASH_SDL
SDL_SetCursor( in_mousecursor );
#endif
}
}
void GAME_EXPORT IN_SetCursor( void *hCursor ) void GAME_EXPORT IN_SetCursor( void *hCursor )
{ {
in_mousecursor = hCursor; // stub
IN_ActivateCursor();
} }
/* /*
@ -192,47 +182,15 @@ void IN_ToggleClientMouse( int newstate, int oldstate )
if( oldstate == key_game ) if( oldstate == key_game )
{ {
if( cls.initialized ) IN_DeactivateMouse();
clgame.dllFuncs.IN_DeactivateMouse();
} }
else if( newstate == key_game ) else if( newstate == key_game )
{ {
// reset mouse pos, so cancel effect in game IN_ActivateMouse();
#if XASH_SDL >= 2
if( CVAR_TO_BOOL( touch_enable ) )
{
SDL_SetRelativeMouseMode( SDL_FALSE );
SDL_SetWindowGrab( host.hWnd, SDL_FALSE );
}
else
#endif // XASH_SDL >= 2
{
Platform_SetMousePos( host.window_center_x, host.window_center_y );
#if XASH_SDL
SDL_SetWindowGrab( host.hWnd, SDL_TRUE );
#if XASH_SDL >= 2
if ( clgame.dllFuncs.pfnLookEvent || ( clgame.client_dll_uses_sdl && CVAR_TO_BOOL( m_rawinput ) ) )
{
SDL_SetRelativeMouseMode( SDL_TRUE );
}
#endif // XASH_SDL >= 2
#endif // XASH_SDL
}
if( cls.initialized )
clgame.dllFuncs.IN_ActivateMouse();
} }
if( ( newstate == key_menu || newstate == key_console || newstate == key_message ) && ( !CL_IsBackgroundMap() || CL_IsBackgroundDemo( ))) if( ( newstate == key_menu || newstate == key_console || newstate == key_message ) && ( !CL_IsBackgroundMap() || CL_IsBackgroundDemo( )))
{ {
#if XASH_SDL
SDL_SetWindowGrab(host.hWnd, SDL_FALSE);
#if XASH_SDL >= 2
if ( clgame.dllFuncs.pfnLookEvent || ( clgame.client_dll_uses_sdl && CVAR_TO_BOOL( m_rawinput ) ) )
{
SDL_SetRelativeMouseMode( SDL_FALSE );
}
#endif // XASH_SDL >= 2
#endif // XASH_SDL
#if XASH_ANDROID #if XASH_ANDROID
Android_ShowMouse( true ); Android_ShowMouse( true );
#endif #endif
@ -251,67 +209,82 @@ void IN_ToggleClientMouse( int newstate, int oldstate )
} }
} }
/* void IN_CheckMouseState( qboolean active )
===========
IN_ActivateMouse
Called when the window gains focus or changes in some way
===========
*/
void IN_ActivateMouse( qboolean force )
{ {
static qboolean oldstate; #if XASH_WIN32
qboolean useRawInput = CVAR_TO_BOOL( m_rawinput ) && clgame.client_dll_uses_sdl;
if( !in_mouseinitialized ) #else
return; qboolean useRawInput = true; // always use SDL code
#endif
if( CL_Active() && host.mouse_visible && !force )
return; // VGUI controls
if( cls.key_dest == key_menu && !Cvar_VariableInteger( "fullscreen" )) if( active && useRawInput && !host.mouse_visible && cls.state == ca_active )
{ {
// check for mouse leave-entering if( !s_bRawInput )
if( !in_mouse_suspended && !UI_MouseInRect( )) {
in_mouse_suspended = true; #if XASH_SDL >= 2
SDL_GetRelativeMouseState( NULL, NULL );
SDL_SetRelativeMouseMode( SDL_TRUE );
#endif
if( oldstate != in_mouse_suspended ) // Con_Printf( "Enable relative mode\n" );
s_bRawInput = true;
}
}
else
{
if( s_bRawInput )
{ {
if( in_mouse_suspended ) #if XASH_SDL >= 2
{ SDL_GetRelativeMouseState( NULL, NULL );
#if XASH_SDL SDL_SetRelativeMouseMode( SDL_FALSE );
/// TODO: Platform_ShowCursor
if( !touch_emulate )
SDL_ShowCursor( SDL_FALSE );
#endif #endif
UI_ShowCursor( false ); // Con_Printf( "Disable relative mode\n" );
} s_bRawInput = false;
} }
}
oldstate = in_mouse_suspended; if( active && !host.mouse_visible && cls.state == ca_active )
{
if( in_mouse_suspended ) if( !s_bMouseGrab )
{ {
in_mouse_suspended = false; #if XASH_SDL
in_mouseactive = false; // re-initialize mouse SDL_SetWindowGrab( host.hWnd, SDL_TRUE );
UI_ShowCursor( true ); #endif
// Con_Printf( "Enable grab\n" );
s_bMouseGrab = true;
} }
} }
else
if( in_mouseactive ) return;
in_mouseactive = true;
if( UI_IsVisible( )) return;
if( cls.key_dest == key_game )
{ {
if( s_bMouseGrab )
{
#if XASH_SDL #if XASH_SDL
SDL_SetRelativeMouseMode( SDL_TRUE ); SDL_SetWindowGrab( host.hWnd, SDL_FALSE );
SDL_GetRelativeMouseState( 0, 0 );
#endif #endif
clgame.dllFuncs.IN_ActivateMouse();
// Con_Printf( "Disable grab\n" );
s_bMouseGrab = false;
}
} }
} }
/*
===========
IN_ActivateMouse
Called when the window gains focus or changes in some way
===========
*/
void IN_ActivateMouse( void )
{
if( !in_mouseinitialized )
return;
IN_CheckMouseState( true );
clgame.dllFuncs.IN_ActivateMouse();
in_mouseactive = true;
}
/* /*
=========== ===========
IN_DeactivateMouse IN_DeactivateMouse
@ -321,22 +294,16 @@ Called when the window loses focus
*/ */
void IN_DeactivateMouse( void ) void IN_DeactivateMouse( void )
{ {
if( !in_mouseinitialized || !in_mouseactive ) if( !in_mouseinitialized )
return; return;
#if XASH_SDL IN_CheckMouseState( false );
SDL_SetRelativeMouseMode( SDL_FALSE ); clgame.dllFuncs.IN_DeactivateMouse();
SDL_SetWindowGrab( host.hWnd, SDL_FALSE );
#endif // XASH_SDL
if( cls.key_dest == key_game )
{
clgame.dllFuncs.IN_DeactivateMouse();
}
in_mouseactive = false; in_mouseactive = false;
} }
/* /*
================ ================
IN_MouseMove IN_MouseMove
@ -346,14 +313,9 @@ void IN_MouseMove( void )
{ {
POINT current_pos; POINT current_pos;
if( !in_mouseinitialized || !in_mouseactive ) if( !in_mouseinitialized )
return; return;
#if XASH_SDL >= 2
if( cls.key_dest == key_game && !host.mouse_visible && touch_emulate->value == 0.0f )
SDL_SetRelativeMouseMode( SDL_TRUE );
#endif // XASH_SDL >= 2
// find mouse movement // find mouse movement
Platform_GetMousePos( &current_pos.x, &current_pos.y ); Platform_GetMousePos( &current_pos.x, &current_pos.y );
@ -368,8 +330,6 @@ void IN_MouseMove( void )
// if the menu is visible, move the menu cursor // if the menu is visible, move the menu cursor
UI_MouseMove( current_pos.x, current_pos.y ); UI_MouseMove( current_pos.x, current_pos.y );
IN_ActivateCursor();
} }
/* /*
@ -381,7 +341,7 @@ void IN_MouseEvent( uint mstate )
{ {
int i; int i;
if( !in_mouseinitialized || !in_mouseactive ) if( !in_mouseinitialized )
return; return;
if( cls.key_dest == key_game ) if( cls.key_dest == key_game )
@ -400,7 +360,8 @@ void IN_MouseEvent( uint mstate )
} }
} }
clgame.dllFuncs.IN_MouseEvent( mstate ); if( in_mouseactive )
clgame.dllFuncs.IN_MouseEvent( mstate );
in_mouse_oldbuttonstate = mstate; in_mouse_oldbuttonstate = mstate;
return; return;
@ -610,9 +571,7 @@ void IN_EngineAppendMove( float frametime, void *cmd1, qboolean active )
{ {
float sensitivity = 1;//( (float)cl.local.scr_fov / (float)90.0f ); float sensitivity = 1;//( (float)cl.local.scr_fov / (float)90.0f );
IN_CollectInput( &forward, &side, &pitch, &yaw, IN_CollectInput( &forward, &side, &pitch, &yaw, false, false );
!host.mouse_visible && in_mouseinitialized && !CVAR_TO_BOOL( m_ignore ),
m_enginemouse->value );
IN_JoyAppendMove( cmd, forward, side ); IN_JoyAppendMove( cmd, forward, side );
@ -626,26 +585,16 @@ void IN_EngineAppendMove( float frametime, void *cmd1, qboolean active )
} }
} }
/* void IN_Commands( void )
==================
Host_InputFrame
Called every frame, even if not generating commands
==================
*/
void Host_InputFrame( void )
{ {
qboolean shutdownMouse = false;
float forward = 0, side = 0, pitch = 0, yaw = 0;
Sys_SendKeyEvents ();
#ifdef XASH_USE_EVDEV #ifdef XASH_USE_EVDEV
IN_EvdevFrame(); IN_EvdevFrame();
#endif #endif
if( clgame.dllFuncs.pfnLookEvent ) if( clgame.dllFuncs.pfnLookEvent )
{ {
float forward = 0, side = 0, pitch = 0, yaw = 0;
IN_CollectInput( &forward, &side, &pitch, &yaw, in_mouseinitialized && !CVAR_TO_BOOL( m_ignore ), true ); IN_CollectInput( &forward, &side, &pitch, &yaw, in_mouseinitialized && !CVAR_TO_BOOL( m_ignore ), true );
if( cls.key_dest == key_game ) if( cls.key_dest == key_game )
@ -658,22 +607,21 @@ void Host_InputFrame( void )
if( !in_mouseinitialized ) if( !in_mouseinitialized )
return; return;
if( host.status != HOST_FRAME ) IN_CheckMouseState( in_mouseactive );
{ }
IN_DeactivateMouse();
return;
}
// release mouse during pause or console typeing /*
if( cl.paused && cls.key_dest == key_game ) ==================
shutdownMouse = true; Host_InputFrame
if( shutdownMouse && !Cvar_VariableInteger( "fullscreen" )) Called every frame, even if not generating commands
{ ==================
IN_DeactivateMouse(); */
return; void Host_InputFrame( void )
} {
Sys_SendKeyEvents ();
IN_Commands();
IN_ActivateMouse( false );
IN_MouseMove(); IN_MouseMove();
} }

2
engine/client/input.h

@ -34,7 +34,7 @@ void IN_Init( void );
void Host_InputFrame( void ); void Host_InputFrame( void );
void IN_Shutdown( void ); void IN_Shutdown( void );
void IN_MouseEvent( uint mstate ); void IN_MouseEvent( uint mstate );
void IN_ActivateMouse( qboolean force ); void IN_ActivateMouse( void );
void IN_DeactivateMouse( void ); void IN_DeactivateMouse( void );
void IN_MouseSavePos( void ); void IN_MouseSavePos( void );
void IN_MouseRestorePos( void ); void IN_MouseRestorePos( void );

7
engine/platform/sdl/events.c

@ -340,7 +340,7 @@ static void SDLash_ActiveEvent( int gain )
if( gain ) if( gain )
{ {
host.status = HOST_FRAME; host.status = HOST_FRAME;
IN_ActivateMouse( true ); IN_ActivateMouse( );
if( dma.initialized && snd_mute_losefocus.value ) if( dma.initialized && snd_mute_losefocus.value )
{ {
SNDDMA_Activate( true ); SNDDMA_Activate( true );
@ -425,7 +425,10 @@ static void SDLash_EventFilter( SDL_Event *event )
{ {
/* Mouse events */ /* Mouse events */
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
/* ignored */ if( host.mouse_visible )
{
SDL_GetRelativeMouseState( NULL, NULL );
}
break; break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:

2
engine/platform/sdl/vid_sdl.c

@ -574,7 +574,6 @@ static qboolean VID_SetScreenResolution( int width, int height )
SDL_SetWindowBordered( host.hWnd, SDL_FALSE ); SDL_SetWindowBordered( host.hWnd, SDL_FALSE );
//SDL_SetWindowPosition( host.hWnd, 0, 0 ); //SDL_SetWindowPosition( host.hWnd, 0, 0 );
SDL_SetWindowGrab( host.hWnd, SDL_TRUE );
SDL_SetWindowSize( host.hWnd, got.w, got.h ); SDL_SetWindowSize( host.hWnd, got.w, got.h );
VID_SaveWindowSize( got.w, got.h ); VID_SaveWindowSize( got.w, got.h );
@ -590,7 +589,6 @@ void VID_RestoreScreenResolution( void )
if( !Cvar_VariableInteger("fullscreen") ) if( !Cvar_VariableInteger("fullscreen") )
{ {
SDL_SetWindowBordered( host.hWnd, SDL_TRUE ); SDL_SetWindowBordered( host.hWnd, SDL_TRUE );
SDL_SetWindowGrab( host.hWnd, SDL_FALSE );
} }
else else
{ {

Loading…
Cancel
Save