BackgroundScreen.cs 3.1 KB

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