[SDL] Help on "alpha fading"

Joseba Garcia Etxebarria josebagar at euskalnet.net
Tue Jan 15 15:33:02 PST 2002


Hi,
i'm quite newby to SDL programming, but i'm trying to make a simple demo 
using sdl, sdl_image and sdl_mixer (it's inredible how easy these are to 
use) the problem is that i want to fade an image until it becomes 
transparent and then unload it. The image loads perfectly, the only 
problem is when i try to make it "fade off".
This is the part of the code that should be in charge of doing so:

if (event.type == SDL_MOUSEBUTTONDOWN ) {         //if mousen's pressed
    if(boton == 1)                                                    
                  //and it's touching the button
      {
        alpha=128;                                                       
                //dirty thing   
        while(alpha != 0)
          {
                SDL_SetAlpha(cursor, SDL_SRCALPHA, alpha--);
                RS_Blit(cursor, x, y);
                printf("Current alpha value is: %d\n", alpha);
                SDL_Delay(10);
          }
      }
    else if(boton == 2)
      {
        done=1;
      }
      }
      if(old_boton != boton)
    {
      if(((boton == 1) || (boton == 2)) && (old_boton == 0))
        {
          Mix_PlayChannel(RUIDITO, sonidos[RUIDITO], 0);
        }
    }

Another thing, can i avoid doing that sucker thing of setting the alpha 
value always to 128, in  other words, can i detect the alpha value of a 
surface with SDL?

To do what i propose, shoul i use something like SDL_BlitSurface or 
something like that?

(Please, don't kick me if you think the code is too dirty, i said i'm a 
newby :-)
Well, what it's supposed to do is that when SDl detects that the mouse 
is pressed, and if the mouse is inside the menu button, it should 
"alpha-fade" the cursor off. It does detect correctly if the mouse is 
pressed and if it's inside the button, but the the image does not fade 
it just changes suddenly into (nearly) transparent. i know that it's a 
problem of redrawing the image, but i've tried to do so and i just get 
very odd things.
Can anyone help, please?
By the way, this is the RS_Blit function in charge of redrawnig the 
selected surface:
void RS_Blit(SDL_Surface *bitmap, Sint16 x, Sint16 y) {
  SDL_Rect dest;

  dest.x = x;
  dest.y = y;
  dest.w = bitmap->w;
  dest.h = bitmap->h;
  SDL_BlitSurface(bitmap, NULL, screen, &dest);      //Just redraws the 
surface
}

Thanks in advance

PD:i got all these SDL versions, but can update them if necessary (as 
far as i'm concerned, it's not, but...) SDL1.2.0 (Linux) SDL1.2.2 
(Windows, but please don't refer to this, i hate trying to program uner 
a system that creates and stdout.txt instead of printing to a terminal 
if started the program from one) SDL1.2.3 (Beos)






More information about the SDL mailing list