We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2442 - SDL_GetMouseState() seems to give raw mouse position when using SDL_RenderSetLogicalSize()
Summary: SDL_GetMouseState() seems to give raw mouse position when using SDL_RenderSet...
Status: ASSIGNED
Alias: None
Product: SDL
Classification: Unclassified
Component: events (show other bugs)
Version: HG 2.1
Hardware: x86_64 Linux
: P2 normal
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-03-11 01:41 UTC by raincomplex
Modified: 2019-10-17 11:45 UTC (History)
3 users (show)

See Also:


Attachments
shows bug (852 bytes, text/x-csrc)
2014-03-11 01:54 UTC, raincomplex
Details

Note You need to log in before you can comment on or make changes to this bug.
Description raincomplex 2014-03-11 01:41:25 UTC
As summary. MOUSEBUTTONDOWN events report the position as expected (in logical screen coordinates). The window is resizable. Probably also happens with fixed-size windows and a logical size that is a different aspect ratio than the window itself.
Comment 1 raincomplex 2014-03-11 01:54:46 UTC
Created attachment 1589 [details]
shows bug

Here is an example program that shows the bug. On my machine, clicking in the corners of the white rectangle:

$ ./mousestate 
MOUSEBUTTONDOWN (1, 2)
GetMouseState (102, 3)

MOUSEBUTTONDOWN (398, 398)
GetMouseState (697, 598)
Comment 2 Leonardo 2014-05-10 15:13:51 UTC
This is true. The problem is that GetMouseState() consults the x/y directly whereas when reported via events, the renderer changes all the mouse events first with an event watcher before they go to the queue.

I don't see this being fixed easily as it would require the GetMouseState function to consult the renderer.

Actually this can come in handy if you ever need the real mouse position as there is no other way of knowing it. If you require your mouse coordinates to be adjusted to the logical size, you can create a event watcher like this:

int MouseWatcher(void* userdata, SDL_Event* event) {
  MouseState * m = (MouseState*) userdata;
  switch(event->type) {
    case SDL_MOUSEBUTTONDOWN:
    case SDL_MOUSEBUTTONUP:
      m->x = event->button.x;
      m->y = event->button.y;
      break;
    case SDL_MOUSEMOTION: 
      m->x = event->motion.x;
      m->y = event->motion.y;
      break;
   default:
      break;
  }
  return 0;
}

And then add it: 

{
  SDL_AddEventWatch(MouseWatcher, mouseState);
}

Note that you'll need at least 2.0.3 because of bug #2414
Comment 3 Sylvain 2014-07-09 07:09:03 UTC
Hey, I am not the original poster but I faced the same issue.
Just some thought:

CreateRenderer assume a Window.
But could we created several Renderer for the Same Window?

If so, converting an "event" (mouse position) from Window Frame to Renderer Frame is king of buggy, we could only convert to 1 specific Renderer Frame (or to the only renderer if only 1 one exists).

If no, maybe we could add a function to get the renderer of a Window : 

SDL_renderer * SDL_GetRendererOfWindow(SDL_window*)

Also. Maybe the code of "SDL_RendererEventWatch" could be factorized to have a function like :

"convert_event_in_window_coordinates_to_renderer_coordinates"

We would be used in SDL_RendererEventWatch, and also exported so that users who call SDL_GetMouseState can convert their event to LogicalRendererSize.

Cheers,

Sylvain
Comment 4 raincomplex 2015-01-09 20:55:05 UTC
It seems to me the resolution of this is just to add Leonardo's comments to the docs/wiki. Right now it doesn't say that SDL_GetMouseState() is the only way to get the real mouse position. In light of that, it makes sense to just get the computed position from the motion or button events.
Comment 5 Arne Döring 2016-06-09 03:02:31 UTC
I would like to ask, if there is anything done on this bug? I found out about this bug, too, and because the logical size didn't even do a recalculation of the mouse coordinates, I decided, to not use the logical size anymore. I really feels like a not well thought through feature, that I wouldn't recommend to anyone who uses SDL.

But the not make this post sound too negative. Thanks for the great work to have SDL2 actually done. I enjoy working with it.
Comment 6 Sylvain 2019-10-17 11:45:16 UTC
It's reasonable to say that querying the state returns raw values,!
We can revisit this for SDL 2.1
(see also bug 4028)