views:

303

answers:

1

I'm using the latest SDL/GFX libs on Fedora 10 and I'm trying to render a PNG or GIF image onto the screen surface. I could not get transparent PNG's to display at all so I replaced the transparent parts or my sprite with all white (and tried Magenta 255,0,255). The white-transparent PNG displays fine using this code:

SDL_Surface *image = load_image("sprite-white.png");
SDL_Surface *roto = SDL_DisplayFormat(image);
SDL_SetColorKey(roto, SDL_SRCCOLORKEY, SDL_MapRGB( roto->format, 255,255,255 ));
SDL_BlitSurface( roto, NULL, surface, &offset );

But when I try to rotate the sprite it does not drop all the white pixels. I replace the above code with this to rotate:

SDL_Surface *image = load_image("sprite-white.png");
SDL_Surface *roto = rotozoomSurface(image,  rotation_degrees, 1, 1);
SDL_Surface *roto2 = SDL_DisplayFormat(roto);
SDL_SetColorKey(roto2, SDL_SRCCOLORKEY, SDL_MapRGB( roto2->format, 255,255,255 ));
SDL_BlitSurface( roto2, NULL, surface, &offset );

I end up with a white outline around some of the good pixels. GIF images give the same results.

When trying with transparent PNG/GIF files the code was the same except I did not call SDL_SetColorKey. Then the PNG did not display properly at all. One strange thing I found was the transparent PNG looked the same in MS Paint as it did in SDL, but GIMP/Photoshop programs opened it correctly.

Is there something I'm missing setting up the destination surface?

Google did not turn up much, a working example would be great.

A: 

I am assuming that when you say "the latest SDL/GFX libs on Fedora 10," you mean the SDL and SDL_gfx libraries. To properly display PNG images with transparency, ensure that you also have SDL_image installed; you will need it, unless you have a different library that can load a PNG image into a SDL_Surface.

You should not need to use a color key for transparency (unless, of course, you want to use one). As for your problem with using a color key, note that the SDL documentation recommends setting the color key before calling SDL_DisplayFormat():

"If you want to take advantage of hardware colorkey or alpha blit acceleration, you should set the colorkey and alpha value before calling this function."

You might also want to try using SDL_DisplayFormatAlpha() instead of SDL_DisplayFormat(), like so:

SDL_Rect imagePosition = { 50, 50, 0, 0 };
SDL_Surface *image = IMG_Load("example.png");
if (image != NULL) {
  image = SDL_DisplayFormatAlpha(image);
}

Later, in your rendering loop, you can then rotate the source image by some floating point angle on to some SDL_Surface screen:

if (image != NULL) {
  SDL_Surface *rotation = rotozoomSurface(image, angle, 1, 1);
  SDL_BlitSurface(rotation, NULL, screen, &imagePosition);
  SDL_FreeSurface(rotation);
}

The above code will properly blit a PNG with transparency, assuming your PNG has proper transparency.

Make sure to link with -lSDL_image for the SDL_image library and -lSDL_gfx for the SDL_gfx library, assuming you are using g++ on your Fedora 10 installation. You can also use sdl-config --libs for the required g++ arguments for SDL itself.

Jeff
The above code gave me the same results, so I found some other png's and it worked fine along with my original code. The png that I was using for testing was somehow bad. Thanks!
amanda