LoadingScreen.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 GameStateManagement
  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. bool loadingIsSlow;
  29. bool otherScreensAreGone;
  30. GameScreen[] screensToLoad;
  31. /// <summary>
  32. /// The constructor is private: loading screens should
  33. /// be activated via the static Load method instead.
  34. /// </summary>
  35. private LoadingScreen(ScreenManager screenManager, bool loadingIsSlow,
  36. GameScreen[] screensToLoad)
  37. {
  38. this.loadingIsSlow = loadingIsSlow;
  39. this.screensToLoad = screensToLoad;
  40. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  41. }
  42. /// <summary>
  43. /// Activates the loading screen.
  44. /// </summary>
  45. public static void Load(ScreenManager screenManager, bool loadingIsSlow,
  46. PlayerIndex? controllingPlayer,
  47. params GameScreen[] screensToLoad)
  48. {
  49. // Tell all the current screens to transition off.
  50. foreach (GameScreen screen in screenManager.GetScreens())
  51. screen.ExitScreen();
  52. // Create and activate the loading screen.
  53. LoadingScreen loadingScreen = new LoadingScreen(screenManager,
  54. loadingIsSlow,
  55. screensToLoad);
  56. screenManager.AddScreen(loadingScreen, controllingPlayer);
  57. }
  58. /// <summary>
  59. /// Updates the loading screen.
  60. /// </summary>
  61. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  62. bool coveredByOtherScreen)
  63. {
  64. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  65. // If all the previous screens have finished transitioning
  66. // off, it is time to actually perform the load.
  67. if (otherScreensAreGone)
  68. {
  69. ScreenManager.RemoveScreen(this);
  70. foreach (GameScreen screen in screensToLoad)
  71. {
  72. if (screen != null)
  73. {
  74. ScreenManager.AddScreen(screen, ControllingPlayer);
  75. }
  76. }
  77. // Once the load has finished, we use ResetElapsedTime to tell
  78. // the game timing mechanism that we have just finished a very
  79. // long frame, and that it should not try to catch up.
  80. ScreenManager.Game.ResetElapsedTime();
  81. }
  82. }
  83. /// <summary>
  84. /// Draws the loading screen.
  85. /// </summary>
  86. public override void Draw(GameTime gameTime)
  87. {
  88. // If we are the only active screen, that means all the previous screens
  89. // must have finished transitioning off. We check for this in the Draw
  90. // method, rather than in Update, because it isn't enough just for the
  91. // screens to be gone: in order for the transition to look good we must
  92. // have actually drawn a frame without them before we perform the load.
  93. if ((ScreenState == ScreenState.Active) &&
  94. (ScreenManager.GetScreens().Length == 1))
  95. {
  96. otherScreensAreGone = true;
  97. }
  98. // The gameplay screen takes a while to load, so we display a loading
  99. // message while that is going on, but the menus load very quickly, and
  100. // it would look silly if we flashed this up for just a fraction of a
  101. // second while returning from the game to the menus. This parameter
  102. // tells us how long the loading is going to take, so we know whether
  103. // to bother drawing the message.
  104. if (loadingIsSlow)
  105. {
  106. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  107. SpriteFont font = ScreenManager.Font;
  108. const string message = "Loading...";
  109. // Center the text in the viewport.
  110. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  111. Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
  112. Vector2 textSize = font.MeasureString(message);
  113. Vector2 textPosition = (viewportSize - textSize) / 2;
  114. Color color = Color.White * TransitionAlpha;
  115. // Draw the text.
  116. spriteBatch.Begin();
  117. spriteBatch.DrawString(font, message, textPosition, color);
  118. spriteBatch.End();
  119. }
  120. }
  121. }
  122. }