- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/PendulumGame/Screens/PendulumLoadingScreen.cs

    00001 #region File Description
    00002 //-----------------------------------------------------------------------------
    00003 // PendulumLoadingScreen.cs
    00004 //-----------------------------------------------------------------------------
    00005 #endregion
    00006
    00007 namespace PendulumGame
    00008 {
    00009     using System;
    00010     using System.Collections.Generic;
    00011     using Microsoft.Xna.Framework;
    00012     using Microsoft.Xna.Framework.Graphics;
    00013
    00014     using NewGamePhysics.StateManager;
    00015     using NewGamePhysics.Physics;
    00016     using NewGamePhysics.GraphicalElements;
    00017
    00023     class PendulumLoadingScreen : GameScreen
    00024     {
    00025         #region Fields
    00026
    00027         bool loadingIsSlow;
    00028         bool otherScreensAreGone;
    00029
    00030         GameScreen[] screensToLoad;
    00031
    00032         #endregion
    00033
    00034         #region Initialization
    00035
    00040         private PendulumLoadingScreen(
    00041             ScreenManager screenManager,
    00042             bool loadingIsSlow,
    00043             GameScreen[] screensToLoad)
    00044         {
    00045             this.loadingIsSlow = loadingIsSlow;
    00046             this.screensToLoad = screensToLoad;
    00047
    00048             TransitionOnTime = TimeSpan.FromSeconds(1.0);
    00049         }
    00050
    00054         public override void LoadContent()
    00055         {
    00056             // Reset game time
    00057             ScreenManager.Game.ResetElapsedTime();
    00058         }
    00059
    00063         public override void UnloadContent()
    00064         {
    00065         }
    00066
    00070         public static void Load(ScreenManager screenManager,
    00071             bool loadingIsSlow,
    00072             PlayerIndex? controllingPlayer,
    00073             params GameScreen[] screensToLoad)
    00074         {
    00075             // Tell all the current screens to transition off.
    00076             foreach (GameScreen screen in screenManager.GetScreens())
    00077             {
    00078                 screen.ExitScreen();
    00079             }
    00080
    00081             // Create and activate the loading screen.
    00082             PendulumLoadingScreen loadingScreen = new PendulumLoadingScreen(screenManager,
    00083                                                             loadingIsSlow,
    00084                                                             screensToLoad);
    00085
    00086             screenManager.AddScreen(loadingScreen, controllingPlayer);
    00087         }
    00088
    00089         #endregion
    00090
    00091         #region Update and Draw
    00092
    00096         public override void Update(GameTime gameTime, bool otherScreenHasFocus,
    00097                                                        bool coveredByOtherScreen)
    00098         {
    00099             base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
    00100
    00101             // If all the previous screens have finished transitioning
    00102             // off, it is time to actually perform the load.
    00103             if (otherScreensAreGone)
    00104             {
    00105                 ScreenManager.RemoveScreen(this);
    00106
    00107                 foreach (GameScreen screen in screensToLoad)
    00108                 {
    00109                     if (screen != null)
    00110                     {
    00111                         ScreenManager.AddScreen(screen, null);
    00112                     }
    00113                 }
    00114
    00115                 // Once the load has finished, we use ResetElapsedTime to tell
    00116                 // the  game timing mechanism that we have just finished a very
    00117                 // long frame, and that it should not try to catch up.
    00118                 ScreenManager.Game.ResetElapsedTime();
    00119             }
    00120         }
    00121
    00125         public override void Draw(GameTime gameTime)
    00126         {
    00127             // If we are the only active screen, that means all the previous screens
    00128             // must have finished transitioning off. We check for this in the Draw
    00129             // method, rather than in Update, because it isn't enough just for the
    00130             // screens to be gone: in order for the transition to look good we must
    00131             // have actually drawn a frame without them before we perform the load.
    00132             if ((ScreenState == ScreenState.Active) &&
    00133                 (ScreenManager.GetScreens().Length == 1))
    00134             {
    00135                 otherScreensAreGone = true;
    00136             }
    00137
    00138             // The gameplay screen takes a while to load, so we display a loading
    00139             // message while that is going on, but the menus load very quickly, and
    00140             // it would look silly if we flashed this up for just a fraction of a
    00141             // second while returning from the game to the menus. This parameter
    00142             // tells us how long the loading is going to take, so we know whether
    00143             // to bother drawing the message.
    00144             if (loadingIsSlow)
    00145             {
    00146                 SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
    00147                 SpriteFont font = ScreenManager.Fonts["game"];
    00148
    00149                 const string message = "Loading...";
    00150
    00151                 // Center the text in the viewport.
    00152                 Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
    00153                 Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
    00154                 Vector2 textSize = font.MeasureString(message);
    00155                 Vector2 textPosition = (viewportSize - textSize) / 2;
    00156
    00157                 Color color = new Color(255, 255, 255, TransitionAlpha);
    00158
    00159                 // Draw the text.
    00160                 spriteBatch.Begin();
    00161                 spriteBatch.DrawString(font, message, textPosition, color);
    00162                 spriteBatch.End();
    00163             }
    00164         }
    00165         #endregion
    00166     }
    00167 }