Sign in to follow this  

C++ SDL Transparencies

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Hi all, sorry for not replying soon (although it's only been a day).

TheBuzzSaw did get something. I have managed to get transparencies on other pictures. Thanks!

However, Servant of the Lord is also correct about the fade-to-black for the explosion. Although I think I may have found a solution around that problem.

Thanks everyone for your help!

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this