Newbie needs heelp

Hi everyone

I have a problem when moving a bitmap: There remains a trail where the
bitmap moved over. It looks like as if
the bitmap has been “smeared” over the screen.
I know that i have to save the background before drawing the bitmap,
then blit the image to the screen, update the part of the screen i
blitted the image to and finally restore the background.
I do all this in a loop. Here’s the source:

  ....

SDL_Surface * window; // the 640 * 480 main window
SDL_Surface * tmp;
SDL_Surface * bitmap; // the image i want to move
SDL_Rect rectangle; // the dimension / x and y Position of the bitmap

// this is the surface i save the background to. It’s initialisied once
at the beginning of my program
tmp =
SDL_CreateRGBSurface(bitmap->flags,bitmap->w,bitmap->h,16,0,0,0,0);

 .....

rectangle.y = 100; // set y Position of the bitmap to 100
rectangle.w = bitmap->w; // set width
rectangle.h = bitmap->h; // set height

for (int i=100;i<400;i++)// my drawing loop
{
rectangle.x += 1; // move the bitmap one pixel
forward by increasing its x Pos.
SDL_BlitSurface(window,&rectangle,tmp,NULL); // save the Background
to the tmp Surface

  SDL_BlitSurface(bitmap,NULL,window,&rectangle); //now blit the

bitmap to the window

 // update the concerned part "rectangle" of the window
  SDL_UpdateRect(window, rectangle.x ,rectangle.y, bitmap->w,

bitmap->h);
SDL_BlitSurface(tmp,NULL,window,&rectangle); // restore the background
}

I must do something wrong , but i really don’t know what it is. I’m
using SDL Version 1.2.2 on Linux and will be very, very happy if someone
could help me please !

Thanks in advance

Altay Cebe

Altay Cebe wrote:

Hi everyone

I have a problem when moving a bitmap: There remains a trail where the
bitmap moved over. It looks like as if
the bitmap has been “smeared” over the screen.
I know that i have to save the background before drawing the bitmap,
then blit the image to the screen, update the part of the screen i
blitted the image to and finally restore the background.
I do all this in a loop. Here’s the source:

 ....

SDL_Surface * window; // the 640 * 480 main window
SDL_Surface * tmp;
SDL_Surface * bitmap; // the image i want to move
SDL_Rect rectangle; // the dimension / x and y Position of the bitmap

// this is the surface i save the background to. It’s initialisied once
at the beginning of my program
tmp =
SDL_CreateRGBSurface(bitmap->flags,bitmap->w,bitmap->h,16,0,0,0,0);

.....

rectangle.y = 100; // set y Position of the bitmap to 100
rectangle.w = bitmap->w; // set width
rectangle.h = bitmap->h; // set height

for (int i=100;i<400;i++)// my drawing loop
{
rectangle.x += 1; // move the bitmap one pixel
forward by increasing its x Pos.
SDL_BlitSurface(window,&rectangle,tmp,NULL); // save the Background
to the tmp Surface

 SDL_BlitSurface(bitmap,NULL,window,&rectangle); //now blit the

bitmap to the window

// update the concerned part "rectangle" of the window
 SDL_UpdateRect(window, rectangle.x ,rectangle.y, bitmap->w,

bitmap->h);
SDL_BlitSurface(tmp,NULL,window,&rectangle); // restore the background
}

I must do something wrong , but i really don’t know what it is. I’m
using SDL Version 1.2.2 on Linux and will be very, very happy if someone
could help me please !

Thanks in advance

Altay Cebe


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

you never make an update of the restored surface

you never make an update of the restored surface

Thanks for the hint ! There aren’t smears anymore, but now my moving
image flickers very badly.
How do i get rid of this ? Hope, i dont’t get on your nerves…

for (int i=100;i<400;i++)
{
rechteck.x += 1;
SDL_BlitSurface(window,&rechteck,tmp,NULL);

 SDL_BlitSurface(bitmap,NULL,window,&rechteck);
 //SDL_UpdateRects(window,1,&rechteck);
 SDL_UpdateRect(window, rechteck.x ,rechteck.y, bitmap->w, bitmap->h);
 SDL_BlitSurface(tmp,NULL,window,&rechteck);

 //Update of the RESTORED screen.
 SDL_UpdateRect(window, rechteck.x ,rechteck.y, bitmap->w, bitmap->h);

}

Altay Cebe writes:

Thanks for the hint ! There aren’t smears anymore, but now my moving
image flickers very badly.
How do i get rid of this ? Hope, i dont’t get on your nerves…

Do both the restore (blit) of the previous background and the blit of
the sprite in its new location location, followed by a SINGLE call to
SDL_UpdateRects().

i.e. never call SDL_Update*() or SDL_Flip() more than once for any one
frame.

Derrell

Thanks for the hint ! There aren’t smears anymore, but now my moving
image flickers very badly.
How do i get rid of this ? Hope, i dont’t get on your nerves…

Do both the restore (blit) of the previous background and the blit of
the sprite in its new location location, followed by a SINGLE call to
SDL_UpdateRects().

Hmm…but i must call SDL_Update twice. Otherwise my moving bitmap
starts smearing. Could you please modify
this loop so that it doesn’t smear and flicker ? I’m kinda confused…

for (int i =100; i < 400;i++)
{
rechteck.x += 1;
SDL_BlitSurface(window,&rectangle,tmp,NULL); //save BG
SDL_BlitSurface(bitmap,NULL,window,&retangle);// blit Image
SDL_UpdateRects(window,1, &rectangle); // first update
SDL_BlitSurface(tmp,NULL,window,&rectangle); // restore BG
SDL_UpdateRects(window,1, &rectangle); //second update (stops
smearing)

}

Altay Cebe writes:

Hmm…but i must call SDL_Update twice. Otherwise my moving bitmap
starts smearing.

You may need two rectangles: one containing the region of the previous
background, and one containing the new sprite. Blit the old
background back to the screen, blit the new sprite to the screen, then
call UpdateRects() with both rectangles.

Derrell

Hmm…but i must call SDL_Update twice. Otherwise my moving bitmap
starts smearing. Could you please modify
this loop so that it doesn’t smear and flicker ? I’m kinda confused…

No guarantees here. This is for having a background
that is the same for the whole surface.

for (int i =100; i < 400;i++)
{
/* If tmp is just like the window surface background the size of the bitmap
* then just do a blit here to erase the previous bitmap
* You just have to use the new coords provided by rectangle.
*/
SDL_BlitSurface(tmp,NULL,window,&rectangle); // restore BG
rectangle.x += 1;

  /* ignore this b/c tmp is already like the background surface */
  //SDL_BlitSurface(window,&rectangle,tmp,NULL); //save BG

  SDL_BlitSurface(bitmap,NULL,window,&retangle);// blit Image
  SDL_UpdateRects(window,1, &rectangle); // first update                
  //SDL_UpdateRects(window,1, &rectangle); //second update (stops smearing)

}> 

SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Derrell.Lipman at UnwiredUniverse.com wrote:

You may need two rectangles: one containing the region of the previous
background, and one containing the new sprite. Blit the old
background back to the screen, blit the new sprite to the screen, then
call UpdateRects() with both rectangles.

or use a bounding rectangle. we’ve discussed it many times before; see
http://www.libsdl.org/pipermail/sdl/2000-September/030071.html

(the mailing list software has munged my ascii art somewhat but it’s
hopefully still readable)