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