Ian Marshall
Ian Marshall

Reputation: 1

SDL_SetColorKey selective transparency!?

So I'm making this little Mario esque game and when i load in the clouds for the background, one of them loads perfect and looks awesome. The other cloud loads in but with a gradient background, which kind of looks like the background image shrunk down. Here is the code for what i'm pretty sure is all you need, but will put more if needed.

cloud1 = load("assets/cloud1.png");
cloud2 = load("assets/cloud2.png");

cloud2->format->Amask = 0xFF000000;
cloud2->format->Ashift = 24;

SDL_SetColorKey(cloud1, SDL_TRUE,SDL_MapRGB(screen->format,0xff,0xff,0xff));
SDL_BlitSurface(cloud1, NULL,screen,&cloudd);
SDL_SetColorKey(cloud2, SDL_TRUE,SDL_MapRGB(screen->format,0xff,0xff,0xff));
SDL_BlitSurface(cloud2, NULL,screen,&cloud);

Everything compiles, the problem is that i want the second cloud to have a transparent background. Both image files has transparent background. :(

Upvotes: 0

Views: 345

Answers (1)

Jonny D
Jonny D

Reputation: 2344

You should decide if you're using a per-pixel transparency or a color-keyed transparency. AFAIK, you can't do both with SDL_Surfaces. If you want to use color-keying, then make sure the PNG files do not have alpha channels (or else remove them in your favorite editor).

Also, do not overwrite the values in the surface format data (e.g. Amask and Ashift).

One possible reason for seeing weird images in the cloud's transparent region is if you created the image by rescaling another image and used an eraser tool to clear it. You may get the old color data preserved, but with the alpha values set to 0. Yes, I know because I've done this before.

Upvotes: 1

Related Questions