Simplest way would be to determine the size of your sprite and where its
going. Then blit a background area the size of your sprite, beginning
at the area where your sprite is supposed to appear to an offscreen
buffer and THEN draw your sprite. After you’re done with that, to erase
your sprite, blit the saved background area back where you got it (clean
background again), and then redraw your sprite at the new location. I
think that there may be more sophisticated ways of doing it, but this is
the cleanest way I know.
One problem with this is when sprites overlap. You want to make sure that
you create all of your save buffers before drawing anything… then
draw all of your sprites.
In Circus Linux! I do something kind of like this, except I have an
animated background, so I keep the entire background bitmap in memory all
the time.
My first step is to draw pieces of the background based on where all of the
objects currently are. For example:
dest.x = object[i].x;
dest.y = object[i].y;
dest.w = 32;
dest.h = 32;
SDL_BlitSurface(background, &dest, screen, &dest);
Then I move everything:
for (i = 0; i < NUM_OBJECTS; i++)
{
object[i].x = …;
object[i].y = …;
}
Then I draw it all:
dest.x = object[i].x;
dest.y = object[i].y;
dest.w = 32;
dest.h = 32;
SDL_BlitSurface(sprite, NULL, screen, &dest);
Now it’s time to actually make all of this appear. I believe I keep track
of all of the rectangular areas I draw in:
SDL_UpdateRects(screen, rects, num_rects);
If tons more were being drawn on the screen, you probably wouldn’t want to
bother with keeping track of dirty rectangles:
SDL_Flip(screen);
And, since this is how I do it in my game, I now wait for a certain amount
of time so that the next frame is drawn about 1/50th or so of a second
later (rather than “as fast as possible,” which would make the game
impossible on very high-end computers, and which would eat up far too much
CPU):
SDL_Delay(…);
-bill!