C++ SDL Transparencies

Started by
9 comments, last by CheddarCheese 13 years, 3 months ago
Hello all.

I've recently come across a problem, and I'm not sure how to address it.

I've learned basic transparency when it comes to making one colour transparent, but the program I'm trying to make has objects that are transparent with many colours.

I am using SDL on C++, and I am using a PNG file on top of a background picture.

Any help would be appreciated. Thanks.
Advertisement
My current code for loading an image is:


SDL_Surface *load_image( std::string filename )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );

if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}
Most people use rgb = {255,0,255} -- that crappy magenta color -- as the color key color since it is ugly and thus not used very much at all in normal CG situations.

So you should make any areas that you want transparent this color; you can use an image editor such as Gimp or Photoshop to accomplish this.

Quote:I've learned basic transparency when it comes to making one colour transparent, but the program I'm trying to make has objects that are transparent with many colours.


I am not understanding what you mean by this. Are you saying that you have multiple color keys per image and that you would like to make certain ones transparent at certain times? Please explain.

If you could explain exactly what you're trying to achieve, people cwould be in a better position to help.
Does this tutorial help? (Googling keyword = 'alpha transparency')

If various levels of transparency are not what you want, could you rephrase what you are looking for?
Perhaps post an example image and explain what parts you want to make transparent, and whether you mean completely invisible, or partially translucent?
Hi, sorry for being unclear the first time around. I'll try to explain better.

See, the picture I want to include isn't a nice and neat 1 colour picture. In fact, it's an explosion picture. Some parts of it are see-through, while some parts are not. Some areas of the explosion are more see-through than other parts. And the explosion is in multiple colours. It is a png file.

However, when I load it up with SDL, all the transparent aspects of the explosion are gone. Everything is a solid colour with no see-through areas. Now, there is a good chance that I'm doing something wrong, but I can't seem to figure it out.
Quote:Original post by CheddarCheese
My current code for loading an image is:


SDL_Surface *load_image( std::string filename )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );

if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}


You need to use the full alpha channel of the PNG image(s).
Is your Display set up right? Have you tried commenting out some code as a test as below.

SDL_Surface *load_image( std::string filename ) {    SDL_Surface* loadedImage;    loadedImage = IMG_Load( filename.c_str() );    return loadedImage}

Cheers

Lee

Code is Life
C/C++, ObjC Programmer, 3D Artist, Media Production

Website : www.leestripp.com
Skype : lee.stripp@bigpond.com
Gmail : leestripp@gmail.com

Use SDL_DisplayFormatAlpha instead of SDL_DisplayFormat.
Amateurs practice until they do it right.Professionals practice until they never do it wrong.
Quote:Original post by CheddarCheese
See, the picture I want to include isn't a nice and neat 1 colour picture. In fact, it's an explosion picture. Some parts of it are see-through, while some parts are not. Some areas of the explosion are more see-through than other parts. And the explosion is in multiple colours. It is a png file.

However, when I load it up with SDL, all the transparent aspects of the explosion are gone. Everything is a solid colour with no see-through areas. Now, there is a good chance that I'm doing something wrong, but I can't seem to figure it out.

Does your image file fade-to-black, or fade-to-alpha? (Is the fully transparent parts of your image black, or is the fully transparent part fully invisible, when you look at it in your image editor?)
If the first (it fades-to-black), then you'll want to look up "additive blending", though I don't have a good link to give you.
Are you using OpenGL textures or just 2D SDL blitting? I assume you're just blitting. If so, I already said: Use SDL_DisplayFormatAlpha.
Amateurs practice until they do it right.Professionals practice until they never do it wrong.
Quote:Original post by TheBuzzSaw
Are you using OpenGL textures or just 2D SDL blitting? I assume you're just blitting. If so, I already said: Use SDL_DisplayFormatAlpha.

It may take the OP a few days to respond to our posts, so we don't know what kind of transparency he's using. If he's using alpha transparency, then your method would work, but if he's using 'fade-to-black' images (I don't know the corrent term) then SDL_DisplayFormatAlpha will not work.
His comments:
Quote:Original post by the original poster
See, the picture I want to include isn't a nice and neat 1 colour picture. In fact, it's an explosion picture. Some parts of it are see-through, while some parts are not. Some areas of the explosion are more see-through than other parts. And the explosion is in multiple colours. It is a png file.

Lead me to think that he's not using alpha (even though it's a PNG files, the image might not be actively using the alpha channel). It's probably some explosion effect he got off the web, like this: [link]

The reason why it's partially working, is he probably has the color key set to black (0,0,0) which is taking out the pure-black but not the variaing shades of black.

You may be correct, you may not be - I'm merely offering a likely alternative possibility. There's no need to resort to such drastic and dramatic measures as reposting your statement in bold - the OP probably hasn't even read your post yet. [smile]


@CheddarCheese: Please post an example of your image, so we can be 100% certain what method you need to use.

This topic is closed to new replies.

Advertisement