InstructionsScreen.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #if ANDROID || IPHONE
  74. // Exit the screen and show the gameplay screen
  75. // with pre-loaded assets
  76. ExitScreen ();
  77. ScreenManager.AddScreen (gameplayScreen, null);
  78. #endif
  79. base.HandleInput (input);
  80. return;
  81. }
  82. PlayerIndex player;
  83. if (input.IsNewKeyPress (Microsoft.Xna.Framework.Input.Keys.Space, ControllingPlayer, out player) ||
  84. input.IsNewKeyPress (Microsoft.Xna.Framework.Input.Keys.Enter, ControllingPlayer, out player) ||
  85. input.MouseGesture.HasFlag(MouseGestureType.LeftClick)||
  86. input.IsNewButtonPress (Microsoft.Xna.Framework.Input.Buttons.Start, ControllingPlayer, out player)) {
  87. // Create a new instance of the gameplay screen
  88. gameplayScreen = new GameplayScreen ();
  89. gameplayScreen.ScreenManager = ScreenManager;
  90. // Start loading the resources in additional thread
  91. #if MONOMAC
  92. // create a new thread using BackgroundWorkerThread as method to execute
  93. thread = new Thread (LoadAssetsWorkerThread as ThreadStart);
  94. #else
  95. thread = new System.Threading.Thread (new System.Threading.ThreadStart (gameplayScreen.LoadAssets));
  96. #endif
  97. isLoading = true;
  98. // start it
  99. thread.Start ();
  100. }
  101. foreach (var gesture in input.Gestures) {
  102. if (gesture.GestureType == GestureType.Tap) {
  103. // Create a new instance of the gameplay screen
  104. gameplayScreen = new GameplayScreen ();
  105. gameplayScreen.ScreenManager = ScreenManager;
  106. #if ANDROID || IPHONE
  107. isLoading = true;
  108. #else
  109. // Start loading the resources in additional thread
  110. thread = new System.Threading.Thread (new System.Threading.ThreadStart (gameplayScreen.LoadAssets));
  111. isLoading = true;
  112. thread.Start ();
  113. #endif
  114. }
  115. }
  116. base.HandleInput (input);
  117. }
  118. void LoadAssetsWorkerThread ()
  119. {
  120. #if MONOMAC || IPHONE
  121. // Create an Autorelease Pool or we will leak objects.
  122. using (var pool = new NSAutoreleasePool()) {
  123. #else
  124. #endif
  125. // Make sure we invoke this on the Main Thread or OpenGL will throw an error
  126. #if MONOMAC
  127. MonoMac.AppKit.NSApplication.SharedApplication.BeginInvokeOnMainThread (delegate {
  128. #endif
  129. #if IPHONE
  130. var invokeOnMainThredObj = new NSObject();
  131. invokeOnMainThredObj.InvokeOnMainThread(delegate {
  132. #endif
  133. gameplayScreen.LoadAssets ();
  134. #if MONOMAC || IPHONE
  135. });
  136. }
  137. #endif
  138. }
  139. #endregion
  140. #region Render
  141. public override void Draw (GameTime gameTime)
  142. {
  143. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  144. spriteBatch.Begin ();
  145. // Draw Background
  146. spriteBatch.Draw (background, new Vector2 (0, 0), new Color (255, 255, 255, TransitionAlpha));
  147. // If loading gameplay screen resource in the
  148. // background show "Loading..." text
  149. if (isLoading) {
  150. string text = "Loading...";
  151. Vector2 size = font.MeasureString (text);
  152. Vector2 position = new Vector2 ((ScreenManager.GraphicsDevice.Viewport.Width - size.X) / 2,
  153. (ScreenManager.GraphicsDevice.Viewport.Height - size.Y) / 2);
  154. spriteBatch.DrawString (font, text, position, Color.Black);
  155. spriteBatch.DrawString (font, text, position - new Vector2 (-4, 4), new Color (255f, 150f, 0f));
  156. }
  157. spriteBatch.End ();
  158. }
  159. #endregion
  160. }
  161. }