From aaa6bf53eceb4c7801966a017dcc8a57c2fdface Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Fri, 5 Apr 2019 00:00:53 +0200 Subject: [PATCH] added function to update the mouse position when changing window size --- src/events.c | 25 ++++++++++++------------- src/libgame/sdl.c | 2 +- src/libgame/sdl.h | 2 +- src/libgame/system.c | 22 ++++++++++++++++++++++ src/libgame/system.h | 2 ++ 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/events.c b/src/events.c index bd82a609..f6442956 100644 --- a/src/events.c +++ b/src/events.c @@ -54,20 +54,13 @@ static void HandleEventActions(void); int FilterMouseMotionEvents(void *userdata, Event *event) { - if (event->type != EVENT_MOTIONNOTIFY) - return 1; - - int mouse_x = ((MotionEvent *)event)->x; - int mouse_y = ((MotionEvent *)event)->y; - - // mouse events do not contain logical screen size corrections at this stage - SDLCorrectMouseEventXY(&mouse_x, &mouse_y); - - mouse_x -= video.screen_xoffset; - mouse_y -= video.screen_yoffset; + if (event->type == EVENT_MOTIONNOTIFY) + { + int mouse_x = ((MotionEvent *)event)->x; + int mouse_y = ((MotionEvent *)event)->y; - gfx.mouse_x = mouse_x; - gfx.mouse_y = mouse_y; + UpdateRawMousePosition(mouse_x, mouse_y); + } return 1; } @@ -593,6 +586,8 @@ void HandleWindowEvent(WindowEvent *event) if (game_status == GAME_MODE_SETUP) RedrawSetupScreenAfterFullscreenToggle(); + UpdateMousePosition(); + SetWindowTitle(); } } @@ -2142,6 +2137,8 @@ void HandleKey(Key key, int key_status) if (game_status == GAME_MODE_SETUP) RedrawSetupScreenAfterFullscreenToggle(); + UpdateMousePosition(); + // set flag to ignore repeated "key pressed" events ignore_repeated_key = TRUE; @@ -2173,6 +2170,8 @@ void HandleKey(Key key, int key_status) if (game_status == GAME_MODE_SETUP) RedrawSetupScreenAfterFullscreenToggle(); + UpdateMousePosition(); + return; } diff --git a/src/libgame/sdl.c b/src/libgame/sdl.c index 45f7f2ce..fca1e8b9 100644 --- a/src/libgame/sdl.c +++ b/src/libgame/sdl.c @@ -2362,7 +2362,7 @@ void SDLWaitEvent(Event *event) SDL_WaitEvent(event); } -void SDLCorrectMouseEventXY(int *x, int *y) +void SDLCorrectRawMousePosition(int *x, int *y) { if (sdl_renderer == NULL) return; diff --git a/src/libgame/sdl.h b/src/libgame/sdl.h index 35d6a241..ad7dca8c 100644 --- a/src/libgame/sdl.h +++ b/src/libgame/sdl.h @@ -423,7 +423,7 @@ void SDLOpenAudio(void); void SDLCloseAudio(void); void SDLWaitEvent(Event *); -void SDLCorrectMouseEventXY(int *, int *); +void SDLCorrectRawMousePosition(int *, int *); void HandleJoystickEvent(Event *); void SDLInitJoysticks(void); diff --git a/src/libgame/system.c b/src/libgame/system.c index 7fcb784d..8c8b7d5a 100644 --- a/src/libgame/system.c +++ b/src/libgame/system.c @@ -1576,6 +1576,28 @@ void SetMouseCursor(int mode) gfx.cursor_mode_final = mode_final; } +void UpdateRawMousePosition(int mouse_x, int mouse_y) +{ + // mouse events do not contain logical screen size corrections yet + SDLCorrectRawMousePosition(&mouse_x, &mouse_y); + + mouse_x -= video.screen_xoffset; + mouse_y -= video.screen_yoffset; + + gfx.mouse_x = mouse_x; + gfx.mouse_y = mouse_y; +} + +void UpdateMousePosition(void) +{ + int mouse_x, mouse_y; + + SDL_PumpEvents(); + SDL_GetMouseState(&mouse_x, &mouse_y); + + UpdateRawMousePosition(mouse_x, mouse_y); +} + // ============================================================================ // audio functions diff --git a/src/libgame/system.h b/src/libgame/system.h index 290f0cb7..30b37544 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -1852,6 +1852,8 @@ void FreeBitmapTextures(Bitmap **); void ScaleBitmap(Bitmap **, int); void SetMouseCursor(int); +void UpdateRawMousePosition(int, int); +void UpdateMousePosition(void); void OpenAudio(void); void CloseAudio(void); -- 2.34.1