Skip to content

Inconsistency between SDL_GetGlobalMouseState position and SDL_GetWindowPosition on Emscripten #12667

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

Closed
vittorioromeo opened this issue Mar 29, 2025 · 1 comment · Fixed by #12669
Milestone

Comments

@vittorioromeo
Copy link
Contributor

vittorioromeo commented Mar 29, 2025

Hi, I am working on a fork of SFML that uses SDL3 as the backend for windows, joystick, and more.

I reimplemented the sf::Mouse::getPosition API as follows:

Vector2i getPosition()
{
    Vector2f result;
    SDL_GetGlobalMouseState(&result.x, &result.y);
    return result.toVector2i();
}

Vector2i getPosition(const WindowBase& relativeTo)
{
    return getMousePosition() - relativeTo.getPosition();
//                              ^~~~~~~~~~~~~~~~~~~~~~~~
//                       uses SDL_GetWindowPosition() internally
}

This works well on every platform I've tested except Emscripten, where it seems that the getPosition(const WindowBase&) overload returns an incorrect position. More precisely, it seems that getPosition() returns coordinates relative to the canvas in the webpage, and SDL_GetWindowPosition() does the same, therefore the final computed position is incorrect.

I've managed to work around the issue like this:

Vector2i getPosition(const WindowBase& relativeTo)
{
#ifdef SFML_SYSTEM_EMSCRIPTEN
    return getPosition(); 
#else
    return getPosition() - relativeTo.getPosition();
//                         ^~~~~~~~~~~~~~~~~~~~~~~~
//                  uses SDL_GetWindowPosition() internally
#endif
}

But it does seem that this is an inconsistency in SDL3.

@vittorioromeo
Copy link
Contributor Author

This still seems to be broken for me after merging both #12669 and #12575. The relative mouse position is reported incorrectly -- see this GIF:

https://i.imgur.com/sqBYbwe.mp4

This is my logic:

////////////////////////////////////////////////////////////
Vector2i getPosition()
{
    Vector2f result;
    SDL_GetGlobalMouseState(&result.x, &result.y);
    return result.toVector2i();
}


////////////////////////////////////////////////////////////
Vector2i getPosition(const WindowBase& relativeTo)
{
    return getPosition() - relativeTo.getPosition();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants