Sign in to follow this  
Iori Branford

[SDL] I can't alpha blend my surface. Why?

Recommended Posts

I have a 32-bit TGA sprite with RLE. I do SDL_DisplayFormatAlpha() after loading:
SDL_Surface* temp_sprite = IMG_Load(SPRITEFILE); 
if(!temp_sprite)
	return false;
sprite = SDL_DisplayFormatAlpha(temp_sprite);
SDL_FreeSurface(temp_sprite);
and SDL_SetAlpha() with SDL_SRCALPHA|SDL_RLEACCEL before it's blitted:
SDL_SetAlpha(sprite, SDL_SRCALPHA|SDL_RLEACCEL, 128);
SDL_BlitSurface(sprite, NULL, screen, &dstrect);
But the sprite stays opaque no matter what. The same if I make the sprite a PNG with a transparent color. [Edited by - Iori Branford on October 2, 2006 3:38:04 PM]

Share this post


Link to post
Share on other sites
If your surface has an alpha channel , the per-surface alpha value will be ignored. That is to say, you can't blit using per-pixel and per-surface alpha at the same time

Checkout the last line in the description section for
SDL_SetAlpha.

If you wish to use the per-pixel and per-surface alpha values to blit, then you have to write your own function.

Good Luck.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jack Sotac
If your surface has an alpha channel , the per-surface alpha value will be ignored. That is to say, you can't blit doing a per-pixel and per-surface alpha at the same time

Checkout the last line of the description section for
SDL_SetAlpha.

If you wish to use the per-pixel and per-surface alpha values to blit, then you have to write your own function.

Good Luck.

I can see why the TGA with its alpha-channel-based transparency wouldn't work that way, but PNG transparency is color-key-based. So why my PNG won't alpha blend, I can't imagine.

Unless Pazera's Focus on SDL was wrong about using per-surface alpha and color keys together? Only difference between his code and mine is that he loads his sprite from a BMP, doesn't do SDL_DisplayFormatAlpha() and sets the color key himself.

[Edited by - Iori Branford on October 2, 2006 4:52:35 PM]

Share this post


Link to post
Share on other sites
I've got it. The problem was twofold.

First, SDL_Image. For whatever reason, an image loaded with it could not be alpha blended. I made a BMP, switched to SDL_LoadBMP and voila.

Second, SDL_DisplayFormatAlpha() was not necessary. In fact, somehow it too disabled alpha blending for the surface I used it on.

Share this post


Link to post
Share on other sites
Quote:
Original post by Iori Branford PNG transparency is color-key-based

I am sure PNG transparency is done through an 8-bit alpha channel. Although some png images may not include an alpha-channel(no transparency), that is rare.

You can blit an image with an alpha-channel using a colorkey if SDL_SRCALPHA is NOT set. But if SDL_SRCALPHA is not set, then you cannot use per-surface or per-pixel alpha, only the colorkey.

It is true that colorkey and per-surface alpha can be used together. But if a surface has an alpha-channel and SDL_SRCALPHA is set, then both colorkey and per-surface alpha will be ignore.

An SDL surface has an alpha-channel if (surface->format->Amask != 0) is true.

See the description of SDL_BlitSurface and specifically the psuedocode segment to see how per-surface,per-pixel alpha, and colorkeying works and doesn't work together.

PNG reference,see Colour depths.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jack Sotac
Quote:
Original post by Iori Branford PNG transparency is color-key-based

I am sure PNG transparency is done through an 8-bit alpha channel. Although some png images may not include an alpha-channel(no transparency), that is rare.

You can blit an image with an alpha-channel using a colorkey if SDL_SRCALPHA is NOT set. But if SDL_SRCALPHA is not set, then you cannot use per-surface or per-pixel alpha, only the colorkey.

It is true that colorkey and per-surface alpha can be used together. But if a surface has an alpha-channel and SDL_SRCALPHA is set, then both colorkey and per-surface alpha will be ignore.

An SDL surface has an alpha-channel if (surface->format->Amask != 0) is true.

See the description of SDL_BlitSurface and specifically the psuedocode segment to see how per-surface,per-pixel alpha, and colorkeying works and doesn't work together.

PNG reference,see Colour depths.

My bad, you're right. I must have been thinking of GIF for some reason.

Share this post


Link to post
Share on other sites
Quote:
Original post by Iori Branford
I've got it. The problem was twofold.

First, SDL_Image. For whatever reason, an image loaded with it could not be alpha blended. I made a BMP, switched to SDL_LoadBMP and voila.


That's not actually true, so your problem must have come from elsewhere. Perhaps it loaded in the alpha channel from the PNG which overrode the per-surface alpha values.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this