Implement fade-out animation on independent object

Hello,

This is a hard question to ask. Imagine a menu designed with some
SDL_Surfaces :

  • Start game
  • Join game
  • Options
  • Quit

All these SDL_Surface are correctly blitted and shown on the screen. Now
you want to make an animation on mouse-hover these surfaces. For example
the mouse pointer goes on “Start game”, the program will glow this
SDL_Surface.

That is easy to do, but the hard part is to “unglow” it with an
animation, you leave the menu item with the pointer thus a fade-out
animation appears, the SDL_Surface associated with “Start game” goes to
the original texture every 50ms.

That’s the question, how you can easily update all SDL_Surface each one
independently?

How would you implement this?

Cheers,–
David Demelier

The easiest way that I can think of is coming up with a struct and
tossing some sort of delay system on it, as well as an alpha channel:

struct FadingSurface {
??SDL_Surface *surf;
??Uint8 alpha;
??Uint8 minAlpha, maxAlpha;
??Uint8 alphaStep, alphaTarget;
??Uint32 internalMilliseconds;
Uint32 lastUpdate;
}

FadingSurface *fadeSurfaceCreate( int surfWidth, int surfHeight, int
surfDepth, Uint8 minAlpha, Uint8 maxAlpha ) {
??FadingSurface *fs = (FadingSurface *)malloc( sizeof( FadingSurface ) );

Uint32 rmask, gmask, bmask, amask;

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
??fs->surf = SDL_CreateSurface( 0, surfWidth, surfHeight, surfDepth,
rmask, bmask, gmask, amask );
if( !fs->surf ) {
free( fs );
return NULL;
}
SDL_SetSurfaceAlphaMod( fs->surf, maxAlpha );
fs->alpha = maxAlpha;
fs->minAlpha = minAlpha;
fs->maxAlpha = maxAlpha;
fs->alphaStep = 0;
}

// Call this when you want an option to fade in (call only once to
start the fade!)
void fadeSurfaceIn( FadingSurface *fs, Uint32 Milliseconds ) {
if( fs == NULL ) return;
if( fs->surf == NULL ) return;
fs->internalMillisecods = SDL_GetTicks( ) + Milliseconds;
fs->alpha = fs->minAlpha; // Comment this out if you want to be able
to cancel out previous animations
fs->alphaStep = (fs->maxAlpha - fs->minAlpha) / Milliseconds;
fs->alphaTarget = fs->maxAlpha;
fs->lastUpdate = SDL_GetTicks() - 1;
}

// Call this when you want an option to fade out (call only once to
start the fade!)
void fadeSurfaceOut( FadingSurface *fs, Uint32 Milliseconds ) {
if( fs == NULL ) return;
if( fs->surf == NULL ) return;
fs->internalMillisecods = SDL_GetTicks( ) + Milliseconds;
fs->alpha = fs->maxAlpha; // Comment this out if you want to be able
to cancel out previous animations
fs->alphaStep = (fs->minAlpha - fs->maxAlpha) / Milliseconds;
fs->alphaTarget = fs->minAlpha;
fs->lastUpdate = SDL_GetTicks() - 1;
}

// Call this every step in your menu loop
void fadeSurface( FadingSurface *fs ) {
if( fs == NULL ) return;
if( fs->surf == NULL ) return;
if( fs->alphaStep == 0 ) return;
if( SDL_GetTicks() > fs->lastUpdate ) {
SDL_SetSurfaceAlphaMod( fs->surf, fs->alpha );
fs->alpha += fs->alphaStep;
fs->lastUpdate = SDL_GetTicks()
}
if( ( SDL_GetTicks() > fs->internalMilliseconds ) || ( fs->alpha >=
fs->alphaTarget ) ) {
fs->alphaStep = 0
fs->alpha = fs->alphaTarget;
SDL_SetSurfaceAlphaMod( fs->surf, fs->alpha );
}
}

