Skip to content

Inconsistency between SDL_GetGlobalMouseState position and SDL_GetWindowPosition on Emscripten #12667

Closed
@vittorioromeo

Description

@vittorioromeo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions