BackgroundScreen.cs 3.3 KB

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