The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
transparent surface problem
Davide "M3xican" Coppola
Guest

Hi,

I have a problem with trasnparent surface and PNG images.

I need to create a transparent surface then blit images on it and
finally blit
this surface on screen.

The images are PNG with alpha channel and transparency, the problem is that
when I create the surface and blit images on it, the partially
transparent pixels
of the images disappear, losing so the AA and several details!

this is the code that load the image:

Code:

SDL_Surface * temp = IMG_Load(file.c_str());
SDL_SetAlpha(temp , SDL_SRCALPHA | SDL_RLEACCEL , SDL_ALPHA_OPAQUE);
img = SDL_DisplayFormatAlpha(temp);


and this is the code that create the surface:

Code:

SDL_PixelFormat * format = img->format;
a_surface = SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCALPHA,w,h,

format->BitsPerPixel,

format->Rmask, format->Gmask,

format->Bmask, format->Amask );


I have also tried the example code in the man page of
SDL_CreateRGBSurface,
but the result is the same.

This is an image that shows the problem:

http://mars.sourceforge.net/data/img/surf_problem.png

the building on the right (2) is the PNG blitted on screen, the one on
the left (1) is
the PNG blitted on the surface.

What's the problem?

Thank you for help.

--

Davide "M3xican" Coppola

--------------------------------------------------------------------
email: dmc at dev-labs.net
homep: http://dmc.dev-labs.net
ICQ: 104241710
MSN: bad_hangover at hotmail.it

Don't use IE and OE!
Download FireFox and ThunderBird: http://www.mozilla.org/
--------------------------------------------------------------------
transparent surface problem
Brian Barrett
Guest

maybe i'm misunderstanding, but can't you manually copy( memcpy() )
the pixels from the source to the destination, rather than using
SDL_BlitSurface() which loses the alpha values?
transparent surface problem
Alex Volkov
Guest

I am assuming that the image of the fortification is separate from the image
of its shadow, and that the shadow image is translucent (partial alpha). In
that case, SDL_BlitSurface is behaving according to doc:
* RGBA->RGBA:
* SDL_SRCALPHA set:
* alpha-blend (using the source alpha channel) the RGB values;
* leave destination alpha untouched.
* SDL_SRCALPHA not set:
* copy all of RGBA to the destination.

If you want the alpha channel to be transferred as well you need to remove
the SDL_SRCALPHA flag from the source image surfaces. But also keep in mind
that if the translucent pixels of your image surfaces overlap when blitted
to your transparent surface, you will not get a 100% correct result -- they
will not be blended, and neither will translucent pixels with opaque ones.
You would need to write your own (transblending) blit for that.

-Alex.

-----Original Message-----
From: Davide "M3xican" Coppola
Sent: Thursday, January 19, 2006 5:17 AM
To: sdl at libsdl.org
Subject: [SDL] transparent surface problem

[...snip...]
I need to create a transparent surface then blit images on it and finally
blit this surface on screen.

The images are PNG with alpha channel and transparency, the problem is that
when I create the surface and blit images on it, the partially transparent
pixels of the images disappear, losing so the AA and several details!

this is the code that load the image:

Code:

SDL_Surface * temp = IMG_Load(file.c_str());
SDL_SetAlpha(temp , SDL_SRCALPHA | SDL_RLEACCEL , SDL_ALPHA_OPAQUE);
img = SDL_DisplayFormatAlpha(temp);


[...snip...]
This is an image that shows the problem:

http://mars.sourceforge.net/data/img/surf_problem.png
transparent surface problem
Bill Kendrick
Guest

On Fri, Jan 20, 2006 at 12:39:39AM +0000, Brian Barrett wrote:
Quote:
maybe i'm misunderstanding, but can't you manually copy( memcpy() )
the pixels from the source to the destination, rather than using
SDL_BlitSurface() which loses the alpha values?

maybe _i'm_ misunderstanding, but wouldn't that not do what you expect
in some cases? (e.g., if src and dest are of different formats;
or one HW surface, the other SW surface)


-bill!
transparent surface problem
Brian Barrett
Guest

Quote:
maybe _i'm_ misunderstanding, but wouldn't that not do what you expect
in some cases? (e.g., if src and dest are of different formats;
or one HW surface, the other SW surface)

well lock the surface, and use the getpixel and putpixel code from the
docs, and is my suggestion any better?

anyway, looking at the original message, i think the pixel frmat will
be the same
"
SDL_Surface * temp = IMG_Load(file.c_str());
//snip...
img = SDL_DisplayFormatAlpha(temp);
//snip....
SDL_PixelFormat * format = img->format;
a_surface = SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCALPHA,w,h,format->BitsPerPixel,

format->Rmask, format->Gmask,

format->Bmask, format->Amask );"
transparent surface problem
Davide "M3xican" Coppola
Guest

Hi, after some tests I have solved the problem, these are the results:

1- it's useless use SDL_BlitSurface() if you need to blit an image with
transparent pixels on a surface totally transparent.

2- it's possible use getpixel() and putpixel() functions, but this
method is
very slow.

3- the fastest method possible is use memcpy() copying pixels data from
the image to the surface.

Thanks to everybody has replied me.

--

Davide "M3xican" Coppola

--------------------------------------------------------------------
email: dmc at dev-labs.net
homep: http://dmc.dev-labs.net
ICQ: 104241710
MSN: bad_hangover at hotmail.it

Don't use IE and OE!
Download FireFox and ThunderBird: http://www.mozilla.org/
--------------------------------------------------------------------
transparent surface problem
Sam Lantinga
Guest

Quote:
Hi,

Quote:
I have a problem with trasnparent surface and PNG images.

Quote:
I need to create a transparent surface then blit images on it and
finally blit
this surface on screen.

Quote:
The images are PNG with alpha channel and transparency, the problem is that
when I create the surface and blit images on it, the partially
transparent pixels
of the images disappear, losing so the AA and several details!

Can you enter this bug in the SDL bugzilla:
http://bugzilla.libsdl.org/

Please include a small test program and image that can be used to
see the problem.

Thanks!
-Sam Lantinga, Senior Software Engineer, Blizzard Entertainment
transparent surface problem
Davide "M3xican" Coppola
Guest

Quote:
Can you enter this bug in the SDL bugzilla:
http://bugzilla.libsdl.org/

Quote:
Please include a small test program and image that can be used to
see the problem.

I have submitted the bug:
https://bugzilla.libsdl.org/show_bug.cgi?id=68

I have also created a package that shows the problem and my (temporary) solutions:
http://mars.sourceforge.net/SDL/trans_bug.tar.gz

I hope this could help you.

--

Davide "M3xican" Coppola

--------------------------------------------------------------------
email: dmc at dev-labs.net
homep: http://dmc.dev-labs.net
ICQ: 104241710
MSN: bad_hangover at hotmail.it

Don't use IE and OE!
Download FireFox and ThunderBird: http://www.mozilla.org/
--------------------------------------------------------------------