Fixed bug 2367 - Bad mouse motion coordinates with two windows where one has changed logical size
authorSam Lantinga <slouken@libsdl.org>
Thu, 28 May 2015 12:18:05 -0700
changeset 9673aea5a2032785
parent 9672 bc1ba207ff16
child 9674 b0f63fe6e012
Fixed bug 2367 - Bad mouse motion coordinates with two windows where one has changed logical size

Andreas Ragnerstam

I have two windows where one has a renderer where the logical size has been changed with SDL_RenderSetLogicalSize. When I get SDL_MOUSEMOTION events belonging to the non-scaled window these will have been scaled with the factor of the scaled window, which is not expected.

Adding some printf debugging to SDL_RendererEventWatch of SDL_render.c, where (event->type == SDL_MOUSEMOTION), I found that for every mouse motion SDL_RendererEventWatch is called twice and the event->motion.x and event.motion.y are set twice for the event, once for each renderer where only the last one set will be saved to the event struct. This will work fine if both renderers have the same scale, but otherwise the motion coordinates will be scaled for the renderer belonging to another window than the mouse was moved in.

I guess one solution would be to check that window == renderer->window for SDL_MOUSEMOTION events, similar to what is done for when SDL_WINDOWEVENT events.

I get the same error on both X11 and Windows.
The same problem also exists for SDL_MOUSEBUTTONDOWN and SDL_MOUSEBUTTONUP events.
src/render/SDL_render.c
     1.1 --- a/src/render/SDL_render.c	Thu May 28 12:06:48 2015 -0700
     1.2 +++ b/src/render/SDL_render.c	Thu May 28 12:18:05 2015 -0700
     1.3 @@ -165,7 +165,8 @@
     1.4              }
     1.5          }
     1.6      } else if (event->type == SDL_MOUSEMOTION) {
     1.7 -        if (renderer->logical_w) {
     1.8 +        SDL_Window *window = SDL_GetWindowFromID(event->motion.windowID);
     1.9 +        if (renderer->logical_w && window == renderer->window) {
    1.10              event->motion.x -= renderer->viewport.x;
    1.11              event->motion.y -= renderer->viewport.y;
    1.12              event->motion.x = (int)(event->motion.x / renderer->scale.x);
    1.13 @@ -183,7 +184,8 @@
    1.14          }
    1.15      } else if (event->type == SDL_MOUSEBUTTONDOWN ||
    1.16                 event->type == SDL_MOUSEBUTTONUP) {
    1.17 -        if (renderer->logical_w) {
    1.18 +        SDL_Window *window = SDL_GetWindowFromID(event->button.windowID);
    1.19 +        if (renderer->logical_w && window == renderer->window) {
    1.20              event->button.x -= renderer->viewport.x;
    1.21              event->button.y -= renderer->viewport.y;
    1.22              event->button.x = (int)(event->button.x / renderer->scale.x);