Commits

Anonymous committed 7ef8577

Added tests for sprite and timer

Comments (0)

Files changed (4)

 
     # Note SDL_SetTimer actually returns 1 on success, not 0 as documented
     # in SDL_timer.h.
-    result = _SDL_SetTimer(interval, _SDL_TimerCallback(callback))
+    if callback:
+        result = _SDL_SetTimer(interval, _SDL_TimerCallback(callback))
+    else:
+        result = _SDL_SetTimer(interval, _SDL_TimerCallback())
     if result == -1:
         raise SDL.error.SDL_Exception, SDL.error.SDL_GetError()
 
 drop opengl.h from constants.py
 SetCaption unicode
 byte swapping in array, esp for audio
+endian check, e.g. for overlay and gl tests
+glitch in upper-left corner of testoverlay2
 
 SDL_syswm.h - platform dependent
 

test/testsprite.py

+#!/usr/bin/env python
+
+'''
+'''
+
+__docformat__ = 'restructuredtext'
+__version__ = '$Id: $'
+
+from copy import copy
+import os
+import random
+import sys
+
+from SDL import *
+
+ICON_BMP = os.path.join(os.path.dirname(sys.argv[0]), 'icon.bmp')
+NUM_SPRITES = 100
+MAX_SPEED = 1
+
+sprite = None
+numsprites = 0
+sprite_rects = None
+positions = None
+velocities = None
+sprites_visible = 0
+debug_flip = 0
+sprite_w = 0
+sprite_h = 0
+
+def LoadSprite(file):
+    global sprite
+    sprite = SDL_LoadBMP(file)
+
+    if sprite.format.palette:
+        SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY | SDL_RLEACCEL), 
+                        sprite.pixels[0])
+
+    temp = SDL_DisplayFormat(sprite)
+    SDL_FreeSurface(sprite)
+    sprite = temp
+
+t = 0
+def MoveSprites(screen, background):
+    global t, sprite_rects, positions, velocities, sprites_visible
+
+    if sprites_visible:
+        SDL_FillRect(screen, None, background)
+
+    sprite_rects = []
+    for i in range(numsprites):
+        position = positions[i]
+        velocity = velocities[i]
+        position.x += velocity.x
+        if position.x < 0 or position.x >= (screen.w - sprite_w):
+            velocity.x = -velocity.x
+            position.x += velocity.x
+        position.y += velocity.y
+        if position.y < 0 or position.y >= (screen.h - sprite_h):
+            velocity.y = -velocity.y
+            position.y += velocity.y
+        area = copy(position)
+        SDL_BlitSurface(sprite, None, screen, area)
+        sprite_rects.append(area)
+
+    if debug_flip:
+        if screen.flags & SDL_DOUBLEBUF:
+            color = SDL_MapRGB(screen.format, 255, 0, 0)
+            r = SDL_Rect((math.sin(t*2*math.pi) + 1) / 2 * (screen.w - 20),
+                         0, 20, screen.h)
+            SDL_FillRect(screen, r, color)
+            t += 2
+
+    if screen.flags & SDL_DOUBLEBUF:
+        SDL_Flip(screen)
+    else:
+        SDL_UpdateRects(screen, sprite_rects)
+    sprites_visible = 1
+
+def FastestFlags(flags, width, height, bpp):
+    flags |= SDL_FULLSCREEN
+
+    info = SDL_GetVideoInfo()
+    if info.blit_hw_CC and info.blit_fill:
+        flags |= SDL_HWSURFACE
+
+    if flags & SDL_HWSURFACE:
+        if info.video_mem*1024 > height * width * bpp / 8:
+            flags |= SDL_DOUBLEBUF
+        else:
+            flags &= ~SDL_HWSURFACE
+
+    return flags
+
+if __name__ == '__main__':
+    SDL_Init(SDL_INIT_VIDEO)
+
+    numsprites = NUM_SPRITES
+    videoflags = SDL_SWSURFACE | SDL_ANYFORMAT
+    width = 640
+    height = 480
+    video_bpp = 8
+    debug_flip = 0
+
+    i = 1
+    while i < len(sys.argv):
+        arg = sys.argv[i]
+        if arg == '-width':
+            i += 1
+            width = int(sys.argv[i])
+        elif arg == '-height':
+            i += 1
+            height = int(sys.argv[i])
+        elif arg == '-bpp':
+            i += 1
+            video_bpp = int(sys.argv[i])
+            videoflags &= ~SDL_ANYFORMAT
+        elif arg == '-fast':
+            videoflags = FastestFlags(videoflags, width, height, video_bpp)
+        elif arg == '-hw':
+            videoflags ^= SDL_HWSURFACE
+        elif arg == '-flip':
+            videoflags ^= SDL_DOUBLEBUF
+        elif arg == '-debugflip':
+            debug_flip ^= 1
+        elif arg == '-fullscreen':
+            videoflags ^= SDL_FULLSCREEN
+        elif arg[0].isdigit():
+            numsprites = int(arg)
+        else:
+            print >> sys.stderr, ('Usage: %s [-bpp N] [-hw] [-flip] ' + \
+                '[-fast] [-fullscreen] [numsprites]') % sys.argv[0]
+            sys.exit(1)
+        i += 1
+
+    screen = SDL_SetVideoMode(width, height, video_bpp, videoflags)
+
+    LoadSprite(ICON_BMP)
+    
+    sprite_w = sprite.w
+    sprite_h = sprite.h
+    positions = []
+    velocities = []
+    for i in range(numsprites):
+        positions.append(SDL_Rect(random.randint(0, screen.w - sprite.w),
+                                  random.randint(0, screen.h - sprite.h),
+                                  sprite.w, sprite.h))
+        velocities.append(SDL_Rect(random.randint(0,2)*2-1,
+                                   random.randint(0,2)*2-1,
+                                   0, 0))
+    background = SDL_MapRGB(screen.format, 0, 0, 0)
+
+    print 'Screen is at %d bits per pixel' % screen.format.BitsPerPixel
+    if screen.flags & SDL_HWSURFACE:
+        print 'Screen is in video memory'
+    else:
+        print 'Screen is in system memory'
+    if screen.flags & SDL_DOUBLEBUF:
+        print 'Screen has double-buffering enabled'
+    if sprite.flags & SDL_HWSURFACE:
+        print 'Sprite is in video memory'
+    else:
+        print 'Sprite is in system memory'
+
+    # Run a sample blit to trigger blit acceleration
+    dst = SDL_Rect(0, 0, sprite.w, sprite.h)
+    SDL_BlitSurface(sprite, None, screen, dst)
+    SDL_FillRect(screen, dst, background)
+
+    if sprite.flags & SDL_HWACCEL:
+        print 'Sprite blit uses hardware acceleration'
+    if sprite.flags & SDL_RLEACCEL:
+        print 'Sprite blit uses RLE acceleration'
+
+    frames = 0
+    then = SDL_GetTicks()
+    done = 0
+    sprites_visible = 0
+    while not done:
+        frames += 1
+        event = SDL_PollEventAndReturn()
+        while event:
+            if event.type == SDL_MOUSEBUTTONDOWN:
+                SDL_WarpMouse(screen.w/2, screen.h/2)
+            elif event.type in (SDL_KEYDOWN, SDL_QUIT):
+                done = 1
+            event = SDL_PollEventAndReturn()
+        MoveSprites(screen, background)
+
+    SDL_FreeSurface(sprite)
+    now = SDL_GetTicks()
+    if now > then:
+        print '%2.2f frames per second' % (frames * 1000.0 / (now - then))
+
+    SDL_Quit()

