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