[SDL] playing multiple sounds simultaneously (SDL_Mixer, BeOS)

Matt Pekar nimdok at jhu.edu
Tue Aug 21 04:27:00 PDT 2001

>for now i understand Per-pixel Alpha value and Per-surface Alpha values..
>so if i make transparent background png file, that would be "Per-pixel 
>surface, so i cant adjust "Per-surface" alpha with "SDL_SetAlpha" function.
>there was a great function set Per-pixel alpha value which posted this 
>mailing list by Plus II.. thanks!

Right, SDL does not allow blits from a surface to use both per-surface
and per-pixel alpha at the same time. I don't know what code you are
referring to, but I presume it walks through the pixels of a surface
and scale their alpha value by a constant amount.

>but it worked a fresh loaded png files, but when i try to give whole screen
>alpha value, i cant make it.

The screen does not have any transparency (unless you are using OPENGLBLIT,
but I don't think you are in this case), because there is nothing "under"
it that could show through.

If you just want to fade the screen to black (or to white, or to any
other primary or combination of primaries), the best solution is to
use the SDL gamma functions (SDL_SetGammaRamp). This is equivalent to
manipulating the screen palette in 8-bit mode and is thus fast and smooth,
but might not always be available (check the return code).

If SDL_SetGammaRamp() doesn't work, you need to do it yourself in some
way. Either repeatedly re-paint the screen at successively darker
levels, like this:

for i = 1..255
   clear the screen (call SDL_FillRect with black pixels)
   paint your graphics on the screen with an alpha of (255-i)

Alternatively, you could try repeated blitting of a completely black
surface to the screen, varying the alpha levels, like this:

make a screen-sized black surface (call it "black")
for i = 1..255
   set the per-surface alpha of "black" to a(i)
   blit "black" on top of the screen

You can use the fact that at iteration i, the screen already has faded
to level (255-(i-1)), so a(i) would be something like
255 - (255 * (255 - (n-1)) / (255 - n). Roundoff errors might cause
it to give undesired results however, so you may want to fade in
fewer steps than 255.

More information about the SDL mailing list