LogoScreen.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Content;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6. namespace FarseerPhysics.SamplesFramework
  7. {
  8. public class LogoScreen : GameScreen
  9. {
  10. private const float LogoScreenHeightRatio = 4f / 6f;
  11. private const float LogoWidthHeightRatio = 1.4f;
  12. private ContentManager _content;
  13. private Rectangle _destination;
  14. private TimeSpan _duration;
  15. private Texture2D _farseerLogoTexture;
  16. public LogoScreen(TimeSpan duration)
  17. {
  18. _duration = duration;
  19. TransitionOffTime = TimeSpan.FromSeconds(2.0);
  20. }
  21. /// <summary>
  22. /// Loads graphics content for this screen. The background texture is quite
  23. /// big, so we use our own local ContentManager to load it. This allows us
  24. /// to unload before going from the menus into the game itself, wheras if we
  25. /// used the shared ContentManager provided by the Game class, the content
  26. /// would remain loaded forever.
  27. /// </summary>
  28. public override void LoadContent()
  29. {
  30. if (_content == null)
  31. {
  32. _content = new ContentManager(ScreenManager.Game.Services, "Content");
  33. }
  34. _farseerLogoTexture = _content.Load<Texture2D>("Common/logo");
  35. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  36. int rectHeight = (int)(viewport.Height * LogoScreenHeightRatio);
  37. int rectWidth = (int)(rectHeight * LogoWidthHeightRatio);
  38. int posX = viewport.Bounds.Center.X - rectWidth / 2;
  39. int posY = viewport.Bounds.Center.Y - rectHeight / 2;
  40. _destination = new Rectangle(posX, posY, rectWidth, rectHeight);
  41. }
  42. /// <summary>
  43. /// Unloads graphics content for this screen.
  44. /// </summary>
  45. public override void UnloadContent()
  46. {
  47. _content.Unload();
  48. }
  49. public override void HandleInput(InputHelper input, GameTime gameTime)
  50. {
  51. if (input.KeyboardState.GetPressedKeys().Length > 0 ||
  52. input.GamePadState.IsButtonDown(Buttons.A | Buttons.Start | Buttons.Back) ||
  53. input.MouseState.LeftButton == ButtonState.Pressed)
  54. {
  55. _duration = TimeSpan.Zero;
  56. }
  57. }
  58. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  59. bool coveredByOtherScreen)
  60. {
  61. _duration -= gameTime.ElapsedGameTime;
  62. if (_duration <= TimeSpan.Zero)
  63. {
  64. ExitScreen();
  65. }
  66. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  67. }
  68. public override void Draw(GameTime gameTime)
  69. {
  70. ScreenManager.GraphicsDevice.Clear(Color.White);
  71. ScreenManager.SpriteBatch.Begin();
  72. ScreenManager.SpriteBatch.Draw(_farseerLogoTexture, _destination, Color.White);
  73. ScreenManager.SpriteBatch.End();
  74. }
  75. }
  76. }