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

Re: [pygame] I nedd speed



Gabriele Farina wrote:
I'm working on a GUI engine writte using Pygame. I'm at a good point, I
finished all the widgets I need, but I have to know how can I impreve my
script speed. I'd like to know some tricks for speeding up my GUI, in
paticular i'd like to know how can I update only a single part of a surface.
i find the most important optimization for gui stuff is if each control has a "dirty" type flag, and only redrawing the control when it has changed. usually this is the only thing that needs to be done.

You can limit the blitting and drawing on a surface by using the set_clip() method. each surface has a clip area which is a rectangular area on the surface. All the drawing and blitting functions will only effect the area inside the clipped region. The SDL blitters are actually optimized for the clipping, and will just ignore the areas outside the blit.


Other 2 things:
1) Using sprites makes the script work slower or not??
the pygame sprite stuff is fairly optimized. i would assume it would be as fast or faster than anything similar you implement yourself. especially if you render your sprites with the 'update regions', which SDL usually needs for full speed. It is a lot of work to get those update regions done correctly, and the sprite groups handle it all automatic.

2) How does surfaces-lock work??When (and why) I have to use it??
surface locking is usually only needed for video surfaces. these are surfaces with the HWSURFACE flag enabled. it means the pixel data for the surface lives on the graphics card memory. in order to access these pixels you need to "lock" them, otherwise the graphics card may feel free to move them around inside its memory. it is safe to lock a surface that doesn't need it though, and that won't be a performance issue either.

with pygame there is really no need to worry about the locking. all pygame functions will make sure things are locked when they need to be. you can optimize batches of functions that need locking by making sandwiching them inside lock() and unlock() calls.

generally you can think that any function that needs to access the pixel data needs the pixels locked. the big exception to this is the blitting functions, since they graphics card can do the blitting in hardware if the surface already lives in graphics card ram.

for the most part though, i'd say don't worry about the locking, pygame handles it all for you.