pygame per-pixel alpha performance

Started by
4 comments, last by Kipple 16 years ago
I'm not trying to practice my alliteration skills. (: My team is developing a 2D TBS game using pygame. So far things are going pretty smoothly and I think pygame has cut our development time into half. We have also succeeded in compiling our game client into an exe using py2exe. Based on our positive experience, I would recommend this setup (Python + pygame [+ py2exe]) to any game programmers, for a quick start. Anyway, onto the main point. We have recently introduced per-pixel alphas. Stuff looks really nice and we are able to do some image scaling using PIL (previously impossible because antialiasing was smudging our pink backgrounds into the image). However, we have noticed a significant performance drop. When the game runs full-screen FPS drops to about 10. I've read the pygame documentation about per-pixel alpha and it basically said it's the slowest. Surely there's a way to make per-pixel happen. Does anyone have any experience with this by any chance and is there anything we might have overlooked? We are currently using software rendering - how much of a gain could we hope for by going to hardware? Thanks for any feedback!
Advertisement
I'd recommend using PyOpenGL. If you keep your graphics code isolated, you can even have two modules (or objects, whatever you want to do) with the same API, one for pure pygame and one for OpenGL. That's what I did for my project.

- Kef
Per-pixel anything is slow, and moving to hardware and still working on a per-pixel basis will probably make matters worse.

What effect are you trying to achieve with what you call 'per-pixel alpha'?
Using 3D graphics hardware to draw your images should get you both scaling and per pixel alpha blending almost for free. A quick look at the pygame website suggests it should be able to make use of OpenGL and D3D for rendering.

If you want to speed up the software per pixel alpha blending then one standard trick is to premultiply the source image by the alpha, but I have a feeling pygame may not support the correct blend mode for using premultiplied images (dest = source + dest * inverse-source-alpha).
I should clarify my last post - when I said 'moving to hardware' I meant having PyGame surfaces using 'hardware'. This is not the same as 3D acceleration! Moving to proper acceleration will bring large benefits. In particular I'd just recommend dumping PyGame for pyglet, which gives you fully hardware accelerated sprites from the start.
Thanks for the suggestions! Pyglet looks pretty useful and the community around it seems active. If we decide to adopt, we'll probably ween ourselves off of pygame one module at a time.

Per-pixel alpha was originally introduced in our code because the only good scaling modes in Python Image Library would cause pink edges in our sprites from smudging with the background. Now we can simply omit the background. A side effect of using per-pixel alpha is that we can also have other neat features like sprites with a "glowing" effect.

This topic is closed to new replies.

Advertisement