BackgroundScreen.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #if IPHONE
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Content;
  14. using Microsoft.Xna.Framework.Graphics;
  15. #else
  16. using Microsoft.Xna.Framework;
  17. using Microsoft.Xna.Framework.Content;
  18. using Microsoft.Xna.Framework.Graphics;
  19. #endif
  20. #endregion
  21. namespace Microsoft.Xna.Samples.GameStateManagement
  22. {
  23. /// <summary>
  24. /// The background screen sits behind all the other menu screens.
  25. /// It draws a background image that remains fixed in place regardless
  26. /// of whatever transitions the screens on top of it may be doing.
  27. /// </summary>
  28. class BackgroundScreen : GameScreen
  29. {
  30. #region Fields
  31. ContentManager content;
  32. Texture2D backgroundTexture;
  33. #endregion
  34. #region Initialization
  35. /// <summary>
  36. /// Constructor.
  37. /// </summary>
  38. public BackgroundScreen()
  39. {
  40. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  41. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  42. }
  43. /// <summary>
  44. /// Loads graphics content for this screen. The background texture is quite
  45. /// big, so we use our own local ContentManager to load it. This allows us
  46. /// to unload before going from the menus into the game itself, wheras if we
  47. /// used the shared ContentManager provided by the Game class, the content
  48. /// would remain loaded forever.
  49. /// </summary>
  50. public override void LoadContent()
  51. {
  52. if (content == null)
  53. content = new ContentManager(ScreenManager.Game.Services, "Content");
  54. backgroundTexture = content.Load<Texture2D>("background");
  55. }
  56. /// <summary>
  57. /// Unloads graphics content for this screen.
  58. /// </summary>
  59. public override void UnloadContent()
  60. {
  61. content.Unload();
  62. }
  63. #endregion
  64. #region Update and Draw
  65. /// <summary>
  66. /// Updates the background screen. Unlike most screens, this should not
  67. /// transition off even if it has been covered by another screen: it is
  68. /// supposed to be covered, after all! This overload forces the
  69. /// coveredByOtherScreen parameter to false in order to stop the base
  70. /// Update method wanting to transition off.
  71. /// </summary>
  72. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  73. bool coveredByOtherScreen)
  74. {
  75. base.Update(gameTime, otherScreenHasFocus, false);
  76. }
  77. /// <summary>
  78. /// Draws the background screen.
  79. /// </summary>
  80. public override void Draw(GameTime gameTime)
  81. {
  82. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  83. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  84. Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);
  85. byte fade = TransitionAlpha;
  86. spriteBatch.Begin(SpriteSortMode.Deferred,BlendState.Opaque);
  87. spriteBatch.Draw(backgroundTexture, fullscreen,
  88. new Color(fade, fade, fade));
  89. spriteBatch.End();
  90. }
  91. #endregion
  92. }
  93. }