Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid tearing with pygame on Linux/X11

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 ?

like image 256
timday Avatar asked Jul 04 '09 16:07

timday


People also ask

Does pygame have VSync?

Pygame lets you turn on VSync, and in Pygame 2, you can do this simply by passing the pygame. SCALED flag and the vsync=1 argument to set_mode() . Now your game will have silky smooth animations and scrolling 5!


2 Answers

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.

like image 156
Zoomulator Avatar answered Sep 19 '22 14:09

Zoomulator


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.

like image 33
timday Avatar answered Sep 21 '22 14:09

timday