BackgroundScreen.cs 3.5 KB

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