LoadingAndInstructionScreen.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. {
  76. LoadResources();
  77. }
  78. }
  79. base.HandleInput(gameTime, input);
  80. }
  81. /// <summary>
  82. /// Screen update logic
  83. /// </summary>
  84. /// <param name="gameTime"></param>
  85. /// <param name="otherScreenHasFocus"></param>
  86. /// <param name="coveredByOtherScreen"></param>
  87. public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  88. {
  89. // If necessary, load the highscore. Remember that calling LoadHighscores multiple times does not have
  90. // and adverse effect. Highscore will only be loaded once.
  91. if (!HighScoreScreen.HighscoreLoaded)
  92. {
  93. HighScoreScreen.LoadHighscores();
  94. }
  95. // If additional thread is running, do nothing
  96. else if (null != thread)
  97. {
  98. // If additional thread finished loading and the screen is not exiting
  99. if (thread.ThreadState == ThreadState.Stopped && !IsExiting)
  100. {
  101. // Move on to the game play screen once highscore data is loaded
  102. foreach (GameScreen screen in ScreenManager.GetScreens())
  103. {
  104. screen.ExitScreen();
  105. }
  106. ScreenManager.AddScreen(gameplayScreen, null);
  107. }
  108. }
  109. else if (assetsLoaded)
  110. {
  111. // Screen is not exiting
  112. if ( !IsExiting)
  113. {
  114. // Move on to the game play screen once highscore data is loaded
  115. foreach (GameScreen screen in ScreenManager.GetScreens())
  116. {
  117. screen.ExitScreen();
  118. }
  119. ScreenManager.AddScreen(gameplayScreen, null);
  120. }
  121. }
  122. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  123. }
  124. #endregion
  125. #region Render
  126. /// <summary>
  127. /// Render screen
  128. /// </summary>
  129. /// <param name="gameTime"></param>
  130. public override void Draw(GameTime gameTime)
  131. {
  132. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  133. spriteBatch.Begin();
  134. // If loading game play screen resource in the
  135. // background, show "Loading..." text
  136. if (isLoading)
  137. {
  138. string text = "Loading...";
  139. Vector2 size = font.MeasureString(text);
  140. Vector2 position = new Vector2((ScreenManager.GraphicsDevice.Viewport.Width - size.X) / 2,
  141. (ScreenManager.GraphicsDevice.Viewport.Height - size.Y) / 2);
  142. spriteBatch.DrawString(font, text, position, Color.White);
  143. }
  144. spriteBatch.End();
  145. }
  146. #endregion
  147. #region Private functionality
  148. private void LoadResources()
  149. {
  150. #if MONOMAC
  151. // Start loading the resources on main thread
  152. // If not then all sorts of errors happen for
  153. // AutoReleasPools and OpenGL does not handle
  154. // multiple thread to well when using Thread
  155. MonoMac.AppKit.NSApplication.SharedApplication.BeginInvokeOnMainThread(delegate {
  156. gameplayScreen.LoadAssets ();
  157. isLoading = false;
  158. assetsLoaded = true;
  159. });
  160. #else
  161. // Start loading the resources in an additional thread
  162. thread = new Thread(new ThreadStart(gameplayScreen.LoadAssets));
  163. thread.Start();
  164. isLoading = true;
  165. #endif
  166. }
  167. #endregion
  168. }
  169. }