Ricardo Markiewicz wrote:
Hi All… i’m trying yo do a simple program with SDL but i don’t
know what i doing wrong…
I want to move an image called ship over a background called mainScreen
( mainScreen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE); ) …
but the olders ships are not clean…
somebody can tell me what i doing wrong!!!.. THX!!
PD : Excuse my terrible english …
this is the main loop :
while (SDL_WaitEvent(&event))
{
SDL_Rect *r;
r = calloc(sizeof(SDL_Rect), 1);
switch (event.key.keysym.sym)
{
case (SDLK_ESCAPE): exit(0); break;
case (SDLK_UP): _y-=5; break;
case (SDLK_DOWN): _y+=5; break;
case (SDLK_LEFT): _x-=5; break;
case (SDLK_RIGHT): _x+=5; break;
}
r->x=_x;
r->y=_y;
r->w=ship->w;
r->h=ship->h;
SDL_BlitSurface(ship, NULL, mainScreen, r);
SDL_UpdateRects(mainScreen, 1, r);
}
Dang… Your English I don’t mind so much, but your
code is atroshous… (somewhat like my spelling ;-> )
First, every time through the while loop you leak an
SDL_Rect… Probably better to declare ‘r’ as an
SDL_Rect rather than an SDL_Rect*… Second, I
think you misunderstand what a blit is… If we
use the analogy of each surface being a wall, than
blitting from one surface to another would be like
painting on one wall what you see on another. If
I paint my name on your living room wall, and you
decide it’s in the wrong place, my painting my
name again won’t erase what I already painted…
I have to paint over the old image first… Maybe
try something like this:
int _x, _y, finished;
SDL_Surface *saved_region;
SDL_Rect r;
finished = 0;
saved_region = NULL;
while ((!finished) && SDL_WaitEvent(&event))
{
switch (event.key.keysym.sym)
{
case (SDLK_ESCAPE): finished=1; break;
case (SDLK_UP): _y-=5; break;
case (SDLK_DOWN): _y+=5; break;
case (SDLK_LEFT): _x-=5; break;
case (SDLK_RIGHT): _x+=5; break;
}
/* This may flicker a little, but there are
better ways to do this… this is for
illustration purposes /
/ Erase previous image /
if ( saved_region != NULL ) {
SDL_BlitSurface(saved_region, NULL, mainScreen, &r);
SDL_UpdateRects(mainScreen, 1, &r);
}
r.x=_x;
r.y=_y;
r.w=ship->w;
r.h=ship->h;
if ( saved_region == NULL ) {
/ Create a new surface if none exists /
SDL_CreateRGBSurface(SDL_HWSURFACE, ship->w, ship->h,
mainScreen->format->BitsPerPixel,
mainScreen->format->Rmask,
mainScreen->format->Gmask,
mainScreen->format->Bmask,
mainScreen->format->Amask, );
}
/ Save image /
SDL_BlitSurface(mainScreen, &r, saved_region, NULL);
/ Blit over it */
SDL_BlitSurface(ship, NULL, mainScreen, &r);
SDL_UpdateRects(mainScreen, 1, &r);
}
if ( saved_region != NULL ) {
SDL_FreeSurface(saved_region);
}
Hope that this helps…
-Loren