//--------------------------------------------------------------------------------------------------------------------------------------------------- // // Copyright (C)2007 DarkWynter Studios. All rights reserved. // //--------------------------------------------------------------------------------------------------------------------------------------------------- // {Contact : darkwynter.com for licensing information //--------------------------------------------------------------------------------------------------------------------------------------------------- namespace DarkWynterEngine { #region Using Statements using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; using System.Threading; using System.Net; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; using System.Xml; #endregion using Audio; using Menus; using Globals; using Draw; using Utilities; using UserInterface; using Controllers; using ObjectLib; using GameObjects; using Physics; using PhysicsGpu; // MOVE GPUPROCESSOR HERE /// /// Base class for creating a game using the DarkWynter Engine. /// DarkWynterEngine extends object modification to the user through XML. /// DakrWynterEngine extends functionality to the user through overridable objects. /// public class DarkWynterGame: Microsoft.Xna.Framework.Game { #region Properties protected List gameObjectTypes; /// /// A scenegraph/collections class, contains all GameObject instants /// protected ObjectLibrary objectLibrary; /// /// Handles collision detection. /// protected Collision collision; /// /// Renders the scene once for each viewport. /// protected Renderer renderer; /// /// Menu System Componenets /// protected MenuSystem menuSystem; /// /// Interface to GPU Uniform variables /// private ShaderParameters shaderParameters; /// /// FPS - Frames Per Second /// public FPSCounter fps; /// /// Controller /// public static Controller playerController; /// /// Controller for the MenuSystem /// public static Controller menuController; /// /// Screen Saver Shader /// private ScreenSaver screenSaver; #endregion #region Methods // --------------- Constructor ----------------- /// /// Main Engine Class. /// Check for Hardware Capabilities. /// Create GraphicsDeviceManager and ContentManager. /// public DarkWynterGame():base() { this.IsFixedTimeStep = false; Statics.SystemSettings.graphics = new GraphicsDeviceManager(this); Statics.SystemSettings.content = new ContentManager(Services); Statics.SystemSettings.graphics.SynchronizeWithVerticalRetrace = false; // Check all available adapters on the system for (int i = 0; i < GraphicsAdapter.Adapters.Count; i++) { // Get the capabilities of the hardware device GraphicsDeviceCapabilities caps = GraphicsAdapter.Adapters[i].GetCapabilities(DeviceType.Hardware); if (caps.MaxPixelShaderProfile < ShaderProfile.PS_3_0) { System.Diagnostics.Debug.WriteLine("This adapter does not support Shader Model 3.0."); Statics.SystemSettings.gameState = Enums.EngineState.NOT_SUPPORTED; } else { //Yay! They can play Statics.SystemSettings.gameState = Enums.EngineState.TITLE_SCREEN; } } } // ----------------- Load ---------------------- /// /// Set Up Display Window. /// Create Logging, XML, GameFlow, Renderer, SpriteBatch, /// FontWriter, MenuSystem, Audio, ShaderParameters, ChatClient, /// Frames Per Second Counter, and Screensaver /// protected virtual void Initialize(GameObjectTypes userDefinedTypes) { // Load System XML Settings XML.LoadSystem(); // Pass in user defined GameObjectTypes gameObjectTypes = new List(); gameObjectTypes.Add(userDefinedTypes); // Initialize Controllers DarkWynterGame.playerController = new Controller(Enums.ControllerType.PC_AND_XBOX, PlayerIndex.One); DarkWynterGame.menuController = new Controller(Enums.ControllerType.PC_AND_XBOX, PlayerIndex.One); objectLibrary = new ObjectLibrary(gameObjectTypes); collision = new Collision(); // Set up display renderer = new Renderer(); renderer.AutoDetectResolution(); renderer.AutoDetectWindowAndDefaultViewport(Window); // Set up Gpu Shader Handles shaderParameters = new ShaderParameters(); // Init the audio engine Audio.Audio.Initialize(); Audio.Audio.PlayLevelSong("Theme4"); // ScreenSaver for menuSystem screenSaver = new ScreenSaver(); Statics.SystemSettings.screenSaverTimer = Stopwatch.StartNew(); // Initialize the Font class FontWriter.initFont(); // Start Logging Logging.setStart(System.DateTime.Now); // Create new FPS class fps = new FPSCounter(); base.Initialize(); } // --------------- Update --------------------- /// /// Update MenuSystemGame, Audio, FPS. /// Check For Level Loading Request. /// Check For Exit. /// /// XNA GameTime protected override void Update(GameTime gameTime) { // Set current change in time Statics.SystemSettings.dt = (float)gameTime.ElapsedGameTime.TotalSeconds; // Crank the audio engine Audio.Audio.Update(); // Update Frame Rate Counter if (Statics.SystemSettings.enableFPSDisplay) { fps.Update(); } // Load Game if (Statics.GameSettings.LoadScreenLoaded) { //Draw Load screen once and now we can do the load level stuff here Statics.GameSettings.LoadScreenLoaded = false; Statics.SystemSettings.gameState = Enums.EngineState.GAME_MODE; // Load Level Data XML.LoadPlayerIndex(); // == GAMEFLOW INSERTION ======= // Call Load Sequence objectLibrary.LoadLevel(); //finalize # players here Controller.CheckForControllers(objectLibrary); } if (Statics.SystemSettings.gameState != Enums.EngineState.GAME_MODE && Statics.SystemSettings.enableCompilerConsole == false) { // Update menu system menuSystem.Update(objectLibrary); } if (Statics.SystemSettings.gameState == Enums.EngineState.GAME_MODE && Statics.SystemSettings.enableCompilerConsole == false) { // Update ObjectLibrary objectLibrary.Update(ref objectLibrary, collision); } // Call XNA Update base.Update(gameTime); } // ----------------- Draw ---------------------- /// /// Draw Game or MenuSystem Screen /// /// XNA GameTime protected override void Draw(GameTime gameTime) { // Clear the device Statics.SystemSettings.elementalGameTime = gameTime; // If in Game Mode (or paused) call game draw function if (Statics.SystemSettings.gameState == Enums.EngineState.GAME_MODE || Statics.SystemSettings.gameState == Enums.EngineState.GAME_PAUSE) { renderer.Draw(objectLibrary); } if (Statics.SystemSettings.gameState != Enums.EngineState.GAME_MODE) { menuSystem.Draw(renderer.spriteBatch, screenSaver); } fps.Draw(renderer.spriteBatch); // Call XNA Draw base.Draw(gameTime); } // ---------------- Exit ----------------------- /// /// Reset the Graphics Device Settings after using SpriteBatch. /// Use this function if you are experiencing weird effects after using SpriteBatch. /// /// /// Check to see if user has requested to exit the application. /// protected virtual void CheckExit() { if (Statics.SystemSettings.gameState == Enums.EngineState.EXIT) { XML.SaveSystem(); Controller.StopRumble(objectLibrary); Logging.genReport(System.DateTime.Now); Logging.saveLog(System.DateTime.Now); //chatClient.CloseConnection(); this.Exit(); } } #endregion #region Utilities /// /// Clean up all files before exiting. /// protected override void UnloadContent() { // Garbage collector Statics.SystemSettings.content.Unload(); base.UnloadContent(); } /// /// Override XNA function. /// Deactivate Mouse when window looses focus. /// /// /// protected override void OnDeactivated(object sender, EventArgs args) { base.OnDeactivated(sender, args); Statics.SystemSettings.WINDOW_IN_FOCUS = false; } /// /// Override XNA function /// Activate Mouse when window gains focus. /// /// /// protected override void OnActivated(object sender, EventArgs args) { base.OnActivated(sender, args); Statics.SystemSettings.WINDOW_IN_FOCUS = true; } #endregion } }