test/testtimer.py

+#!/usr/bin/env python
+
+'''Test program to check the resolution of the SDL timer on the current
+platform.
+'''
+
+__docformat__ = 'restructuredtext'
+__version__ = '$Id: $'
+
+import sys
+
+from SDL import *
+
+DEFAULT_RESOLUTION = 1
+
+ticks = 0
+
+def ticktock(interval):
+    global ticks
+    ticks += 1
+    return interval
+
+def callback(interval, param):
+    print 'Timer %d : param = %d' % (interval, param)
+    return interval
+
+if __name__ == '__main__':
+    SDL_Init(SDL_INIT_TIMER)
+
+    desired = 0
+    if len(sys.argv) > 1:
+        desired = int(sys.argv[1])
+    if desired == 0:
+        desired = DEFAULT_RESOLUTION
+
+    SDL_SetTimer(desired, ticktock)
+
+    print 'Waiting 10 seconds'
+    SDL_Delay(10*1000)
+
+    SDL_SetTimer(0, None)
+    if ticks:
+        print 'Timer resolution: desired = %d ms, actual = %f ms' % \
+            (desired, 10*1000.0/ticks)
+
+    print 'Testing multiple timers...'
+    t1 = SDL_AddTimer(100, callback, 1)
+    t2 = SDL_AddTimer(50, callback, 2)
+    t3 = SDL_AddTimer(233, callback, 3)
+
+    print 'Waiting 10 seconds'
+    SDL_Delay(10*1000)
+    print 'Removing timer 1 and waiting 5 more seconds'
+    SDL_RemoveTimer(t1)
+    SDL_Delay(5*1000)
+
+    SDL_RemoveTimer(t2)
+    SDL_RemoveTimer(t3)
+
+    SDL_Quit()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.