Question about animation; erasing where my sprite has been

I’ve drawn my sprite in it’s original position. I move it to a new
location and redraw the screen. The sprite is drawn in then new
position, but also still shows in the old position. How to fix this?
I’m using double-buffering, if that matters. The full code of my
project in it’s current state can be found at
http://www.espersunited.com/~michael/needhelp/ourrpg/ if reference to it
is necessary…

I’ve drawn my sprite in it’s original position. I move it to a new
location and redraw the screen. The sprite is drawn in then new
position, but also still shows in the old position. How to fix this?

Once you have drawn over a surface, whatever was on it is erased and
replaced (alpha compositing aside). So, you will need to redraw the
backdrop, or at least the portion of it that you want to be visible
again after the sprite moves.

  • ?Il giorno 01/mar/08, alle ore 16:14, Michael Sullivan ha scritto:

Make sure you’re flipping the screen everytime the character changes
position.On Sat, Mar 1, 2008 at 7:14 AM, Michael Sullivan wrote:

I’ve drawn my sprite in it’s original position. I move it to a new
location and redraw the screen. The sprite is drawn in then new
position, but also still shows in the old position. How to fix this?
I’m using double-buffering, if that matters. The full code of my
project in it’s current state can be found at
http://www.espersunited.com/~michael/needhelp/ourrpg/http://www.espersunited.com/~michael/needhelp/ourrpg/if reference to it
is necessary…


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Make sure you’re flipping the screen everytime the character changes
position.

    I've drawn my sprite in it's original position.  I move it to
    a new
    location and redraw the screen.  The sprite is drawn in then
    new
    position, but also still shows in the old position.  How to
    fix this?
    I'm using double-buffering, if that matters.  The full code of
    my
    project in it's current state can be found at
    http://www.espersunited.com/~michael/needhelp/ourrpg/ if
    reference to it
    is necessary...

Everytime a sprite changes position, Draw::drawScreen is called. Here it is:

void Draw::drawScreen()
{
//Draw allies
for (int i = 0; i < 4; i++)
{
printf(“imageXPos[%d] = %i; imageYPos[%d] = %i\n”, i,
imageXPos[i], i, imageYPos[i]);
SDL_BlitSurface(image[i], &src[i], screen, &dest[i]);
}

//Draw enemies

drawStats();

/Draw the battle screen lines/
drawLines();

SDL_Flip(screen);

}On Sat, 2008-03-01 at 08:57 -0800, JDE wrote:

On Sat, Mar 1, 2008 at 7:14 AM, Michael Sullivan wrote:

Like infinity said, at the top of your drawscreen function you need to fill
your entire screen with black again (or whatever color your background is).

Or, like he also said, when you move an enemy do it this way:

  1. Draw a black square over where the current enemy is
  2. Move the enemy (ie change their coordinates)
  3. Redraw the enemy in the new place.

This gets tricky if the enemies overlap, but it’s faster if you can get it
working.> ----- Original Message -----

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Michael Sullivan
Sent: Saturday, March 01, 2008 11:07 AM
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: Re: [SDL] Question about animation; erasing where my sprite has
been

On Sat, 2008-03-01 at 08:57 -0800, JDE wrote:

Make sure you’re flipping the screen everytime the character changes
position.

On Sat, Mar 1, 2008 at 7:14 AM, Michael Sullivan wrote:
I’ve drawn my sprite in it’s original position. I move it to
a new
location and redraw the screen. The sprite is drawn in then
new
position, but also still shows in the old position. How to
fix this?
I’m using double-buffering, if that matters. The full code of
my
project in it’s current state can be found at
http://www.espersunited.com/~michael/needhelp/ourrpg/ if
reference to it
is necessary…

Everytime a sprite changes position, Draw::drawScreen is called. Here it
is:

void Draw::drawScreen()
{
//Draw allies
for (int i = 0; i < 4; i++)
{
printf(“imageXPos[%d] = %i; imageYPos[%d] = %i\n”, i,
imageXPos[i], i, imageYPos[i]);
SDL_BlitSurface(image[i], &src[i], screen, &dest[i]);
}

//Draw enemies

drawStats();

/Draw the battle screen lines/
drawLines();

SDL_Flip(screen);

}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

The simplest solution is to establish a background (either an image or a solid color) and fill the screen with it as the first line of Draw::drawScreen(). Then draw everything else on top of that.

Also, redrawing everything every time something changes position works for very simple games, but if you have several sprites moving at once you’ll desync things quickly doing it that way. (Not to mention wasting a ton of cycles.) A better solution is to use an SDL_Timer to call drawScreen every X number of miliseconds to establish a constant framerate.

Michael, read up on dirty rectangles, this may help you:

http://www.whisqu.se/per/docs/graphics82.htm

Hi,

Basically, you have 2 strategies:

  1. Clear screen and redraw everything.

  2. Only redraw the bits that have changed.

Strategy 1 is the easiest, however, if you are working in 2D, with
limited PROCESSOR or memory BANDWIDTH, it is very wasteful, as you will
be moving many more blocks of memory about.

If you’re working in 3D, you clearly don’t have such limits, so you can
just wipe the screen.

Strategy 2 is much more complicated (as many optimisations are!);

Assuming you have a fixed background (no scrolling) you need to do this:

  1. Draw the background once.

  2. Decide where you are going to draw your sprite (do collision
    detection etc.)

  3. Save the portion of the background where you are going to put the
    sprite into memory associated with the sprite (so that you can restore
    it later).

  4. Draw the sprite.

  5. Remove the sprite by re-drawing the background from the backup.

  6. Goto 2.

As I said, it matters what your target hardware is and whether you have
scrolling etc.

Eddy

Michael Sullivan wrote:> I’ve drawn my sprite in it’s original position. I move it to a new

location and redraw the screen. The sprite is drawn in then new
position, but also still shows in the old position. How to fix this?
I’m using double-buffering, if that matters. The full code of my
project in it’s current state can be found at
http://www.espersunited.com/~michael/needhelp/ourrpg/ if reference to it
is necessary…


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

  1. Draw the background once.

  2. Decide where you are going to draw your sprite (do collision
    detection etc.)

  3. Save the portion of the background where you are going to put the
    sprite into memory associated with the sprite (so that you can restore
    it later).

  4. Draw the sprite.

  5. Remove the sprite by re-drawing the background from the backup.

  6. Goto 2.

i don’t think this will work very well. 1st, overlapping sprites
require even more constrains (order of storing backups and redrawing).
2nd, additional surfaces are required for the backups. 3rd, there is a
blit overhead for saving the background.

IMHO the best aproach is to just rebuild the screen within the changed
regions (use SetClipRect with one or more dirty rects) from the
surfaces you already have anyway (background and sprite surfaces).

hope this helps …
clemensOn Mon, Mar 3, 2008 at 11:09 PM, Edward Cullen wrote: