[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Fast simple transposition (90-degree rotation)?



On Jun 10, 2008, at 12:24 AM, Terry Hancock wrote:

What's the best way to do (exact) 90-degree rotations in PyGame?

There aren't many options, so I suspect pygame.transform.rotate() is the best way. Have you tried it for your application?

If that isn't fast enough, your next best option is probably OpenGL, which will let you push the work off to the graphics hardware. The overhead then becomes shuttling the bits back and forth to the graphics card, which can be avoided if you use OpenGL for everything and the image is static.

There doesn't seem to be a separate "transpose" operation in the
"transform" module (I.e. an operation that swaps x & y coordinates).

Does the "rotate" function special-case 90-degree interval rotations?

I believe so.

Or is it more efficient to convert a PyGame Surface to a Python Imaging
Library image and then transpose that? (PIL does have a separate
"transpose" method).

Doubtful, though it would be easy for you to test ;^) (the timeit module and cProfile are your friends)

How efficient can this be? If I need to get a mirror image or a
transposition of an existing surface (say a full-screen 1024x768 image),
is it worth caching the result to disk? Or would it be faster to just
flip it in memory whenever I need it? (IOW, how does a 90-degree
rotation or a "flip" compare to a disk I/O on most computers?)

Caching to disk would be very slow, because you not only have to read and write from disk, you also have to recreate the surface in memory. On modern machines caching to disk would actually mean caching to RAM (unless the surface is too big to fit), but it might occasionally be very slow if the cached copy got flushed from memory and the disk was busy when you needed the cached copy. IOW the performance would fluctuate randomly which is usually a bad thing[tm]

BTW, I tried to look this up, but I can't find a discussion of it in the
documentation, and wasn't sure how to look it up in the ML archives.

Here are some thoughts:

If the surface changes frequently (like once per frame), you have little choice than to do the transform every frame.

If the surface changes infrequently, do the rotate every time it changes and cache the rotated surface. On a modern machine I doubt it will consume enough RAM to matter, but it all depends how many of these surfaces you have.

If the surface changes infrequently, and pygame.transform.rotate() is too slow or you want to conserve RAM for something else, use OpenGL to have the video card memory and GPU handle things.

Further general thought: Start by doing the simplest thing that could possibly work, and only do something more complicated if that doesn't work.

-Casey