BackgroundScreen.cs 3.5 KB

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