Fade in SDL

Started by
8 comments, last by JAlonso 21 years, 8 months ago
How can I make a fade to black effect in SDL? thanks in advance.
Advertisement
see and software fade routine tutorial for directdraw (or non directdraw). search the forums. plenty of actual code right here, heck fade is just a speclized alpha blend. gamedev has tutorials for mmx fade and alpha blend under resources.
The following is theoretical - I haven''t tested it nor know that it works:

Copy your screen surface into some other surface.

Now start a little loop that clears the original screen surface to black and then bilts the copied surface onto the original surface, but with a slightly lower alpha value than on the previous loop.


---
cone3d
http://cone3d.gamedev.net
My software never has any bugs - it just generates random featureshello?
---cone3dhttp://cone3d.gamedev.netMy software never has any bugs - it just generates random features
I tried this, but it does not work :

	SDL_Surface *tmp;	tmp = SDL_CreateRGBSurface (SDL_HWSURFACE,m_PrimarySurface->w, m_PrimarySurface->h,m_PrimarySurface->format->BitsPerPixel, m_PrimarySurface->format->Rmask, m_PrimarySurface->format->Gmask, m_PrimarySurface->format->Bmask, m_PrimarySurface->format->Amask);	SDL_BlitSurface(m_PrimarySurface, NULL, tmp, NULL);	for (int i = 0; i < 255; i++) {		SDL_SetAlpha(tmp, SDL_SRCALPHA|SDL_RLEACCEL, i);		SDL_BlitSurface(tmp, NULL, m_PrimarySurface, NULL);		Flip();	} 
go to the sdl site www.libsdl.org,
go to the games section,
find the site for Maelstrom(it''s an asteroid clone, and a good one at that),
dl the source, it fades in several instances, haven''t looked at the code myself

THANKS
Look up SDL_GammaRamp or something like that (been a while since I used SDL)
JAlonso - I just went through the same thing. Here''s a bit of code that I wrote using per surface alpha that produces a "fade-out" effect. Quite useful:



void fade_out(SDL_Surface *target)
{
/*
target is our target surface, which would normally
be the screen.
*/

SDL_Surface *temp;
temp = SDL_DisplayFormat(target);
if (temp == NULL)
{
printf("SDL_DisplayFormatAlpha() failed.\n");
exit(1);
}
/*This starts it off with 50% opacity*/
SDL_SetAlpha(temp,SDL_SRCALPHA,128);
SDL_BlitSurface(target,NULL,temp,NULL);

for (int i=128; i >= 110; i--)
{
SDL_SetAlpha(temp,SDL_SRCALPHA,i);
SDL_BlitSurface(target,NULL,temp,NULL);
SDL_FillRect(target,NULL,0x000000);
SDL_BlitSurface(temp,NULL,target,NULL);
SDL_Flip(target);
SDL_Delay(20);
}
SDL_FreeSurface(temp);
}

Sorry. Here's the source again:

  void fade_out(SDL_Surface *target){     /*        target is our target surface, which would normally        be the screen.     */          SDL_Surface *temp;     temp = SDL_DisplayFormat(target);     if (temp == NULL)     {        printf("SDL_DisplayFormat() failed.\n");        exit(1);     }     /*This starts it out with 50% opacity.*/     SDL_SetAlpha(temp,SDL_SRCALPHA,128);          SDL_BlitSurface(target,NULL,temp,NULL);          for (int i=128; i >= 110; i--)     {          SDL_SetAlpha(temp,SDL_SRCALPHA,i);          SDL_BlitSurface(target,NULL,temp,NULL);                         SDL_FillRect(target,NULL,0x000000);               SDL_BlitSurface(temp,NULL,target,NULL);          SDL_Flip(target);          SDL_Delay(20);     }     SDL_FreeSurface(temp);}  


[edited by - dklon on July 24, 2002 1:28:59 AM]

[edited by - dklon on July 24, 2002 1:29:34 AM]
Thanks dklon

This topic is closed to new replies.

Advertisement