Jump to content
  • Advertisement
Sign in to follow this  
ilostthegame

PNG Transparency

This topic is 3809 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! I'm writing a game using SDL. I have a sprite sheet that is a PNG file. It contains the sprites on a transparent background. Here is some code:
SDL_Surface *bg = IMG_Load("img/bg.png");
SDL_DisplayFormatAlpha(bg); /* is this needed? */

SDL_Surface *sprites = IMG_Load("img/sprites.png");
SDL_DisplayFormatAlpha(sprites);

SDL_BlitSurface(bg, NULL, screen, NULL);
SDL_BlitSurface(sprites, NULL, screen, NULL);
SDL_Flip(screen);



Now, the above code works. I see the background with the sprite sheet on it and the transparency works. Now, I try to clip out the sprites from the sprite sheet:
SDL_Surface *sprite = *(SDL_CreateRGBSurface(SDL_HWSURFACE|SDL_SRCALPHA, 32, 32, sprites->format->BitsPerPixel, 0, 0, 0, 255));
SDL_DisplayFormatAlpha(sprite);
SDL_BlitSurface(sprites, (SDL_Rect) {0, 0, 32, 32}, sprite, NULL);
SDL_BlitSurface(sprite, NULL, screen, NULL);
SDL_Flip(screen);



Now, the transparency is gone. I see the background and the individual sprite on top, but it's inside a black box. Any ideas? Thanks! [Edited by - ilostthegame on January 12, 2008 10:15:35 PM]

Share this post


Link to post
Share on other sites
Advertisement
Sorry, that was a typo in my post.

I'm taking a stab in the dark but I think the problem is that SDL_Surface sprites (the sprite sheet) and SDL_Surface sprite (an individual sprite from the sprite sheet) have different alpha settings.

Share this post


Link to post
Share on other sites
Get color masks from source surface's display format.
I believe you produces a lot of memory leaks, look to hint on http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fDisplayFormat

Share this post


Link to post
Share on other sites
It's not using a color key though. It's a transparent PNG. However, my knowledge of this stuff is very limited. Do I need a color key with a transparent PNG?

I'll fix the memory leak, thanks.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!