2
0

InstructionsScreen.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // BackgroundScreen.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.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using Microsoft.Xna.Framework;
  17. using GameStateManagement;
  18. using Microsoft.Xna.Framework.Input.Touch;
  19. #if MONOMAC
  20. using MonoMac.AppKit;
  21. using MonoMac.Foundation;
  22. #endif
  23. #if IPHONE
  24. using MonoTouch.UIKit;
  25. using MonoTouch.Foundation;
  26. #endif
  27. #endregion
  28. namespace CatapultGame
  29. {
  30. class InstructionsScreen : GameScreen
  31. {
  32. #region Fields
  33. Texture2D background;
  34. SpriteFont font;
  35. bool isLoading;
  36. GameplayScreen gameplayScreen;
  37. System.Threading.Thread thread;
  38. #endregion
  39. #region Initialization
  40. public InstructionsScreen ()
  41. {
  42. EnabledGestures = GestureType.Tap;
  43. TransitionOnTime = TimeSpan.FromSeconds (0);
  44. TransitionOffTime = TimeSpan.FromSeconds (0.5);
  45. }
  46. #endregion
  47. #region Loading
  48. public override void LoadContent ()
  49. {
  50. background = Load<Texture2D> ("Textures/Backgrounds/instructions");
  51. font = Load<SpriteFont> ("Fonts/MenuFont");
  52. }
  53. #endregion
  54. public override void Update (GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  55. {
  56. // If additional thread is running, skip
  57. if (null != thread) {
  58. // If additional thread finished loading and the screen is not exiting
  59. if (thread.ThreadState == System.Threading.ThreadState.Stopped && !IsExiting) {
  60. isLoading = false;
  61. // Exit the screen and show the gameplay screen
  62. // with pre-loaded assets
  63. ExitScreen ();
  64. ScreenManager.AddScreen (gameplayScreen, null);
  65. }
  66. }
  67. base.Update (gameTime, otherScreenHasFocus, coveredByOtherScreen);
  68. }
  69. #region Handle input
  70. public override void HandleInput (InputState input)
  71. {
  72. if (isLoading == true)
  73. {
  74. #if ANDROID || IPHONE || LINUX || WINDOWS
  75. // Exit the screen and show the gameplay screen
  76. // with pre-loaded assets
  77. ExitScreen ();
  78. ScreenManager.AddScreen (gameplayScreen, null);
  79. #endif
  80. base.HandleInput (input);
  81. return;
  82. }
  83. PlayerIndex player;
  84. if (input.IsNewKeyPress (Microsoft.Xna.Framework.Input.Keys.Space, ControllingPlayer, out player) ||
  85. input.IsNewKeyPress (Microsoft.Xna.Framework.Input.Keys.Enter, ControllingPlayer, out player) ||
  86. input.MouseGesture.HasFlag(MouseGestureType.LeftClick) ||
  87. input.IsNewButtonPress (Microsoft.Xna.Framework.Input.Buttons.Start, ControllingPlayer, out player)) {
  88. // Create a new instance of the gameplay screen
  89. gameplayScreen = new GameplayScreen ();
  90. gameplayScreen.ScreenManager = ScreenManager;
  91. // Start loading the resources in additional thread
  92. #if !LINUX && !WINDOWS
  93. #if MONOMAC
  94. // create a new thread using BackgroundWorkerThread as method to execute
  95. thread = new Thread (LoadAssetsWorkerThread as ThreadStart);
  96. #else
  97. thread = new System.Threading.Thread (new System.Threading.ThreadStart (gameplayScreen.LoadAssets));
  98. #endif
  99. isLoading = true;
  100. // start it
  101. thread.Start ();
  102. #else
  103. isLoading = true;
  104. #endif
  105. }
  106. foreach (var gesture in input.Gestures) {
  107. if (gesture.GestureType == GestureType.Tap) {
  108. // Create a new instance of the gameplay screen
  109. gameplayScreen = new GameplayScreen ();
  110. gameplayScreen.ScreenManager = ScreenManager;
  111. #if ANDROID || IPHONE || LINUX || WINDOWS
  112. isLoading = true;
  113. #else
  114. // Start loading the resources in additional thread
  115. thread = new System.Threading.Thread (new System.Threading.ThreadStart (gameplayScreen.LoadAssets));
  116. isLoading = true;
  117. thread.Start ();
  118. #endif
  119. }
  120. }
  121. base.HandleInput (input);
  122. }
  123. void LoadAssetsWorkerThread ()
  124. {
  125. #if MONOMAC || IPHONE
  126. // Create an Autorelease Pool or we will leak objects.
  127. using (var pool = new NSAutoreleasePool()) {
  128. #else
  129. #endif
  130. // Make sure we invoke this on the Main Thread or OpenGL will throw an error
  131. #if MONOMAC
  132. MonoMac.AppKit.NSApplication.SharedApplication.BeginInvokeOnMainThread (delegate {
  133. #endif
  134. #if IPHONE
  135. var invokeOnMainThredObj = new NSObject();
  136. invokeOnMainThredObj.InvokeOnMainThread(delegate {
  137. #endif
  138. gameplayScreen.LoadAssets ();
  139. #if MONOMAC || IPHONE
  140. });
  141. }
  142. #endif
  143. }
  144. #endregion
  145. #region Render
  146. public override void Draw (GameTime gameTime)
  147. {
  148. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  149. spriteBatch.Begin ();
  150. // Draw Background
  151. spriteBatch.Draw (background, new Vector2 (0, 0),
  152. new Color (255, 255, 255, TransitionAlpha));
  153. // If loading gameplay screen resource in the
  154. // background show "Loading..." text
  155. if (isLoading) {
  156. string text = "Loading...";
  157. Vector2 size = font.MeasureString (text);
  158. Vector2 position = new Vector2 (
  159. (ScreenManager.GraphicsDevice.Viewport.Width - size.X) / 2,
  160. (ScreenManager.GraphicsDevice.Viewport.Height - size.Y) / 2);
  161. spriteBatch.DrawString (font, text, position, Color.Black);
  162. spriteBatch.DrawString (font, text, position - new Vector2 (-4, 4), new Color (255f, 150f, 0f));
  163. }
  164. spriteBatch.End ();
  165. }
  166. #endregion
  167. }
  168. }