This would also be easy to implement some sort of callback system to
fade in and out repetatively.
I hope that helps - make sure you load some sort of an image to
fs->surface!!! I just added the surface create, but you’ll have to
make sure it has some sort of valid RGBA image.
Also, I didn’t test that code, so you might have to do some minor debugging.
Take care,
-AlexOn Tue, Apr 12, 2011 at 4:46 AM, David Demelier <demelier.david at gmail.com> wrote:

Hello,

This is a hard question to ask. Imagine a menu designed with some SDL_Surfaces :

  • Start game
  • Join game
  • Options
  • Quit

All these SDL_Surface are correctly blitted and shown on the screen. Now you want to make an animation on mouse-hover these surfaces. For example the mouse pointer goes on “Start game”, the program will glow this SDL_Surface.

That is easy to do, but the hard part is to “unglow” it with an animation, you leave the menu item with the pointer thus a fade-out animation appears, the SDL_Surface associated with “Start game” goes to the original texture every 50ms.

That’s the question, how you can easily update all SDL_Surface each one independently?

How would you implement this?

Cheers,


David Demelier


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

The easiest way that I can think of is coming up with a struct and
tossing some sort of delay system on it, as well as an alpha channel:

struct FadingSurface {
SDL_Surface *surf;
Uint8 alpha;
Uint8 minAlpha, maxAlpha;
Uint8 alphaStep, alphaTarget;
Uint32 internalMilliseconds;
Uint32 lastUpdate;
}

FadingSurface *fadeSurfaceCreate( int surfWidth, int surfHeight, int
surfDepth, Uint8 minAlpha, Uint8 maxAlpha ) {
FadingSurface *fs = (FadingSurface *)malloc( sizeof( FadingSurface ) );

Uint32 rmask, gmask, bmask, amask;

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
fs->surf = SDL_CreateSurface( 0, surfWidth, surfHeight, surfDepth,
rmask, bmask, gmask, amask );
if( !fs->surf ) {
free( fs );
return NULL;
}
SDL_SetSurfaceAlphaMod( fs->surf, maxAlpha );
fs->alpha = maxAlpha;
fs->minAlpha = minAlpha;
fs->maxAlpha = maxAlpha;
fs->alphaStep = 0;
}

// Call this when you want an option to fade in (call only once to
start the fade!)
void fadeSurfaceIn( FadingSurface *fs, Uint32 Milliseconds ) {
if( fs == NULL ) return;
if( fs->surf == NULL ) return;
fs->internalMillisecods = SDL_GetTicks( ) + Milliseconds;
fs->alpha = fs->minAlpha; // Comment this out if you want to be able
to cancel out previous animations
fs->alphaStep = (fs->maxAlpha - fs->minAlpha) / Milliseconds;
fs->alphaTarget = fs->maxAlpha;
fs->lastUpdate = SDL_GetTicks() - 1;
}

// Call this when you want an option to fade out (call only once to
start the fade!)
void fadeSurfaceOut( FadingSurface *fs, Uint32 Milliseconds ) {
if( fs == NULL ) return;
if( fs->surf == NULL ) return;
fs->internalMillisecods = SDL_GetTicks( ) + Milliseconds;
fs->alpha = fs->maxAlpha; // Comment this out if you want to be able
to cancel out previous animations
fs->alphaStep = (fs->minAlpha - fs->maxAlpha) / Milliseconds;
fs->alphaTarget = fs->minAlpha;
fs->lastUpdate = SDL_GetTicks() - 1;
}

// Call this every step in your menu loop
void fadeSurface( FadingSurface *fs ) {
if( fs == NULL ) return;
if( fs->surf == NULL ) return;
if( fs->alphaStep == 0 ) return;
if( SDL_GetTicks()> fs->lastUpdate ) {
SDL_SetSurfaceAlphaMod( fs->surf, fs->alpha );
fs->alpha += fs->alphaStep;
fs->lastUpdate = SDL_GetTicks()
}
if( ( SDL_GetTicks()> fs->internalMilliseconds ) || ( fs->alpha>=
fs->alphaTarget ) ) {
fs->alphaStep = 0
fs->alpha = fs->alphaTarget;
SDL_SetSurfaceAlphaMod( fs->surf, fs->alpha );
}
}

