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

Re: [pygame] FastRenderGroup



Hello

Payback time: since I'm using pygame quite a while now, I though I can give something back.


Perhpas some of you remember my questions about dirty rects and other related optimizations about one year ago. Now I have produced a (hopefully) usefull code. I have written a FastRenderGroup. It is a complete rewrite and it is a bit faster too than the version from last year.

It is one of the fastest render algorithmer known to me (at least for pygame written in pure python). It uses dirty sprites and rects and has a layer system. It will switch automatically to fullscreen update if dirty rects are slower. It can be used with normal pygame.sprite.Sprite but preferable with the DirtySprite because it provides you with some extended functionality (new attributes: dirty, layer, visible, blendmode). The only Limitation it has is that all sprites must be in this FastRenderGroup for a correct result (but most games have a group which contains all sprites anyway). But I have written it that way that you can add normal pygame.sprite.Sprite to that group too (for exitsting code). It will fill in the missing attributes. The only attribute that must be set is the image attribute, any other attributes will be set by the add() method to default values.


I also have written a small demo program (for many of you it will look familar ;-) ) to demonstrate how to use my FastRenderGroup (although all know how to use pygame.sprite.Group) and to see how fast it really is.

It is fast, but if you have enough sprites then a full screen udpate will be faster. On my machine (Pentium M 1.5 Ghz, 1GB ram, ATI Radeon 9600) that is the case around 130 sprites (of this demo code). It is because of the overhead involved using dirty rects (should beas minimal as possible). I wonder, how many sprite you can diplay until it starts to use full screen update.

Another thing is, if the it is stuttering from time to time, I assume that is either because of the garbagecollector or the CPU hogging (because I run it as fast as possible and never let other programs run).

Use the keys 'a' and 'q' to add and remove sprites from screen. Pressing 'f' toggles to 10fps mode and back to "fast as possible". Start it from the console, then you will see when it is using dirty rects and what the current fps is. It seems to me that on AMD machines the render code will execute slower ( I do not know why).

If you want to try it out, you have to add a "background"-sprite (bgd-sprite) (because it handles only sprites). The bgd-sprite should be as big as the screen or if you are using clipping, as big as the dawing area of the FastRenderGroup. If you forget the bgd-sprite, then the sprites will leave a trail on screen.

If you see, that anything in the code can be done even faster, then feel free to tell me. I hope that you are pleased and I would not mind if it would be included in the standart distribution of pygame (after some more test).

If you find anything behave strangly or not working right please report it back (as specific as possible). Any other suggestions are welcome too.

~DR0ID


PS: sorry for the long email and my bad english style.

Attachment: RendererGroup_demo.py
Description: application/python

Attachment: RenderGroup.py
Description: application/python