[SDL] Odd update problem

Sami Näätänen sami.naatanen at kolumbus.fi
Fri Jan 10 09:04:01 PST 2003

On Friday 10 January 2003 01:56, Nick Whitelegg wrote:
> Hello,
> I'm having an odd update problem with SDL, wrapped via my library
> As an example ZXLIB program I'm developing a somewhat-Pacman-like
> game in which you play a professor who has to avoid the bunsen
> burners and build his robot (based on an old Spectrum game). On each
> game cycle I do the following with the professor and bunsen burner
> sprites:
> - blank out the previous position, by restoring the background
> - redraw the sprite image
> - update the sprite rectangles, if each sprite moved
> It *normally* works fine. However if two sprites overlap (e.g. two
> bunsen burners next to each other chasing the professor down the same
> passage), the restoration of the background doesn't happen under them
> and they leave a trail.
> I've double checked my code and it isn't anything obvious. I can only
> assume it's something subtle to do with updating, something I have to
> be honest consider a bit of a black box and don't understand fully.
> (if you blit an image to screen memory, why do you have to update the
> screen? This was not necessary under simpler setups such as MSDOS or
> 80s micros graphics mode).

Simple answer. You of course save the background just before each sprite 
is blitted. So if two sprites overlap and you don't restore the 
backgrounds in reverse order of the order you blit the sprites it will 
leave the parts of the first two overlaping sprites to the screen.

I think this will explane it better.

Initial image:


After first sprite


After second sprite


After restoring the first sprites background


After restoring the second sprites background


This is because when you blit the second sprite the background that is 
saved contains the first sprite.

So if your code is something like this.

// Clean the monster backgrounds
for(i=0; i<MONSTERS; i++)
  MyBlit(monster[i].background, monster[i].oldx, monster[i].oldy);

make it go the list backwards like this

// Clean the monster backgrounds
for(i=MONSTERS-1; i>=0; i--)
  MyBlit(monster[i].background, monster[i].oldx, monster[i].oldy);

And everything works right.

More information about the SDL mailing list