This would also be easy to implement some sort of callback system to
fade in and out repetatively.
I hope that helps - make sure you load some sort of an image to
fs->surface!!! I just added the surface create, but you’ll have to
make sure it has some sort of valid RGBA image.
Also, I didn’t test that code, so you might have to do some minor debugging.
Take care,
-Alex

Thanks for your interest!

I voluntary slimmed down a lot the code to test and I changed a lot of
things but the idea is the same.

My code located here : http://markand.malikania.fr/fade.c

Works well with a SDL_Surface filled with some colors but not if I load
an png image on it.

In the current state of the code the image called as const char smpath[]
which is a transparent background png does not fade. it stays at the
same alpha.

If you comment out the lines from 121 to 122 it will works.

Is there any restriction to the SDL_Surface format due to the png IMG_Load?

Cheers :-)On 12/04/2011 14:47, Alex Barry wrote:

On Tue, Apr 12, 2011 at 4:46 AM, David Demelier <@David_Demelier> wrote:

Hello,

This is a hard question to ask. Imagine a menu designed with some SDL_Surfaces :

  • Start game
  • Join game
  • Options
  • Quit

All these SDL_Surface are correctly blitted and shown on the screen. Now you want to make an animation on mouse-hover these surfaces. For example the mouse pointer goes on “Start game”, the program will glow this SDL_Surface.

That is easy to do, but the hard part is to “unglow” it with an animation, you leave the menu item with the pointer thus a fade-out animation appears, the SDL_Surface associated with “Start game” goes to the original texture every 50ms.

That’s the question, how you can easily update all SDL_Surface each one independently?

How would you implement this?

Cheers,


David Demelier


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


David Demelier

It’s probably because your PNG images don’t have an alpha channel when
you save them - either change them, or convert them to an RGBA format
in SDL.
If you’re still having problems, let me know,
-AlexOn Tue, Apr 19, 2011 at 11:16 AM, David Demelier <demelier.david at gmail.com> wrote:

On 12/04/2011 14:47, Alex Barry wrote:

The easiest way that I can think of is coming up with a struct and
tossing some sort of delay system on it, as well as an alpha channel:

struct FadingSurface {
? SDL_Surface *surf;
? Uint8 alpha;
? Uint8 minAlpha, maxAlpha;
? Uint8 alphaStep, alphaTarget;
? Uint32 internalMilliseconds;
? Uint32 lastUpdate;
}

FadingSurface *fadeSurfaceCreate( int surfWidth, int surfHeight, int
surfDepth, Uint8 minAlpha, Uint8 maxAlpha ) {
? FadingSurface *fs = (FadingSurface *)malloc( sizeof( FadingSurface ) );

? Uint32 rmask, gmask, bmask, amask;

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
? rmask = 0xff000000;
? gmask = 0x00ff0000;
? bmask = 0x0000ff00;
? amask = 0x000000ff;
#else
? rmask = 0x000000ff;
? gmask = 0x0000ff00;
? bmask = 0x00ff0000;
? amask = 0xff000000;
#endif
? fs->surf = SDL_CreateSurface( 0, surfWidth, surfHeight, surfDepth,
rmask, bmask, gmask, amask );
? if( !fs->surf ) {
? ? free( fs );
? ? return NULL;
? }
? SDL_SetSurfaceAlphaMod( fs->surf, maxAlpha );
? fs->alpha = maxAlpha;
? fs->minAlpha = minAlpha;
? fs->maxAlpha = maxAlpha;
? fs->alphaStep = 0;
}

