InstructionsScreen.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 MACOS
  20. using MonoMac.AppKit;
  21. using MonoMac.Foundation;
  22. #endif
  23. #endregion
  24. namespace CatapultGame
  25. {
  26. class InstructionsScreen : GameScreen
  27. {
  28. #region Fields
  29. Texture2D background;
  30. SpriteFont font;
  31. bool isLoading;
  32. GameplayScreen gameplayScreen;
  33. System.Threading.Thread thread;
  34. #endregion
  35. #region Initialization
  36. public InstructionsScreen ()
  37. {
  38. EnabledGestures = GestureType.Tap;
  39. TransitionOnTime = TimeSpan.FromSeconds (0);
  40. TransitionOffTime = TimeSpan.FromSeconds (0.5);
  41. }
  42. #endregion
  43. #region Loading
  44. public override void LoadContent ()
  45. {
  46. background = Load<Texture2D> ("Textures/Backgrounds/instructions");
  47. font = Load<SpriteFont> ("Fonts/MenuFont");
  48. }
  49. #endregion
  50. public override void Update (GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  51. {
  52. // If additional thread is running, skip
  53. if (null != thread) {
  54. // If additional thread finished loading and the screen is not exiting
  55. if (thread.ThreadState == System.Threading.ThreadState.Stopped && !IsExiting) {
  56. isLoading = false;
  57. // Exit the screen and show the gameplay screen
  58. // with pre-loaded assets
  59. ExitScreen ();
  60. ScreenManager.AddScreen (gameplayScreen, null);
  61. }
  62. }
  63. base.Update (gameTime, otherScreenHasFocus, coveredByOtherScreen);
  64. }
  65. #region Handle input
  66. public override void HandleInput (InputState input)
  67. {
  68. if (isLoading == true) {
  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 MACOS
  82. // create a new thread using BackgroundWorkerThread as method to execute
  83. thread = new Thread (LoadAssetsWorkerThread as ThreadStart);
  84. #else
  85. thread = new System.Threading.Thread (new System.Threading.ThreadStart (gameplayScreen.LoadAssets));
  86. #endif
  87. isLoading = true;
  88. // start it
  89. thread.Start ();
  90. }
  91. foreach (var gesture in input.Gestures) {
  92. if (gesture.GestureType == GestureType.Tap) {
  93. // Create a new instance of the gameplay screen
  94. gameplayScreen = new GameplayScreen ();
  95. gameplayScreen.ScreenManager = ScreenManager;
  96. // Start loading the resources in additional thread
  97. thread = new System.Threading.Thread (new System.Threading.ThreadStart (gameplayScreen.LoadAssets));
  98. isLoading = true;
  99. thread.Start ();
  100. }
  101. }
  102. base.HandleInput (input);
  103. }
  104. void LoadAssetsWorkerThread ()
  105. {
  106. // Create an Autorelease Pool or we will leak objects.
  107. using (var pool = new NSAutoreleasePool()) {
  108. // Make sure we invoke this on the Main Thread or OpenGL will throw an error
  109. MonoMac.AppKit.NSApplication.SharedApplication.BeginInvokeOnMainThread (delegate {
  110. gameplayScreen.LoadAssets ();
  111. });
  112. }
  113. }
  114. #endregion
  115. #region Render
  116. public override void Draw (GameTime gameTime)
  117. {
  118. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  119. spriteBatch.Begin ();
  120. // Draw Background
  121. spriteBatch.Draw (background, new Vector2 (0, 0),
  122. new Color (255, 255, 255, TransitionAlpha));
  123. // If loading gameplay screen resource in the
  124. // background show "Loading..." text
  125. if (isLoading) {
  126. string text = "Loading...";
  127. Vector2 size = font.MeasureString (text);
  128. Vector2 position = new Vector2 (
  129. (ScreenManager.GraphicsDevice.Viewport.Width - size.X) / 2,
  130. (ScreenManager.GraphicsDevice.Viewport.Height - size.Y) / 2);
  131. spriteBatch.DrawString (font, text, position, Color.Black);
  132. spriteBatch.DrawString (font, text, position - new Vector2 (-4, 4), new Color (255f, 150f, 0f));
  133. }
  134. spriteBatch.End ();
  135. }
  136. #endregion
  137. }
  138. }