Skip to content

SDL_GetGlobalMouseState and SDL_GetWindowPosition inconsistency still present on Emscripten #12770

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

Open
vittorioromeo opened this issue Apr 7, 2025 · 3 comments
Milestone

Comments

@vittorioromeo
Copy link
Contributor

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.

@vittorioromeo vittorioromeo changed the title SDL_GetGlobalMouseState and SDL_GetWindowPosition still present on Emscripten SDL_GetGlobalMouseState and SDL_GetWindowPosition inconsistency still present on Emscripten Apr 7, 2025
@slouken slouken added this to the 3.4.0 milestone Apr 7, 2025
@Temdog007
Copy link
Contributor

Temdog007 commented Apr 7, 2025

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants