Skip to content
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

Support multiple windows #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

adamgreig
Copy link

I needed multiple windows support for simulating my embedded-graphics project, and this was the cleanest way I could think of to make it work without changing the current API. Basically, the SDL context is created once in Lazy cell from once_cell, which is then stored in a thread-local static RefCell, from where it can be used by the windows as required. Previously it was created when a Window was made and stored in the Window, which panics if done twice.

From the user's point of view, nothing is different with a single Window, but now instead of a panic if you create a second Window, it just works and is created as expected.

Most of the change is around event handling: because all events are shared by all windows, it doesn't make a difference which Window you poll for events. The underlying SDL events have a "window ID" which you can match against an SDL window ID to know where they came from, but the current embedded-graphics-simulator API doesn't expose this. Instead, I have all keyboard events appear no matter which Window you poll, and only mouse events related to the Window being polled are returned. This means mouse events on another Window are completely lost, and the end-user's code should pick a single Window to poll.

This seemed like a good trade-off but it does mean you can't handle multiple windows where both of them need mouse-like events -- I think that would need a slightly different API or something cleverer under-the-hood to poll SDL events and distribute them out to each Window. Still, for projects where zero or only one window needs mouse events, at least with these changes it's easy to have two windows.

I'm more or less submitting this just where I got to with it, but i'm happy to add some more documentation or changelog entries if you'd like!

This requires updating sdl2 to use the `event.get_window_id()` method.

Filtered events are discarded entirely.
@hansl
Copy link

hansl commented Jul 20, 2023

I'm going to pull this in a new repo and try to get CI to agree with it. It shouldn't be too hard.

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 this pull request may close these issues.

2 participants