BackgroundScreen.cs 6.3 KB

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