Sign in to follow this  

Fade In and out in SDL

This topic is 4519 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I´m programming a game in VS .NET 2003 using SDL, and I wanted to make an intro for the game like this: "Presented by..." this would appear in the middle of a black screen, it would slowly fade in and then fade out then another line would come and would do the same. Got the idea? How can I do it? Any ideas? Thanks very much, Victor Freire

Share this post


Link to post
Share on other sites
The way you would accomplish this is by specifying an alpha value. Use a texure with that text written on it, apply it to a sprite/quad, then slowly increase the alpha of the sprite/quad, then decrease it again. I am sure there are a few tutorials on this somewhere...

Share this post


Link to post
Share on other sites
Phew, I can say that was by no means easy. Just for reference, I couldn't find anything on this topic anymore. There used to be some great fade in/out code I used before, but didn't see it on Google right away. It took me an hour and a half to figure out how to get it all working. There's just so much too do in terms of tweaking the settings and values of this and that with functions. Anyways, here's a complete sample of what you can do.

#include "SDL.h"
#pragma comment( lib, "SDLmain.lib" )
#pragma comment( lib, "SDL.lib" )

void DrawIMG(SDL_Surface *img, SDL_Surface* des, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, des, &dest);
}

int main( int argc, char* argv[] )
{
SDL_Surface* screen;

if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
printf( "Unable to initialzie SDL: %s\n", SDL_GetError() );
return -1;
}

// We try to set the video mode, if it fails, then we do not want to go on
if ( (screen=SDL_SetVideoMode( 640, 480, 0, SDL_DOUBLEBUF )) == NULL )
{
printf( "Unable to set the video mode: %s\n", SDL_GetError() );
return -1;
}


bool toggled = 0;
SDL_Event Event;
Uint8 *Keys = 0;
bool Done = false;

//Load the bitmap from a file
SDL_Surface* bmpFile = SDL_LoadBMP("test.bmp");

// Convert it to the format of the screen
SDL_Surface* bmpFile2 = SDL_DisplayFormat( bmpFile );

// Free the original bitmap
SDL_FreeSurface( bmpFile );

// Create a blank surface that is the same size as our screen
SDL_Surface* tempScreen = SDL_CreateRGBSurface( SDL_SWSURFACE | SDL_SRCALPHA, 640, 480, 32, 0xff000000,0x00ff0000,0x0000ff00,0x000000ff);

// Convert it to the format of the screen
SDL_Surface* tempScreen2 = SDL_DisplayFormat( tempScreen );

// Free the created surface
SDL_FreeSurface( tempScreen );

// Timer variable
int start = SDL_GetTicks();

// Start the screen as Opaque(255)
int AlphaValue = 255;

while( Done == false )
{
while ( SDL_PollEvent(&Event) )
{
if ( Event.type == SDL_QUIT )
Done = 1;
}

Keys = SDL_GetKeyState( 0 );

if( Keys[ SDLK_ESCAPE ] )
Done = 1;

if( Keys[ SDLK_UP ] )
{
if(toggled)
AlphaValue++;
}
if( Keys[ SDLK_DOWN ] )
{
if(toggled)
AlphaValue--;
}

// Toggle between fading out and manual control
if( Keys[ SDLK_SPACE ] )
{
// Toggle the value
toggled = !toggled;

// Clear the key
Keys[ SDLK_SPACE ] = 0;
}

// Only if we are in this mode
if(!toggled)
{
// Only call this when 100 ms have elapsed
if( SDL_GetTicks() - start > 100 )
{
// Reassign the time elapsed
start = SDL_GetTicks();

// Decrease the alpha value by this much
AlphaValue -= 5;
}
}

// Set the bounds of this variable between transparent(0) and opaque(255)
if( AlphaValue < 0 )
{
// Reset the value to Opaque
AlphaValue = 255;
}
if( AlphaValue > 255)
{
// Reset the value to Transparent
AlphaValue = 0;
}

// Clear both the screens
SDL_FillRect( tempScreen2, 0, SDL_MapRGBA(tempScreen2->format, 0, 0, 0, 0) );
SDL_FillRect( screen, 0, SDL_MapRGBA(tempScreen2->format, 0, 0, 0, 0) );

// Draw the bitmap to the constructed vitual screen
DrawIMG( bmpFile2, tempScreen2, 25, 25 );

// Set the alpha of the constructed screen
SDL_SetAlpha( tempScreen2, SDL_SRCALPHA, AlphaValue);

// Draw the constructed surface to the primary surface now
DrawIMG( tempScreen2, screen, 0, 0 );

SDL_Flip( screen );
}

//Free surfaces after using it
SDL_FreeSurface( bmpFile2 );
SDL_FreeSurface( tempScreen2 );

// Quit SDL
SDL_Quit();

return 0;
}




Now that's far from optimal but it does show what you want to do. I'm sure ther are probabally other ways, but that's what I have right now/ The basic principle is as follows:
1 - Create a surface the same size as the screen. Note that it needs to be in the same format as well.
2 - Blit all your images to this "virtual screen".
3 - Set the alpha of this virtual screen to match what you want to do.
4 - Finally, blit the virtual screen to the main screen to see it in action.

For the demo it will automatically fade out. If you hit space bar, you can change the mode so you can control the alpha with the Up/Down arrow keys. I'll probabally make a few functions that will handle fade in/outs in SDL and put it on my web page, but just not right now. Feel free to expieriment with this to get it how you want.

If anyone else messes with the code and can figure out more efficient methods of doing this or making it faster, easier, please don't hesitate to share it!

Share this post


Link to post
Share on other sites
Sign in to follow this