How to avoid tearing with pygame on Linux/X11

╄→гoц情女王★ 提交于 2019-12-06 01:39:23

问题


I've been playing with pygame (on Debian/Lenny). It seems to work nicely, except for annoying tearing of blits (fullscreen or windowed mode).

I'm using the default SDL X11 driver. Googling suggests that it's a known issue with SDL that X11 provides no vsync facility (even with a display created with FULLSCREEN|DOUBLEBUF|HWSURFACE flags), and I should use the "dga" driver instead.

However, running

SDL_VIDEODRIVER=dga ./mygame.py

throws in pygame initialisation with

pygame.error: No available video device

(despite xdpyinfo showing an XFree86-DGA extension present).

So: what's the trick to getting tear-free vsynced flips ? Either by getting this dga thing working or some other mechanism ?


回答1:


Well my eventual solution was to switch to Pyglet, which seems to support OpenGL much better than Pygame, and doesn't have any flicker problems.




回答2:


The best way to keep tearing to a minimum is to keep your frame rate as close to the screen's frequency as possible. The SDL library doesn't have a vsync unless you're running OpenGL through it, so the only way is to approximate the frame rate yourself. The SDL hardware double buffer isn't guaranteed, although nice when it works. I've seldomly seen it in action.

In my experience with SDL you have to use OpenGL to completely eliminate tearing. It's a bit of an adjustment, but drawing simple 2D textures isn't all that complicated and you get a few other added bonuses that you're able to implement like rotation, scaling, blending and so on.

However, if you still want to use the software rendering, I'd recommend using dirty rectangle updating. It's also a bit difficult to get used to, but it saves loads of processing which may make it easier to keep the updates up to pace and it avoids the whole screen being teared (unless you're scrolling the whole play area or something). As well as the time it takes to draw to the buffer is at a minimum which may avoid the blitting taking place while the screen is updating, which is the cause of the tearing.



来源:https://stackoverflow.com/questions/1082562/how-to-avoid-tearing-with-pygame-on-linux-x11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!