SDL_BlitSurface with an alpha channel

Hi,

I’m writing a program which loads PNG images and converts them into
OpenGL textures. Everything’s been working great so far. I can load
an image of any arbitrary size, copy it into a texel-sized surface, and
send the texture to OpenGL. If the image has a transparent color key
it copies correctly using SDL_BlitSurface(). However if the image
source has an alpha channel SDL_BlitSurface() produces a completely
transparent image in the destination - obviously not what I want.

My solution was to copy the pixel data verbatim from the source surface
to the texel-sized surface when there’s an alpha channel:

if (source->flags & SDL_SRCALPHA && source->format->BitsPerPixel ==
32)
My_CopyPixels(source, surface); // yes, it handles disparate sizes
else
SDL_BlitSurface(source, NULL, surface, NULL);

This seems pretty silly. Is there something else I (c|sh)ould have
done to preserve the contents of the alpha channel?

Thanks,
Scott Lahteine
Portland, OR

In another thread I posted the following:

http://www.libsdl.org/pipermail/sdl/2002-October/050061.html

The thread stopped after my post, so I assume it helped the original author.
I think it would also solve your problem.
The problem with my solution is that it requires patching the SDL blitcode.

Huib-Jan> ----- Original Message -----

From: slur@qwest.net (Scott Lahteine)
To:
Sent: Wednesday, November 20, 2002 1:31 PM
Subject: [SDL] SDL_BlitSurface with an alpha channel

Hi,

I’m writing a program which loads PNG images and converts them into
OpenGL textures. Everything’s been working great so far. I can load
an image of any arbitrary size, copy it into a texel-sized surface, and
send the texture to OpenGL. If the image has a transparent color key
it copies correctly using SDL_BlitSurface(). However if the image
source has an alpha channel SDL_BlitSurface() produces a completely
transparent image in the destination - obviously not what I want.

My solution was to copy the pixel data verbatim from the source surface
to the texel-sized surface when there’s an alpha channel:

if (source->flags & SDL_SRCALPHA && source->format->BitsPerPixel ==
32)
My_CopyPixels(source, surface); // yes, it handles disparate sizes
else
SDL_BlitSurface(source, NULL, surface, NULL);

This seems pretty silly. Is there something else I (c|sh)ould have
done to preserve the contents of the alpha channel?

Thanks,
Scott Lahteine
Portland, OR


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

The solution would have worked if I had used it. However, considering
this text overlay only needed to be used on two surfaces, instead of
making and maintaining a fix in SDL, that doesn’t look like it’s going
to make it into the main line of code, I just kept two copies of the
text blended with the two surfaces.
RobertOn Wed, 2002-11-20 at 08:17, Huib-Jan Imbens wrote:

In another thread I posted the following:

http://www.libsdl.org/pipermail/sdl/2002-October/050061.html

The thread stopped after my post, so I assume it helped the original author.
I think it would also solve your problem.
The problem with my solution is that it requires patching the SDL blitcode.

Huib-Jan

----- Original Message -----
From: Scott Lahteine
To:
Sent: Wednesday, November 20, 2002 1:31 PM
Subject: [SDL] SDL_BlitSurface with an alpha channel

Hi,

I’m writing a program which loads PNG images and converts them into
OpenGL textures. Everything’s been working great so far. I can load
an image of any arbitrary size, copy it into a texel-sized surface, and
send the texture to OpenGL. If the image has a transparent color key
it copies correctly using SDL_BlitSurface(). However if the image
source has an alpha channel SDL_BlitSurface() produces a completely
transparent image in the destination - obviously not what I want.

My solution was to copy the pixel data verbatim from the source surface
to the texel-sized surface when there’s an alpha channel:

if (source->flags & SDL_SRCALPHA && source->format->BitsPerPixel ==
32)
My_CopyPixels(source, surface); // yes, it handles disparate sizes
else
SDL_BlitSurface(source, NULL, surface, NULL);

This seems pretty silly. Is there something else I (c|sh)ould have
done to preserve the contents of the alpha channel?

Thanks,
Scott Lahteine
Portland, OR


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


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

Turn off SDL_SRCALPHA in the source surface:

SDL_SetAlpha(image, 0, SDL_ALPHA_OPAQUE);

SDL_SRCALPHA has nothing directly to do with the alpha channel. (You
can have an alpha channel and have SDL_SRCALPHA disabled.)

Rather, it tells blits that they should blend the alpha value (which may
be from an alpha channel, color key or the default provided by SDL_SetAlpha’s
third argument). Since it seems you’re blitting to a dummy surface to
prepare it for sending to OpenGL, you don’t want source alpha; you just
want to blit alpha values directly.On Wed, Nov 20, 2002 at 04:31:36AM -0800, Scott Lahteine wrote:

My solution was to copy the pixel data verbatim from the source surface
to the texel-sized surface when there’s an alpha channel:

if (source->flags & SDL_SRCALPHA && source->format->BitsPerPixel ==
32)
My_CopyPixels(source, surface); // yes, it handles disparate sizes
else
SDL_BlitSurface(source, NULL, surface, NULL);


Glenn Maynard