Issue: I wrote the following test code to illustrate my issue. Whenever there is a sdl2 window event, sdl2.SDL_Event().window.windowID
always returned a value of 2, which means the window is hidden and this is incorrect. The value 2 is also returned when the mouse pointer moves in and out of the window, when the window is moved, resized, minimized, maximized, ......
Question: How do I get it to work correctly?
Test code:
#!/bin/env python3.5
'''
test sdl2 event.window.windowID values
'''
# Python3 modules
import ctypes
# PyPI module
import sdl2
class Window (object):
'''Create SDL2 WINDOW.'''
def __init__(self, title='SDL2 Window', width=400, height=400):
self.title = title
self.width = width
self.height = height
self.window = None # SDL2 Window Object
self.event =sdl2.SDL_Event() # SDL2 Event object
if sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) != 0:
err = 'sdl2.SDL_Init failed: {}'.format(sdl2.SDL_GetError())
print(err)
flags = sdl2.SDL_WINDOW_RESIZABLE
self.window = sdl2.SDL_CreateWindow(self.title.encode('utf-8'),
sdl2.SDL_WINDOWPOS_UNDEFINED,
sdl2.SDL_WINDOWPOS_UNDEFINED,
ctypes.c_int(self.width),
ctypes.c_int(self.height),
ctypes.c_uint32(flags))
def end(self):
'''Method to destroy SDL2 window and clean up SDL2'''
sdl2.SDL_DestroyWindow(self.window) #Close and destroy SDL2 window.
sdl2.SDL_Quit() #Clean up SDL2
print('Ended Window')
def mainloop():
running = True
print('sdl2.SDL_WINDOWEVENT ', sdl2.SDL_WINDOWEVENT)
print('sdl2.SDL_WINDOWEVENT_SHOWN ', sdl2.SDL_WINDOWEVENT_SHOWN)
print('sdl2.SDL_WINDOWEVENT_HIDDEN ', sdl2.SDL_WINDOWEVENT_HIDDEN)
print('sdl2.SDL_WINDOWEVENT_EXPOSED ', sdl2.SDL_WINDOWEVENT_EXPOSED)
print('sdl2.SDL_WINDOWEVENT_MOVED ', sdl2.SDL_WINDOWEVENT_MOVED)
print('sdl2.SDL_WINDOWEVENT_RESIZED ', sdl2.SDL_WINDOWEVENT_RESIZED)
print('sdl2.SDL_WINDOWEVENT_SIZE_CHANGED ', sdl2.SDL_WINDOWEVENT_SIZE_CHANGED)
print('sdl2.SDL_WINDOWEVENT_MINIMIZED ', sdl2.SDL_WINDOWEVENT_MINIMIZED)
print('sdl2.SDL_WINDOWEVENT_MAXIMIZED ', sdl2.SDL_WINDOWEVENT_MAXIMIZED)
print('sdl2.SDL_WINDOWEVENT_RESTORED ', sdl2.SDL_WINDOWEVENT_RESTORED)
print('sdl2.SDL_WINDOWEVENT_ENTER ', sdl2.SDL_WINDOWEVENT_ENTER)
print('sdl2.SDL_WINDOWEVENT_LEAVE ', sdl2.SDL_WINDOWEVENT_LEAVE)
print('sdl2.SDL_WINDOWEVENT_FOCUS_GAINED ', sdl2.SDL_WINDOWEVENT_FOCUS_GAINED)
print('sdl2.SDL_WINDOWEVENT_FOCUS_LOST ', sdl2.SDL_WINDOWEVENT_FOCUS_LOST)
print('sdl2.SDL_WINDOWEVENT_CLOSE ', sdl2.SDL_WINDOWEVENT_CLOSE)
print('sdl2.SDL_WINDOWEVENT_TAKE_FOCUS ', sdl2.SDL_WINDOWEVENT_TAKE_FOCUS)
print('sdl2.SDL_WINDOWEVENT_HIT_TEST ', sdl2.SDL_WINDOWEVENT_HIT_TEST)
while running:
event = sdl2.SDL_Event()
#while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0:
while sdl2.SDL_PollEvent(event) != 0:
if event.type == sdl2.SDL_QUIT:
running = False
print('sdl2.SDL_QUIT')
break
if event.type == sdl2.SDL_WINDOWEVENT:
print('event.window.windowID = {}'.format(
event.window.windowID))
if event.window.windowID == sdl2.SDL_WINDOWEVENT_SHOWN:
print("Window shown")
break
if event.window.windowID == sdl2.SDL_WINDOWEVENT_HIDDEN:
print("Window hidden")
break
if event.window.windowID == sdl2.SDL_WINDOWEVENT_EXPOSED:
print("Window exposed")
break
if event.window.windowID == sdl2.SDL_WINDOWEVENT_MOVED:
print("Window moved to {},{}".format(event.window.data1,
event.window.data2))
break
if event.window.windowID == sdl2.SDL_WINDOWEVENT_RESIZED:
print("Window resized to {}x{}".format(event.window.data1,
event.window.data2))
break
if event.window.windowID == sdl2.SDL_WINDOWEVENT_SIZE_CHANGED:
print("Window size changed to {}x{}".format(
event.window.data1, event.window.data2))
break
if event.window.windowID == sdl2.SDL_WINDOWEVENT_MINIMIZED:
print("Window minimized")
break
if event.window.windowID == sdl2.SDL_WINDOWEVENT_MAXIMIZED:
print("Window maximized")
break
if event.window.windowID == sdl2.SDL_WINDOWEVENT_RESTORED:
print("Window restored")
break
if event.window.windowID == sdl2.SDL_WINDOWEVENT_ENTER:
print("Mouse entered window.")
break
if event.window.windowID == sdl2.SDL_WINDOWEVENT_LEAVE:
print("Mouse left window")
break
if event.window.windowID == sdl2.SDL_WINDOWEVENT_FOCUS_GAINED:
print("Window gained keyboard focus")
break
if event.window.windowID == sdl2.SDL_WINDOWEVENT_FOCUS_LOST:
print("Window lost keyboard focus")
break
if event.window.windowID == sdl2.SDL_WINDOWEVENT_CLOSE:
print("Window closed")
break
def main():
window = Window()
mainloop()
window.end()
if __name__== "__main__":
main()
You are confusing windowID
and event
. windowID
is an identifier corresponding to the window associated with the event. event
is the actual event and is a SDL_WindowEventID.
Replace event.window.windowID
with event.window.event
in your series of ifs.