Skip to content

SDL_GetGlobalMouseState and SDL_GetWindowPosition inconsistency still present on Emscripten #12770

Open
@vittorioromeo

Description

@vittorioromeo

Follow up from #12667.

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();
//                         ^~~~~~~~~~~~~~~~~~~~~~~~
//                       uses SDL_GetWindowPosition() internally 
}

@Temdog007: my guess here is that SDL_GetWindowPosition is not returning the correct position of the canvas relative to the parent web page shell, but I might be incorrect.

Activity

changed the title [-]`SDL_GetGlobalMouseState` and `SDL_GetWindowPosition` still present on Emscripten[/-] [+]`SDL_GetGlobalMouseState` and `SDL_GetWindowPosition` inconsistency still present on Emscripten[/+] on Apr 7, 2025
added this to the 3.4.0 milestone on Apr 7, 2025
Temdog007

Temdog007 commented on Apr 7, 2025

@Temdog007
Contributor

A call to SDL_SyncWindow is necessary to ensure that the window position is sync'd with position in the DOM.

Calling SDL_SetHint(SDL_HINT_VIDEO_SYNC_WINDOW_OPERATIONS, "1"); before calling SDL_Init will ensure that the window position is always sync'd after the position or size of the window changes.

The functions in SDL where this occurs:

  • SDL_SetWindowFullscreenMode
  • SDL_SetWindowPosition
  • SDL_SetWindowSize
  • SDL_MaximizeWindow
  • SDL_MinimizeWindow
  • SDL_RestoreWindow
  • SDL_SetWindowFullscreen

If the canvas is moved outside of SDL, the window position won't be sync'd without explicitly calling SDL_SyncWindow.

vittorioromeo

vittorioromeo commented on Apr 8, 2025

@vittorioromeo
ContributorAuthor

A call to SDL_SyncWindow is necessary to ensure that the window position is sync'd with position in the DOM.

Calling SDL_SetHint(SDL_HINT_VIDEO_SYNC_WINDOW_OPERATIONS, "1"); before calling SDL_Init will ensure that the window position is always sync'd after the position or size of the window changes.

If the canvas is moved outside of SDL, the window position won't be sync'd without explicitly calling SDL_SyncWindow.

Even with SDL_SetHint(SDL_HINT_VIDEO_SYNC_WINDOW_OPERATIONS, "1") I am getting inconsistent results. When I open the browser at first, the relative position is screwed up as usual. If I resize the window once, the position seems to fix itself, but stops being correct again as soon as I resize the browser window.

Is there a chance I am doing something wrong? How can I debug this and figure out what is the culprit?

vittorioromeo

vittorioromeo commented on Apr 8, 2025

@vittorioromeo
ContributorAuthor

A call to SDL_SyncWindow is necessary to ensure that the window position is sync'd with position in the DOM.

Calling SDL_SetHint(SDL_HINT_VIDEO_SYNC_WINDOW_OPERATIONS, "1"); before calling SDL_Init will ensure that the window position is always sync'd after the position or size of the window changes.

If the canvas is moved outside of SDL, the window position won't be sync'd without explicitly calling SDL_SyncWindow.

So, calling SDL_SetHint before SDL_Init doesn't work, but calling SDL_SyncWindow every frame does. I will experiment further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @vittorioromeo@slouken@Temdog007

        Issue actions

          `SDL_GetGlobalMouseState` and `SDL_GetWindowPosition` inconsistency still present on Emscripten · Issue #12770 · libsdl-org/SDL