Intro Screen and Music Fade In

From C4 Engine Wiki

Jump to: navigation, search

The following code snippet shows how to add an intro splash screen and intro music that fade in. In your game-specific Application sub-class, add the following member declarations:

class Game : public Singleton<Game>, public Application
{
    ...
 
    private:
        ImageElement   *mSplash;
        QuadElement    *mBlack;
        Interpolator   *mFadeInterp;
        Sound          *mIntroMusic;
 
        static const long kFadeInTime; // Time to fade in intro screen (in ms)
 
    ...
}

Then, in your Application code, add the following:

const long Game::kFadeInTime = 5000;
 
...
 
// Called when our game is started
Game::Game() : Singleton<Game>(TheGame)
{
    ...
 
    // Fade in intro music
    mIntroMusic = new Sound;
    WaveStreamer *streamer = new WaveStreamer;
    streamer->AddComponent("IntroMusic");
    mIntroMusic ->Stream(streamer);
    mIntroMusic->SetLoopCount(kSoundLoopInfinite);
    mIntroMusic->SetSoundProperty(kSoundVolume, 0.0f);
    mIntroMusic->Play();
    mIntroMusic->Fade(1.0f, kFadeInTime);
 
    // Fade intro screen from black
    mFadeInterp = new Interpolator;
    mFadeInterp->SetRange(0.0f, 1.0f);
    mFadeInterp->SetRate(1.0f / (float) kFadeInTime);
 
    mSplash = new ImageElement(TheDisplayMgr->GetDisplayWidth(),
        TheDisplayMgr->GetDisplayHeight(), "IntroScreen");
    mSplash->SetVertexTexcoord2D(0, Point2D(0, 1));
    mSplash->SetVertexTexcoord2D(1, Point2D(0, 0));
    mSplash->SetVertexTexcoord2D(2, Point2D(1, 0));
    mSplash->SetVertexTexcoord2D(3, Point2D(1, 1));
    mSplash->SetQuadColor(ColorRGBA(1.0f, 1.0f, 1.0f, mFadeInterp->GetValue()));
    TheInterfaceMgr->AddElement(mSplash);
 
    mBlack = new QuadElement(TheDisplayMgr->GetDisplayWidth(),
        TheDisplayMgr->GetDisplayHeight());
    mBlack->SetQuadColor(ColorRGBA(0.0f, 0.0f, 0.0f, 1.0f - mFadeInterp->GetValue()));
    TheInterfaceMgr->AddElement(mBlack);
 
    mFadeInterp->SetMode(kInterpolatorForward);
 
    ...
}

Finally, in the application's ApplicationTask() function, add the following code to fade in the screen.

void Game::ApplicationTask()
{
    ...
 
    // Fade from black
    float t = mFadeInterp->UpdateValue();
    mSplash->SetQuadColor(ColorRGBA(1.0f, 1.0f, 1.0f, t));
    mBlack->SetQuadColor(ColorRGBA(0.0f, 0.0f, 0.0f, 1.0f - t));
 
    ...
}

You will need to import a texture into Data/*/IntroScreen.tex for the intro screen, and put a sound file in Data/*/IntroMusic.wav for the intro music. In this example, we import the sound file with streaming set to true. Make sure, your IntroMusic.wav is encoded in compressed 44.1 kHz MS ADPCM format. This is required if you create the Sound object with streaming set to true.

Personal tools