| Summary: | SDL_GetMouseState() seems to give raw mouse position when using SDL_RenderSetLogicalSize() | ||
|---|---|---|---|
| Product: | SDL | Reporter: | raincomplex |
| Component: | events | Assignee: | Sam Lantinga <slouken> |
| Status: | ASSIGNED --- | QA Contact: | Sam Lantinga <slouken> |
| Severity: | normal | ||
| Priority: | P2 | CC: | arne.doering, leonardo.guilherme, sylvain.becker |
| Version: | HG 2.1 | ||
| Hardware: | x86_64 | ||
| OS: | Linux | ||
| Attachments: | shows bug | ||
|
Description
raincomplex
2014-03-11 01:41:25 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)
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
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 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. 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. |