#region File Description //----------------------------------------------------------------------------- // Screen.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; #endregion namespace Spacewar { /// /// Screen represents a unit of rendering for the game, generally transitional point such /// as splash screens, selection screens and the actual game levels. /// abstract public class Screen { /// /// The root of the scene graph for this screen /// protected SceneItem scene = null; /// /// Overlay points to a screen that will be drawn AFTER this one, more than likely overlaying it /// protected Screen overlay; protected SpriteBatch batch = null; protected Game game = null; public Game GameInstance { get { return game; } } public SpriteBatch SpriteBatch { get { return batch; } } public Screen(Game game) { this.game = game; this.scene = new SceneItem(game); if (game != null) { IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)game.Services.GetService(typeof(IGraphicsDeviceService)); batch = new SpriteBatch(graphicsService.GraphicsDevice); } } /// /// Update changes the layout and positions based on input or other variables /// Base class updates all items in the scene then calls any overlays to get them to render themselves /// /// Total game time since it was started /// Elapsed game time since the last call to update /// The next gamestate to transition to. Default is the return value of an overlay or NONE. Override Update if you want to change this behaviour public virtual GameState Update(TimeSpan time, TimeSpan elapsedTime) { //Update the Scene scene.Update(time, elapsedTime); //Default is no state changes, override the class if you want a different state return (overlay == null) ? GameState.None : overlay.Update(time, elapsedTime); } /// /// Renders this scene. The base class renders everything in the sceen graph and then calls any overlays to /// get them to render themselves /// public virtual void Render() { //Render this scene then any overlays scene.Render(); if (overlay != null) overlay.Render(); } /// /// Tidies up the scene. Base class does nothing but calls the overlays /// public virtual void Shutdown() { if (overlay != null) overlay.Shutdown(); if (batch != null) { batch.Dispose(); batch = null; } } /// /// OnCreateDevice is called when the device is created /// public virtual void OnCreateDevice() { //Re-Create the Sprite Batch! IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)game.Services.GetService(typeof(IGraphicsDeviceService)); batch = new SpriteBatch(graphicsService.GraphicsDevice); } } }