3
votes

I'm looking for a way to draw smooth animations in Python. I want to use cairo, partly because I like the sub-pixel filtering and partly because I'm familiar with the API. My first approach was to use a GTK.DrawingArea as the target for a cairo surface. While the drawing was quick I couldn't find any reliable way to tie the display / buffering to the vertical sync so the animation was jerky and unreliable.

My next approach was to try PyGame. Using the examples in the wiki as a starting point I've written some simple code to animate various bouncing balls. The different approaches on the linked page fall into two categories:

  1. Draw to an offscreen cairo ImageSurface and then use Numpy to convert the pixel buffer to a Pygame surface.
  2. Share the same memory for both surfaces

The first approach sucks for performance as the conversion takes about 10ms, which is most of the time-slice that I have for 60hz frames. I'm running the code on a Macbook-pro with 2.2Ghz Core2Duo and an Nvidia 8400. The time is very dependent on the size of the surface, this is for a 800x800 window.

The second approach surfaces from the ordering of pixel coordinates. Both cairo and pygame insist that they can only use RGB pixel ordering and don't support conversion. The problem is that when I setup a pygame surface it uses BGRA pixel ordering, which completely shafts me.

So now for the questions:

  1. Is it possible to change the pixel format used by either library on the mac to be compatible with each other?
  2. If it is not possible what is the fastest way to do the conversion purely in Python?
  3. If the fastest Python way still sucks up most of the time for a frame then how can I interface to some C code to do the conversion?
  4. Depending on how complex the interface to C is, how much point is there in using pygame instead of just writing the SDL interface in C?

.

2

2 Answers

2
votes