// Call this when you want an option to fade in (call only once to
start the fade!)
void fadeSurfaceIn( FadingSurface *fs, Uint32 Milliseconds ) {
? if( fs == NULL ) return;
? if( fs->surf == NULL ) return;
? fs->internalMillisecods = SDL_GetTicks( ) + Milliseconds;
? fs->alpha = fs->minAlpha; // Comment this out if you want to be able
to cancel out previous animations
? fs->alphaStep = (fs->maxAlpha - fs->minAlpha) / Milliseconds;
? fs->alphaTarget = fs->maxAlpha;
? fs->lastUpdate = SDL_GetTicks() - 1;
}

// Call this when you want an option to fade out (call only once to
start the fade!)
void fadeSurfaceOut( FadingSurface *fs, Uint32 Milliseconds ) {
? if( fs == NULL ) return;
? if( fs->surf == NULL ) return;
? fs->internalMillisecods = SDL_GetTicks( ) + Milliseconds;
? fs->alpha = fs->maxAlpha; // Comment this out if you want to be able
to cancel out previous animations
? fs->alphaStep = (fs->minAlpha - fs->maxAlpha) / Milliseconds;
? fs->alphaTarget = fs->minAlpha;
? fs->lastUpdate = SDL_GetTicks() - 1;
}

// Call this every step in your menu loop
void fadeSurface( FadingSurface *fs ) {
? if( fs == NULL ) return;
? if( fs->surf == NULL ) return;
? if( fs->alphaStep == 0 ) return;
? if( SDL_GetTicks()> ?fs->lastUpdate ) {
? ? SDL_SetSurfaceAlphaMod( fs->surf, fs->alpha );
? ? fs->alpha += fs->alphaStep;
? ? fs->lastUpdate = SDL_GetTicks()
? }
? if( ( SDL_GetTicks()> ?fs->internalMilliseconds ) || ( fs->alpha>=
fs->alphaTarget ) ) {
? ? fs->alphaStep = 0
? ? fs->alpha = fs->alphaTarget;
? ? SDL_SetSurfaceAlphaMod( fs->surf, fs->alpha );
? }
}

This would also be easy to implement some sort of callback system to
fade in and out repetatively.
I hope that helps - make sure you load some sort of an image to
fs->surface!!! ?I just added the surface create, but you’ll have to
make sure it has some sort of valid RGBA image.
Also, I didn’t test that code, so you might have to do some minor
debugging.
Take care,
-Alex

Thanks for your interest!

I voluntary slimmed down a lot the code to test and I changed a lot of
things but the idea is the same.

My code located here : http://markand.malikania.fr/fade.c

Works well with a SDL_Surface filled with some colors but not if I load an
png image on it.

In the current state of the code the image called as const char smpath[]
which is a transparent background png does not fade. it stays at the same
alpha.

If you comment out the lines from 121 to 122 it will works.

Is there any restriction to the SDL_Surface format due to the png IMG_Load?

Cheers :slight_smile:

On Tue, Apr 12, 2011 at 4:46 AM, David Demelier <demelier.david at gmail.com> ?wrote:

Hello,

This is a hard question to ask. Imagine a menu designed with some
SDL_Surfaces :

  • Start game
  • Join game
  • Options
  • Quit

All these SDL_Surface are correctly blitted and shown on the screen. Now
you want to make an animation on mouse-hover these surfaces. For example the
mouse pointer goes on “Start game”, the program will glow this SDL_Surface.

That is easy to do, but the hard part is to “unglow” it with an
animation, you leave the menu item with the pointer thus a fade-out
animation appears, the SDL_Surface associated with “Start game” goes to the
original texture every 50ms.

That’s the question, how you can easily update all SDL_Surface each one
independently?

How would you implement this?

Cheers,


David Demelier


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


David Demelier

It’s probably because your PNG images don’t have an alpha channel when
you save them - either change them, or convert them to an RGBA format
in SDL.
If you’re still having problems, let me know,
-Alex

