LoadingScreen.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 Microsoft.Xna.Framework.Content;
  14. #endregion
  15. namespace RolePlaying
  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. /// <remarks>
  32. /// Similar to a class found in the Game State Management sample on the
  33. /// XNA Creators Club Online website (http://creators.xna.com).
  34. /// </remarks>
  35. class LoadingScreen : GameScreen
  36. {
  37. #region Screens Data
  38. bool loadingIsSlow;
  39. bool otherScreensAreGone;
  40. GameScreen[] screensToLoad;
  41. #endregion
  42. #region Graphics Data
  43. private Texture2D loadingTexture;
  44. private Vector2 loadingPosition;
  45. private Texture2D loadingBlackTexture;
  46. private Rectangle loadingBlackTextureDestination;
  47. #endregion
  48. #region Initialization
  49. /// <summary>
  50. /// The constructor is private: loading screens should
  51. /// be activated via the static Load method instead.
  52. /// </summary>
  53. private LoadingScreen(ScreenManager screenManager, bool loadingIsSlow,
  54. GameScreen[] screensToLoad)
  55. {
  56. this.loadingIsSlow = loadingIsSlow;
  57. this.screensToLoad = screensToLoad;
  58. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  59. }
  60. /// <summary>
  61. /// Activates the loading screen.
  62. /// </summary>
  63. public static void Load(ScreenManager screenManager, bool loadingIsSlow,
  64. params GameScreen[] screensToLoad)
  65. {
  66. // Tell all the current screens to transition off.
  67. foreach (GameScreen screen in screenManager.GetScreens())
  68. screen.ExitScreen();
  69. // Create and activate the loading screen.
  70. LoadingScreen loadingScreen = new LoadingScreen(screenManager,
  71. loadingIsSlow,
  72. screensToLoad);
  73. screenManager.AddScreen(loadingScreen);
  74. }
  75. public override void LoadContent()
  76. {
  77. ContentManager content = ScreenManager.Game.Content;
  78. loadingTexture = content.Load<Texture2D>(@"Textures\MainMenu\LoadingPause");
  79. loadingBlackTexture =
  80. content.Load<Texture2D>(@"Textures\GameScreens\FadeScreen");
  81. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  82. loadingBlackTextureDestination = new Rectangle(viewport.X, viewport.Y,
  83. viewport.Width, viewport.Height);
  84. loadingPosition = new Vector2(
  85. viewport.X + (float)Math.Floor((viewport.Width -
  86. loadingTexture.Width) / 2f),
  87. viewport.Y + (float)Math.Floor((viewport.Height -
  88. loadingTexture.Height) / 2f));
  89. base.LoadContent();
  90. }
  91. #endregion
  92. #region Update and Draw
  93. /// <summary>
  94. /// Updates the loading screen.
  95. /// </summary>
  96. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  97. bool coveredByOtherScreen)
  98. {
  99. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  100. // If all the previous screens have finished transitioning
  101. // off, it is time to actually perform the load.
  102. if (otherScreensAreGone)
  103. {
  104. ScreenManager.RemoveScreen(this);
  105. foreach (GameScreen screen in screensToLoad)
  106. {
  107. if (screen != null)
  108. {
  109. ScreenManager.AddScreen(screen);
  110. }
  111. }
  112. // Once the load has finished, we use ResetElapsedTime to tell
  113. // the game timing mechanism that we have just finished a very
  114. // long frame, and that it should not try to catch up.
  115. ScreenManager.Game.ResetElapsedTime();
  116. }
  117. }
  118. /// <summary>
  119. /// Draws the loading screen.
  120. /// </summary>
  121. public override void Draw(GameTime gameTime)
  122. {
  123. // If we are the only active screen, that means all the previous screens
  124. // must have finished transitioning off. We check for this in the Draw
  125. // method, rather than in Update, because it isn't enough just for the
  126. // screens to be gone: in order for the transition to look good we must
  127. // have actually drawn a frame without them before we perform the load.
  128. if ((ScreenState == ScreenState.Active) &&
  129. (ScreenManager.GetScreens().Length == 1))
  130. {
  131. otherScreensAreGone = true;
  132. }
  133. // The gameplay screen takes a while to load, so we display a loading
  134. // message while that is going on, but the menus load very quickly, and
  135. // it would look silly if we flashed this up for just a fraction of a
  136. // second while returning from the game to the menus. This parameter
  137. // tells us how long the loading is going to take, so we know whether
  138. // to bother drawing the message.
  139. if (loadingIsSlow)
  140. {
  141. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  142. // Center the text in the viewport.
  143. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  144. Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
  145. Color color = new Color(255, 255, 255, TransitionAlpha);
  146. spriteBatch.Begin();
  147. spriteBatch.Draw(loadingBlackTexture, loadingBlackTextureDestination,
  148. Color.White);
  149. spriteBatch.Draw(loadingTexture, loadingPosition, Color.White);
  150. spriteBatch.End();
  151. }
  152. }
  153. #endregion
  154. }
  155. }