GetOutOfBox
GetOutOfBox

Reputation: 121

Copying SDL_Surfaces with Alpha Channels

I've run into issues preserving the alpha channels of surfaces being copied/clip blitted (blitting sections of a surface onto a smaller surface, they're spritesheets). I've tried various solutions, but the end result is that any surface that's supposed to have transparency ends up becoming fully opaque (alpha mask becomes white).

So my question is, how does one copy one RGBA SDL_Surface to another new surface (also RGBA), including the alpha channel? And if it's any different, how does one copy a section of an RGBA surface, to a new RGBA surface (the same size of the clipped portion of the source surface), ala tilesheet blitting.

It seems that SDL_BlitSurface blends the alpha channels, so when for example, I want to copy a tile from my tilesheet surface to a new surface (which is of course, blank, I'm assuming SDL fills surfaces with black or white by default), it ends up losing it's alpha mask, so that when that tile is finally blitted to the screen, it doesn't blend with whatever is on the screen.

SDL_DisplayFormatAlpha works great to copy surfaces with an alpha mask, but it doesn't take clip parameters, it's only intended to copy the entire surface, not a portion of it, hence my problem.

Upvotes: 2

Views: 1106

Answers (2)

Bemos
Bemos

Reputation: 11

If anyone is still wondering after all these years:

Before bliting the surface, you need to make sure that the blending mode of the source (which is an SDL_Surface) is set to SDL_BLENDMODE_NONE as described in the documentation: SDL_SetSurfaceBlendMode(). Is should look something simple like this:

SDL_SetSurfaceBlendMode(source, SDL_BLENDMODE_BLEND);
SDL_BlitSurface(source, sourceRect, destination, destinationRect);

Upvotes: 1

Rxz_14
Rxz_14

Reputation: 11

I had this problem before and have not come to an official answer yet. However, I think the only way to do it will be to write your own copy function. http://www.libsdl.org/docs/html/sdlpixelformat.html This page will help you understand how SDL_Surface stores color information. Note that there is a huge difference between colors above and below 8 bits.

Upvotes: 0

Related Questions