LoadingScreen.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // LoadingScreen.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using GameStateManagement;
  14. #endregion
  15. namespace GameStateManagementSample
  16. {
  17. /// <summary>
  18. /// The loading screen coordinates transitions between the menu system and the
  19. /// game itself. Normally one screen will transition off at the same time as
  20. /// the next screen is transitioning on, but for larger transitions that can
  21. /// take a longer time to load their data, we want the menu system to be entirely
  22. /// gone before we start loading the game. This is done as follows:
  23. ///
  24. /// - Tell all the existing screens to transition off.
  25. /// - Activate a loading screen, which will transition on at the same time.
  26. /// - The loading screen watches the state of the previous screens.
  27. /// - When it sees they have finished transitioning off, it activates the real
  28. /// next screen, which may take a long time to load its data. The loading
  29. /// screen will be the only thing displayed while this load is taking place.
  30. /// </summary>
  31. class LoadingScreen : GameScreen
  32. {
  33. #region Fields
  34. bool loadingIsSlow;
  35. bool otherScreensAreGone;
  36. GameScreen[] screensToLoad;
  37. #endregion
  38. #region Initialization
  39. /// <summary>
  40. /// The constructor is private: loading screens should
  41. /// be activated via the static Load method instead.
  42. /// </summary>
  43. private LoadingScreen(ScreenManager screenManager, bool loadingIsSlow,
  44. GameScreen[] screensToLoad)
  45. {
  46. this.loadingIsSlow = loadingIsSlow;
  47. this.screensToLoad = screensToLoad;
  48. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  49. }
  50. /// <summary>
  51. /// Activates the loading screen.
  52. /// </summary>
  53. public static void Load(ScreenManager screenManager, bool loadingIsSlow,
  54. PlayerIndex? controllingPlayer,
  55. params GameScreen[] screensToLoad)
  56. {
  57. // Tell all the current screens to transition off.
  58. foreach (GameScreen screen in screenManager.GetScreens())
  59. screen.ExitScreen();
  60. // Create and activate the loading screen.
  61. LoadingScreen loadingScreen = new LoadingScreen(screenManager,
  62. loadingIsSlow,
  63. screensToLoad);
  64. screenManager.AddScreen(loadingScreen, controllingPlayer);
  65. }
  66. #endregion
  67. #region Update and Draw
  68. /// <summary>
  69. /// Updates the loading screen.
  70. /// </summary>
  71. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  72. bool coveredByOtherScreen)
  73. {
  74. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  75. // If all the previous screens have finished transitioning
  76. // off, it is time to actually perform the load.
  77. if (otherScreensAreGone)
  78. {
  79. ScreenManager.RemoveScreen(this);
  80. foreach (GameScreen screen in screensToLoad)
  81. {
  82. if (screen != null)
  83. {
  84. ScreenManager.AddScreen(screen, ControllingPlayer);
  85. }
  86. }
  87. // Once the load has finished, we use ResetElapsedTime to tell
  88. // the game timing mechanism that we have just finished a very
  89. // long frame, and that it should not try to catch up.
  90. ScreenManager.Game.ResetElapsedTime();
  91. }
  92. }
  93. /// <summary>
  94. /// Draws the loading screen.
  95. /// </summary>
  96. public override void Draw(GameTime gameTime)
  97. {
  98. // If we are the only active screen, that means all the previous screens
  99. // must have finished transitioning off. We check for this in the Draw
  100. // method, rather than in Update, because it isn't enough just for the
  101. // screens to be gone: in order for the transition to look good we must
  102. // have actually drawn a frame without them before we perform the load.
  103. if ((ScreenState == ScreenState.Active) &&
  104. (ScreenManager.GetScreens().Length == 1))
  105. {
  106. otherScreensAreGone = true;
  107. }
  108. // The gameplay screen takes a while to load, so we display a loading
  109. // message while that is going on, but the menus load very quickly, and
  110. // it would look silly if we flashed this up for just a fraction of a
  111. // second while returning from the game to the menus. This parameter
  112. // tells us how long the loading is going to take, so we know whether
  113. // to bother drawing the message.
  114. if (loadingIsSlow)
  115. {
  116. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  117. SpriteFont font = ScreenManager.Font;
  118. const string message = "Loading...";
  119. // Center the text in the viewport.
  120. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  121. Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
  122. Vector2 textSize = font.MeasureString(message);
  123. Vector2 textPosition = (viewportSize - textSize) / 2;
  124. Color color = Color.White * TransitionAlpha;
  125. // Draw the text.
  126. spriteBatch.Begin();
  127. spriteBatch.DrawString(font, message, textPosition, color);
  128. spriteBatch.End();
  129. }
  130. }
  131. #endregion
  132. }
  133. }