I have lots of sprites with alpha but only few of them moving at
a time, so drawing and updating only the areas in which
something is moving should be lots faster?!
Yes, of course.
The basic idea is to keep track of the areas of the screen that you
paint. For example, let’s say you have a background image. You blit the
character over it and update the screen. Next frame you move the
character, so you repaint the background over the old position of the
character, and blit the character in its new position. Note that these
two operations just touch the areas of the screen that actually change.
You keep track of these rectangles, where the contents of the back
buffer and the front buffer don’t match - and are called “dirty”, as in
After you processed your frame, you have the new frame you want to
display in the back buffer, the old frame still in the front buffer and
therefore on screen, and a list of rectangles specifying the parts of
the back and front buffers that don’t match. You then copy only these
areas from the back buffer to the front buffer, and discard the
Of course this is only the general idea, there’s a lot of detail to
discuss. I described double buffering here, but it will work with page
flipping too, with some modifications.
One of the important points is what you do with what I just described as
a “rectangle list”. It’s not that easy in practice. Yes, you could
update every rect in the list, but you could get a lot of overlap and
therefore extra work.
You can do some rectangle merging and splitting code. You can also use
tile arrays (divide your screen in fixed size squares, say 64x64, and
whenever a dirty rect overlaps a tile, mark the whole tile as dirty) -
draws more than needed, but it’s very easy to implement and performs
well. You can go further and use micro tile arrays, which involve
keeping track of dirty sub-rectangles inside each tile and merge them
before updating, which tends to be easier than a general rectangle
merge, and does a lot less overdraw. This is what I’m currently using.
Hope that helps! As always, Google is your friend.