# [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 on other sites
Can you post the code you are using to load the image, and the code to blit it please.

##### 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 on other sites
Quote:
 Original post by Jack SotacIf 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 timeCheckout the last line of the description section forSDL_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 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 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 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 on other sites
Quote:
 Original post by Iori BranfordI'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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627718
• Total Posts
2978793

• 9
• 9
• 21
• 14
• 12