Loading alpha-channeled PNGs?

Hi! ^^

I just subscribed to this mailing list in hopes someone here would be able
to help me (as I haven’t found a SDL forum yet). I’ve been trying to learn
SDL and so far so good, I’ve learnt how to put an image, animate a sprite,
change resolution… well, just basic stuff. Now, however, my sprites are in
PNG. Since there are black areas on it I choosed not to use SDL_SetColorKey.
Testing, I used it this way:

SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, 0, 0, 0));

It works, there’s no black border around it, but the black areas on the
sprite are transparent too! (well, that’s logical) I’m loading the PNG
using…

if((temp = IMG_Load("sprites/ship0001.png")) == NULL){ printf("Error

loading sprite"); return 1; }

SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, 0, 0,

0));

mAnim[0].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

Somehow I’m missing something here. My question is: Anybody knows how to
load the PNG file, and use the Alpha channel as the transparency one, and
not a RGB 0,0,0 color key as if it was a plain 8-bit image?

Thanks in advance :slight_smile:

  • DARKGuy

Somehow I’m missing something here. My question is: Anybody knows how to
load the PNG file, and use the Alpha channel as the transparency one,
and not a RGB 0,0,0 color key as if it was a plain 8-bit image?

Thanks in advance :slight_smile:

Just don’t use a color key…the PNG should retain its alpha channel in
the surface that IMG_Load() returns.

–ryan.

Hi! ^^

Thanks for answering :slight_smile:

Well something must be going wrong here then, look:

The image has alpha channel as you can notice in the PSP window, and in the
SDL, it doesn’t seem to get it. That’s what happens when I remove the
SDL_SetColorKey instruction. Any idea? =/On 9/13/06, Ryan C. Gordon wrote:

Somehow I’m missing something here. My question is: Anybody knows how to
load the PNG file, and use the Alpha channel as the transparency one,
and not a RGB 0,0,0 color key as if it was a plain 8-bit image?

Thanks in advance :slight_smile:

Just don’t use a color key…the PNG should retain its alpha channel in
the surface that IMG_Load() returns.

–ryan.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

  • Alemar

The image has alpha channel as you can notice in the PSP window, and in
the SDL, it doesn’t seem to get it. That’s what happens when I remove
the SDL_SetColorKey instruction. Any idea? =/

Oh, you have to enable the alpha channel explicitly:

SDL_SetAlpha(mypngsurface, SDL_SRCALPHA, 0xFF);

If all you care about is getting rid of the black border, though, you’ll
want to use color key blitting, since it’s much faster: just pick a
color other than black (like, bright purple) that doesn’t appear in the
image, and convert the border from black to that color in PSP.

–ryan.

Hi ^^

Thanks again for the answer :slight_smile: however it didn’t seem to work =/ the black
border is there again.

Yeah, your suggestion is a good one and I could use it for the ship’s sprite
and other stuff, but how would I do when dealing with other transparent
stuff, such as explosions, “windows”, plasma and other eye-candy? the
checkered-pixel method is now obsolete :wink:

I’m gonna paste the whole source code… hope you don’t mind. Isn’t much
anyways, and it’s more of a test (following a tutorial and modifying it to
suit my needs) rather than a bare program, but here it goes (maybe the error
is in the blitting instructions? dunno :P)---------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>

struct CSpriteFrame
{
SDL_Surface *image;
};

SDL_Surface *screen;
Mix_Music *cancion;

void InitAll()
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1){
printf(“SDLInit -> Error: %s\n”, SDL_GetError());
}

screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE);
if (screen == NULL){
   printf("SDLInitVideoMode -> Error: %s\n", SDL_GetError());
}

if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) == -1)
{
   printf("SDLInitAudio: %s\n", Mix_GetError());
}

}

int main(int argc, char *argv[])
{
SDL_Surface *temp;
SDL_Surface *bg;
SDL_Event event;
CSpriteFrame *mAnim;
int salir = 0;
int count = 0;
int count2 = 0;

InitAll();
SDL_WM_SetCaption("Star Solarian", NULL);

if((temp = IMG_Load("bg.png")) == NULL){ printf("Error cargando

imagen"); return 1; }
bg = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

mAnim = new CSpriteFrame[9];

if((temp = IMG_Load("sprites/ship0001.png")) == NULL){ printf("Error

cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[0].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

if((temp = IMG_Load("sprites/ship0002.png")) == NULL){ printf("Error

cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[1].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

if((temp = IMG_Load("sprites/ship0003.png")) == NULL){ printf("Error

cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[2].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

if((temp = IMG_Load("sprites/ship0004.png")) == NULL){ printf("Error

cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[3].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

if((temp = IMG_Load("sprites/ship0005.png")) == NULL){ printf("Error

cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[4].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

if((temp = IMG_Load("sprites/ship0006.png")) == NULL){ printf("Error

cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[5].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

if((temp = IMG_Load("sprites/ship0007.png")) == NULL){ printf("Error

cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[6].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

if((temp = IMG_Load("sprites/ship0008.png")) == NULL){ printf("Error

cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[7].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

if((temp = IMG_Load("sprites/ship0009.png")) == NULL){ printf("Error

cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[8].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

if((temp = IMG_Load("sprites/ship0010.png")) == NULL){ printf("Error

cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[9].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

while (! salir)
{
      SDL_Rect dest;

      dest.x = 0;
      dest.y = 0;
      SDL_BlitSurface(bg, NULL, screen, &dest);

      dest.x = 10;
      dest.y = 10;
      SDL_BlitSurface(mAnim[count].image, NULL, screen, &dest);
      count2=count2+1;
      if(count2>10){ count2=0; count=count+1; }
      if(count>9){ count=0; }

      SDL_UpdateRect(screen, 0, 0, 0, 0);

    SDL_PollEvent(&event);
    if (event.type == SDL_QUIT){ salir = 1; }
}

Mix_CloseAudio();
SDL_Quit();
return 0;

}

On 9/13/06, Ryan C. Gordon wrote:

The image has alpha channel as you can notice in the PSP window, and in
the SDL, it doesn’t seem to get it. That’s what happens when I remove
the SDL_SetColorKey instruction. Any idea? =/

Oh, you have to enable the alpha channel explicitly:

SDL_SetAlpha(mypngsurface, SDL_SRCALPHA, 0xFF);

If all you care about is getting rid of the black border, though, you’ll
want to use color key blitting, since it’s much faster: just pick a
color other than black (like, bright purple) that doesn’t appear in the
image, and convert the border from black to that color in PSP.

–ryan.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

  • Alemar

Hi!

I think you might be losing your alpha channel here:

mAnim[0].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); 

Try using SDL_DisplayFormatAlpha(temp).–
Jukka-Pekka Manninen

YAY!!!

IT WORKED!!! :smiley: :smiley: :smiley: :slight_smile: is so happy xD

Like, thanksthanksthanksthanksthanksthanksthanksthanksthanksthanks you all!
:smiley: ^^!!!On 9/13/06, Jukka-Pekka Manninen wrote:

Hi!

I think you might be losing your alpha channel here:

mAnim[0].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

Try using SDL_DisplayFormatAlpha(temp).


*Jukka-Pekka Manninen


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl