#region File Description //----------------------------------------------------------------------------- // Game.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion using System; using GameStateManagement; using Microsoft.Xna.Framework; namespace GameStateManagementSample { /// /// Sample showing how to manage different game states, with transitions /// between menu screens, a loading screen, the game itself, and a pause /// menu. This main game class is extremely simple: all the interesting /// stuff happens in the ScreenManager component. /// public class GameStateManagementGame : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; ScreenManager screenManager; ScreenFactory screenFactory; /// /// The main game constructor. /// public GameStateManagementGame() { Content.RootDirectory = "Content"; graphics = new GraphicsDeviceManager(this); TargetElapsedTime = TimeSpan.FromTicks(333333); #if WINDOWS_PHONE graphics.IsFullScreen = true; // Choose whether you want a landscape or portait game by using one of the two helper functions. InitializeLandscapeGraphics(); // InitializePortraitGraphics(); #endif // Create the screen factory and add it to the Services screenFactory = new ScreenFactory(); Services.AddService(typeof(IScreenFactory), screenFactory); // Create the screen manager component. screenManager = new ScreenManager(this); Components.Add(screenManager); #if WINDOWS_PHONE // Hook events on the PhoneApplicationService so we're notified of the application's life cycle Microsoft.Phone.Shell.PhoneApplicationService.Current.Launching += new EventHandler(GameLaunching); Microsoft.Phone.Shell.PhoneApplicationService.Current.Activated += new EventHandler(GameActivated); Microsoft.Phone.Shell.PhoneApplicationService.Current.Deactivated += new EventHandler(GameDeactivated); #else // On Windows and Xbox we just add the initial screens AddInitialScreens(); #endif } private void AddInitialScreens() { // Activate the first screens. screenManager.AddScreen(new BackgroundScreen(), null); // We have different menus for Windows Phone to take advantage of the touch interface #if WINDOWS_PHONE screenManager.AddScreen(new PhoneMainMenuScreen(), null); #else screenManager.AddScreen(new MainMenuScreen(), null); #endif } /// /// This is called when the game should draw itself. /// protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.Black); // The real drawing happens inside the screen manager component. base.Draw(gameTime); } #if WINDOWS_PHONE /// /// Helper method to the initialize the game to be a portrait game. /// private void InitializePortraitGraphics() { graphics.PreferredBackBufferWidth = 480; graphics.PreferredBackBufferHeight = 800; } /// /// Helper method to initialize the game to be a landscape game. /// private void InitializeLandscapeGraphics() { graphics.PreferredBackBufferWidth = 800; graphics.PreferredBackBufferHeight = 480; } void GameLaunching(object sender, Microsoft.Phone.Shell.LaunchingEventArgs e) { AddInitialScreens(); } void GameActivated(object sender, Microsoft.Phone.Shell.ActivatedEventArgs e) { // Try to deserialize the screen manager if (!screenManager.Activate(e.IsApplicationInstancePreserved)) { // If the screen manager fails to deserialize, add the initial screens AddInitialScreens(); } } void GameDeactivated(object sender, Microsoft.Phone.Shell.DeactivatedEventArgs e) { // Serialize the screen manager when the game deactivated screenManager.Deactivate(); } #endif } }