PageRenderTime 211ms CodeModel.GetById 90ms app.highlight 9ms RepoModel.GetById 92ms app.codeStats 0ms

/MonocleEngine/Engine.cs

https://bitbucket.org/gamblore/monocleengine-monogame
C# | 190 lines | 156 code | 31 blank | 3 comment | 27 complexity | 0d8c962679308cd3d6d898528dad04f9 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using Microsoft.Xna.Framework;
  5using Microsoft.Xna.Framework.Audio;
  6using Microsoft.Xna.Framework.Content;
  7using Microsoft.Xna.Framework.GamerServices;
  8using Microsoft.Xna.Framework.Graphics;
  9using Microsoft.Xna.Framework.Input;
 10using Microsoft.Xna.Framework.Media;
 11
 12namespace Monocle
 13{
 14    public enum GameTags { Player = 0, Corpse, Solid, Actor, Arrow, Target, PlayerCollider, Coin, Orb, LightSource, Enemy, Pickup, Climbable, HUD, OWLocation };
 15
 16    public class Engine : Game
 17    {
 18        static public Engine Instance { get; private set; }
 19        static internal int TagAmount = Enum.GetNames(typeof(GameTags)).Length;
 20
 21        public GraphicsDeviceManager Graphics { get; private set; }
 22        public Screen Screen { get; private set; }
 23        public float DeltaTime { get; private set; }
 24
 25        private Scene scene;
 26        private Scene nextScene;
 27        private string windowTitle;
 28        private float framesPerSecond;
 29        private float changeFPSTo;
 30        private int fpsChangeDelay;
 31
 32#if DEBUG
 33        public Commands Commands { get; private set; }
 34        private TimeSpan counterElapsed = TimeSpan.Zero;
 35        private int counterFrames = 0;
 36#endif
 37
 38        public Engine(int width, int height, float framesPerSecond, string windowTitle)
 39        {
 40            this.windowTitle = windowTitle;
 41
 42            Instance = this;
 43            Graphics = new GraphicsDeviceManager(this);
 44            Content.RootDirectory = "Content";
 45            IsMouseVisible = true;
 46            Screen = new Screen(this, width, height);
 47
 48            IsFixedTimeStep = true;
 49            FramesPerSecond = framesPerSecond;
 50        }
 51
 52        public float FramesPerSecond
 53        {
 54            get { return framesPerSecond; }
 55            set
 56            {
 57                if (framesPerSecond != value)
 58                {
 59                    framesPerSecond = value;
 60                    TargetElapsedTime = TimeSpan.FromMilliseconds(Math.Floor(1000.0 / (double)framesPerSecond));
 61                    DeltaTime = 1.0f / framesPerSecond;
 62                    fpsChangeDelay = 0;
 63                }
 64            }
 65        }
 66
 67        protected override void Initialize()
 68        {
 69            base.Initialize();
 70
 71#if DEBUG
 72            Commands = new Commands();
 73#endif
 74
 75            Input.Initialize();
 76            Screen.Initialize();
 77            Monocle.Draw.Init(GraphicsDevice);
 78            Graphics.DeviceReset += OnGraphicsReset;
 79
 80            Window.Title = windowTitle;
 81        }
 82
 83        private void OnGraphicsReset(object sender, EventArgs e)
 84        {
 85            if (scene != null)
 86                scene.HandleGraphicsReset();
 87            if (nextScene != null)
 88                nextScene.HandleGraphicsReset();
 89        }
 90
 91        protected override void Update(GameTime gameTime)
 92        {
 93#if DEBUG
 94            //In Debug mode, press Left Shift + ESC to quit the game
 95            if (Input.Pressed(Keys.Escape) && Input.Check(Keys.LeftShift))
 96                this.Exit();
 97
 98            if (Commands.Open)
 99            {
100                Commands.UpdateOpen();
101                Input.UpdateNoKeyboard();
102            }
103            else
104            {
105                Commands.UpdateClosed();
106                Input.Update();
107            }
108#else
109            Input.Update();
110#endif            
111 
112            //FPS change counter
113            if (fpsChangeDelay > 0)
114            {
115                fpsChangeDelay--;
116                if (fpsChangeDelay == 0)
117                    FramesPerSecond = changeFPSTo;
118            }
119
120            if (scene != null && scene.Active)
121                scene.Update();
122
123            if (scene != nextScene)
124            {
125                if (scene != null)
126                    scene.End();
127                CancelChangeFPS();
128                scene = nextScene;
129                if (scene != null)
130                    scene.Begin();
131            }
132
133            base.Update(gameTime);
134        }
135
136        protected override void Draw(GameTime gameTime)
137        {
138            GraphicsDevice.SetRenderTarget(Screen.RenderTarget);
139            GraphicsDevice.Clear(Screen.ClearColor);
140
141            if (scene != null)
142                scene.Render();
143
144            GraphicsDevice.SetRenderTarget(null);
145            GraphicsDevice.Clear(Color.Black);
146            Screen.Render();
147
148            base.Draw(gameTime);
149
150#if DEBUG
151            if (Commands.Open)
152                Commands.Render();
153
154            //Frame counter
155            counterFrames++;
156            counterElapsed += gameTime.ElapsedGameTime;
157            if (counterElapsed > TimeSpan.FromSeconds(1))
158            {
159                Window.Title = windowTitle + " " + counterFrames.ToString() + " fps";
160                counterFrames = 0;
161                counterElapsed -= TimeSpan.FromSeconds(1);
162            }
163#endif
164        }
165
166        public Scene Scene
167        {
168            get { return scene; }
169            set { nextScene = value; }
170        }
171
172        #region Change the FPS
173
174        public void ChangeFPS(float newFPS, int frameDelay)
175        {
176            if (newFPS != framesPerSecond)
177            {
178                changeFPSTo = newFPS;
179                fpsChangeDelay = frameDelay;
180            }
181        }
182
183        public void CancelChangeFPS()
184        {
185            fpsChangeDelay = 0;
186        }
187
188        #endregion
189    }
190}