Loading PNG files with transparency already set

sorry if i am beating a dead fish by asking this question but i did briefly
google but got no sense from any results

i am converting a game i mad using a different platform to run on SDL, its
images are PNG with transparency already set in the image, Does SDL_Image
have the capabilities to load this image with its mask already set? some of
the images have alpha in the colored pixes, but that doesn’t really matter
but it would be nice to know if i can load everything from the png.

thanks

hope that makes sense

Neil

Do you mean the surface’s Amask? Yeah, it works. That’s just part of the
description of the pixel representation. The surface will store this info
and an extra byte (for 32-bit pngs) per pixel to store the alpha at that
pixel. What are you trying to do?

Jonny DOn Fri, Jul 2, 2010 at 4:01 AM, Neil White wrote:

sorry if i am beating a dead fish by asking this question but i did briefly
google but got no sense from any results

i am converting a game i mad using a different platform to run on SDL, its
images are PNG with transparency already set in the image, Does SDL_Image
have the capabilities to load this image with its mask already set? some of
the images have alpha in the colored pixes, but that doesn’t really matter
but it would be nice to know if i can load everything from the png.

thanks

hope that makes sense

Neil


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Do you mean the surface’s Amask? Yeah, it works. That’s just part of the
description of the pixel representation. The surface will store this info
and an extra byte (for 32-bit pngs) per pixel to store the alpha at that
pixel. What are you trying to do?

Jonny D

i have some pngs where in gimp i already set the background as transparent
for a flash game, which loads the images with the transparent bits, there
are ways to set transparency after you loaded but its a pain, anyways, so i
have all the images and all i really want to do is load them as they appear
in gimp with all their transparent bits intact.

my image loading function looks like this ( i only use png’s )

SDL_Surface *getimage(int alpha, int mask, char *name)
{
SDL_Surface *surface;

char path[256];
char msg[256];
strcpy(path, DATA_PREFIX);

    strcat(path, "gfx/");

    strcat(path, name);
    strcat(path, ".png");

surface = IMG_Load(path);
if (surface == NULL)
{
     strcpy(msg, path);
    strcat(msg, " Not Loaded");
    debug(msg,0);

}
else
{
    SDL_Surface *image;
    if (mask==0) SDL_SetColorKey(surface, SDL_SRCCOLORKEY,

SDL_MapRGB(surface->format, 0, 0, 0));
if (mask==1) SDL_SetColorKey(surface, SDL_SRCCOLORKEY,
SDL_MapRGB(surface->format, 255, 255, 255));
image = SDL_DisplayFormat(surface);

    SDL_FreeSurface(surface);
     strcpy(msg, path);
    strcat(msg, " Loaded");
    debug(msg,0);
    if (alpha>-1) SDL_SetAlpha(image, SDL_SRCALPHA, alpha);
    return image;
}



return 0;

}

i just actually got to the part of loading and plotting and i set them to
have mask black, which works on the un-previously edited ones, pngs seem to
be able to remember what was underneath the transparency so some images work
fine with black set and some still have remnants of previous editing, but it
would still be nice to be able to know how to load pngs with proper alpha so
certain parts of the images are semi transparent etc.

Thanks!On 2 July 2010 12:57, Jonathan Dearborn wrote:

i just quicky went through my images filling in the transparent bits black
and they turned out looking rubbish, some have random edge lines creeping in
( probably cos of gimps fill function ) and some i didnt realise just how
much they relied on being able to be semi transparent… , i really need to
get this to work or i’ll have to re do all the gfx ( AGAIN ) :slight_smile:

It looks like you’re trying to do something fancy, but SDL_image does it for
you. Just to be clear, you’re simply trying to load a png with an alpha
channel? I think your biggest concern here is that you’re using
SDL_DisplayFormat() instead of SDL_DisplayFormatAlpha(). Try this?

SDL_Surface *getimage(const char *name)
{
SDL_Surface *surface;

char path[256];
char msg[256];
strcpy(path, DATA_PREFIX);

    strcat(path, "gfx/");

    strcat(path, name);
    strcat(path, ".png");

surface = IMG_Load(path);
if (surface == NULL)
{
     strcpy(msg, path);
    strcat(msg, " Not Loaded");
    debug(msg,0);
}
else
{
    SDL_Surface *image = surface;
    surface = SDL_DisplayFormatAlpha(image);

    SDL_FreeSurface(image);
     strcpy(msg, path);
    strcat(msg, " Loaded");
    debug(msg,0);
}

return surface;

}

Jonny DOn Fri, Jul 2, 2010 at 9:07 AM, Neil White wrote:

i just quicky went through my images filling in the transparent bits black
and they turned out looking rubbish, some have random edge lines creeping in
( probably cos of gimps fill function ) and some i didnt realise just how
much they relied on being able to be semi transparent… , i really need to
get this to work or i’ll have to re do all the gfx ( AGAIN ) :slight_smile:


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

SDL_DisplayFormat and SDL_DisplayFormatAlpha are not necessary, by the way.
They just improve performance when blitting to the display surface.

Jonny DOn Fri, Jul 2, 2010 at 9:32 AM, Jonathan Dearborn <@Jonathan_Dearborn>wrote:

It looks like you’re trying to do something fancy, but SDL_image does it
for you. Just to be clear, you’re simply trying to load a png with an alpha
channel? I think your biggest concern here is that you’re using
SDL_DisplayFormat() instead of SDL_DisplayFormatAlpha(). Try this?

SDL_Surface *getimage(const char *name)

{
SDL_Surface *surface;

char path[256];
char msg[256];
strcpy(path, DATA_PREFIX);

    strcat(path, "gfx/");

    strcat(path, name);
    strcat(path, ".png");

surface = IMG_Load(path);
if (surface == NULL)
{
     strcpy(msg, path);
    strcat(msg, " Not Loaded");
    debug(msg,0);
}
else
{
    SDL_Surface *image = surface;
    surface = SDL_DisplayFormatAlpha(image);

    SDL_FreeSurface(image);

     strcpy(msg, path);
    strcat(msg, " Loaded");
    debug(msg,0);
}

return surface;

}

Jonny D

On Fri, Jul 2, 2010 at 9:07 AM, Neil White wrote:

i just quicky went through my images filling in the transparent bits black
and they turned out looking rubbish, some have random edge lines creeping in
( probably cos of gimps fill function ) and some i didnt realise just how
much they relied on being able to be semi transparent… , i really need to
get this to work or i’ll have to re do all the gfx ( AGAIN ) :slight_smile:


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

SDL_DisplayFormat and SDL_DisplayFormatAlpha are not necessary, by the way.
They just improve performance when blitting to the display surface.

thanks that works great, i thought it was that simple, but was confusing
myself with my silly code ;)On 2 July 2010 14:35, Jonathan Dearborn wrote:

Yes, SDL_image loads the transparency data from the PNG into the SDL_Surface. At least it does for me.On July 2, 2010, at 14:01:15.00, Neil White wrote:

sorry if i am beating a dead fish by asking this question but i did briefly google but got no sense from any results

i am converting a game i mad using a different platform to run on SDL, its images are PNG with transparency already set in the image, Does SDL_Image have the capabilities to load this image with its mask already set? some of the images have alpha in the colored pixes, but that doesn’t really matter but it would be nice to know if i can load everything from the png.

thanks

hope that makes sense

Neil


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org