[pypy-svn] r54867 - in pypy/dist/pypy/rlib/rsdl: . test

arigo at codespeak.net arigo at codespeak.net
Sat May 17 19:04:51 CEST 2008


Author: arigo
Date: Sat May 17 19:04:49 2008
New Revision: 54867

Modified:
   pypy/dist/pypy/rlib/rsdl/RSDL.py
   pypy/dist/pypy/rlib/rsdl/test/test_video.py
Log:
Event support.  Support for keyboard events so far.


Modified: pypy/dist/pypy/rlib/rsdl/RSDL.py
==============================================================================
--- pypy/dist/pypy/rlib/rsdl/RSDL.py	(original)
+++ pypy/dist/pypy/rlib/rsdl/RSDL.py	Sat May 17 19:04:49 2008
@@ -26,6 +26,8 @@
 RectPtr        = lltype.Ptr(lltype.ForwardReference())
 SurfacePtr     = lltype.Ptr(lltype.ForwardReference())
 PixelFormatPtr = lltype.Ptr(lltype.ForwardReference())
+EventPtr       = lltype.Ptr(lltype.ForwardReference())
+KeyboardEventPtr = lltype.Ptr(lltype.ForwardReference())
 
 class CConfig:
     _compilation_info_ = eci
@@ -46,6 +48,16 @@
     PixelFormat = platform.Struct('SDL_PixelFormat',
                                   [('BytesPerPixel', rffi.INT)])
 
+    Event = platform.Struct('SDL_Event', [('type', rffi.INT)])
+    keysym = platform.Struct('SDL_keysym', [('scancode', rffi.INT),
+                                            ('sym', rffi.INT),
+                                            ('mod', rffi.INT),
+                                            ('unicode', rffi.INT)])
+    KeyboardEvent = platform.Struct('SDL_KeyboardEvent',
+                                    [('type', rffi.INT),
+                                     ('state', rffi.INT),
+                                     ('keysym', keysym)])
+
 for _prefix, _list in _constants.items():
     for _name in _list:
         setattr(CConfig, _name, platform.ConstantInteger(_prefix+_name))
@@ -55,6 +67,8 @@
 RectPtr.TO.become(Rect)
 SurfacePtr.TO.become(Surface)
 PixelFormatPtr.TO.become(PixelFormat)
+EventPtr.TO.become(Event)
+KeyboardEventPtr.TO.become(KeyboardEvent)
 
 Uint8P = lltype.Ptr(lltype.Array(Uint8, hints={'nolength': True}))
 Uint16P = lltype.Ptr(lltype.Array(Uint16, hints={'nolength': True}))
@@ -73,6 +87,8 @@
                         SurfacePtr)
 WM_SetCaption = external('SDL_WM_SetCaption', [rffi.CCHARP, rffi.CCHARP],
                          lltype.Void)
+EnableUNICODE = external('SDL_EnableUNICODE', [rffi.INT], rffi.INT)
+WaitEvent = external('SDL_WaitEvent', [EventPtr], rffi.INT)
 Flip = external('SDL_Flip', [SurfacePtr], rffi.INT)
 CreateRGBSurface = external('SDL_CreateRGBSurface', [Uint32, rffi.INT,
                                                      rffi.INT, rffi.INT,

Modified: pypy/dist/pypy/rlib/rsdl/test/test_video.py
==============================================================================
--- pypy/dist/pypy/rlib/rsdl/test/test_video.py	(original)
+++ pypy/dist/pypy/rlib/rsdl/test/test_video.py	Sat May 17 19:04:49 2008
@@ -20,9 +20,10 @@
         assert RSDL.Init(RSDL.INIT_VIDEO) >= 0
         self.screen = RSDL.SetVideoMode(640, 480, 32, 0)
         assert self.screen
+        self.is_interactive = sys.stdout.isatty()
 
     def check(self, msg):
-        if sys.stdout.isatty():
+        if self.is_interactive:
             print
             answer = raw_input('Interactive test: %s - ok? [Y] ' % msg)
             if answer and not answer.upper().startswith('Y'):
@@ -47,6 +48,34 @@
         RSDL.WM_SetCaption("Hello World!", "Hello World!")
         self.check('The window caption is "Hello World!"')
 
+    def test_keypresses(self):
+        if not self.is_interactive:
+            py.test.skip("interactive test only")
+        RSDL.EnableUNICODE(1)
+        print
+        print "Keys pressed in the Pygame window should be printed below."
+        print "Use Escape to quit."
+        while True:
+            event = lltype.malloc(RSDL.Event, flavor='raw')
+            try:
+                ok = RSDL.WaitEvent(event)
+                assert rffi.cast(lltype.Signed, ok) == 1
+                c_type = rffi.getintfield(event, 'c_type')
+                if c_type == RSDL.KEYDOWN:
+                    p = rffi.cast(RSDL.KeyboardEventPtr, event)
+                    if rffi.getintfield(p.c_keysym, 'c_sym') == RSDL.K_ESCAPE:
+                        print 'Escape key'
+                        break
+                    char = rffi.getintfield(p.c_keysym, 'c_unicode')
+                    if char != 0:
+                        print 'Key:', unichr(char).encode('utf-8')
+                    else:
+                        print 'Some special key'
+                else:
+                    print '(event of type %d)' % c_type
+            finally:
+                lltype.free(event, flavor='raw')
+
     def test_blit_rect(self):
         surface = RSDL.CreateRGBSurface(0, 150, 50, 32,
                                         r_uint(0x000000FF),



More information about the Pypy-commit mailing list