Skip to main content
gamedev.net
🔒 Locked

SDL

Started by luxm 2 replies 650+ views
Original Post
luxm
luxm
Hi! i have 2 questions related to sdl , i hope you can help me. 1) There's a problem when im loading the images. Each time i move a sprite the screen fills with copies of the main sprite. First i'll show you the load_image function , which i found it at lazyfoo's site , but i've done some modifications. code: SDL_Surface *load_image( std::string filename ) { SDL_Surface* loadedImage = NULL; SDL_Surface* optimizedImage = NULL; SDL_Surface* final_img=NULL; loadedImage = SDL_LoadBMP( filename.c_str() ); if( loadedImage != NULL ) { optimizedImage = SDL_DisplayFormat( loadedImage ); SDL_FreeSurface( loadedImage ); } if( optimizedImage != NULL ) { //transparent //Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0, 0 ); SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, optimizedImage->format->colorkey); final_img=SDL_DisplayFormat( optimizedImage ); SDL_FreeSurface(optimizedImage); } return final_img; } I think the problem is here: //Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0, 0 ); SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, optimizedImage->format->colorkey); return final_img; because , if i do this: Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0xFF, 0xFF, 0xFF ); SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY,colorkey); Everything's working fine , BUT , im lacking of transparency!!...yes that means that the game looks WEIRD with the background of every image visible. Can you help me to fix the function?? 2) How can i get the "Top[Y] & left[X]" position of a surface , without adding temp variables while doing some action(moving for example)?? I found out(while searching on google) that calling ->GetX() & GetY() will work , but compiler says that such member doesnt exists.
baumep
baumep
1. If I understand your problem correctly you don't clear the screen. After you flip the main surface try "clearing" it e.g. by drawing a black rect in the size of the screen.
SDL_Flip(screen);SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

2. When you draw a surface you provide a rect structure with the position. The top coordinates are the x and y members of that struct. The image itself has no top and left position.
baumep
luxm
luxm
Okay thanks , that worked ,but , there's another problem!
the background isnt visible anymore , it turns white.

Im applying the surfaces like this:

apply_surface( Background )
>>>>>>>>>>>>>( image1 ) etc..
flip screen()
then i call the fill_rect()
luxm
luxm
Okay , i found it , the fill rect must be :
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, Background->format->colorkey,
Background->format->colorkey,
Background->format->colorkey)
);

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.