InstructionsScreen.cs 5.0 KB

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