LoadingAndInstructionScreen.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // LoadingAndInstructionScreen.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 System.Threading;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Input.Touch;
  15. using Microsoft.Xna.Framework.Input;
  16. #endregion
  17. namespace HoneycombRush
  18. {
  19. class LoadingAndInstructionScreen : GameScreen
  20. {
  21. #region Fields
  22. SpriteFont font;
  23. bool isLoading;
  24. GameplayScreen gameplayScreen;
  25. System.Threading.Thread thread;
  26. bool assetsLoaded = false;
  27. #endregion
  28. #region Initialization
  29. public LoadingAndInstructionScreen()
  30. {
  31. TransitionOnTime = TimeSpan.FromSeconds(0);
  32. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  33. EnabledGestures = GestureType.Tap;
  34. }
  35. /// <summary>
  36. /// Load the screen resources
  37. /// </summary>
  38. public override void LoadContent()
  39. {
  40. font = Load<SpriteFont>(@"Fonts\MenuFont");
  41. // Create a new instance of the gameplay screen
  42. gameplayScreen = new GameplayScreen(DifficultyMode.Easy);
  43. gameplayScreen.ScreenManager = ScreenManager;
  44. }
  45. #endregion
  46. #region Update
  47. /// <summary>
  48. /// Exit the screen after a tap gesture
  49. /// </summary>
  50. /// <param name="input"></param>
  51. public override void HandleInput(GameTime gameTime, InputState input)
  52. {
  53. if (!isLoading)
  54. {
  55. PlayerIndex player;
  56. // Handle touch input
  57. if (input.Gestures.Count > 0)
  58. {
  59. if (input.Gestures[0].GestureType == GestureType.Tap)
  60. {
  61. LoadResources();
  62. }
  63. }
  64. else if (input.IsNewKeyPress(Keys.Escape, ControllingPlayer, out player))
  65. {
  66. foreach (GameScreen screen in ScreenManager.GetScreens())
  67. {
  68. screen.ExitScreen();
  69. }
  70. ScreenManager.AddScreen(new BackgroundScreen("titleScreen"), null);
  71. ScreenManager.AddScreen(new MainMenuScreen(), PlayerIndex.One);
  72. }
  73. else if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
  74. input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player) ||
  75. input.IsNewMouseClick(InputState.MouseButton.Left, ControllingPlayer, out player))
  76. {
  77. LoadResources();
  78. }
  79. }
  80. base.HandleInput(gameTime, input);
  81. }
  82. /// <summary>
  83. /// Screen update logic
  84. /// </summary>
  85. /// <param name="gameTime"></param>
  86. /// <param name="otherScreenHasFocus"></param>
  87. /// <param name="coveredByOtherScreen"></param>
  88. public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  89. {
  90. // If necessary, load the highscore. Remember that calling LoadHighscores multiple times does not have
  91. // and adverse effect. Highscore will only be loaded once.
  92. if (!HighScoreScreen.HighscoreLoaded)
  93. {
  94. HighScoreScreen.LoadHighscores();
  95. }
  96. // If additional thread is running, do nothing
  97. else if (null != thread)
  98. {
  99. // If additional thread finished loading and the screen is not exiting
  100. if (thread.ThreadState == ThreadState.Stopped && !IsExiting)
  101. {
  102. // Move on to the game play screen once highscore data is loaded
  103. foreach (GameScreen screen in ScreenManager.GetScreens())
  104. {
  105. screen.ExitScreen();
  106. }
  107. ScreenManager.AddScreen(gameplayScreen, null);
  108. }
  109. }
  110. else if (assetsLoaded)
  111. {
  112. // Screen is not exiting
  113. if ( !IsExiting)
  114. {
  115. // Move on to the game play screen once highscore data is loaded
  116. foreach (GameScreen screen in ScreenManager.GetScreens())
  117. {
  118. screen.ExitScreen();
  119. }
  120. ScreenManager.AddScreen(gameplayScreen, null);
  121. }
  122. }
  123. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  124. }
  125. #endregion
  126. #region Render
  127. /// <summary>
  128. /// Render screen
  129. /// </summary>
  130. /// <param name="gameTime"></param>
  131. public override void Draw(GameTime gameTime)
  132. {
  133. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  134. spriteBatch.Begin();
  135. // If loading game play screen resource in the
  136. // background, show "Loading..." text
  137. if (isLoading)
  138. {
  139. string text = "Loading...";
  140. Vector2 size = font.MeasureString(text);
  141. Vector2 position = new Vector2((ScreenManager.GraphicsDevice.Viewport.Width - size.X) / 2,
  142. (ScreenManager.GraphicsDevice.Viewport.Height - size.Y) / 2);
  143. spriteBatch.DrawString(font, text, position, Color.White);
  144. }
  145. spriteBatch.End();
  146. }
  147. #endregion
  148. #region Private functionality
  149. private void LoadResources()
  150. {
  151. #if MONOMAC
  152. // Start loading the resources on main thread
  153. // If not then all sorts of errors happen for
  154. // AutoReleasPools and OpenGL does not handle
  155. // multiple thread to well when using Thread
  156. MonoMac.AppKit.NSApplication.SharedApplication.BeginInvokeOnMainThread(delegate {
  157. gameplayScreen.LoadAssets ();
  158. isLoading = false;
  159. assetsLoaded = true;
  160. });
  161. #else
  162. // Start loading the resources in an additional thread
  163. thread = new Thread(new ThreadStart(gameplayScreen.LoadAssets));
  164. thread.Start();
  165. isLoading = true;
  166. #endif
  167. }
  168. #endregion
  169. }
  170. }