BackgroundScreen.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.Graphics;
  10. using Microsoft.Xna.Framework.Input;
  11. namespace AlienGameSample
  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. ///
  18. /// A bit boring right now, but needed something in place...
  19. /// </summary>
  20. class BackgroundScreen : GameScreen
  21. {
  22. Texture2D title;
  23. Texture2D background;
  24. private Texture2D gamepadTexture;
  25. /// <summary>
  26. /// Constructor.
  27. /// </summary>
  28. public BackgroundScreen()
  29. {
  30. TransitionOnTime = TimeSpan.FromSeconds(0.0);
  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. title = ScreenManager.Game.Content.Load<Texture2D>("title");
  43. background = ScreenManager.Game.Content.Load<Texture2D>("background");
  44. // Setup virtual gamepad
  45. gamepadTexture = ScreenManager.Game.Content.Load<Texture2D>("gamepad");
  46. ButtonDefinition BButton = new ButtonDefinition();
  47. BButton.Texture = gamepadTexture;
  48. BButton.Position = new Vector2(270,240);
  49. BButton.Type = Buttons.Back;
  50. BButton.TextureRect = new Rectangle(72,77,36,36);
  51. ButtonDefinition AButton = new ButtonDefinition();
  52. AButton.Texture = gamepadTexture;
  53. AButton.Position = new Vector2(30,420);
  54. AButton.Type = Buttons.A;
  55. AButton.TextureRect = new Rectangle(73,114,36,36);
  56. GamePad.ButtonsDefinitions.Add(BButton);
  57. GamePad.ButtonsDefinitions.Add(AButton);
  58. ThumbStickDefinition thumbStick = new ThumbStickDefinition();
  59. thumbStick.Position = new Vector2(220,400);
  60. thumbStick.Texture = gamepadTexture;
  61. thumbStick.TextureRect = new Rectangle(2,2,68,68);
  62. GamePad.LeftThumbStickDefinition = thumbStick;
  63. }
  64. /// <summary>
  65. /// Updates the background screen. Unlike most screens, this should not
  66. /// transition off even if it has been covered by another screen: it is
  67. /// supposed to be covered, after all! This overload forces the
  68. /// coveredByOtherScreen parameter to false in order to stop the base
  69. /// Update method wanting to transition off.
  70. /// </summary>
  71. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  72. bool coveredByOtherScreen)
  73. {
  74. base.Update(gameTime, otherScreenHasFocus, false);
  75. }
  76. /// <summary>
  77. /// Draws the background screen.
  78. /// </summary>
  79. public override void Draw(GameTime gameTime)
  80. {
  81. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  82. // Make the menu slide into place during transitions, using a
  83. // power curve to make things look more interesting (this makes
  84. // the movement slow down as it nears the end).
  85. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  86. spriteBatch.Begin();
  87. // Background
  88. spriteBatch.Draw(background,new Rectangle(0,0,320,480),new Color(255, 255, 255, TransitionAlpha));
  89. // Title
  90. spriteBatch.Draw(title, new Vector2((320-title.Width)/2, 60), new Color(255, 255, 255, TransitionAlpha));
  91. #if TARGET_IPHONE_SIMULATOR
  92. // Draw the GamePad
  93. GamePad.Draw(gameTime,spriteBatch);
  94. #endif
  95. spriteBatch.End();
  96. }
  97. }
  98. }