Wednesday, November 2, 2016

43. How to Add Splash Screens to the Menu System

       The following tutorial is based on the Game State Management Sample and shows how to add splash screens as the startup of the game. This tutorial also works with the Network Game State Management Sample. I have posted my entire class below with comments that you can add to your screens folder. I named mine SplaceScreens.cs and make sure to change the namespace of this class to match you project. Also check out Robot Foot Games' tutorial on how to add multiple columns to the Game State Management Sample.


#region File Description
/*  ============================================
 *  SplashScreens.cs
 *  ============================================
 *
 * Upon the game's startup, Splash Screens are shown. The first screen shows the 
 * company logo and then it slowly fades away and displays the game engine logo. 
 * After the game engine logo fades away, the user is taken to the Title Screen.
 * 
 * 
*/
#endregion


#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace CycloneGameEngine
{
    class SplashScreens : GameScreen
  {
      #region Fields
      public enum GameType { Splash1, Splash2}
   
      GameType gametype = new GameType();
   
      #region Splash Screens

      #region Studio Logo
      Texture2D StudioLogo;
      float logoFade = 0;
      bool wasNorm = false;
      bool logoStarted = false;
      #endregion End of Studio Logo

      #region Engine Logo
      Texture2D EngineLogo;
      float logoFade2 = 0;
      bool wasNorm2 = false;
      bool logoStarted2 = false;
      #endregion End of Engine Logo

      #endregion End of Splash Screens

        ContentManager content;
   
        #endregion


        #region Initialization

        // Constructor.
        public SplashScreens()
        {
     
        }


        // Loads graphics content for this screen. The background texture is quite
        // big, so we use our own local ContentManager to load it. This allows us
        // to unload before going from the menus into the game itself, wheras if we
        // used the shared ContentManager provided by the Game class, the content
        // would remain loaded forever.
        //
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");

            StudioLogo = content.Load<Texture2D>("Images/Splash_Screens/logo2Main");
            EngineLogo = content.Load<Texture2D>("Images/Splash_Screens/Engine-Logo");
        }



        // Unloads graphics content for this screen.
        public override void UnloadContent()
        {
            content.Unload();
        }

        #endregion


        #region Update and Draw


        // Updates the background screen. Unlike most screens, this should not
        // transition off even if it has been covered by another screen: it is
        // supposed to be covered, after all! This overload forces the
        // coveredByOtherScreen parameter to false in order to stop the base
        // Update method wanting to transition off.
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            #region Update the 1st Splash Screen
            gametype = GameType.Splash1;

            // If we are at the splash screen
            if (gametype == GameType.Splash1)
            {

                if (!logoStarted)
                {
                    logoStarted = true;
                }

                // Fade the logo in , then out.
                if ((logoFade <= 245) && (wasNorm == false))
                    logoFade += (float)0.8;
                if (logoFade > 244)
                    wasNorm = true;
                if ((logoFade > 11) && (wasNorm == true))
                    logoFade -= 1;
                if ((logoFade < 11) && (wasNorm == true))
                {
                    gametype = GameType.Splash2;
                }
            }
            #endregion End of Drawing the 1st Splash Screen


            #region Update 2nd Splash Screen
            if (gametype == GameType.Splash2)
            {

                if (!logoStarted2)
                {
                    logoStarted2 = true;
                }

                // Fade the logo in , then out.
                if ((logoFade2 <= 245) && (wasNorm2 == false))
                    logoFade2 += (float)0.8;
                if (logoFade2 > 244)
                    wasNorm2 = true;
                if ((logoFade2 > 11) && (wasNorm2 == true))
                    logoFade2 -= 1;
                if ((logoFade2 < 11) && (wasNorm2 == true))
                {
                    ExitScreen();
                 
                }
            }
            #endregion End of Drawing the 2nd Splash Screen

            base.Update(gameTime, false, false);

        }


        // Draws the splash screens.
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
         
            #region Draw the 1st Splash Screen
            if (gametype == GameType.Splash1)
            {
                // Set the background color to black
                ScreenManager.GraphicsDevice.Clear(Color.Black);
                spriteBatch.Begin(SpriteSortMode.Texture, BlendState.Additive, SamplerState.PointWrap, DepthStencilState.Default, null);

                // Draw the logo to screen.              
                spriteBatch.Draw(StudioLogo, new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2 - (StudioLogo.Width / 2), ScreenManager.GraphicsDevice.Viewport.Height / 2 - (StudioLogo.Height / 2)), new Color(255, 255, 255, (byte)logoFade));

                // End the spritebatch
                spriteBatch.End();
            }
            #endregion End of Drawing the 1st Splash Screen

            #region Draw the 2nd Splash Screen
            if (gametype == GameType.Splash2)
            {
                // Set the background color to black
                ScreenManager.GraphicsDevice.Clear(Color.Black);
                spriteBatch.Begin(SpriteSortMode.Texture, BlendState.Additive, SamplerState.PointWrap, DepthStencilState.Default, null);

                // Draw the logo to screen.              
                spriteBatch.Draw(EngineLogo, new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2 - (EngineLogo.Width / 2), ScreenManager.GraphicsDevice.Viewport.Height / 2 - (EngineLogo.Height / 2)), new Color(255, 255, 255, (byte)logoFade2));

                // End the spritebatch
                spriteBatch.End();

            }
            #endregion End of Drawing the 2nd Splash Screen
        }
        #endregion
  }
}



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.