Sharing - An Easy Way to Fade In Fade Out Screen

Just sharing.
It simplifies things for me, and gives me better control of my screens.
Hopefully it’s useful to someone.

namespace Game1
{
  public class Game1 : Game
  {
    Texture2D mySplash;			
    float splashAlpha = 0.0f
    SpriteFont texty;
    	
    int tHours; 

            
    int tMinutes;               
    int tSeconds;
    double secs;                
    double secsRounded;
    int timer = 0;
	
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
		
        // Get the center and size of the screen
        screenCenter = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2,
                                    graphics.GraphicsDevice.Viewport.Height / 2);
        screenW = graphics.GraphicsDevice.Viewport.Width;
        screenH = graphics.GraphicsDevice.Viewport.Height;
        screenCenterX = screenW / 2; screenCenterY = screenH / 2;
		
    mySplash = Content.Load<Texture2D>("splash");
    texty = Content.Load<SpriteFont>("Fonts/text");
}
	
	
protected override void Update(GameTime gameTime)
    {
        /*
    // ---------------------------------------------------------------------------------------------------- \\
       Set this time at any point to get the hrs:mins:secs something runs for
    \\ ---------------------------------------------------------------------------------------------------- //
    */
        //secs += gameTime.ElapsedGameTime.TotalSeconds;
        //secsRounded = Math.Floor(secs); // now we have seconds
        //secsRounded = (MathHelper.Clamp((int)secsRounded, 1, 60)); // limited to 60
        //tSeconds = (int)secsRounded; // my seconds are an integer
        //if (secs >= 60) { secs = 1; }
        ////tSeconds++; // too fast
        //if (tSeconds >= 60) { tMinutes = MathHelper.Clamp(tMinutes += 1, 0, 60); tSeconds = 1; }
        //if (tMinutes >= 60) { tHours = MathHelper.Clamp(tHours += 1, 0, 24); tMinutes = 1; }
    	
    	
    /*
    // ---------------------------------------------------------------------- \\
       This can be set on all screens to faded
    \\ ---------------------------------------------------------------------- //
    */
        // fade in and out Splash
        if (drawSplashScreen)
        {
            if (splashAlpha < 1.0f && tSeconds < 3) { splashAlpha += 0.01f; }
            if (splashAlpha >= 1.0f)
            {
                secs += gameTime.ElapsedGameTime.TotalSeconds;
                secsRounded = Math.Floor(secs); // now we have seconds...
                secsRounded = (MathHelper.Clamp((int)secsRounded, 1, 60)); // limited to 60
                tSeconds = (int)secsRounded; // my seconds are an integer
                splashAlpha = 1.0f;
                
                if (tSeconds >= 3) {tSeconds = 3;} // not necessary 
                	// ...but you would have to adjust the two if statements below 
            }
            if (splashAlpha > 0.0f && tSeconds == 3) { splashAlpha -= 0.01f; }
            if (splashAlpha <= 0.0f && tSeconds == 3)
            {
                splashAlpha = 0.0f;
                drawSplashScreen = false; // drawing splash complete
                secs = 0; secsRounded = 0; tSeconds = 0; // reset time
                timer = 0; steps = 1; // reset timer - proceed to next step
            }
        }

        base.Update(gameTime);
    }
    
    protected override void Draw(GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear(backgroundCol); 
        
        // set the timer - when to start the splash
        if (timer < 25 && steps == 0) { timer += 1; }
        if (timer >= 25) { timer = 25; drawSplashScreen = true; }

        if (drawSplashScreen == true)
        {
            //Splash Logo
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
            spriteBatch.Draw(mySplash, new Vector2(screenW / 2, screenH / 2), null, Color.White * splashAlpha, 0,
                new Vector2(mySplash.Width / 2, mySplash.Height / 2), 1.0f, SpriteEffects.None, 1.0f);
            spriteBatch.End();
        }
        
        // just to see a text display...
        spriteBatch.Begin();
        spriteBatch.DrawString(texty, tHours + " : " + tMinutes + " : " + tSeconds + "  - " + splashAlpha, new Vector2(25, 25), Color.Wheat);
        spriteBatch.End();
    }
}

}

Forgive me. …can’t get the code set right on this page.

Thank you! Pointed me in the right direction.

You’re welcome.
I’m glad it was useful.