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

cami at codespeak.net cami at codespeak.net
Mon May 19 11:28:25 CEST 2008


Author: cami
Date: Mon May 19 11:28:24 2008
New Revision: 54920

Modified:
   pypy/dist/pypy/rlib/rsdl/RSDL.py
   pypy/dist/pypy/rlib/rsdl/constants.py
   pypy/dist/pypy/rlib/rsdl/test/test_video.py
Log:
added mouse tests
formatted source code


Modified: pypy/dist/pypy/rlib/rsdl/RSDL.py
==============================================================================
--- pypy/dist/pypy/rlib/rsdl/RSDL.py	(original)
+++ pypy/dist/pypy/rlib/rsdl/RSDL.py	Mon May 19 11:28:24 2008
@@ -20,90 +20,184 @@
         )
     eci = eci.merge(ExternalCompilationInfo.from_config_tool('sdl-config'))
 
+# ------------------------------------------------------------------------------
+
 def external(name, args, result):
     return rffi.llexternal(name, args, result, compilation_info=eci)
 
-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())
+# ------------------------------------------------------------------------------
+
+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())
+MouseButtonEventPtr = lltype.Ptr(lltype.ForwardReference())
+MouseMotionEventPtr = lltype.Ptr(lltype.ForwardReference())
+
+# ------------------------------------------------------------------------------
 
 class CConfig:
     _compilation_info_ = eci
 
     Uint8  = platform.SimpleType('Uint8',  rffi.INT)
     Uint16 = platform.SimpleType('Uint16', rffi.INT)
+    Sint16 = platform.SimpleType('Sint16', rffi.INT)
     Uint32 = platform.SimpleType('Uint32', rffi.INT)
 
-    Rect = platform.Struct('SDL_Rect', [('x', rffi.INT),
-                                        ('y', rffi.INT),
-                                        ('w', rffi.INT),
-                                        ('h', rffi.INT)])
-    Surface = platform.Struct('SDL_Surface', [('w', rffi.INT),
-                                              ('h', rffi.INT),
-                                              ('format', PixelFormatPtr),
-                                              ('pitch', rffi.INT),
-                                              ('pixels', rffi.UCHARP)])
-    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',
+    Rect             = platform.Struct('SDL_Rect', 
+                                    [('x', rffi.INT),
+                                     ('y', rffi.INT),
+                                     ('w', rffi.INT),
+                                     ('h', rffi.INT)])
+    
+    Surface          = platform.Struct('SDL_Surface', 
+                                    [('w', rffi.INT),
+                                     ('h', rffi.INT),
+                                     ('format', PixelFormatPtr),
+                                     ('pitch', rffi.INT),
+                                     ('pixels', rffi.UCHARP)])
+    
+    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)])
+    
+    MouseButtonEvent = platform.Struct('SDL_MouseButtonEvent',
+                                    [('type', rffi.INT),
+                                     ('button', rffi.INT),
+                                     ('state', rffi.INT),
+                                     ('x', rffi.INT),
+                                     ('y', rffi.INT)])
+    
+    MouseMotionEvent = platform.Struct('SDL_MouseMotionEvent',
+                                    [('type', rffi.INT),
+                                     ('state', rffi.INT),
+                                     ('x', rffi.INT),
+                                     ('y', rffi.INT),
+                                     ('xrel', rffi.INT),
+                                     ('yrel', rffi.INT)])
+    
+
+# ------------------------------------------------------------------------------
 
 for _prefix, _list in _constants.items():
     for _name in _list:
         setattr(CConfig, _name, platform.ConstantInteger(_prefix+_name))
 
+# ------------------------------------------------------------------------------
+
 globals().update(platform.configure(CConfig))
 
+# ------------------------------------------------------------------------------
+
 RectPtr.TO.become(Rect)
 SurfacePtr.TO.become(Surface)
 PixelFormatPtr.TO.become(PixelFormat)
 EventPtr.TO.become(Event)
 KeyboardEventPtr.TO.become(KeyboardEvent)
+MouseButtonEventPtr.TO.become(MouseButtonEvent)
+MouseMotionEventPtr.TO.become(MouseMotionEvent)
 
-Uint8P = lltype.Ptr(lltype.Array(Uint8, hints={'nolength': True}))
+# ------------------------------------------------------------------------------
+
+Uint8P  = lltype.Ptr(lltype.Array(Uint8, hints={'nolength': True}))
 Uint16P = lltype.Ptr(lltype.Array(Uint16, hints={'nolength': True}))
+# need to add signed hint here
+Sint16P = lltype.Ptr(lltype.Array(Sint16, hints={'nolength': True}))
 Uint32P = lltype.Ptr(lltype.Array(Uint32, hints={'nolength': True}))
 
+# ------------------------------------------------------------------------------
+
 def Init(flags):
     if sys.platform == 'darwin':
         from AppKit import NSApplication
         NSApplication.sharedApplication()
     return _Init(flags)
 
