Help on "alpha fading"

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 :slight_smile:
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)

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”.

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?

It’s part of the surface’s format structure; you can get the current value
(from 0:transparent to 255:opaque) from surface->format->alpha

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

Yup, don’t think there’s really any other way, aside from manually filling
in all the pixels yourself. And why would you want to do that? :slight_smile:

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?

Well, it works for me :wink: The only thing I can think of is that you don’t
seem to be updating the screen - with SDL_UpdateRects or SDL_Flip(). If
you don’t do this then the changes you make to the screen will probably
never be seen.

In general, your blit call should look something like:

rect.x = x;
rect.y = y;
rect.w = object->w;
rect.h = object->h;

SDL_BlitSurface (object, NULL, screen, &rect);
SDL_UpdateRects (screen, 1, &rect);

If you’re trying to display more than one object at a time, you get much
better performance by only calling SDL_UpdateRects() once - and if possible,
avoid updating the same area twice.On Wed, 16 Jan 2002, Joseba Garcia Etxebarria wrote:

Mike.

Thanks, i’'l try it right now>It’s part of the surface’s format structure; you can get the current value

(from 0:transparent to 255:opaque) from surface->format->alpha

Hi people,

I’m writing a quake-like console. While coding I’ve run into a small
problem. I don’t know how to get a ‘:’ and so on…I’m using
SDL_GetKeyName() to get the chars of the key…no problem so far…but
how can I manage it to pass the modifiers to SDL_GetKeyName() to get ':'
and ‘’ etc…

hope you know what I mean…

Bye and thnx,

Alex Bierbrauer

Hi people,

I’m writing a quake-like console. While coding I’ve run into a small
problem. I don’t know how to get a ‘:’ and so on…I’m using
SDL_GetKeyName() to get the chars of the key…no problem so far…but
how can I manage it to pass the modifiers to SDL_GetKeyName() to get ':'
and ‘’ etc…

hope you know what I mean…

You need to call SDL_EnableUNICODE(1) and then look at the 'unicode’
member of the keysym in the key down event.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

alex at polyfrag.com wrote:

Hi people,

I’m writing a quake-like console. While coding I’ve run into a small
problem. I don’t know how to get a ‘:’ and so on…I’m using
SDL_GetKeyName() to get the chars of the key…no problem so far…but
how can I manage it to pass the modifiers to SDL_GetKeyName() to get ':'
and ‘’ etc…

hope you know what I mean…

Yeah, I know exactly what you mean. Turns out you don’t use
SDL_GetKeyName()
for this purpose. It just returns the key name, not the character that
you
are trying to get. You get that my enabling unicode and looking at the
unicode item in the SDL_keysym structure. If you look carefully at
checkkeys.c
in the test directory in the SDL distribution you will see an example of
how
to do this.

Bob P.

P.S.

I can mail sample code or post it to the list if you can’t get it off
the net.–
±-----------------------------------+

  • Bob Pendleton is seeking contract +
  • and consulting work. Find out more +
  • at http://www.jump.net/~bobp +
    ±-----------------------------------+