Screen.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Screen.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 Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Graphics;
  15. #endregion
  16. namespace Spacewar
  17. {
  18. /// <summary>
  19. /// Screen represents a unit of rendering for the game, generally transitional point such
  20. /// as splash screens, selection screens and the actual game levels.
  21. /// </summary>
  22. abstract public class Screen
  23. {
  24. /// <summary>
  25. /// The root of the scene graph for this screen
  26. /// </summary>
  27. protected SceneItem scene = null;
  28. /// <summary>
  29. /// Overlay points to a screen that will be drawn AFTER this one, more than likely overlaying it
  30. /// </summary>
  31. protected Screen overlay;
  32. protected SpriteBatch batch = null;
  33. protected Game game = null;
  34. public Game GameInstance
  35. {
  36. get
  37. {
  38. return game;
  39. }
  40. }
  41. public SpriteBatch SpriteBatch
  42. {
  43. get
  44. {
  45. return batch;
  46. }
  47. }
  48. public Screen(Game game)
  49. {
  50. this.game = game;
  51. this.scene = new SceneItem(game);
  52. if (game != null)
  53. {
  54. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)game.Services.GetService(typeof(IGraphicsDeviceService));
  55. batch = new SpriteBatch(graphicsService.GraphicsDevice);
  56. }
  57. }
  58. /// <summary>
  59. /// Update changes the layout and positions based on input or other variables
  60. /// Base class updates all items in the scene then calls any overlays to get them to render themselves
  61. /// </summary>
  62. /// <param name="time">Total game time since it was started</param>
  63. /// <param name="elapsedTime">Elapsed game time since the last call to update</param>
  64. /// <returns>The next gamestate to transition to. Default is the return value of an overlay or NONE. Override Update if you want to change this behaviour</returns>
  65. public virtual GameState Update(TimeSpan time, TimeSpan elapsedTime)
  66. {
  67. //Update the Scene
  68. scene.Update(time, elapsedTime);
  69. //Default is no state changes, override the class if you want a different state
  70. return (overlay == null) ? GameState.None : overlay.Update(time, elapsedTime);
  71. }
  72. /// <summary>
  73. /// Renders this scene. The base class renders everything in the sceen graph and then calls any overlays to
  74. /// get them to render themselves
  75. /// </summary>
  76. public virtual void Render()
  77. {
  78. //Render this scene then any overlays
  79. scene.Render();
  80. if (overlay != null)
  81. overlay.Render();
  82. }
  83. /// <summary>
  84. /// Tidies up the scene. Base class does nothing but calls the overlays
  85. /// </summary>
  86. public virtual void Shutdown()
  87. {
  88. if (overlay != null)
  89. overlay.Shutdown();
  90. if (batch != null)
  91. {
  92. batch.Dispose();
  93. batch = null;
  94. }
  95. }
  96. /// <summary>
  97. /// OnCreateDevice is called when the device is created
  98. /// </summary>
  99. public virtual void OnCreateDevice()
  100. {
  101. //Re-Create the Sprite Batch!
  102. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)game.Services.GetService(typeof(IGraphicsDeviceService));
  103. batch = new SpriteBatch(graphicsService.GraphicsDevice);
  104. }
  105. }
  106. }