FullScreenSplash.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // FullScreenSplash.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 System.Collections.Generic;
  12. using System.Text;
  13. using System.Diagnostics;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using Microsoft.Xna.Framework.Input;
  17. #endregion
  18. namespace Spacewar
  19. {
  20. /// <summary>
  21. /// FullScreenSplash will show and close with a press of the A button or by a particular timeout
  22. /// Its behaviour can be changed by overriding the UpdateMethod
  23. /// </summary>
  24. public class FullScreenSplash : Screen
  25. {
  26. private string textureName;
  27. private double timeout;
  28. private double endTime = -1;
  29. private GameState nextState = GameState.None;
  30. /// <summary>
  31. /// Creates a FullScreenSplash with just a texture. State handling must be dealt with in an overridden Update()
  32. /// </summary>
  33. /// <param name="textureName">The texture to fill the screen with</param>
  34. protected FullScreenSplash(Game game, string textureName)
  35. : base(game)
  36. {
  37. setTexture(textureName);
  38. }
  39. /// <summary>
  40. /// Creates a FullScreenSplash with a texture, a timeout and the next state to transition to
  41. /// </summary>
  42. /// <param name="textureName">The texture to fill the screen with</param>
  43. /// <param name="timeout">Time in seconds to display</param>
  44. /// <param name="nextState">The gamestate to transition to</param>
  45. public FullScreenSplash(Game game, string textureName, TimeSpan timeoutSpan, GameState nextState)
  46. : base(game)
  47. {
  48. setTexture(textureName);
  49. timeout = timeoutSpan.TotalSeconds;
  50. this.nextState = nextState;
  51. }
  52. private void setTexture(string textureName)
  53. {
  54. this.textureName = textureName;
  55. }
  56. /// <summary>
  57. /// Checks for timeout or 'A' and moves to next state if that happens
  58. /// </summary>
  59. /// <param name="time">Gametime</param>
  60. /// <param name="elapsedTime">Elapsed time since last call</param>
  61. /// <returns>If the state has changed return the new state here - otherwise GameState.None</returns>
  62. public override GameState Update(TimeSpan time, TimeSpan elapsedTime)
  63. {
  64. if (endTime < 0)
  65. {
  66. endTime = time.TotalSeconds + timeout;
  67. }
  68. //If there is a state to progress to then wait for the timer or 'A'
  69. if (nextState != GameState.None &&
  70. ((timeout != 0 && time.TotalSeconds > endTime)
  71. || XInputHelper.GamePads[PlayerIndex.One].APressed
  72. || XInputHelper.GamePads[PlayerIndex.Two].APressed))
  73. {
  74. return nextState;
  75. }
  76. else
  77. {
  78. return base.Update(time, elapsedTime);
  79. }
  80. }
  81. /// <summary>
  82. /// Renders the full screen texture
  83. /// </summary>
  84. public override void Render()
  85. {
  86. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  87. GraphicsDevice device = graphicsService.GraphicsDevice;
  88. SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque);
  89. device.DepthStencilState = DepthStencilState.DepthRead;
  90. SpriteBatch.Draw(SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + textureName), Vector2.Zero, null, Color.White);
  91. SpriteBatch.End();
  92. //Render the backdrop
  93. base.Render();
  94. }
  95. }
  96. }