multiple windows

Started by KSergeyP, 06 November 2024, 12:18:28

KSergeyP

How?)
Backend: SDL3_OPENGL(Using the new SDL3 Callback App Structure)
Tried to launch SDL_EnterAppMainCallbacks (2 windows) from different threads. Windows are created, but the first created window slows down, the second created window works well.
I also tried to create 2 windows at once from one thread - both work well, but it is unclear how to distinguish from which window I receive events?
Are there any examples of correctly launching multiple windows. Preferably in different threads.

texus

The closest thing that I have is an older example that I once wrote: https://tgui.eu/tutorials/0.9/sdl-backend/#multiple-windows (it's for TGUI 0.9 which is why it e.g. uses GuiSDL instead of just Gui)

You basically need the correct OpenGL context selected whenever interacting with the gui (with SDL_GL_MakeCurrent in SDL2), and you need to use the windowID in the events to figure out which gui they belong to.

I just noticed that the example code actually contains a mistake. It accesses event.window.windowID but that is only allowed for window events (e.g. windows resize). Other events (e.g. SDL_KeyboardEvent) also contain a windowID property though.

I don't recommend using different threads for this though. TGUI code can only be accessed from one thread at a time (so you can't be handling events or drawing on both threads at the same time). You would need locks everywhere. Also OpenGL isn't really thread-safe. You need to activate the correct OpenGL context (with SDL_GL_MakeCurrent) before rendering, so you can't render with both threads at the same time even if TGUI was thread-safe.

KSergeyP

Thanks, it's clearer now.
It looks like you need to write a wrapper to determine which window the event is from, analyzing all events that have a WindowID.
But it would be better to implement this in TGUI

texus

Quote from: KSergeyP on 06 November 2024, 14:00:53But it would be better to implement this in TGUI
Since TGUI already has to handle each type of event separately, it should indeed be possible to do the filtering inside "gui.handleEvent".

The only issue with it, is that it would become harder to add fake events (e.g. create your own mouse move event and passing that to the gui). So I'll add the filtering to TGUI, but I might put it behind some option that you need to enable first to keep the filtering on windowID disabled by default.

texus

Apparently SDL3 added a SDL_GetWindowFromEvent(event) function that you can use to determine which window the event came from (and thus you can use that to forward the event to the right gui, or forward it to both guis when the function returns NULL).

texus

In the latest TGUI version, gui.handleEvent will now return false when the event is for a different window.
I didn't put it behind an option, the filter is always enabled.