3

現在、pycairo を使用して描画し、PyGame を介して SDL サーフェスにレンダリングするコードがいくつかあります。これは Linux と Windows では問題なく動作しますが、Mac では頭が痛くなります。すべてが青またはピンクになります バイトオーダーは ARGB ではなく BGRA のようです。これは、Mac (osx 10.6) でのみ壊れています。

import cairo
import pygame

width = 300
height = 200

pygame.init()
pygame.fastevent.init()
clock = pygame.time.Clock()
sdl_surface = pygame.display.set_mode((width, height))

c_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
ctx = cairo.Context(c_surface)

while True:
    pygame.fastevent.get()
    clock.tick(30)
    ctx.rectangle(10, 10, 50, 50)
    ctx.set_source_rgba(0.0, 0.0, 0.0, 1.0)
    ctx.fill_preserve()

    dest = pygame.surfarray.pixels2d(sdl_surface)
    dest.data[:] = c_surface.get_data()
    pygame.display.flip()

配列のスライスまたは PIL を使用して修正できますが、フレーム レートが低下します。これをインプレースまたは設定で行う方法はありますか?

4

1 に答える 1

2

多くの髪を引き裂いた後、配列を逆にするだけでフレームレートをあまり損なわない回避策があります。

import cairo
import pygame

width = 300
height = 200

pygame.init()
pygame.fastevent.init()
clock = pygame.time.Clock()
sdl_surface = pygame.display.set_mode((width, height))

c_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
ctx = cairo.Context(c_surface)

while True:
    pygame.fastevent.get()
    clock.tick(30)
    ctx.rectangle(10, 10, 50, 50)
    ctx.set_source_rgba(1.0, 0.0, 0.0, 1.0)
    ctx.fill_preserve()

    dest = pygame.surfarray.pixels2d(sdl_surface)
    dest.data[:] = c_surface.get_data()[::-1]
    tmp = pygame.transform.flip(sdl_surface, True, True)
    sdl_surface.fill((0,0,0))    #workaround to clear the display
    del dest     #this is needed to unlock the display surface
    sdl_surface.blit(tmp, (0,0))
    pygame.display.flip()

一時的なサーフェスから配列とブリットを削除する必要があるという事実は正しくないように見えますが、それがディスプレイを反転させる唯一の方法でした。ここでよりクリーンな提案がある場合は、コメントしてください。

于 2012-09-04T14:25:00.877 に答える