From 9decba873be75ffec804a432af46ae9f7a21abec Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sat, 22 Feb 2025 17:35:45 +0300 Subject: [PATCH] engine: platform: sdl: try to save and restore mouse position during showing or hiding cursor --- engine/platform/sdl/in_sdl.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/engine/platform/sdl/in_sdl.c b/engine/platform/sdl/in_sdl.c index 458c3d12..01991d10 100644 --- a/engine/platform/sdl/in_sdl.c +++ b/engine/platform/sdl/in_sdl.c @@ -33,6 +33,12 @@ static struct } cursors; #endif +static struct +{ + int x, y; + qboolean pushed; +} in_visible_cursor_pos; + /* ============= Platform_GetMousePos @@ -203,11 +209,6 @@ void Platform_SetCursorType( VGUI_DefaultCursor type ) { qboolean visible; -#if SDL_VERSION_ATLEAST( 2, 0, 0 ) - if( !cursors.initialized ) - return; -#endif - switch( type ) { case dc_user: @@ -229,11 +230,28 @@ void Platform_SetCursorType( VGUI_DefaultCursor type ) #if SDL_VERSION_ATLEAST( 2, 0, 0 ) if( host.mouse_visible ) { - SDL_SetCursor( cursors.cursors[type] ); + if( cursors.initialized ) + SDL_SetCursor( cursors.cursors[type] ); + SDL_ShowCursor( true ); + + // restore the last mouse position + if( in_visible_cursor_pos.pushed ) + { + SDL_WarpMouseInWindow( host.hWnd, in_visible_cursor_pos.x, in_visible_cursor_pos.y ); + in_visible_cursor_pos.pushed = false; + } } else { + // save last mouse position and warp it to the center + if( !in_visible_cursor_pos.pushed ) + { + SDL_GetMouseState( &in_visible_cursor_pos.x, &in_visible_cursor_pos.y ); + SDL_WarpMouseInWindow( host.hWnd, host.window_center_x, host.window_center_y ); + in_visible_cursor_pos.pushed = true; + } + SDL_ShowCursor( false ); } #else