monocleengine-monogame /MonocleEngine/Engine.cs

Language C# Lines 191
MD5 Hash 0d8c962679308cd3d6d898528dad04f9 Estimated Cost $3,841 (why?)
Repository https://bitbucket.org/gamblore/monocleengine-monogame View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Monocle
{
    public enum GameTags { Player = 0, Corpse, Solid, Actor, Arrow, Target, PlayerCollider, Coin, Orb, LightSource, Enemy, Pickup, Climbable, HUD, OWLocation };

    public class Engine : Game
    {
        static public Engine Instance { get; private set; }
        static internal int TagAmount = Enum.GetNames(typeof(GameTags)).Length;

        public GraphicsDeviceManager Graphics { get; private set; }
        public Screen Screen { get; private set; }
        public float DeltaTime { get; private set; }

        private Scene scene;
        private Scene nextScene;
        private string windowTitle;
        private float framesPerSecond;
        private float changeFPSTo;
        private int fpsChangeDelay;

#if DEBUG
        public Commands Commands { get; private set; }
        private TimeSpan counterElapsed = TimeSpan.Zero;
        private int counterFrames = 0;
#endif

        public Engine(int width, int height, float framesPerSecond, string windowTitle)
        {
            this.windowTitle = windowTitle;

            Instance = this;
            Graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
            Screen = new Screen(this, width, height);

            IsFixedTimeStep = true;
            FramesPerSecond = framesPerSecond;
        }

        public float FramesPerSecond
        {
            get { return framesPerSecond; }
            set
            {
                if (framesPerSecond != value)
                {
                    framesPerSecond = value;
                    TargetElapsedTime = TimeSpan.FromMilliseconds(Math.Floor(1000.0 / (double)framesPerSecond));
                    DeltaTime = 1.0f / framesPerSecond;
                    fpsChangeDelay = 0;
                }
            }
        }

        protected override void Initialize()
        {
            base.Initialize();

#if DEBUG
            Commands = new Commands();
#endif

            Input.Initialize();
            Screen.Initialize();
            Monocle.Draw.Init(GraphicsDevice);
            Graphics.DeviceReset += OnGraphicsReset;

            Window.Title = windowTitle;
        }

        private void OnGraphicsReset(object sender, EventArgs e)
        {
            if (scene != null)
                scene.HandleGraphicsReset();
            if (nextScene != null)
                nextScene.HandleGraphicsReset();
        }

        protected override void Update(GameTime gameTime)
        {
#if DEBUG
            //In Debug mode, press Left Shift + ESC to quit the game
            if (Input.Pressed(Keys.Escape) && Input.Check(Keys.LeftShift))
                this.Exit();

            if (Commands.Open)
            {
                Commands.UpdateOpen();
                Input.UpdateNoKeyboard();
            }
            else
            {
                Commands.UpdateClosed();
                Input.Update();
            }
#else
            Input.Update();
#endif            
 
            //FPS change counter
            if (fpsChangeDelay > 0)
            {
                fpsChangeDelay--;
                if (fpsChangeDelay == 0)
                    FramesPerSecond = changeFPSTo;
            }

            if (scene != null && scene.Active)
                scene.Update();

            if (scene != nextScene)
            {
                if (scene != null)
                    scene.End();
                CancelChangeFPS();
                scene = nextScene;
                if (scene != null)
                    scene.Begin();
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.SetRenderTarget(Screen.RenderTarget);
            GraphicsDevice.Clear(Screen.ClearColor);

            if (scene != null)
                scene.Render();

            GraphicsDevice.SetRenderTarget(null);
            GraphicsDevice.Clear(Color.Black);
            Screen.Render();

            base.Draw(gameTime);

#if DEBUG
            if (Commands.Open)
                Commands.Render();

            //Frame counter
            counterFrames++;
            counterElapsed += gameTime.ElapsedGameTime;
            if (counterElapsed > TimeSpan.FromSeconds(1))
            {
                Window.Title = windowTitle + " " + counterFrames.ToString() + " fps";
                counterFrames = 0;
                counterElapsed -= TimeSpan.FromSeconds(1);
            }
#endif
        }

        public Scene Scene
        {
            get { return scene; }
            set { nextScene = value; }
        }

        #region Change the FPS

        public void ChangeFPS(float newFPS, int frameDelay)
        {
            if (newFPS != framesPerSecond)
            {
                changeFPSTo = newFPS;
                fpsChangeDelay = frameDelay;
            }
        }

        public void CancelChangeFPS()
        {
            fpsChangeDelay = 0;
        }

        #endregion
    }
}
Back to Top