Commits

Marcus von Appen committed a85f53d

- fixed issue #6: sdl.ext.get_events() incorrectly handles more than 10
events

Comments (2)

  1. markhildreth

    Actually, now that I look at this again, this probably isn't the best way to go about it either. If you have 6 items, you create an array of 10 elements, get the six items, then create another array of 10 items when you KNOW that there aren't any events left. Perhaps it's best to also add...

    if ret < 10:
        break
    

    To the end of this loop just to avoid the creation of another array and call to PeepEvents?

  2. Marcus von Appen author

    You're right. The code would look something like this (taking a shutdown of the event queue into account, too)

    if ret <= 0:
        break
    evlist += list(evarray)[:ret]
    if ret < 10:
        break
    

    The two break statements look a bit weird, though.

Files changed (2)

 
     evlist = []
     SDL_PeepEvents = events.SDL_PeepEvents
-    evarray = (events.SDL_Event * 10)()
-    ptr = ctypes.cast(evarray, ctypes.POINTER(events.SDL_Event))
 
     op = events.SDL_GETEVENT
     first = events.SDL_FIRSTEVENT
     last = events.SDL_LASTEVENT
 
-    ret = SDL_PeepEvents(ptr, 10, op, first, last)
-    while ret > 0:
+    while True:
+        evarray = (events.SDL_Event * 10)()
+        ptr = ctypes.cast(evarray, ctypes.POINTER(events.SDL_Event))
+        ret = SDL_PeepEvents(ptr, 10, op, first, last)
+        if ret == 0:
+            break
         evlist += list(evarray)[:ret]
-        ret = SDL_PeepEvents(ptr, 10, op, first, last)
     return evlist
 
 

sdl2/test/sdl2ext_test.py

 import sys
 import unittest
 from .. import ext as sdl2ext
-from .. import SDL_WasInit, SDL_INIT_VIDEO
+from .. import SDL_WasInit, SDL_INIT_VIDEO, SDL_FlushEvent, SDL_USEREVENT, \
+    SDL_FIRSTEVENT, SDL_LASTEVENT, SDL_Event, SDL_UserEvent, SDL_PushEvent
 
 
 class SDL2ExtTest(unittest.TestCase):
     def test_get_events(self):
         pass
 
+    def test_get_events_issue_6(self):
+        sdl2ext.init()
+        SDL_FlushEvent(SDL_FIRSTEVENT, SDL_LASTEVENT)
+        for x in range(12):
+            event = SDL_Event()
+            event.type = SDL_USEREVENT + x
+            event.user = SDL_UserEvent(type=event.type, timestamp=0,
+                                       windowID=0, code=0)
+            SDL_PushEvent(event)
+        results = sdl2ext.get_events()
+        for idx, r in enumerate(results):
+            self.assertEqual(idx, r.type - SDL_USEREVENT)
+
     def test_TestEventProcessor(self):
         proc = sdl2ext.TestEventProcessor()
         self.assertIsInstance(proc, sdl2ext.TestEventProcessor)