pythonsdl-2pysdl2

SDL2 resize event goes to wrong thread?


I use SDL2 from Python 3.5 and I have 2 threads, each drawing to their own OpenGl window.

Drawing goes fine, and all mouse and keyboard events arrive in the right thread. Only when I resize one of the window's, the resize event goes to the wrong window.

Here's part of my code:

def pollEvent (self):
    event = s2.SDL_Event ()
    if s2.SDL_PollEvent (ct.byref (event)):
        if event.type == s2.SDL_QUIT:
            self.running = False
        elif event.type == s2.SDL_WINDOWEVENT:
            if event.window.event == s2.SDL_WINDOWEVENT_RESIZED:
                print (111, self.name, 222)
                self.event = resizeEvent
                self.width = event.window.data1
                self.height = event.window.data2
                self.reshape ()
            elif event.window.event == s2.SDL_WINDOWEVENT_CLOSE:
                self.running = False
            self.renderDisplayList ()
        elif event.type == s2.SDL_MOUSEBUTTONDOWN:
            print (222, self.name, 333)

The SDL_MOUSEBUTTONDOWN goes to the right window, printing the right name, but the SDL_WINDOWEVENT_RESIZED goes to the wrong one, printing the wrong name.

Anyone any idea what could cause this? Each SDL window is only approached from its own thread.


Solution

  • SDL makes only one event queue. Event queue is mostly thread-safe, but it would be logically hard to use it in multiple threads. It is probably better to read events in only one thread and send notifications to other threads through some other things.

    As for window events, they contain windowID field. You can get ID of a window with SDL_GetWindowID call.