Jp Calderone (jcalderone) wrote,
Jp Calderone

tricks for good 2d rendering performance with pygame

pygame's API for initializing a display has a couple attractive sounding flags. Or at least, they sound attractive once you notice that updating your 320x240 window at 30fps is consuming all available cycles on your brand new Intel i9 CPU. They're HWSURFACE and DOUBLEBUF. Hardware surfaces and double buffering is how you make graphics fast, right?

Well... no. You probably can't get a hardware surface anyway, and double buffering is unlikely to help until you figure out how to have a window larger than 320x240 anyway.

What you really need to do to get reasonable rendering performance is make sure that any images you blit to the display have the same color depth. You can do this with the Surface.convert method (you get back a Surface when you load an image from a file, eg a png or a gif). Check the depth of your display surface and then convert your image surfaces to that depth (and just keep the converted versions). Blitting them will get hundreds of times faster.

It's a pretty simple thing, but it's easy to get distracted by HWSURFACE and not notice the depth mismatch (like I did for three days).

Tags: performance, pygame, python

  • Upgrading from buildbot 0.7.12 to 0.8.2

    Some notes from my experience upgrading Twisted's buildmaster installation: Jinja2 is at, not the URL in the BuildBot…

  • pyn is dead, long live pyn

    Just about eight years ago I started working on an IRC bot using Twisted, pynfo. I managed to stay interested in working on it for about six months.…

  • filepath 0.1

    Hello all, I'm happy to announce the initial release of filepath. filepath is an abstract interface to the filesystem. It provides APIs for path…

Comments for this post were disabled by the author