Jump to content
  • Advertisement
Sign in to follow this  
codeking

SDL Image PNG transparency

This topic is 4145 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

I make a simple program that displays an image. I make it load a PNG-24 file (Made using Adobe Photoshop CS2 and saved with the Save For Web function) I load the image, and the image displays, but the transparency is gone!

Share this post


Link to post
Share on other sites
Advertisement
SDL_DisplayFormatAlpha() only changes one color to transparent

im talking about complex transparent (random scribbles of an eraser with an opaquesy of 50%)

Share this post


Link to post
Share on other sites
Quote:
Original post by codeking
SDL_DisplayFormatAlpha() only changes one color to transparent

im talking about complex transparent (random scribbles of an eraser with an opaquesy of 50%)


I had a bad day, so I won't argue. Instead I'll give you some code that you can just copy & paste it in your favourite editor/IDE. Build it, try it, and see for yourself.


#include <iostream>
#include <string>
#include "SDL_image.h"

::SDL_Surface *load_image(std::string const& path, bool alpha = false) {
::SDL_Surface *result = ::IMG_Load(path.c_str());
if (result) {
::SDL_Surface *tmp;
if (alpha) {
tmp = ::SDL_DisplayFormatAlpha(result);
}
else {
tmp = ::SDL_DisplayFormat(result);
}
if (tmp) {
::SDL_FreeSurface(result);
result = tmp;
}
else {
// signal the error, return the original, unconverted
// surface
std::cerr << ::SDL_GetError() << std::endl;
}
}
else {
std::cerr << ::SDL_GetError() << std::endl;
}
return result;
}

int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
::SDL_Init(SDL_INIT_VIDEO);
::SDL_Surface *video = ::SDL_SetVideoMode(640, 480, 0,
SDL_SWSURFACE |
SDL_DOUBLEBUF);
::SDL_FillRect(video, 0, 0L);
::SDL_Surface *img = load_image("foo.png", true);
::SDL_BlitSurface(img, 0, video, 0);
::SDL_Flip(video);
::SDL_Delay(5000);
::SDL_FreeSurface(img);
::SDL_Quit();
}




$ uname -a
Linux urthona 2.6.21-1-amd64 #1 SMP Sat May 26 17:22:54 CEST 2007 x86_64 GNU/Linux
$ sdl-config --version
1.2.11
$ g++ -ansi -Wall -Wextra -o foo foo.cc `sdl-config --cflags --libs` -lSDL_image
$ ./foo


I'm pretty sure you're mistaking SDL_DisplayFormatAlpha() for SDL_SetAlpha() or SDL_SetColorKey(). SDL_DisplayFormatAlpha() simply converts the surface to the current bit-depth like SDL_DisplayFormat(). Unlike SDL_DisplayFormat(), however, it preserves per-pixel alpha. Neither SDL_DisplayFormat() nor SDL_DisplayFormatAlpha() sets any kind of colour to any kind of transparency.

Hope this helps.

Share this post


Link to post
Share on other sites
You said you saved it as a PNG 24. Don't you mean a PNG 32? The per-pixel alpha-blending is saved in 8 bits of each pixel. A 24-bit image contains no per-pixel alpha-blending.

Share this post


Link to post
Share on other sites
thanks let_bound, it works now, but I noticed the color code 0L in the function SDL_FillRect. How do you use these color codes?

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!