New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SDL_GetMouseState() seems to give raw mouse position when using SDL_RenderSetLogicalSize() #1397
Comments
One option could be to have a HINT that modifies what SDL_GetMouseState returns. It seems having a way to get unmodified mouse coordinates can be useful but in most cases, users will want the logical coordinates, something consistent with what event contain. |
This issue and the discussion from http://forums.libsdl.org/viewtopic.php?p=40516 make me think that SDL_RenderSetLogicalSize() is indeed not really thought through and should be avoided. |
🙄 Let's look at this for 2.0.18, it seems like we can probably make this work in some way, even if it's just a hint. |
Not sure if this is perfect, but this is what I use for getting a "logical" mouse position after setting a logical render size void
windowMousePositionToLogicalMousePosition(SDL_Renderer *renderer, SDL_Window *window, int realMouseX, int realMouseY, int *logicalMouseX, int *logicalMouseY) {
int wWidth, wHeight;
int rLogicalWidth, rLogicalHeight;
int rRealWidth, rRealHeight;
float rScaleX, rScaleY;
int rMidpointY, wMidpointY;
int rMidpointX, wMidpointX;
int rY, rX;
SDL_GetWindowSize(window, &wWidth, &wHeight);
wMidpointY = wHeight/2;
wMidpointX = wWidth/2;
SDL_RenderGetLogicalSize(renderer, &rLogicalWidth, &rLogicalHeight);
SDL_RenderGetScale(renderer, &rScaleX, &rScaleY);
rRealWidth = (float)rLogicalWidth*(float)rScaleX;
rRealHeight = (float)rLogicalHeight*(float)rScaleY;
rMidpointY = rRealHeight/2;
rMidpointX = rRealWidth/2;
rY = wMidpointY - rMidpointY;
rX = wMidpointX - rMidpointX;
int adjustedMouseY = realMouseY - rY; // takes into account any border when keeping aspect ratio
int adjustedMouseX = realMouseX - rX;
*logicalMouseX = (float)adjustedMouseX / (float)rRealWidth * (float)rLogicalWidth;
*logicalMouseY = (float) adjustedMouseY / (float)rRealHeight * (float)rLogicalHeight;
} |
Let's add a function in the render API that takes real coordinates and scales them into the logical coordinates, and then document that SDL_GetMouseState() gets real coordinates. |
This function has been added, and should be used to transform mouse coordinates as needed. |
This bug report was migrated from our old Bugzilla tracker.
These attachments are available in the static archive:
Reported in version: HG 2.1
Reported for operating system, platform: Linux, x86_64
Comments on the original bug report:
On 2014-03-11 01:41:25 +0000, wrote:
On 2014-03-11 01:54:46 +0000, wrote:
On 2014-05-10 15:13:51 +0000, Leonardo wrote:
On 2014-07-09 07:09:03 +0000, Sylvain wrote:
On 2015-01-09 20:55:05 +0000, wrote:
On 2016-06-09 03:02:31 +0000, Arne Döring wrote:
On 2019-10-17 11:45:16 +0000, Sylvain wrote:
The text was updated successfully, but these errors were encountered: