Alpha blending and color keying

  1. I noticed when I load or create a surface without alpha channel, alpha
    blending is not active, while if I create a surface with alpha channel,
    blending is active. Why? Does this happen loading images? And setting a 32
    bit video mode?

  2. if I set 32 bit video mode, is it considered to have an alpha channel?

  3. Why if I save a surface with an alpha channel, alpha channel is not saved
    in the bitmap?

  4. Has the SRC_RLEACCEL flag in SDL_SetAlpha() the same effect it has with
    SDL_SetColorKey(). Is the surface packed for RLE and depacked when I access
    it…?

  5. In this case, if I set RLE with SetColorKey and the call SetAlpha without
    that flag, neither ColorKey will have RLE acceleration, right? Will the
    SCR_RLEACCEL flag in the flags member of SDL_Surface no more be present? Is
    that flag usaed just for color key or for alpha, too?

  6. The docs say that passing 0 as value of SetAlpha(), per-surface blending
    is disabled. But, when there is an alpha channel, value is simply ignored,
    so I can pass any value I want, while, when there’s not an alpha channel,
    passing 0 means the surface is completely transparent.

  7. The docs also say that copying from RGB to RGBA without SRCALPHA, the
    destination alpha channel is set zero to opaque. Doesn’t it mean 255 instead
    (opaque is 255, not 0)?

  8. SRC_ALPHA flag in the flags member or SDL_Surface is set when per-suface
    blending is active, but also when alha channel blending is, right?

  9. RLE with Color Key and Alpha Blending are useful only when alpha and
    color key blits are not hardware accelerated, right?

Thanx.

  1. I noticed when I load or create a surface without alpha channel, alpha
    blending is not active, while if I create a surface with alpha channel,
    blending is active. Why? Does this happen loading images? And setting a 32
    bit video mode?

Alpha blending occurs if the source surface has the SDL_SRCALPHA flag set.
It is automatically set when a surface with alpha channel is created,
but you can set/clear it yourself with SDL_SetAlpha()

  1. if I set 32 bit video mode, is it considered to have an alpha channel?

No, the screen does not have an alpha channel. The exception is when
you use SDL_OPENGLBLIT.

  1. Why if I save a surface with an alpha channel, alpha channel is not saved
    in the bitmap?

The SDL core library can only load/save BMP files, and it does not (to
my knowledge) support alpha channels. The SDL_image library loads a
variety of formats, many of which support an alpha channel.
SDL_image does not support saving yet, but it may be added if there is
sufficient demand

  1. Has the SRC_RLEACCEL flag in SDL_SetAlpha() the same effect it has with
    SDL_SetColorKey(). Is the surface packed for RLE and depacked when I access
    it…?

Yes, both SDL_SetAlpha() and SDL_SetColorKey() can be used to set/remove
the RLEACCEL flag (it’s the same flag). An RLE-encoded surface is unpacked
when you lock it with SDL_LockSurface(), and re-packed when you unlock it

  1. In this case, if I set RLE with SetColorKey and the call SetAlpha without
    that flag, neither ColorKey will have RLE acceleration, right? Will the
    SCR_RLEACCEL flag in the flags member of SDL_Surface no more be present? Is
    that flag usaed just for color key or for alpha, too?

If you call either SetColorKey or SetAlpha without SDL_RLEACCEL, RLE
will be disabled for the surface. The last call wins. Both colourkeyed
and alpha-channel images can be RLE-encoded, and profitably so.

  1. The docs say that passing 0 as value of SetAlpha(), per-surface blending
    is disabled. But, when there is an alpha channel, value is simply ignored,
    so I can pass any value I want, while, when there’s not an alpha channel,
    passing 0 means the surface is completely transparent.

The docs say no such thing - please read the docs coming with the source
distribution, or the ongoing doc project at http://sdldoc.csn.ul.ie/ .
See in particular the SDL_BlitSurface() and SDL_SetAlpha pages of the online
docs.

  1. The docs also say that copying from RGB to RGBA without SRCALPHA, the
    destination alpha channel is set zero to opaque. Doesn’t it mean 255 instead
    (opaque is 255, not 0)?

Again, you must be reading ancient docs. 255 is opaque alpha since SDL-1.1.5.

  1. SRC_ALPHA flag in the flags member or SDL_Surface is set when per-suface
    blending is active, but also when alha channel blending is, right?

SRCALPHA not set => no alpha-blending takes place
SRCALPHA set => alpha channel used if available, otherwise per-surface alpha.

  1. RLE with Color Key and Alpha Blending are useful only when alpha and
    color key blits are not hardware accelerated, right?

Mostly so; they also save memory. Also, very few targets accelerate
alpha blits, and many targets (especially X11) don’t accelerate any
operations at all

  1. The docs say that passing 0 as value of SetAlpha(), per-surface
    blending

is disabled. But, when there is an alpha channel, value is simply
ignored,

so I can pass any value I want, while, when there’s not an alpha channel,
passing 0 means the surface is completely transparent.

The docs say no such thing - please read the docs coming with the source
distribution, or the ongoing doc project at http://sdldoc.csn.ul.ie/ .
See in particular the SDL_BlitSurface() and SDL_SetAlpha pages of the
online
docs.
From the docs:
“Setting the per-surface alpha value to 0 disables per-surface alpha
blending.”

  1. The docs also say that copying from RGB to RGBA without SRCALPHA, the
    destination alpha channel is set zero to opaque. Doesn’t it mean 255
    instead

(opaque is 255, not 0)?

Again, you must be reading ancient docs. 255 is opaque alpha since
SDL-1.1.5.
From the docs:
“RGBA->RGB without SDL_SRCALPHA
The RGB data is copied from the source and the destination alpha is zero to
opaque. If SDL_SRCCOLORKEY is set, only the pixels not matching the colorkey
value are copied.”

The docs say that not 255 is considered opaque and 0 transaparent, in the
beginning, but after say these two things…–
Marco Iannaccone
marciann at tin.it
ICQ UIN: 18748121 MetalCoder

"I bei film drammatici fanno sentire intelligenti anche gli stupidi, i bei
film comici fanno tornare bambini anche le persone intelligenti.
Peccato che sia pi? facile trovare stupidi che vogliono sembrare
intelligenti che persone intelligenti col coraggio di saper tornare bambini"
Stan Laurel

“Setting the per-surface alpha value to 0 disables per-surface alpha
blending.”

thank you, that is indeed an error (even if the claim could be made that it
is right in a way)

“RGBA->RGB without SDL_SRCALPHA
The RGB data is copied from the source and the destination alpha is zero to
opaque. If SDL_SRCCOLORKEY is set, only the pixels not matching the colorkey
value are copied.”

that is an unfortunate and misleading sentence, although not really incorrect.
It should be changed in any case

  1. if I set 32 bit video mode, is it considered to have an alpha channel?

No, the screen does not have an alpha channel. The exception is when
you use SDL_OPENGLBLIT.
And with SDL_OPENGL? Shouldn’t there be an alpha channel too?–
Marco Iannaccone
marciann at tin.it
ICQ UIN: 18748121 MetalCoder

“I bei film drammatici fanno sentire intelligenti anche gli stupidi, i bei
film comici fanno tornare bambini anche le persone intelligenti.
Peccato che sia pi? facile trovare stupidi che vogliono sembrare
intelligenti che persone intelligenti col coraggio di saper tornare bambini"
Stan Laurel
"Mattias Engdeg?rd” ha scritto nel messaggio
news:200104200835.KAA27551 at my.nada.kth.se

  1. I noticed when I load or create a surface without alpha channel, alpha
    blending is not active, while if I create a surface with alpha channel,
    blending is active. Why? Does this happen loading images? And setting a
    32

bit video mode?

Alpha blending occurs if the source surface has the SDL_SRCALPHA flag set.
It is automatically set when a surface with alpha channel is created,
but you can set/clear it yourself with SDL_SetAlpha()

  1. if I set 32 bit video mode, is it considered to have an alpha channel?

No, the screen does not have an alpha channel. The exception is when
you use SDL_OPENGLBLIT.

  1. Why if I save a surface with an alpha channel, alpha channel is not
    saved

in the bitmap?

The SDL core library can only load/save BMP files, and it does not (to
my knowledge) support alpha channels. The SDL_image library loads a
variety of formats, many of which support an alpha channel.
SDL_image does not support saving yet, but it may be added if there is
sufficient demand

  1. Has the SRC_RLEACCEL flag in SDL_SetAlpha() the same effect it has
    with

SDL_SetColorKey(). Is the surface packed for RLE and depacked when I
access

it…?

Yes, both SDL_SetAlpha() and SDL_SetColorKey() can be used to set/remove
the RLEACCEL flag (it’s the same flag). An RLE-encoded surface is unpacked
when you lock it with SDL_LockSurface(), and re-packed when you unlock it

  1. In this case, if I set RLE with SetColorKey and the call SetAlpha
    without

that flag, neither ColorKey will have RLE acceleration, right? Will the
SCR_RLEACCEL flag in the flags member of SDL_Surface no more be present?
Is

that flag usaed just for color key or for alpha, too?

If you call either SetColorKey or SetAlpha without SDL_RLEACCEL, RLE
will be disabled for the surface. The last call wins. Both colourkeyed
and alpha-channel images can be RLE-encoded, and profitably so.

  1. The docs say that passing 0 as value of SetAlpha(), per-surface
    blending

is disabled. But, when there is an alpha channel, value is simply
ignored,

so I can pass any value I want, while, when there’s not an alpha channel,
passing 0 means the surface is completely transparent.

The docs say no such thing - please read the docs coming with the source
distribution, or the ongoing doc project at http://sdldoc.csn.ul.ie/ .
See in particular the SDL_BlitSurface() and SDL_SetAlpha pages of the
online
docs.

  1. The docs also say that copying from RGB to RGBA without SRCALPHA, the
    destination alpha channel is set zero to opaque. Doesn’t it mean 255
    instead

(opaque is 255, not 0)?

Again, you must be reading ancient docs. 255 is opaque alpha since
SDL-1.1.5.

  1. SRC_ALPHA flag in the flags member or SDL_Surface is set when
    per-suface

blending is active, but also when alha channel blending is, right?

SRCALPHA not set => no alpha-blending takes place
SRCALPHA set => alpha channel used if available, otherwise per-surface
alpha.

  1. RLE with Color Key and Alpha Blending are useful only when alpha and
    color key blits are not hardware accelerated, right?

Mostly so; they also save memory. Also, very few targets accelerate
alpha blits, and many targets (especially X11) don’t accelerate any
operations at all