我正在寻找一种方法来提高PySDL2的性能,从而渲染出大量的精灵。贝娄是我的尝试,试图尽可能多地使用硬件渲染:
import sdl2.ext as sdl2ext, sdl2
import sys
from random import randint
def run():
sdl2ext.init()
disp_mode = sdl2.video.SDL_DisplayMode()
sdl2.SDL_GetDesktopDisplayMode(0, disp_mode)
window = sdl2ext.Window('pysdl2 benchmark', size=(disp_mode.w, disp_mode.h),
flags=sdl2.SDL_WINDOW_SHOWN | sdl2.SDL_WINDOW_FULLSCREEN_DESKTOP)
window_w, window_h = window.size
window.show()
renderer = sdl2ext.Renderer(window)
factory = sdl2ext.SpriteFactory(sprite_type=sdl2ext.TEXTURE,
renderer=renderer)
spr_image = sdl2ext.image.load_image('big_sprite.png')
sprites = [factory.from_surface(spr_image) for _ in xrange(100)]
for spr in sprites:
spr.position = randint(0,160), randint(0,90)
spr.w, spr.h = spr.size
spr.dx = randint(1,16); spr.dy = randint(1,16)
renderer_spr = sdl2.ext.TextureSpriteRenderSystem(renderer)
running = True
while running:
events = sdl2ext.get_events()
for event in events:
if event.type == sdl2.SDL_QUIT:
running = False
break
renderer.clear(COLOR_CARMINE) # instance of sdl2ext.Color
renderer_spr.render(sprites)
renderer.present()
# Updating sprites positions
for spr in sprites:
if spr.x + spr.w + spr.dx > window_w or \
spr.x + spr.dx < 0:
spr.dx = -spr.dx
if spr.y + spr.h + spr.dy > window_h or \
spr.y + spr.dy < 0:
spr.dy = -spr.dy
spr.x += spr.dx
spr.y += spr.dy
#window.refresh()
sdl2ext.quit()
return 0
由于PySDL2的文档相当稀疏,我很好奇,如果我可能错过了一些重要的东西,而且编写的代码在某种程度上是不理想的。