InstructionsScreen.cs 5.1 KB

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