How to check if the png image has a alpha channel ? Gimp says RGB-alpha.
And how to convert it to a RGBA ?

I tried the following :

SDL_Surface *img, *tmp;
tmp = IMG_Load(“file.png”);
img = SDL_DisplayFormatAlpha(tmp);

But with no effects.

Cheers,On 19/04/2011 19:09, Alex Barry wrote:

On Tue, Apr 19, 2011 at 11:16 AM, David Demelier <@David_Demelier> wrote:

On 12/04/2011 14:47, Alex Barry wrote:

The easiest way that I can think of is coming up with a struct and
tossing some sort of delay system on it, as well as an alpha channel:

struct FadingSurface {
SDL_Surface *surf;
Uint8 alpha;
Uint8 minAlpha, maxAlpha;
Uint8 alphaStep, alphaTarget;
Uint32 internalMilliseconds;
Uint32 lastUpdate;
}

FadingSurface *fadeSurfaceCreate( int surfWidth, int surfHeight, int
surfDepth, Uint8 minAlpha, Uint8 maxAlpha ) {
FadingSurface *fs = (FadingSurface *)malloc( sizeof( FadingSurface ) );

Uint32 rmask, gmask, bmask, amask;

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
fs->surf = SDL_CreateSurface( 0, surfWidth, surfHeight, surfDepth,
rmask, bmask, gmask, amask );
if( !fs->surf ) {
free( fs );
return NULL;
}
SDL_SetSurfaceAlphaMod( fs->surf, maxAlpha );
fs->alpha = maxAlpha;
fs->minAlpha = minAlpha;
fs->maxAlpha = maxAlpha;
fs->alphaStep = 0;
}

// Call this when you want an option to fade in (call only once to
start the fade!)
void fadeSurfaceIn( FadingSurface *fs, Uint32 Milliseconds ) {
if( fs == NULL ) return;
if( fs->surf == NULL ) return;
fs->internalMillisecods = SDL_GetTicks( ) + Milliseconds;
fs->alpha = fs->minAlpha; // Comment this out if you want to be able
to cancel out previous animations
fs->alphaStep = (fs->maxAlpha - fs->minAlpha) / Milliseconds;
fs->alphaTarget = fs->maxAlpha;
fs->lastUpdate = SDL_GetTicks() - 1;
}

// Call this when you want an option to fade out (call only once to
start the fade!)
void fadeSurfaceOut( FadingSurface *fs, Uint32 Milliseconds ) {
if( fs == NULL ) return;
if( fs->surf == NULL ) return;
fs->internalMillisecods = SDL_GetTicks( ) + Milliseconds;
fs->alpha = fs->maxAlpha; // Comment this out if you want to be able
to cancel out previous animations
fs->alphaStep = (fs->minAlpha - fs->maxAlpha) / Milliseconds;
fs->alphaTarget = fs->minAlpha;
fs->lastUpdate = SDL_GetTicks() - 1;
}

// Call this every step in your menu loop
void fadeSurface( FadingSurface *fs ) {
if( fs == NULL ) return;
if( fs->surf == NULL ) return;
if( fs->alphaStep == 0 ) return;
if( SDL_GetTicks()> fs->lastUpdate ) {
SDL_SetSurfaceAlphaMod( fs->surf, fs->alpha );
fs->alpha += fs->alphaStep;
fs->lastUpdate = SDL_GetTicks()
}
if( ( SDL_GetTicks()> fs->internalMilliseconds ) || ( fs->alpha>=
fs->alphaTarget ) ) {
fs->alphaStep = 0
fs->alpha = fs->alphaTarget;
SDL_SetSurfaceAlphaMod( fs->surf, fs->alpha );
}
}

This would also be easy to implement some sort of callback system to
fade in and out repetatively.
I hope that helps - make sure you load some sort of an image to
fs->surface!!! I just added the surface create, but you’ll have to
make sure it has some sort of valid RGBA image.
Also, I didn’t test that code, so you might have to do some minor
debugging.
Take care,
-Alex

