- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/NewGamePhysics/StateManager/ScreenManager.cs

    00001 //-----------------------------------------------------------------------------
    00002 // ScreenManager.cs
    00003 // (Based on XNA Community Game Platform Demo, Microsoft Corp., 2008)
    00004 //-----------------------------------------------------------------------------
    00005
    00006 namespace NewGamePhysics.StateManager
    00007 {
    00008     using System.Collections.Generic;
    00009     using System.Diagnostics;
    00010
    00011     using Microsoft.Xna.Framework;
    00012     using Microsoft.Xna.Framework.Audio;
    00013     using Microsoft.Xna.Framework.Content;
    00014     using Microsoft.Xna.Framework.Graphics;
    00015     using Microsoft.Xna.Framework.Media;
    00016
    00017     using NewGamePhysics.Utilities;
    00018
    00025     public class ScreenManager : DrawableGameComponent
    00026     {
    00027         #region Fields
    00028
    00032         private List<GameScreen> screens = new List<GameScreen>();
    00033
    00037         private List<GameScreen> screensToUpdate = new List<GameScreen>();
    00038
    00042         private InputState input = new InputState();
    00043
    00047         private SpriteBatch spriteBatch;
    00048
    00052         private PrimitiveBatch primitiveBatch;
    00053
    00057         private VideoPlayer videoPlayer;
    00058
    00062         private Dictionary<string, Texture2D> textures;
    00063
    00067         private Dictionary<string, SpriteFont> fonts;
    00068
    00072         private Dictionary<string, Model> models;
    00073
    00077         private Dictionary<string, SoundEffect> sounds;
    00078
    00082         private AudioEngine engine;
    00083
    00087         private SoundBank soundBank;
    00088
    00092         private WaveBank waveBank;
    00093
    00097         private bool isInitialized;
    00098
    00102         private bool traceEnabled;
    00103
    00104         #endregion
    00105
    00106         #region Properties
    00107
    00112         public SpriteBatch SpriteBatch
    00113         {
    00114             get { return this.spriteBatch; }
    00115         }
    00116
    00121         public PrimitiveBatch PrimitiveBatch
    00122         {
    00123             get { return this.primitiveBatch; }
    00124         }
    00125
    00130         public VideoPlayer VideoPlayer
    00131         {
    00132             get { return this.videoPlayer; }
    00133         }
    00134
    00138         public Dictionary<string, Texture2D> Textures
    00139         {
    00140             get { return this.textures; }
    00141         }
    00142
    00146         public Dictionary<string, SpriteFont> Fonts
    00147         {
    00148             get { return this.fonts; }
    00149         }
    00150
    00154         public Dictionary<string, Model> Models
    00155         {
    00156             get { return this.models; }
    00157         }
    00158
    00162         public Dictionary<string, SoundEffect> Sounds
    00163         {
    00164             get { return this.sounds; }
    00165         }
    00166
    00170         public SoundBank SoundBank
    00171         {
    00172             get { return this.soundBank; }
    00173         }
    00174
    00180         public bool TraceEnabled
    00181         {
    00182             get { return this.traceEnabled; }
    00183             set { this.traceEnabled = value; }
    00184         }
    00185
    00186         #endregion
    00187
    00188         #region Initialization
    00189
    00193         public ScreenManager(Game game)
    00194             : base(game)
    00195         {
    00196         }
    00197
    00201         public override void Initialize()
    00202         {
    00203             base.Initialize();
    00204
    00205             // Initialize audio objects.
    00206             this.engine = new AudioEngine("Content\\Sounds\\audio.xgs");
    00207             this.soundBank = new SoundBank(engine, "Content\\Sounds\\Sound Bank.xsb");
    00208             this.waveBank = new WaveBank(engine, "Content\\Sounds\\Wave Bank.xwb");
    00209
    00210             isInitialized = true;
    00211         }
    00212
    00216         protected override void LoadContent()
    00217         {
    00218             // Load content belonging to the screen manager.
    00219             ContentManager content = Game.Content;
    00220             this.spriteBatch = new SpriteBatch(GraphicsDevice);
    00221             this.primitiveBatch = new PrimitiveBatch(GraphicsDevice);
    00222             this.videoPlayer = new VideoPlayer();
    00223
    00224             // Prepare caches
    00225             this.textures = new Dictionary<string, Texture2D>();
    00226             this.fonts = new Dictionary<string, SpriteFont>();
    00227             this.models = new Dictionary<string, Model>();
    00228             this.sounds = new Dictionary<string, SoundEffect>();
    00229
    00230             // Prep font cache
    00231             this.fonts.Add("menu", content.Load<SpriteFont>(@"Fonts\menufont"));
    00232             this.fonts.Add("game", content.Load<SpriteFont>(@"Fonts\gamefont"));
    00233             this.fonts.Add("small", content.Load<SpriteFont>(@"Fonts\smallfont"));
    00234
    00235             // Prep texture cache
    00236             this.textures.Add("blank",content.Load<Texture2D>(@"Sprites\blank"));
    00237
    00238             // Tell each of the screens to load their content.
    00239             foreach (GameScreen screen in screens)
    00240             {
    00241                 screen.LoadContent();
    00242             }
    00243         }
    00244
    00248         protected override void UnloadContent()
    00249         {
    00250             // Tell each of the screens to unload their content.
    00251             foreach (GameScreen screen in screens)
    00252             {
    00253                 screen.UnloadContent();
    00254             }
    00255         }
    00256
    00257         #endregion
    00258
    00259         #region Update and Draw
    00260
    00264         public override void Update(GameTime gameTime)
    00265         {
    00266             // Read the keyboard and gamepad.
    00267             this.input.Update();
    00268
    00269             // Update the sound engine
    00270             this.engine.Update();
    00271
    00272             // Make a copy of the master screen list, to avoid confusion if
    00273             // the process of updating one screen adds or removes others.
    00274             screensToUpdate.Clear();
    00275
    00276             foreach (GameScreen screen in screens)
    00277             {
    00278                 screensToUpdate.Add(screen);
    00279             }
    00280
    00281             bool otherScreenHasFocus = !Game.IsActive;
    00282             bool coveredByOtherScreen = false;
    00283
    00284             // Loop as long as there are screens waiting to be updated.
    00285             while (screensToUpdate.Count > 0)
    00286             {
    00287                 // Pop the topmost screen off the waiting list.
    00288                 GameScreen screen = screensToUpdate[screensToUpdate.Count - 1];
    00289
    00290                 screensToUpdate.RemoveAt(screensToUpdate.Count - 1);
    00291
    00292                 // Update the screen.
    00293                 screen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
    00294
    00295                 if (screen.ScreenState == ScreenState.TransitionOn ||
    00296                     screen.ScreenState == ScreenState.Active)
    00297                 {
    00298                     // If this is the first active screen we came across,
    00299                     // give it a chance to handle input.
    00300                     if (!otherScreenHasFocus)
    00301                     {
    00302                         screen.HandleInput(input);
    00303
    00304                         otherScreenHasFocus = true;
    00305                     }
    00306
    00307                     // If this is an active non-popup, inform any subsequent
    00308                     // screens that they are covered by it.
    00309                     if (!screen.IsPopup)
    00310                     {
    00311                         coveredByOtherScreen = true;
    00312                     }
    00313                 }
    00314             }
    00315
    00316             // Print debug trace?
    00317             if (traceEnabled)
    00318             {
    00319                 TraceScreens();
    00320             }
    00321         }
    00322
    00326         void TraceScreens()
    00327         {
    00328             List<string> screenNames = new List<string>();
    00329
    00330             foreach (GameScreen screen in screens)
    00331             {
    00332                 screenNames.Add(screen.GetType().Name);
    00333             }
    00334
    00335             Trace.WriteLine(string.Join(", ", screenNames.ToArray()));
    00336         }
    00337
    00341         public override void Draw(GameTime gameTime)
    00342         {
    00343             foreach (GameScreen screen in screens)
    00344             {
    00345                 if (screen.ScreenState == ScreenState.Hidden)
    00346                 {
    00347                     continue;
    00348                 }
    00349
    00350                 screen.Draw(gameTime);
    00351             }
    00352         }
    00353
    00354         #endregion
    00355
    00356         #region Public Methods
    00357
    00364         public void AddTexture(string textureKey, string texturePath)
    00365         {
    00366             if (!this.textures.ContainsKey(textureKey))
    00367             {
    00368                 this.textures.Add(textureKey, Game.Content.Load<Texture2D>(texturePath));
    00369             }
    00370         }
    00371
    00378         public void AddFont(string fontKey, string fontPath)
    00379         {
    00380             if (!this.fonts.ContainsKey(fontKey))
    00381             {
    00382                 this.fonts.Add(fontKey, Game.Content.Load<SpriteFont>(fontPath));
    00383             }
    00384         }
    00385
    00392         public void AddSound(string soundKey, string soundPath)
    00393         {
    00394             if (!this.sounds.ContainsKey(soundKey))
    00395             {
    00396                 this.sounds.Add(soundKey, Game.Content.Load<SoundEffect>(soundPath));
    00397             }
    00398         }
    00399
    00403         public void AddScreen(GameScreen screen, PlayerIndex? controllingPlayer)
    00404         {
    00405             screen.ControllingPlayer = controllingPlayer;
    00406             screen.ScreenManager = this;
    00407             screen.IsExiting = false;
    00408
    00409             // If we have a graphics device, tell the screen to load content.
    00410             if (isInitialized)
    00411             {
    00412                 screen.LoadContent();
    00413             }
    00414
    00415             screens.Add(screen);
    00416         }
    00417
    00424         public void RemoveScreen(GameScreen screen)
    00425         {
    00426             // If we have a graphics device, tell the screen to unload content.
    00427             if (isInitialized)
    00428             {
    00429                 screen.UnloadContent();
    00430             }
    00431
    00432             screens.Remove(screen);
    00433             screensToUpdate.Remove(screen);
    00434         }
    00435
    00441         public GameScreen[] GetScreens()
    00442         {
    00443             return screens.ToArray();
    00444         }
    00445
    00450         public void FadeBackBufferToBlack(int alpha)
    00451         {
    00452             Viewport viewport = GraphicsDevice.Viewport;
    00453
    00454             spriteBatch.Begin();
    00455
    00456             spriteBatch.Draw(this.Textures["blank"],
    00457                              new Rectangle(0, 0, viewport.Width, viewport.Height),
    00458                              new Color(0, 0, 0, (byte)alpha));
    00459
    00460             spriteBatch.End();
    00461         }
    00462
    00468         public void SetVolume(string category, float volume)
    00469         {
    00470             AudioCategory musicCategory = this.engine.GetCategory(category);
    00471             if (null != musicCategory)
    00472             {
    00473                 musicCategory.SetVolume(volume);
    00474             }
    00475         }
    00476
    00477         #endregion
    00478     }
    00479 }