The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL video and fading the screen
Alex Barry
Guest

I'm designing an introduction to a game that I'm making, and I wanted the
screen to fade to black. I think I need a YUV Overlay, but I don't know,
nor do I know how to use it.

here is the applicable code I have thus far:
void engine_powered( SDL_Surface *scrn, Uint32 delayed )
{
SDL_Event event;
Uint32 color;

Uint32 delaytimer;

SDL_Rect coord;

color = SDL_MapRGB( scrn->format, 255, 255, 255 );

SDL_Surface *logo1 = SDL_LoadBMP( "sdl_powered.bmp" );
SDL_Surface *logo_and = SDL_LoadBMP( "and.bmp" );
SDL_Surface *logo2 = SDL_LoadBMP( "opengl_logo.bmp" );

int ready;

delaytimer = SDL_GetTicks();
ready = 0;
while( (ready == 0) || ( SDL_GetTicks() - delaytimer < delayed ) )
{

SDL_FillRect( scrn, NULL, color );

coord.x = 200 - (logo2->w / 2);
coord.y = 300 - (logo2->h / 2);

SDL_BlitSurface( logo2, NULL, scrn, &coord );

coord.x = 400 - (logo_and->w / 2);
coord.y = 300 - (logo_and->h / 2);

SDL_BlitSurface( logo_and, NULL, scrn, &coord );

coord.x = 600 - (logo1->w / 2);
coord.y = 300 - (logo1->h / 2);

SDL_BlitSurface( logo1, NULL, scrn, &coord );

while( SDL_PollEvent( &event ) ) { ready = 1; }

SDL_Flip( scrn );
SDL_Delay( 1 );

}

SDL_FreeSurface( logo2 );
SDL_FreeSurface( logo_and );
SDL_FreeSurface( logo1 );
}

inputs the current work page, and how long they want the delay; outputs the
logos. I want to use SDL, opposed to OpenGL for this section.

thanks,
Alex~


--
Smith: "...Why, Mr Anderson, Why - Do - You - Persist?"
Neo: "Because I choose to."
-Matrix Revolutions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20060104/89791301/attachment.html
SDL video and fading the screen
Ryan C. Gordon
Guest

Quote:
I'm designing an introduction to a game that I'm making, and I wanted
the screen to fade to black. I think I need a YUV Overlay, but I don't
know, nor do I know how to use it.

A YUV overlay isn't what you need.

You'll need to enable alpha blending on the surfaces before blitting
them. Like this code...

SDL_SetAlpha(SDL_SRCALPHA, 127);
SDL_BlitSurface( logo2, NULL, scrn, &coord );

...will blit logo2 half-blended with the contents of the screen (which
will be black, in this case, since you did a FillRect()...). An alpha of
0 is completely faded out, and 255 is completely visible.

So just change the alpha every frame (or, rather, you'll want to take
SDL_GetTicks() into account so you fade it smoothly over X milliseconds).

Alpha blending is a performance hit, so be careful how and when you
enable it.

--ryan.
SDL video and fading the screen
Ricardo Cruz
Guest

Yep, I think that's the best solution. I saw a guy using SDL_SetGamma(), but
that's really ugly, because it changes the gamma of the whole desktop, at
least in X11.

By the way, feel free to use this code of mine:
?
#define LOOP_DELAY 20.0

void Screen::fadeout(int fade_time)
{
float alpha_inc = 256 / (fade_time / LOOP_DELAY);
float alpha = 255;

SDL_Surface *screen_copy, *black_surface;

screen_copy = SDL_CreateRGBSurface (surface->flags|SDL_SRCALPHA,
surface->w, surface->h, surface->format->BitsPerPixel,
surface->format->Rmask, surface->format->Gmask, surface->format->Bmask,
surface->format->Amask);
if(screen_copy == NULL)
{
std::cerr << "Error: could not create screen copy for fading.\n"
"SDL error: " << SDL_GetError() << std::endl;
return;
}
SDL_BlitSurface(surface, NULL, screen_copy, NULL) ;

black_surface = SDL_CreateRGBSurface (surface->flags|SDL_SRCALPHA,
surface->w, surface->h, surface->format->BitsPerPixel,
surface->format->Rmask, surface->format->Gmask, surface->format->Bmask,
surface->format->Amask);
if(black_surface == NULL)
{
std::cerr << "Error: could not create black surface for fading.\n"
"SDL error: " << SDL_GetError() << std::endl;
SDL_FreeSurface(screen_copy);
return;
}
SDL_FillRect(black_surface, NULL, 0);

while(alpha > 0)
{
SDL_SetAlpha(black_surface, SDL_SRCALPHA, (Uint8)(255-alpha));

SDL_BlitSurface(screen_copy, NULL, surface, NULL);
SDL_BlitSurface(black_surface, NULL, surface, NULL);

SDL_Flip(surface);
SDL_Delay(int(LOOP_DELAY));

alpha -= alpha_inc;
}

clear_screen(Color::black);
update();
}

