Game1.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using Tutorial013.States;
  5. namespace Tutorial013
  6. {
  7. /// <summary>
  8. /// This is the main type for your game.
  9. /// </summary>
  10. public class Game1 : Game
  11. {
  12. GraphicsDeviceManager graphics;
  13. SpriteBatch spriteBatch;
  14. private State _currentState;
  15. private State _nextState;
  16. public void ChangeState(State state)
  17. {
  18. _nextState = state;
  19. }
  20. public Game1()
  21. {
  22. graphics = new GraphicsDeviceManager(this);
  23. Content.RootDirectory = "Content";
  24. }
  25. /// <summary>
  26. /// Allows the game to perform any initialization it needs to before starting to run.
  27. /// This is where it can query for any required services and load any non-graphic
  28. /// related content. Calling base.Initialize will enumerate through any components
  29. /// and initialize them as well.
  30. /// </summary>
  31. protected override void Initialize()
  32. {
  33. IsMouseVisible = true;
  34. base.Initialize();
  35. }
  36. /// <summary>
  37. /// LoadContent will be called once per game and is the place to load
  38. /// all of your content.
  39. /// </summary>
  40. protected override void LoadContent()
  41. {
  42. // Create a new SpriteBatch, which can be used to draw textures.
  43. spriteBatch = new SpriteBatch(GraphicsDevice);
  44. _currentState = new MenuState(this, graphics.GraphicsDevice, Content);
  45. }
  46. /// <summary>
  47. /// UnloadContent will be called once per game and is the place to unload
  48. /// game-specific content.
  49. /// </summary>
  50. protected override void UnloadContent()
  51. {
  52. // TODO: Unload any non ContentManager content here
  53. }
  54. /// <summary>
  55. /// Allows the game to run logic such as updating the world,
  56. /// checking for collisions, gathering input, and playing audio.
  57. /// </summary>
  58. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  59. protected override void Update(GameTime gameTime)
  60. {
  61. if(_nextState != null)
  62. {
  63. _currentState = _nextState;
  64. _nextState = null;
  65. }
  66. _currentState.Update(gameTime);
  67. _currentState.PostUpdate(gameTime);
  68. base.Update(gameTime);
  69. }
  70. /// <summary>
  71. /// This is called when the game should draw itself.
  72. /// </summary>
  73. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  74. protected override void Draw(GameTime gameTime)
  75. {
  76. GraphicsDevice.Clear(Color.CornflowerBlue);
  77. _currentState.Draw(gameTime, spriteBatch);
  78. base.Draw(gameTime);
  79. }
  80. }
  81. }