2
0

BackgroundScreen.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. namespace FarseerPhysics.SamplesFramework
  5. {
  6. /// <summary>
  7. /// The background screen sits behind all the other menu screens.
  8. /// It draws a background image that remains fixed in place regardless
  9. /// of whatever transitions the screens on top of it may be doing.
  10. /// </summary>
  11. public class BackgroundScreen : GameScreen
  12. {
  13. private const float LogoScreenHeightRatio = 0.25f;
  14. private const float LogoScreenBorderRatio = 0.0375f;
  15. private const float LogoWidthHeightRatio = 1.4f;
  16. private Texture2D _backgroundTexture;
  17. private Rectangle _logoDestination;
  18. private Texture2D _logoTexture;
  19. private Rectangle _viewport;
  20. /// <summary>
  21. /// Constructor.
  22. /// </summary>
  23. public BackgroundScreen()
  24. {
  25. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  26. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  27. }
  28. public override void LoadContent()
  29. {
  30. _logoTexture = ScreenManager.Content.Load<Texture2D>("Common/logo");
  31. _backgroundTexture = ScreenManager.Content.Load<Texture2D>("Common/gradient");
  32. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  33. Vector2 logoSize = new Vector2();
  34. logoSize.Y = viewport.Height * LogoScreenHeightRatio;
  35. logoSize.X = logoSize.Y * LogoWidthHeightRatio;
  36. float border = viewport.Height * LogoScreenBorderRatio;
  37. Vector2 logoPosition = new Vector2(viewport.Width - border - logoSize.X,
  38. viewport.Height - border - logoSize.Y);
  39. _logoDestination = new Rectangle((int)logoPosition.X, (int)logoPosition.Y, (int)logoSize.X,
  40. (int)logoSize.Y);
  41. _viewport = viewport.Bounds;
  42. }
  43. /// <summary>
  44. /// Updates the background screen. Unlike most screens, this should not
  45. /// transition off even if it has been covered by another screen: it is
  46. /// supposed to be covered, after all! This overload forces the
  47. /// coveredByOtherScreen parameter to false in order to stop the base
  48. /// Update method wanting to transition off.
  49. /// </summary>
  50. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  51. bool coveredByOtherScreen)
  52. {
  53. base.Update(gameTime, otherScreenHasFocus, false);
  54. }
  55. /// <summary>
  56. /// Draws the background screen.
  57. /// </summary>
  58. public override void Draw(GameTime gameTime)
  59. {
  60. ScreenManager.SpriteBatch.Begin();
  61. ScreenManager.SpriteBatch.Draw(_backgroundTexture, _viewport, Color.White);
  62. ScreenManager.SpriteBatch.Draw(_logoTexture, _logoDestination, Color.White * 0.6f);
  63. ScreenManager.SpriteBatch.End();
  64. }
  65. }
  66. }