?
Cheers,
Ricardo Cruz

Em Quarta, 4 de Janeiro de 2006 22:43, o Ryan C. Gordon escreveu:
Quote:
Quote:
I'm designing an introduction to a game that I'm making, and I wanted
the screen to fade to black. I think I need a YUV Overlay, but I don't
know, nor do I know how to use it.

A YUV overlay isn't what you need.

You'll need to enable alpha blending on the surfaces before blitting
them. Like this code...

SDL_SetAlpha(SDL_SRCALPHA, 127);
SDL_BlitSurface( logo2, NULL, scrn, &coord );

...will blit logo2 half-blended with the contents of the screen (which
will be black, in this case, since you did a FillRect()...). An alpha of
0 is completely faded out, and 255 is completely visible.

So just change the alpha every frame (or, rather, you'll want to take
SDL_GetTicks() into account so you fade it smoothly over X milliseconds).

Alpha blending is a performance hit, so be careful how and when you
enable it.

--ryan.


--
We don't understand the software, and sometimes we don't understand the
hardware, but we can *___see* the blinking lights!
SDL video and fading the screen
Alex Barry
Guest

this is teh code I have currently, but it doesn't work...

class ScreenData
{
public:
// I didn't put these functions in this code...no point
void set( int xres, int yres, int bbp, Uint32 flags ); /* Set video
(will clear previous page if set) */
void setgl( int xmin, int ymin, int xmax, int ymax ); /* set opengl
window */

void fade( Uint32 timelimit, int steps, int sfadealpha, int ffadealpha
);

SDL_Surface *page; /* Screen
Surface */
};

void ScreenData::fade( Uint32 stepdelay, int steps, int sfadealpha, int
ffadealpha )
/*
Fades the screen in [steps] amount of steps within the given [timelimit]
ms
If timelimit or steps is 0, displays blit (directly from fadetor,
fadetog, fadetob)

*/
{
Uint32 starttime = SDL_GetTicks (); // base time on
current ticks

if( steps > 0 ) {

Uint8 alphadist = (Uint8)ffadealpha - (Uint8)sfadealpha;

Uint8 alphastep = (Uint8)steps / alphadist;

Uint8 calpha = (Uint8)sfadealpha;

int cstep;

int res;

cstep = steps;
while( cstep > 0 )
// Loop while we are under our time limit ( + 1 is used so we hit
the time right on the dot )
{
res = SDL_SetAlpha( page, SDL_SRCALPHA, sfadealpha ); // reset
if( res == -1 ) system( "PAUSE : ECHO cannot set alpha!" );

res = SDL_SetAlpha( page, SDL_SRCALPHA, calpha );
if( res == -1 ) system( "PAUSE : ECHO cannot set alpha!" );
SDL_Flip( page );
SDL_Delay( stepdelay );

calpha += alphastep;

cstep -= 1;
}
} else {
// alphblend, then flip...bakes in under a second (unless you have a
wicked huge screen res)
SDL_SetAlpha( page, SDL_SRCALPHA, (Uint8)ffadealpha );
}
}

any suggestions? I'm not quite sure what's wrong with this code...in
theory, it would work, but evidently not in practice

Alex~

On 1/5/06, Ricardo Cruz <rpmcruz at clix.pt> wrote:
Quote:


Yep, I think that's the best solution. I saw a guy using SDL_SetGamma(),
but
that's really ugly, because it changes the gamma of the whole desktop, at
least in X11.

By the way, feel free to use this code of mine:
?
#define LOOP_DELAY 20.0

