LoadingScreen.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 NetRumble
  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. /// <remarks>
  27. /// This public class is similar to one in the GameStateManagement sample.
  28. /// </remarks>
  29. public class LoadingScreen : GameScreen
  30. {
  31. bool loadingIsSlow;
  32. bool otherScreensAreGone;
  33. EventHandler<EventArgs> loadNextScreen;
  34. /// <summary>
  35. /// The constructor is private: loading screens should
  36. /// be activated via the static Load method instead.
  37. /// </summary>
  38. private LoadingScreen()
  39. {
  40. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  41. }
  42. /// <summary>
  43. /// Activates the loading screen.
  44. /// </summary>
  45. public static void Load(ScreenManager screenManager,
  46. EventHandler<EventArgs> loadNextScreen,
  47. bool loadingIsSlow)
  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();
  54. loadingScreen.loadingIsSlow = loadingIsSlow;
  55. loadingScreen.loadNextScreen = loadNextScreen;
  56. screenManager.AddScreen(loadingScreen);
  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. loadNextScreen(this, EventArgs.Empty);
  71. }
  72. }
  73. /// <summary>
  74. /// Draws the loading screen.
  75. /// </summary>
  76. public override void Draw(GameTime gameTime)
  77. {
  78. // If we are the only active screen, that means all the previous screens
  79. // must have finished transitioning off. We check for this in the Draw
  80. // method, rather than in Update, because it isn't enough just for the
  81. // screens to be gone: in order for the transition to look good we must
  82. // have actually drawn a frame without them before we perform the load.
  83. if ((ScreenState == ScreenState.Active) &&
  84. (ScreenManager.GetScreens().Length == 1))
  85. {
  86. otherScreensAreGone = true;
  87. }
  88. // The gameplay screen takes a while to load, so we display a loading
  89. // message while that is going on, but the menus load very quickly, and
  90. // it would look silly if we flashed this up for just a fraction of a
  91. // second while returning from the game to the menus. This parameter
  92. // tells us how long the loading is going to take, so we know whether
  93. // to bother drawing the message.
  94. if (loadingIsSlow)
  95. {
  96. const string message = "Loading...";
  97. // Center the text in the viewport.
  98. Vector2 viewportSize = new Vector2(ScreenManager.BASE_BUFFER_WIDTH, ScreenManager.BASE_BUFFER_HEIGHT);
  99. Vector2 textSize = ScreenManager.Font.MeasureString(message);
  100. Vector2 textPosition = (viewportSize - textSize) / 2;
  101. Color color = new Color((byte)255, (byte)255, (byte)255, (byte)TransitionAlpha);
  102. // Draw the text.
  103. ScreenManager.SpriteBatch.Begin();
  104. ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, message,
  105. textPosition, color);
  106. ScreenManager.SpriteBatch.End();
  107. }
  108. }
  109. }
  110. }