Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Emscripten: Allow for Multiple Windows #12575

Open
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

Temdog007
Copy link
Contributor

@Temdog007 Temdog007 commented Mar 18, 2025

  • Updated the software renderer to store each canvas and context in a separate object
  • Updated drag and pointer event handlers to store each event listener function in a separate object
  • Added hash table to store which canvas ID has been mapped to an SDL_Window
  • Updated Emscripten_GLES_MakeCurrent to point Module['canvas'] to the canvas of the renderer's window that is currently being drawn to
  • Allow SDL tests applications to show multiple windows on Emscripten builds
  • Detect if the canvas doesn't already exist in the DOM and create and manage the canvas if it doesn't
  • Add support for SDL_ShowWindow, SDL_HideWindow, and SDL_SetWindowPosition for Emscripten windows

Closes #12512

@slouken slouken added this to the 3.4.0 milestone Mar 18, 2025
@slouken slouken requested a review from icculus March 18, 2025 05:11
@Temdog007 Temdog007 changed the title Remove Singletons from Emscripten code Emscripten: Allow for Multiple Windows Mar 22, 2025
@sezero
Copy link
Contributor

sezero commented Mar 29, 2025

Also, can you please do:

git pull --rebase https://github.com/libsdl-org/SDL.git main

.. and then do a git push --force

Because those merge commits are really annoying.

@vittorioromeo
Copy link
Contributor

Possibly related to #12678 and #12667 (due to SDL_GetWindowPosition)

@madebr
Copy link
Contributor

madebr commented Mar 30, 2025

I think it would be useful to add multi emscripten window support to at least one test. (Or create a new test?)


props = SDL_CreateProperties();
SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, title);
SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_EMSCRIPTEN_CANVAS_ID, title);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emscripten support files can be stored in test/emscripten.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is not specific to Emscripten. It works on desktops and also tests multiple things:

  • Creating multiple windows
  • Unique rendering to each window
  • Getting the global mouse position and spawning a window at that position
  • Destroying windows

Copy link
Contributor

@madebr madebr Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good, but without multiple HTML items to show windows, the test won't work on emscripten.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've verified this PR with this test on Emscripten. So, it does work.

In this PR, a new canvas element is created in SDL_CreateWindow if the canvas ID specified wasn't already present in the DOM.

@slouken
Copy link
Collaborator

slouken commented Apr 3, 2025

Does creating a hidden window with OpenGL context work the same way on other platforms? e.g. would this PR fix #12678?

@Temdog007
Copy link
Contributor Author

Does creating a hidden window with OpenGL context work the same way on other platforms? e.g. would this PR fix #12678?

I left a comment for the author of the bug to verify.

@vittorioromeo
Copy link
Contributor

I've done some quick testing but this still seems broken. Mouse position is still offset when using

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

and now my game also seems to crash when recreating the HTML canvas via a window resize.

@vittorioromeo
Copy link
Contributor

OK, I've done some further digging and the crash seems to happen because SDL_EVENT_WINDOW_RESIZED is unexpectedly 0x0 (data1 is 0, data2 is 0)

Make windows change their position every iteration
Make windows have random sizes
Add popup functionality
Update SDL_SyncWindow so that it syncs the position and size
Ensure windows with parents are offseted by their parent's position
Improved multi window test
@Temdog007
Copy link
Contributor Author

OK, I've done some further digging and the crash seems to happen because SDL_EVENT_WINDOW_RESIZED is unexpectedly 0x0 (data1 is 0, data2 is 0)

The window size wasn't being updated in SDL_CreateWindow or SDL_SyncWindow. So, the width and height was left at 0. This has been fixed.

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

Successfully merging this pull request may close these issues.

Creating Mutliple Windows not possible on Emscripten
5 participants