Using SDL_SetAlpha with 32-bit images

I’m upgrading a game from 8bit graphics to 32bit graphics. So far most things are working ok, but I have this problem.

In the old code, the function SDL_SetAlpha was used to make isometric walls fade to 50% opacity so you could see behind them sometimes. With the 32bit sprites, this no longer works. SDL ignores the command and refers to the sprite’s alpha channel instead at all times.

Is there some other command I can use? What alternatives are there?

Thanks.

SDL_SetAlpha() is an SDL 1.2 function and unfortunately, the blitter in SDL
1.2 has the limitation that it does not support a combination of per-pixel
alpha and per-surface alpha. The only way to get a similar effect is to
use a colorkeyed transparency on an RGB surface and use SDL_SetAlpha() on
it.

I would recommend that you use SDL 2.0, which does let you combine these
types of transparency in its new rendering API. When you use textures, you
can use SDL_SetTextureAlphaMod() just like you used SDL_SetAlpha().

Also, SDL_gpu enables this as well (for either SDL 1.2 or SDL 2.0). Use
GPU_SetRGBA() to set the modulation color of a GPU_Image or use
GPU_SetTargetRGBA() to set it for an entire render target.

Jonny DOn Thu, Mar 10, 2016 at 5:36 PM, mikd wrote:

I’m upgrading a game from 8bit graphics to 32bit graphics. So far most
things are working ok, but I have this problem.

In the old code, the function SDL_SetAlpha was used to make isometric
walls fade to 50% opacity so you could see behind them sometimes. With the
32bit sprites, this no longer works. SDL ignores the command and refers to
the sprite’s alpha channel instead at all times.

Is there some other command I can use? What alternatives are there?

Thanks.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

This article mentions a library called Sprig that could help. (I’m too much of a newbie to know how to switch to SDL 2.0.)

http://www.sdltutorials.com/the-ins-and-outs-and-overlays-of-alpha-blending

How would I use this library in a FreePascal program?

Yeah… I wrote that article and SPriG. :slight_smile:

Sprig works alright and does have the functionality (though the performance
isn’t great), but I’m not maintaining it anymore in favor of
hardware-accelerated graphics (SDL_gpu). Sprig doesn’t have a FreePascal
port, so you’d have to figure out and set it up yourself.

Porting to SDL 2 and its rendering system doesn’t actually take many
changes (see the migration guide in the SDL wiki) and it is worth it for
the new features.

The other way, using SDL_gpu, means you have to replace more function calls
and variable types, but the API is more similar in feel to SDL 1.2’s
rendering.

Also, do the walls actually need an alpha channel? You might get what you
want just by disabling it with SDL_SetAlpha(surface, 0, alpha).

Jonny DOn Fri, Mar 11, 2016 at 12:08 PM, mikd wrote:

This article mentions a library called Sprig that could help. (I’m too
much of a newbie to know how to switch to SDL 2.0.)

http://www.sdltutorials.com/the-ins-and-outs-and-overlays-of-alpha-blending

How would I use this library in a FreePascal program?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

(or, failing that, convert it to RGB with SDL_DisplayFormat())On Fri, Mar 11, 2016 at 12:25 PM, Jonathan Dearborn <@Jonathan_Dearborn> wrote:

Yeah… I wrote that article and SPriG. :slight_smile:

Sprig works alright and does have the functionality (though the
performance isn’t great), but I’m not maintaining it anymore in favor of
hardware-accelerated graphics (SDL_gpu). Sprig doesn’t have a FreePascal
port, so you’d have to figure out and set it up yourself.

Porting to SDL 2 and its rendering system doesn’t actually take many
changes (see the migration guide in the SDL wiki) and it is worth it for
the new features.

The other way, using SDL_gpu, means you have to replace more function
calls and variable types, but the API is more similar in feel to SDL 1.2’s
rendering.

Also, do the walls actually need an alpha channel? You might get what you
want just by disabling it with SDL_SetAlpha(surface, 0, alpha).

Jonny D

On Fri, Mar 11, 2016 at 12:08 PM, mikd wrote:

This article mentions a library called Sprig that could help. (I’m too
much of a newbie to know how to switch to SDL 2.0.)

http://www.sdltutorials.com/the-ins-and-outs-and-overlays-of-alpha-blending

How would I use this library in a FreePascal program?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Jonny D wrote:

Also, do the walls actually need an alpha channel??? You might get what you want just by disabling it with SDL_SetAlpha(surface, 0, alpha).

Jonny D

I don’t understand. Disable what? The wall sprites look like this, so RGBA is necessary.

[Image: http://i.imgur.com/bkdZu8z.png ]

Okay, well you could use a color key for that kind of binary transparency.

Jonny DOn Fri, Mar 11, 2016 at 12:57 PM, mikd wrote:

Jonny D wrote:

Also, do the walls actually need an alpha channel?? You might get what
you want just by disabling it with SDL_SetAlpha(surface, 0, alpha).

Jonny D

I don’t understand. Disable what? The wall sprites look like this, so RGBA
is necessary.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Jonny D wrote:

Okay, well you could use a color key for that kind of binary transparency.

Jonny D

It’s not binary. The wall has soft edges and a glass window.

Okay.On Fri, Mar 11, 2016 at 4:25 PM, mikd wrote:

Jonny D wrote:

Okay, well you could use a color key for that kind of binary
transparency.

Jonny D

It’s not binary. The wall has soft edges and a glass window.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I haven’t tried, perhaps SDL_SetSurfaceAlphaMod in SDL2 might give you what you want (make sure to enable the blend mode first).

Jonny mentioned an SDL 2 migration guide. I will take a look at that.

mikd wrote:

Jonny mentioned an SDL 2 migration guide. I will take a look at that.

I just found out there is no SDL 2.0 for FreePascal.