- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/GravityChooser/Screens/GravityChooserLoadingScreen.cs

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