Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411595 Posts in 69386 Topics- by 58444 Members - Latest Member: FightingFoxGame

May 07, 2024, 01:51:12 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Screen Fades and Program Flows
Pages: [1]
Print
Author Topic: Screen Fades and Program Flows  (Read 1511 times)
increpare
Guest
« on: February 01, 2009, 10:49:33 PM »

How do you fellows do screen transitions in your programs?

My solution, which I don't particularly like, because it upsets the heirarchy of my code, is to have a fade() function which has direct access to the timer, and to call it where needed.

Oh, even worse actually sometimes, it seems my fade function in Mirror Stage makes its own timer!

Code:
void fadetoblack(bool fade)
{
  Timer fps;

  glClearColor(0, 0, 0, 1);

  for (float alpha = 0; alpha <= 1; alpha += 0.03)
    {
      if (fade)
        setvolume(1 - alpha);

      fps.start();


      glDisable(GL_BLEND);
      glReadBuffer(GL_FRONT);
      glDrawBuffer(GL_BACK);
      glCopyPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_COLOR);
      glEnable(GL_BLEND);

      while (SDL_PollEvent(&event))
        {
          //Gobble up all events while fading
        }

      glLoadIdentity();

      glColor4f(0, 0, 0, alpha);
      glBegin(GL_QUADS);
      glVertex2f(-3, -3);
      glVertex2f(-3, 3);
      glVertex2f(3, 3);
      glVertex2f(3, -3);
      glEnd();

      if (fps.get_ticks() < 1000 / FRAMES_PER_SECOND)
        {
          SDL_Delay((1000 / FRAMES_PER_SECOND) - fps.get_ticks());
        }


      SDL_GL_SwapBuffers();
    }
}

This is, I'm sure people will agree, quite a violent way of doing things. 

The other easy alternative would be to have a int somewhere which, if greater than zero, would effect a transition.  I'm not a fan of cluttering stuff up with conditional statements (though I do it more than I am comfortable with).
Logged
David Pittman
Level 2
**


MAEK GAEM


View Profile WWW
« Reply #1 on: February 01, 2009, 11:07:23 PM »

I push a fade screen onto my UI stack and initialize it with target colors and a duration. When it ticks, it updates its color value based on the delta time.

Code:
void UIScreenFade::Fade( const Vector4& StartColor, const Vector4& EndColor, float Duration )
{
m_FadeInterpolator.Reset( Interpolator< Vector4 >::EIT_Linear, StartColor, EndColor, Duration );
}

UIScreen::ETickReturn UIScreenFade::Tick( float DeltaTime, bool HasFocus )
{
ETickReturn RetVal = UIScreen::Tick( DeltaTime, HasFocus );

// HACKY: Getting widget by index
UIWidgetImage* pFadeImage = static_cast< UIWidgetImage* >( m_RenderWidgets[0] );

m_FadeInterpolator.Tick( DeltaTime );
pFadeImage->m_Color = m_FadeInterpolator.GetValue().ToColor();

return RetVal;
}

So it's decoupled from the program flow and doesn't block while the fade completes.
« Last Edit: February 01, 2009, 11:47:24 PM by David Pittman » Logged

increpare
Guest
« Reply #2 on: February 01, 2009, 11:29:19 PM »

So it's decoupled from the program flow and doesn't block while the fade completes.

Not unpleasant.  However, how to you control what happens when the fade is done, what the fade fades to?
Logged
David Pittman
Level 2
**


MAEK GAEM


View Profile WWW
« Reply #3 on: February 01, 2009, 11:37:23 PM »

So it's decoupled from the program flow and doesn't block while the fade completes.

However, how to you control what happens when the fade is done, what the fade fades to?


Like I said, it's completely decoupled, but whatever was responsible for initiating the fade would probably handle that as well. A typical case would be a script to transition between levels, which would look like this in my horrendous INI file script format:

Code:
InstructionType0 = "Command"
InstructionName0 = "fadeout 2.0"

InstructionType1 = "Wait"
InstructionName1 = "2.0"

InstructionType2 = "Command"
InstructionName2 = "transition Level2"

This is where my process doesn't look so pretty anymore. Roll Eyes It works for me, though. The "fadeout" command calls the aforementioned UIScreenFade::Fade function, the Wait command blocks the script for the given time, and the "transition" command loads the next level. Fading in would be handled by a separate on-level-load script executed automatically after the transition.
Logged

Glaiel-Gamer
Guest
« Reply #4 on: February 01, 2009, 11:42:56 PM »

Since I mostly use flash, my fades are as so:

A movieclip with a tween (from alpha 0 to alpha 100 of a black box, then back to 0) is on the top layer above everything else. When I need to fade, i tell it to play. About halfway through the clip, when its completely black, there's a line of code on that frame that calls a function on its parent to do whatever it is needed to be done during the fade (switching menu screens or whatever), the playhead continues moving in the clip and then stops at a blank frame, waiting for me to tell it to play again.

Using flash movieclips for program control ftw, it makes the development time really really fast (although has to be used sparingly cause it makes pause buttons ridiculously hard to implement).
Logged
Ivan
Owl Country
Level 10
*


alright, let's see what we can see


View Profile
« Reply #5 on: February 02, 2009, 08:22:14 AM »

In my engine, all 2D elements are grouped under Screen classes and I have a method in the ScreenManager to transition between two screens (launches a Tween class, with screen1's alpha going from 1 to 0 and screen2's alpha going from 0 to 1). They also automatically serve as game states, because input to disabled screens is ignored.
Logged

http://polycode.org/ - Free, cross-platform, open-source engine.
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic