LoadingScreen.cs 6.2 KB

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