LoadingScreen.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. using Microsoft.Xna.Framework.Content;
  11. using System.IO;
  12. namespace RolePlaying
  13. {
  14. /// <summary>
  15. /// The loading screen coordinates transitions between the menu system and the
  16. /// game itself. Normally one screen will transition off at the same time as
  17. /// the next screen is transitioning on, but for larger transitions that can
  18. /// take a longer time to load their data, we want the menu system to be entirely
  19. /// gone before we start loading the game. This is done as follows:
  20. ///
  21. /// - Tell all the existing screens to transition off.
  22. /// - Activate a loading screen, which will transition on at the same time.
  23. /// - The loading screen watches the state of the previous screens.
  24. /// - When it sees they have finished transitioning off, it activates the real
  25. /// next screen, which may take a long time to load its data. The loading
  26. /// screen will be the only thing displayed while this load is taking place.
  27. /// </summary>
  28. /// <remarks>
  29. /// Similar to a class found in the Game State Management sample on the
  30. /// XNA Creators Club Online website (http://creators.xna.com).
  31. /// </remarks>
  32. class LoadingScreen : GameScreen
  33. {
  34. bool loadingIsSlow;
  35. bool otherScreensAreGone;
  36. GameScreen[] screensToLoad;
  37. private Texture2D loadingTexture;
  38. private Vector2 loadingPosition;
  39. private Texture2D loadingBlackTexture;
  40. private Rectangle loadingBlackTextureDestination;
  41. /// <summary>
  42. /// The constructor is private: loading screens should
  43. /// be activated via the static Load method instead.
  44. /// </summary>
  45. private LoadingScreen(ScreenManager screenManager, bool loadingIsSlow,
  46. GameScreen[] screensToLoad)
  47. {
  48. this.loadingIsSlow = loadingIsSlow;
  49. this.screensToLoad = screensToLoad;
  50. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  51. }
  52. /// <summary>
  53. /// Activates the loading screen.
  54. /// </summary>
  55. public static void Load(ScreenManager screenManager, bool loadingIsSlow,
  56. params GameScreen[] screensToLoad)
  57. {
  58. // Tell all the current screens to transition off.
  59. foreach (GameScreen screen in screenManager.GetScreens())
  60. screen.ExitScreen();
  61. // Create and activate the loading screen.
  62. LoadingScreen loadingScreen = new LoadingScreen(screenManager,
  63. loadingIsSlow,
  64. screensToLoad);
  65. screenManager.AddScreen(loadingScreen);
  66. }
  67. public override void LoadContent()
  68. {
  69. ContentManager content = ScreenManager.Game.Content;
  70. loadingTexture = content.Load<Texture2D>(Path.Combine("Textures", "MainMenu", "LoadingPause"));
  71. loadingBlackTexture = content.Load<Texture2D>(Path.Combine("Textures", "GameScreens", "FadeScreen"));
  72. loadingBlackTextureDestination = new Rectangle(0, 0,
  73. Session.BACK_BUFFER_WIDTH, Session.BACK_BUFFER_HEIGHT);
  74. loadingPosition = new Vector2((float)Math.Floor((Session.BACK_BUFFER_WIDTH -
  75. loadingTexture.Width) / 2f), (float)Math.Floor((Session.BACK_BUFFER_HEIGHT -
  76. loadingTexture.Height) / 2f));
  77. base.LoadContent();
  78. }
  79. /// <summary>
  80. /// Updates the loading screen.
  81. /// </summary>
  82. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  83. bool coveredByOtherScreen)
  84. {
  85. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  86. // If all the previous screens have finished transitioning
  87. // off, it is time to actually perform the load.
  88. if (otherScreensAreGone)
  89. {
  90. ScreenManager.RemoveScreen(this);
  91. foreach (GameScreen screen in screensToLoad)
  92. {
  93. if (screen != null)
  94. {
  95. ScreenManager.AddScreen(screen);
  96. }
  97. }
  98. // Once the load has finished, we use ResetElapsedTime to tell
  99. // the game timing mechanism that we have just finished a very
  100. // long frame, and that it should not try to catch up.
  101. ScreenManager.Game.ResetElapsedTime();
  102. }
  103. }
  104. /// <summary>
  105. /// Draws the loading screen.
  106. /// </summary>
  107. public override void Draw(GameTime gameTime)
  108. {
  109. // If we are the only active screen, that means all the previous screens
  110. // must have finished transitioning off. We check for this in the Draw
  111. // method, rather than in Update, because it isn't enough just for the
  112. // screens to be gone: in order for the transition to look good we must
  113. // have actually drawn a frame without them before we perform the load.
  114. if ((ScreenState == ScreenState.Active) &&
  115. (ScreenManager.GetScreens().Length == 1))
  116. {
  117. otherScreensAreGone = true;
  118. }
  119. // The gameplay screen takes a while to load, so we display a loading
  120. // message while that is going on, but the menus load very quickly, and
  121. // it would look silly if we flashed this up for just a fraction of a
  122. // second while returning from the game to the menus. This parameter
  123. // tells us how long the loading is going to take, so we know whether
  124. // to bother drawing the message.
  125. if (loadingIsSlow)
  126. {
  127. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  128. // Center the text in the viewport.
  129. Color color = new Color((byte)255, (byte)255, (byte)255, TransitionAlpha);
  130. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation);
  131. spriteBatch.Draw(loadingBlackTexture, loadingBlackTextureDestination,
  132. Color.White);
  133. spriteBatch.Draw(loadingTexture, loadingPosition, Color.White);
  134. spriteBatch.End();
  135. }
  136. }
  137. }
  138. }