-_Init = external('SDL_Init', [Uint32], rffi.INT)
-Quit = external('SDL_Quit', [], lltype.Void)
-SetVideoMode = external('SDL_SetVideoMode', [rffi.INT, rffi.INT,
-                                             rffi.INT, Uint32],
-                        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,
-                                                     Uint32, Uint32,
-                                                     Uint32, Uint32],
-                            SurfacePtr)
-LockSurface = external('SDL_LockSurface', [SurfacePtr], rffi.INT)
-UnlockSurface = external('SDL_UnlockSurface', [SurfacePtr], lltype.Void)
-FreeSurface = external('SDL_FreeSurface', [SurfacePtr], lltype.Void)
-
-MapRGB = external('SDL_MapRGB', [PixelFormatPtr, Uint8, Uint8, Uint8], Uint32)
-GetRGB = external('SDL_GetRGB', [Uint32, PixelFormatPtr,
-                                 Uint8P, Uint8P, Uint8P], lltype.Void)
-GetRGBA = external('SDL_GetRGBA', [Uint32, PixelFormatPtr,
-                                 Uint8P, Uint8P, Uint8P, Uint8P], lltype.Void)
-FillRect = external('SDL_FillRect', [SurfacePtr, RectPtr, Uint32], rffi.INT)
-BlitSurface = external('SDL_UpperBlit', [SurfacePtr, RectPtr, SurfacePtr, RectPtr], rffi.INT)
-SetAlpha = external('SDL_SetAlpha', [SurfacePtr, Uint32, Uint8], rffi.INT)
+# ------------------------------------------------------------------------------
+
+_Init            = external('SDL_Init', 
+                             [Uint32], 
+                             rffi.INT)
+
+Quit             = external('SDL_Quit', [], 
+                            lltype.Void)
+
+SetVideoMode     = external('SDL_SetVideoMode', 
+                             [rffi.INT, rffi.INT, rffi.INT, Uint32],
+                             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,
+                              Uint32, Uint32, Uint32, Uint32],
+                             SurfacePtr)
+
+LockSurface      = external('SDL_LockSurface', 
+                             [SurfacePtr], 
+                             rffi.INT)
+
+UnlockSurface    = external('SDL_UnlockSurface', 
+                             [SurfacePtr],
+                             lltype.Void)
+
+FreeSurface      = external('SDL_FreeSurface', 
+                             [SurfacePtr],
+                             lltype.Void)
+
+MapRGB           = external('SDL_MapRGB', 
+                             [PixelFormatPtr, Uint8, Uint8,  Uint8], 
+                             Uint32)
+
+GetRGB           = external('SDL_GetRGB',
+                             [Uint32, PixelFormatPtr, Uint8P, Uint8P, Uint8P], 
+                             lltype.Void)
+
+GetRGBA          = external('SDL_GetRGBA', 
+                             [Uint32, PixelFormatPtr, Uint8P, Uint8P, 
+                             Uint8P, Uint8P], 
+                             lltype.Void)
+
+FillRect         = external('SDL_FillRect', 
+                             [SurfacePtr, RectPtr, Uint32], 
+                             rffi.INT)
+
+BlitSurface      = external('SDL_UpperBlit', 
+                             [SurfacePtr, RectPtr, SurfacePtr,  RectPtr], 
+                             rffi.INT)
+
+SetAlpha         = external('SDL_SetAlpha', 
+                             [SurfacePtr, Uint32, Uint8], 
+                             rffi.INT)
+
+ShowCursor       = external('SDL_ShowCursor',
+                            [rffi.INT],
+                            rffi.INT)

Modified: pypy/dist/pypy/rlib/rsdl/constants.py
==============================================================================
--- pypy/dist/pypy/rlib/rsdl/constants.py	(original)
+++ pypy/dist/pypy/rlib/rsdl/constants.py	Mon May 19 11:28:24 2008
@@ -48,6 +48,10 @@
         "MOUSEMOTION",
         "MOUSEBUTTONDOWN",
         "MOUSEBUTTONUP",
+        "BUTTON_LEFT",
+        "BUTTON_LEFT", 
+        "BUTTON_MIDDLE", 
+        "BUTTON_RIGHT",
         "JOYAXISMOTION",
         "JOYBALLMOTION",
         "JOYHATMOTION",
@@ -69,6 +73,9 @@
         "HAT_LEFTDOWN",
         "HAT_LEFT",
         "HAT_LEFTUP",
+        
+        "DISABLE",
+        "ENABLE",
 
         # the following ones are not exposed in Pygame
         "INIT_VIDEO",

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	Mon May 19 11:28:24 2008
@@ -1,3 +1,4 @@
+
 import py, sys
 from pypy.rlib.rsdl import RSDL
 from pypy.rlib.rarithmetic import r_uint
@@ -54,28 +55,110 @@
         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:
