| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- #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
- {
- /// <summary>
- /// 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.
- /// </summary>
- public class GameStateManagementGame : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- ScreenManager screenManager;
- ScreenFactory screenFactory;
- /// <summary>
- /// The main game constructor.
- /// </summary>
- 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<Microsoft.Phone.Shell.LaunchingEventArgs>(GameLaunching);
- Microsoft.Phone.Shell.PhoneApplicationService.Current.Activated +=
- new EventHandler<Microsoft.Phone.Shell.ActivatedEventArgs>(GameActivated);
- Microsoft.Phone.Shell.PhoneApplicationService.Current.Deactivated +=
- new EventHandler<Microsoft.Phone.Shell.DeactivatedEventArgs>(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
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- 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
- /// <summary>
- /// Helper method to the initialize the game to be a portrait game.
- /// </summary>
- private void InitializePortraitGraphics()
- {
- graphics.PreferredBackBufferWidth = 480;
- graphics.PreferredBackBufferHeight = 800;
- }
- /// <summary>
- /// Helper method to initialize the game to be a landscape game.
- /// </summary>
- 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
- }
- }
|