Thanks for your interest!

I voluntary slimmed down a lot the code to test and I changed a lot of
things but the idea is the same.

My code located here : http://markand.malikania.fr/fade.c

Works well with a SDL_Surface filled with some colors but not if I load an
png image on it.

In the current state of the code the image called as const char smpath[]
which is a transparent background png does not fade. it stays at the same
alpha.

If you comment out the lines from 121 to 122 it will works.

Is there any restriction to the SDL_Surface format due to the png IMG_Load?

Cheers :slight_smile:

On Tue, Apr 12, 2011 at 4:46 AM, David Demelier <@David_Demelier> wrote:

Hello,

This is a hard question to ask. Imagine a menu designed with some
SDL_Surfaces :

  • Start game
  • Join game
  • Options
  • Quit

All these SDL_Surface are correctly blitted and shown on the screen. Now
you want to make an animation on mouse-hover these surfaces. For example the
mouse pointer goes on “Start game”, the program will glow this SDL_Surface.

That is easy to do, but the hard part is to “unglow” it with an
animation, you leave the menu item with the pointer thus a fade-out
animation appears, the SDL_Surface associated with “Start game” goes to the
original texture every 50ms.

That’s the question, how you can easily update all SDL_Surface each one
independently?

How would you implement this?

Cheers,


David Demelier


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


David Demelier


David Demelier

you’ll probably want to use a function like
http://wiki.libsdl.org/moin.cgi/SDL_ConvertPixels or the SDL1.2
equivalent ( http://www.libsdl.org/cgi/docwiki.cgi/SDL_DisplayFormatAlpha
).
-AlexOn Tue, Apr 19, 2011 at 3:20 PM, David Demelier <demelier.david at gmail.com> wrote:

On 19/04/2011 19:09, Alex Barry wrote:

It’s probably because your PNG images don’t have an alpha channel when
you save them - either change them, or convert them to an RGBA format
in SDL.
If you’re still having problems, let me know,
-Alex

How to check if the png image has a alpha channel ? Gimp says RGB-alpha. And
how to convert it to a RGBA ?

I tried the following :

SDL_Surface *img, *tmp;
tmp = IMG_Load(“file.png”);
img = SDL_DisplayFormatAlpha(tmp);

But with no effects.

Cheers,

On Tue, Apr 19, 2011 at 11:16 AM, David Demelier <demelier.david at gmail.com> ?wrote:

On 12/04/2011 14:47, Alex Barry wrote:

The easiest way that I can think of is coming up with a struct and
tossing some sort of delay system on it, as well as an alpha channel:

struct FadingSurface {
? SDL_Surface *surf;
? Uint8 alpha;
? Uint8 minAlpha, maxAlpha;
? Uint8 alphaStep, alphaTarget;
? Uint32 internalMilliseconds;
? Uint32 lastUpdate;
}

FadingSurface *fadeSurfaceCreate( int surfWidth, int surfHeight, int
surfDepth, Uint8 minAlpha, Uint8 maxAlpha ) {
? FadingSurface *fs = (FadingSurface *)malloc( sizeof( FadingSurface )
);

? Uint32 rmask, gmask, bmask, amask;

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
? rmask = 0xff000000;
? gmask = 0x00ff0000;
? bmask = 0x0000ff00;
? amask = 0x000000ff;
#else
? rmask = 0x000000ff;
? gmask = 0x0000ff00;
? bmask = 0x00ff0000;
? amask = 0xff000000;
#endif
? fs->surf = SDL_CreateSurface( 0, surfWidth, surfHeight, surfDepth,
rmask, bmask, gmask, amask );
? if( !fs->surf ) {
? ? free( fs );
? ? return NULL;
? }
? SDL_SetSurfaceAlphaMod( fs->surf, maxAlpha );
? fs->alpha = maxAlpha;
? fs->minAlpha = minAlpha;
? fs->maxAlpha = maxAlpha;
? fs->alphaStep = 0;
}

