SDL Image PNG transparency

Started by
5 comments, last by codeking 16 years, 10 months ago
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!
Take it easy,CodeKing
Advertisement
Convert the surface to the current depth. Make sure you convert it with SDL_DisplayFormatAlpha().
SDL_DisplayFormatAlpha() only changes one color to transparent

im talking about complex transparent (random scribbles of an eraser with an opaquesy of 50%)
Take it easy,CodeKing
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 -aLinux urthona 2.6.21-1-amd64 #1 SMP Sat May 26 17:22:54 CEST 2007 x86_64 GNU/Linux$ sdl-config --version1.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.
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.
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?
Take it easy,CodeKing
ignore that comment, i figured it out
Take it easy,CodeKing

This topic is closed to new replies.

Advertisement