+        print "    Use Escape to quit."
+        event = lltype.malloc(RSDL.Event, flavor='raw')
+        try:
+            while True:
+                    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_mousemove(self):
+        if not self.is_interactive:
+            py.test.skip("interactive test only")
+        print
+        print "Move the Mouse up and down:"
+        print "    Use Escape to quit."
+        event = lltype.malloc(RSDL.Event, flavor="raw")
+        directions = [False]*4
+        try:
+            while True:
                 ok = RSDL.WaitEvent(event)
                 assert rffi.cast(lltype.Signed, ok) == 1
-                c_type = rffi.getintfield(event, 'c_type')
-                if c_type == RSDL.KEYDOWN:
+                c_type = rffi.getintfield(event, "c_type")
+                if c_type == RSDL.MOUSEMOTION:
+                    m = rffi.cast(RSDL.MouseMotionEventPtr, event)
+                    assert rffi.getintfield(m, "c_x") >= 0
+                    assert rffi.getintfield(m, "c_y") >= 0
+                    print rffi.getintfield(m, "c_xrel")
+                    directions[0] |= rffi.getintfield(m, "c_xrel")>0
+                    directions[1] |= rffi.getintfield(m, "c_xrel")<0
+                    directions[2] |= rffi.getintfield(m, "c_yrel")>0
+                    directions[3] |= rffi.getintfield(m, "c_yrel")<0
+                    if False not in directions:
+                        break
+                elif c_type == RSDL.KEYUP:
                     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')
+                        print "    test manually aborted"
+                        py.test.fail(" mousemovement test aborted")
+                        break  
+        finally:
+            lltype.free(event, flavor='raw')
+                
+        
 
+
+    def test_mousebutton(self):
+        if not self.is_interactive:
+            py.test.skip("interactive test only")
+        print
+        print "Press the given MouseButtons:"
+        print "        Use Escape to quit."
+        
+        event_tests = [("left button",   RSDL.BUTTON_LEFT),
+                       ("middle button", RSDL.BUTTON_MIDDLE),
+                       ("right button",  RSDL.BUTTON_RIGHT)]
+        test_success = []
+        event = lltype.malloc(RSDL.Event, flavor='raw')
+        try:
+            for button_test in event_tests:
+                print "    press %s:" % button_test[0]
+                while True:
+                    ok = RSDL.WaitEvent(event)
+                    assert rffi.cast(lltype.Signed, ok) == 1
+                    c_type = rffi.getintfield(event, 'c_type')
+                    if c_type == RSDL.MOUSEBUTTONDOWN:
+                        pass
+                    elif c_type == RSDL.MOUSEBUTTONUP:
+                        b = rffi.cast(RSDL.MouseButtonEventPtr, event)
+                        if rffi.getintfield(b, 'c_button') == button_test[1]:
+                            test_success.append(True)
+                            break
+                    elif c_type == RSDL.KEYUP:
+                        p = rffi.cast(RSDL.KeyboardEventPtr, event)
+                        if rffi.getintfield(p.c_keysym, 'c_sym') == RSDL.K_ESCAPE:
+                            test_success.append(False) 
+                            print "        manually aborted"
+                            break
+                        #break
+            if False in test_success:
+                py.test.fail("")
+        finally:
+            lltype.free(event, flavor='raw')
+                
+        
+    def test_show_hide_cursor(self):
+        RSDL.ShowCursor(RSDL.DISABLE)
+        self.check("Is the cursor hidden? ")
+        RSDL.ShowCursor(RSDL.ENABLE)
+        self.check("Is the cursor shown? ")
+        
     def test_blit_rect(self):
         surface = RSDL.CreateRGBSurface(0, 150, 50, 32,
                                         r_uint(0x000000FF),
@@ -85,6 +168,15 @@
         fmt = surface.c_format
         color = RSDL.MapRGB(fmt, 255, 0, 0)
         RSDL.FillRect(surface, lltype.nullptr(RSDL.Rect), color)
+        
+        paintrect = lltype.malloc(RSDL.Rect, flavor='raw')
+        rffi.setintfield(paintrect, 'c_x',  75)
+        rffi.setintfield(paintrect, 'c_y',  0)
+        rffi.setintfield(paintrect, 'c_w', 150)
+        rffi.setintfield(paintrect, 'c_h',  50)
+        color = RSDL.MapRGB(fmt, 255, 128, 0)
+        RSDL.FillRect(surface, paintrect, color)
+        
         dstrect = lltype.malloc(RSDL.Rect, flavor='raw')
         try:
             rffi.setintfield(dstrect, 'c_x',  10)
@@ -96,7 +188,7 @@
         finally:
             lltype.free(dstrect, flavor='raw')
         RSDL.FreeSurface(surface)
-        self.check("Red rectangle(150px * 50px) at the top left, 10 pixels from the border")
+        self.check("Half Red/Orange rectangle(150px * 50px) at the top left, 10 pixels from the border")
 
     def teardown_method(self, meth):
         RSDL.Quit()



More information about the Pypy-commit mailing list