# Making a transparent surface?

This topic is 4181 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone! Hey, quick question, how can I make a SDL_Surface* without the black background? I have a couple of transparent PNG's that I want to blit to it, but I get the black on the background... Thanks! [grin] BTW: Is loading transparent PNG's "healthy"? Why do most games use BMP's with pink backgrounds?

##### Share on other sites
Well this is how you do it for BMP's that have a colored background so I'm not sure if it will work for your PNG's.

For each of your surfaces call SDL_SetColorKey() its definition is

 int SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key);

Take a better look at it here http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fSetColorKey

For the key parameter use SDL_MapRGB:

 Uint32 SDL_MapRGB(SDL_PixelFormat *fmt, Uint8 r, Uint8 g, Uint8 b);

Where r, g and b would make up the colour you want to be transparent, in this case black. But that also means any black in your image will be transparent too.

##### Share on other sites
Using the pink color is called "color keying." Magenta (255,0,255) is the standard color because it's buttUgly and typically used in APIs where you don't have access to (or ignore) the alpha channel.

##### Share on other sites
Colorkeyed (pink background) images work faster than alpha blending. If you don't need partial transparency (alpha layer), you should use color key.

To use the alpha blending, you have to enable it in all the surfaces that use it. I can't remember how, but it was SDL_SetAlpha or something. It's been a long time since I last used SDL blitting (been doing OpenGL recently), so I might remember wrong.

Using colorkeys is not dependent on the file format you use. You can use BMP's or PNG's or whatever. Many SDL games use BMP's because they're included in the SDL core, you don't need SDL_image, libpng and zlib to open BMP's. On the other hand, PNG's do lossless compression so the files are smaller.

If you're doing just a few pics and don't use alpha blending, use BMP's. If you have a lot of images and/or you need alpha, use PNG. In my opinion, there is no reason to use any other image format in games, so don't waste your time with TIFF's or others. Perhaps if you use photos (why would you do that?) JPEG's might be a smart move, but not otherwise.

-Riku

##### Share on other sites
Well,
in my game I use PNG's. I used to use BMP's with color keying, but it got VERY annoying, seeing little pixel dots around it, and IMO made my screenshots look VERY ugly. It made it look like my game was not polished or anything.

IMO, if you have a image editor that can create transparent PNG's then use PNG's. You will just need SDL_Image, and some other libraries to get it working. and there are also a lot more DLL's that you will need to include in your project.

So, it is up to you.

EDIT: I re-read your post, and I am not really sure what you are asking, but are you asking for a way to change the background color? If you are loading transparent PNG's then the transparent part will be what is behind it. In this case the black background is behind it, so you see that. If you want the background different then you use the function SDL_FillRect.

I'm sorry if this is not what you are asking.

##### Share on other sites
Quote:
 In this case the black background is behind it, so you see that.

Hmm... Yeah, so even if I use PNG's or BMP's, that will still be there... How can I use SDL_MapRGB to get transparent color (to use with SDL_FillRect)? Thanks!

##### Share on other sites
Quote:
 In this case the black background is behind it, so you see that.

Hmm... Yeah, so even if I use PNG's or BMP's, that will still be there... How can I use SDL_MapRGB to get transparent color (to use with SDL_FillRect)? Thanks!

##### Share on other sites
Quote:
Original post by Kixdemp
Quote:
 In this case the black background is behind it, so you see that.

Hmm... Yeah, so even if I use PNG's or BMP's, that will still be there... How can I use SDL_MapRGB to get transparent color (to use with SDL_FillRect)? Thanks!

Use SDL_MapRGBA

Though I'm not really sure what you are actually trying to do [rolleyes]

##### Share on other sites
so, are you tyring to change the background color? I am kind of confused, but if you are then this is how you use SDL_FillRect.

SDL_FillRect(surface, NULL, SDL_MapRGBA(surface->format, 0, 255, 255, 0);

I just picked some color values on that. So, if you want to change the background color, then just call that function. Although, I am confused on what you are asking though. I may just be really dumb, which is probally what it is!

##### Share on other sites
Don't worry, I solved it:

this->vehicles[0].bmp = SDL_CreateRGBSurface(SDL_HWSURFACE, 96, 96, 32, 0, 0, 0, 0);if (!vehicles[0].bmp){	this->SetError(IMG_GetError());	return false;}int c = SDL_MapRGBA(this->vehicles[0].bmp->format, 0, 0, 0, 0);if (SDL_SetColorKey(this->vehicles[0].bmp, SDL_SRCCOLORKEY, c) == -1) return false;

Thanks!

BTW: Did you get it at last? [lol]