How to blit to a screen without getting junk?

Hi guys!

I have been toying with some code that inits SDL, opens a VideoMode and
uses BlitSurface to show the picture.

Now I want to moev this picture and make it bounce around on the
screen. This I can do, but of course the old image stays in memory and
after awhile all the screen is “smeared” with the image data.

I have looked at many code examples and can’t see what is is they are
doing that I’m don’t. I must be missing something and would be thankful
for some helpful pointers.

Bascally my code looks like this (simplified):

for(i=1; i<100; i=i+1) {
dstrect.x = 200+i;
dstrect.y = (screen->h-image->h)/2;
dstrect.w = image->w;
dstrect.h = image->h;
SDL_BlitSurface(image, NULL, screen, &dstrect);
SDL_Flip(screen);
}

any ideas?

/andreas

=================== Emacs =====================
mail: @Andreas_Davour
or try: Koraq at yahoo.com
groove out: http://www.update.uu.se/~ante/
====== The choice of a GNU generation! ========

it sounds like your problem is because you arent redrawing whats underneat
it. You can either redraw everything each time you move it (ugh) or before
you draw your image you scan the pixels from the screen behind your picture
and store that in a picture. That way you can use this to ‘erase’ the
picture your moving around before you move it and redraw. Theres some more
complicated algorithms too, try searching google for ‘dirty rectangles’.> ----- Original Message -----

From: ante@update.uu.se (Andreas Davour)
To:
Sent: Saturday, August 31, 2002 10:44 AM
Subject: [SDL] How to blit to a screen without getting junk?

Hi guys!

I have been toying with some code that inits SDL, opens a VideoMode and
uses BlitSurface to show the picture.

Now I want to moev this picture and make it bounce around on the
screen. This I can do, but of course the old image stays in memory and
after awhile all the screen is “smeared” with the image data.

I have looked at many code examples and can’t see what is is they are
doing that I’m don’t. I must be missing something and would be thankful
for some helpful pointers.

Bascally my code looks like this (simplified):

for(i=1; i<100; i=i+1) {
dstrect.x = 200+i;
dstrect.y = (screen->h-image->h)/2;
dstrect.w = image->w;
dstrect.h = image->h;
SDL_BlitSurface(image, NULL, screen, &dstrect);
SDL_Flip(screen);
}

any ideas?

/andreas

=================== Emacs =====================
mail: ante at update.uu.se
or try: Koraq at yahoo.com
groove out: http://www.update.uu.se/~ante/
====== The choice of a GNU generation! ========


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

Now I want to moev this picture and make it bounce around on the
screen. This I can do, but of course the old image stays in memory and
after awhile all the screen is “smeared” with the image data.

There are many solutions to this problem. If you’re just messing around and
want a simple one, I’d just fill the background with black every frame, then
draw on it.

//establish a black color somewhere before your loop
Uint32 black = SDL_MapRGB(screen->format, 0,0,0);

for(i=1; i<100; i=i+1) {
dstrect.x = 200+i;
dstrect.y = (screen->h-image->h)/2;
dstrect.w = image->w;
dstrect.h = image->h;

//fill the background with black
SDL_FillRect(screen, NULL, black);

SDL_BlitSurface(image, NULL, screen, &dstrect);
SDL_Flip(screen);          

}

MattOn Saturday 31 August 2002 12:44 pm, Andreas Davour wrote:

Andreas Davour wrote:

Now I want to moev this picture and make it bounce around on the
screen. This I can do, but of course the old image stays in memory and
after awhile all the screen is “smeared” with the image data.

i’ve got a tutorial named “Help! How Do I Move An Image” on my site. it’s
for people just starting out with graphics stuff. unfortunately for you it
is written for the Python language and the Pygame library, which mixes SDL
and Python. still, the “theory” behind it is probably good for any
language. it starts by moving elements in a list/sequence, and slowly moves
up to smooth animated graphics.

http://pygame.org/docs/tut/MoveIt.html