BackgroundScreen.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Content;
  14. using Microsoft.Xna.Framework.Graphics;
  15. #endregion
  16. namespace NetRumble
  17. {
  18. /// <summary>
  19. /// The background screen sits behind all the other menu screens.
  20. /// It draws a background image that remains fixed in place regardless
  21. /// of whatever transitions the screens on top of it may be doing.
  22. /// </summary>
  23. /// <remarks>
  24. /// This public class is somewhat similar to one of the same name in the
  25. /// GameStateManagement sample.
  26. /// </remarks>
  27. public class BackgroundScreen : GameScreen, IDisposable
  28. {
  29. #region Constants
  30. /// <summary>
  31. /// The period of the parallax motion in the starfield.
  32. /// </summary>
  33. const float starsParallaxPeriod = 30f;
  34. /// <summary>
  35. /// The amplitude of the parallax motion in the starfield.
  36. /// </summary>
  37. const float starsParallaxAmplitude = 2048f;
  38. #endregion
  39. #region Graphics Data
  40. /// <summary>
  41. /// The star field rendering in the background.
  42. /// </summary>
  43. private Starfield starfield;
  44. /// <summary>
  45. /// Persistent movement tracker, used to slightly parallax the stars.
  46. /// </summary>
  47. private double movement;
  48. /// <summary>
  49. /// The application's title texture.
  50. /// </summary>
  51. private Texture2D titleTexture;
  52. #endregion
  53. #region Initialization Methods
  54. /// <summary>
  55. /// Construct a new BackgroundScreen object.
  56. /// </summary>
  57. public BackgroundScreen()
  58. {
  59. TransitionOnTime = TimeSpan.FromSeconds(1.0);
  60. TransitionOffTime = TimeSpan.FromSeconds(1.0);
  61. }
  62. /// <summary>
  63. /// Loads graphics content for this screen. The background texture is quite
  64. /// big, so we use our own local ContentManager to load it. This allows us
  65. /// to unload before going from the menus into the game itself, wheras if we
  66. /// used the shared ContentManager provided by the ScreenManager, the content
  67. /// would remain loaded forever.
  68. /// </summary>
  69. public override void LoadContent()
  70. {
  71. // load the title texture
  72. titleTexture = ScreenManager.Content.Load<Texture2D>("Textures/title");
  73. movement = 0f;
  74. starfield = new Starfield(
  75. Vector2.Multiply(new Vector2(
  76. (float)Math.Cos(movement / starsParallaxPeriod),
  77. (float)Math.Sin(movement / starsParallaxPeriod)),
  78. starsParallaxAmplitude),
  79. ScreenManager.GraphicsDevice, ScreenManager.Content);
  80. starfield.LoadContent();
  81. base.LoadContent();
  82. }
  83. /// <summary>
  84. /// Release graphics data.
  85. /// </summary>
  86. public override void UnloadContent()
  87. {
  88. if (starfield != null)
  89. {
  90. starfield.UnloadContent();
  91. starfield = null;
  92. }
  93. base.UnloadContent();
  94. }
  95. #endregion
  96. #region Updating Methods
  97. /// <summary>
  98. /// Updates the background screen. Unlike most screens, this should not
  99. /// transition off even if it has been covered by another screen: it is
  100. /// supposed to be covered, after all! This overload forces the
  101. /// coveredByOtherScreen parameter to false in order to stop the base
  102. /// Update method wanting to transition off.
  103. /// </summary>
  104. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  105. bool coveredByOtherScreen)
  106. {
  107. base.Update(gameTime, otherScreenHasFocus, false);
  108. }
  109. #endregion
  110. #region Drawing Methods
  111. /// <summary>
  112. /// Draws the background screen.
  113. /// </summary>
  114. public override void Draw(GameTime gameTime)
  115. {
  116. if (starfield != null)
  117. {
  118. // update the parallax movement
  119. movement += gameTime.ElapsedGameTime.TotalSeconds;
  120. Vector2 position = Vector2.Multiply(new Vector2(
  121. (float)Math.Cos(movement / starsParallaxPeriod),
  122. (float)Math.Sin(movement / starsParallaxPeriod)),
  123. starsParallaxAmplitude);
  124. // draw the stars
  125. starfield.Draw(position);
  126. }
  127. // draw the title texture
  128. if (titleTexture != null)
  129. {
  130. Vector2 titlePosition = new Vector2(
  131. ScreenManager.TitleSafeArea.X +
  132. (ScreenManager.TitleSafeArea.Width - titleTexture.Width) / 2f,
  133. ScreenManager.TitleSafeArea.Y +
  134. ScreenManager.TitleSafeArea.Height * 0.05f);
  135. titlePosition.Y -= (float)Math.Pow(TransitionPosition, 2) *
  136. titlePosition.Y;
  137. ScreenManager.SpriteBatch.Begin();
  138. ScreenManager.SpriteBatch.Draw(titleTexture, titlePosition,
  139. new Color(255, 255, 255, TransitionAlpha));
  140. ScreenManager.SpriteBatch.End();
  141. }
  142. }
  143. #endregion
  144. #region IDisposable Implementation
  145. /// <summary>
  146. /// Finalizes the BackgroundScreen object, calls Dispose(false)
  147. /// </summary>
  148. ~BackgroundScreen()
  149. {
  150. Dispose(false);
  151. }
  152. /// <summary>
  153. /// Disposes the BackgroundScreen object.
  154. /// </summary>
  155. public void Dispose()
  156. {
  157. Dispose(true);
  158. GC.SuppressFinalize(this);
  159. }
  160. /// <summary>
  161. /// Disposes this object.
  162. /// </summary>
  163. /// <param name="disposing">
  164. /// True if this method was called as part of the Dispose method.
  165. /// </param>
  166. protected virtual void Dispose(bool disposing)
  167. {
  168. if (disposing)
  169. {
  170. lock (this)
  171. {
  172. if (starfield != null)
  173. {
  174. starfield.Dispose();
  175. starfield = null;
  176. }
  177. }
  178. }
  179. }
  180. #endregion
  181. }
  182. }