// Call this when you want an option to fade in (call only once to
start the fade!)
void fadeSurfaceIn( FadingSurface *fs, Uint32 Milliseconds ) {
? if( fs == NULL ) return;
? if( fs->surf == NULL ) return;
? fs->internalMillisecods = SDL_GetTicks( ) + Milliseconds;
? fs->alpha = fs->minAlpha; // Comment this out if you want to be able
to cancel out previous animations
? fs->alphaStep = (fs->maxAlpha - fs->minAlpha) / Milliseconds;
? fs->alphaTarget = fs->maxAlpha;
? fs->lastUpdate = SDL_GetTicks() - 1;
}

// Call this when you want an option to fade out (call only once to
start the fade!)
void fadeSurfaceOut( FadingSurface *fs, Uint32 Milliseconds ) {
? if( fs == NULL ) return;
? if( fs->surf == NULL ) return;
? fs->internalMillisecods = SDL_GetTicks( ) + Milliseconds;
? fs->alpha = fs->maxAlpha; // Comment this out if you want to be able
to cancel out previous animations
? fs->alphaStep = (fs->minAlpha - fs->maxAlpha) / Milliseconds;
? fs->alphaTarget = fs->minAlpha;
? fs->lastUpdate = SDL_GetTicks() - 1;
}

// Call this every step in your menu loop
void fadeSurface( FadingSurface *fs ) {
? if( fs == NULL ) return;
? if( fs->surf == NULL ) return;
? if( fs->alphaStep == 0 ) return;
? if( SDL_GetTicks()> ? ?fs->lastUpdate ) {
? ? SDL_SetSurfaceAlphaMod( fs->surf, fs->alpha );
? ? fs->alpha += fs->alphaStep;
? ? fs->lastUpdate = SDL_GetTicks()
? }
? if( ( SDL_GetTicks()> ? ?fs->internalMilliseconds ) || ( fs->alpha>=
fs->alphaTarget ) ) {
? ? fs->alphaStep = 0
? ? fs->alpha = fs->alphaTarget;
? ? SDL_SetSurfaceAlphaMod( fs->surf, fs->alpha );
? }
}

This would also be easy to implement some sort of callback system to
fade in and out repetatively.
I hope that helps - make sure you load some sort of an image to
fs->surface!!! ?I just added the surface create, but you’ll have to
make sure it has some sort of valid RGBA image.
Also, I didn’t test that code, so you might have to do some minor
debugging.
Take care,
-Alex

Thanks for your interest!

I voluntary slimmed down a lot the code to test and I changed a lot of
things but the idea is the same.

My code located here : http://markand.malikania.fr/fade.c

Works well with a SDL_Surface filled with some colors but not if I load
an
png image on it.

In the current state of the code the image called as const char smpath[]
which is a transparent background png does not fade. it stays at the same
alpha.

If you comment out the lines from 121 to 122 it will works.

Is there any restriction to the SDL_Surface format due to the png
IMG_Load?

Cheers :slight_smile:

On Tue, Apr 12, 2011 at 4:46 AM, David Demelier <demelier.david at gmail.com> ? ?wrote:

Hello,

This is a hard question to ask. Imagine a menu designed with some
SDL_Surfaces :

  • Start game
  • Join game
  • Options
  • Quit

All these SDL_Surface are correctly blitted and shown on the screen.
Now
you want to make an animation on mouse-hover these surfaces. For
example the
mouse pointer goes on “Start game”, the program will glow this
SDL_Surface.

That is easy to do, but the hard part is to “unglow” it with an
animation, you leave the menu item with the pointer thus a fade-out
animation appears, the SDL_Surface associated with “Start game” goes to
the
original texture every 50ms.

That’s the question, how you can easily update all SDL_Surface each one
independently?

How would you implement this?

Cheers,


David Demelier


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


David Demelier


David Demelier