Game.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Game.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using GameStateManagement;
  11. using Microsoft.Xna.Framework;
  12. namespace GameStateManagementSample
  13. {
  14. /// <summary>
  15. /// Sample showing how to manage different game states, with transitions
  16. /// between menu screens, a loading screen, the game itself, and a pause
  17. /// menu. This main game class is extremely simple: all the interesting
  18. /// stuff happens in the ScreenManager component.
  19. /// </summary>
  20. public class GameStateManagementGame : Microsoft.Xna.Framework.Game
  21. {
  22. GraphicsDeviceManager graphics;
  23. ScreenManager screenManager;
  24. ScreenFactory screenFactory;
  25. /// <summary>
  26. /// The main game constructor.
  27. /// </summary>
  28. public GameStateManagementGame()
  29. {
  30. Content.RootDirectory = "Content";
  31. graphics = new GraphicsDeviceManager(this);
  32. TargetElapsedTime = TimeSpan.FromTicks(333333);
  33. #if WINDOWS_PHONE
  34. graphics.IsFullScreen = true;
  35. // Choose whether you want a landscape or portait game by using one of the two helper functions.
  36. InitializeLandscapeGraphics();
  37. // InitializePortraitGraphics();
  38. #endif
  39. // Create the screen factory and add it to the Services
  40. screenFactory = new ScreenFactory();
  41. Services.AddService(typeof(IScreenFactory), screenFactory);
  42. // Create the screen manager component.
  43. screenManager = new ScreenManager(this);
  44. Components.Add(screenManager);
  45. #if WINDOWS_PHONE
  46. // Hook events on the PhoneApplicationService so we're notified of the application's life cycle
  47. Microsoft.Phone.Shell.PhoneApplicationService.Current.Launching +=
  48. new EventHandler<Microsoft.Phone.Shell.LaunchingEventArgs>(GameLaunching);
  49. Microsoft.Phone.Shell.PhoneApplicationService.Current.Activated +=
  50. new EventHandler<Microsoft.Phone.Shell.ActivatedEventArgs>(GameActivated);
  51. Microsoft.Phone.Shell.PhoneApplicationService.Current.Deactivated +=
  52. new EventHandler<Microsoft.Phone.Shell.DeactivatedEventArgs>(GameDeactivated);
  53. #else
  54. // On Windows and Xbox we just add the initial screens
  55. AddInitialScreens();
  56. #endif
  57. }
  58. private void AddInitialScreens()
  59. {
  60. // Activate the first screens.
  61. screenManager.AddScreen(new BackgroundScreen(), null);
  62. // We have different menus for Windows Phone to take advantage of the touch interface
  63. #if WINDOWS_PHONE
  64. screenManager.AddScreen(new PhoneMainMenuScreen(), null);
  65. #else
  66. screenManager.AddScreen(new MainMenuScreen(), null);
  67. #endif
  68. }
  69. /// <summary>
  70. /// This is called when the game should draw itself.
  71. /// </summary>
  72. protected override void Draw(GameTime gameTime)
  73. {
  74. graphics.GraphicsDevice.Clear(Color.Black);
  75. // The real drawing happens inside the screen manager component.
  76. base.Draw(gameTime);
  77. }
  78. #if WINDOWS_PHONE
  79. /// <summary>
  80. /// Helper method to the initialize the game to be a portrait game.
  81. /// </summary>
  82. private void InitializePortraitGraphics()
  83. {
  84. graphics.PreferredBackBufferWidth = 480;
  85. graphics.PreferredBackBufferHeight = 800;
  86. }
  87. /// <summary>
  88. /// Helper method to initialize the game to be a landscape game.
  89. /// </summary>
  90. private void InitializeLandscapeGraphics()
  91. {
  92. graphics.PreferredBackBufferWidth = 800;
  93. graphics.PreferredBackBufferHeight = 480;
  94. }
  95. void GameLaunching(object sender, Microsoft.Phone.Shell.LaunchingEventArgs e)
  96. {
  97. AddInitialScreens();
  98. }
  99. void GameActivated(object sender, Microsoft.Phone.Shell.ActivatedEventArgs e)
  100. {
  101. // Try to deserialize the screen manager
  102. if (!screenManager.Activate(e.IsApplicationInstancePreserved))
  103. {
  104. // If the screen manager fails to deserialize, add the initial screens
  105. AddInitialScreens();
  106. }
  107. }
  108. void GameDeactivated(object sender, Microsoft.Phone.Shell.DeactivatedEventArgs e)
  109. {
  110. // Serialize the screen manager when the game deactivated
  111. screenManager.Deactivate();
  112. }
  113. #endif
  114. }
  115. }