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

Re: [pygame] sprite engine



Brian Fisher wrote:

The method of only drawing sprites where they are dirty (i.e. they
animated or move) you can save quite a lot of time with the right
game, but if you have moving objects behind your static ones, you need
to worry about marking places dirty both before and after you draw if
you want to get things drawn right, in all cases...

Yes, you have to be careful with that. You must dirty before you change the rect, AND after you change the rect... to both erase the sprite in its old position, and draw it in the new position.


Back when I maintained 2 rectangles, I had dirty rectcangle bugs, but not any more. I call a routine 'SetRect' that does both for me so I don't have to think about it. Set rect does the following:

1. Dirties the screen with the sprite rectangle
2. Updates the rectangle to reflect the new position and size
3. Dirties the screen again with the new rectangle.

I call setrect when a picture is 'painted' (loaded), and when it is moved - that's it. It's worked like a charm for me, and has been one of the areas of code I haven't had to revisit, ever.

I perfer to
avoid using approaches like that in favor of figuring out what simple
game-specific constraints you can exploit in a simple and provably
reliable way (like maybe you can preflatten a bunch of static objects
in a layer and erase to that, etc.) because I really dislike having to
fix graphical bugs and flicker (it's quite hard to write unit tests
for them, for instance)


I use that too. My worldbuilder creates quite complex, realistic looking screens, and flatting them into a single 'walkon' object saves quite a bit of CPU time on older machines. It's flattened outside of worldbuilding mode... inside of worldbuilding mode, they're individual objects so she can pick up and move them.


Like I said - it's quite an elaborate, involved program I'm making. Game programming is harder than business app programming, by quite a bit. :-D

--Kamilche