void Screen::fadeout(int fade_time)
{
float alpha_inc = 256 / (fade_time / LOOP_DELAY);
float alpha = 255;

SDL_Surface *screen_copy, *black_surface;

screen_copy = SDL_CreateRGBSurface (surface->flags|SDL_SRCALPHA,
surface->w, surface->h, surface->format->BitsPerPixel,
surface->format->Rmask, surface->format->Gmask,
surface->format->Bmask,
surface->format->Amask);
if(screen_copy == NULL)
{
std::cerr << "Error: could not create screen copy for fading.\n"
"SDL error: " << SDL_GetError() << std::endl;
return;
}
SDL_BlitSurface(surface, NULL, screen_copy, NULL) ;

black_surface = SDL_CreateRGBSurface (surface->flags|SDL_SRCALPHA,
surface->w, surface->h, surface->format->BitsPerPixel,
surface->format->Rmask, surface->format->Gmask,
surface->format->Bmask,
surface->format->Amask);
if(black_surface == NULL)
{
std::cerr << "Error: could not create black surface for fading.\n"
"SDL error: " << SDL_GetError() << std::endl;
SDL_FreeSurface(screen_copy);
return;
}
SDL_FillRect(black_surface, NULL, 0);

while(alpha > 0)
{
SDL_SetAlpha(black_surface, SDL_SRCALPHA, (Uint8)(255-alpha));

SDL_BlitSurface(screen_copy, NULL, surface, NULL);
SDL_BlitSurface(black_surface, NULL, surface, NULL);

SDL_Flip(surface);
SDL_Delay(int(LOOP_DELAY));

alpha -= alpha_inc;
}

clear_screen(Color::black);
update();
}

?
Cheers,
Ricardo Cruz

Em Quarta, 4 de Janeiro de 2006 22:43, o Ryan C. Gordon escreveu:
Quote:
Quote:
I'm designing an introduction to a game that I'm making, and I wanted
the screen to fade to black. I think I need a YUV Overlay, but I
don't
Quote:
Quote:
know, nor do I know how to use it.

A YUV overlay isn't what you need.

You'll need to enable alpha blending on the surfaces before blitting
them. Like this code...

SDL_SetAlpha(SDL_SRCALPHA, 127);
SDL_BlitSurface( logo2, NULL, scrn, &coord );

...will blit logo2 half-blended with the contents of the screen (which
will be black, in this case, since you did a FillRect()...). An alpha of
0 is completely faded out, and 255 is completely visible.

So just change the alpha every frame (or, rather, you'll want to take
SDL_GetTicks() into account so you fade it smoothly over X
milliseconds).
Quote:

Alpha blending is a performance hit, so be careful how and when you
enable it.

--ryan.


--
We don't understand the software, and sometimes we don't understand the
hardware, but we can *___see* the blinking lights!

_______________________________________________
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl




--
Smith: "...Why, Mr Anderson, Why - Do - You - Persist?"
Neo: "Because I choose to."
-Matrix Revolutions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20060105/ed191d0c/attachment.htm
SDL video and fading the screen
Ryan C. Gordon
Guest

Quote:
any suggestions? I'm not quite sure what's wrong with this code...in
theory, it would work, but evidently not in practice

There's no blit in your loop.

Again, you have to:

1) Clear the screen surface to black (or whatever) with SDL_FillRect().
2) Set the alpha for the surface you want to blit to the screen.
2) Blit it to the screen surface.
3) SDL_Flip() the screen surface so it becomes visible to the user.
4) If not time to stop fading, go to #1.

Don't set the alpha on the screen surface, set it on the surfaces you're
going to blit to the screen.

--ryan.
SDL video and fading the screen
Alex Barry
Guest

That code is supposed to be used after everything is blitted to the surface
(page), and then it's alpha blended, then flipped (to the monitor)

On 1/5/06, Ryan C. Gordon <icculus at icculus.org> wrote:
Quote:


Quote:
any suggestions? I'm not quite sure what's wrong with this code...in
theory, it would work, but evidently not in practice

There's no blit in your loop.

Again, you have to:

1) Clear the screen surface to black (or whatever) with SDL_FillRect().
2) Set the alpha for the surface you want to blit to the screen.
2) Blit it to the screen surface.
3) SDL_Flip() the screen surface so it becomes visible to the user.
4) If not time to stop fading, go to #1.

Don't set the alpha on the screen surface, set it on the surfaces you're
going to blit to the screen.

--ryan.


_______________________________________________
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl




--
Smith: "...Why, Mr Anderson, Why - Do - You - Persist?"
Neo: "Because I choose to."
-Matrix Revolutions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20060105/836ad61c/attachment.html