Game1.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using OpenVIII.Encoding.Tags;
  4. using System;
  5. using System.Linq;
  6. using System.Reflection;
  7. namespace OpenVIII
  8. {
  9. public class Game1 : Game
  10. {
  11. #region Fields
  12. private readonly GraphicsDeviceManager _graphics;
  13. private Core.ImGuiRenderer _imgui;
  14. private SpriteBatch _spriteBatch;
  15. #endregion Fields
  16. #region Constructors
  17. public Game1()
  18. {
  19. _graphics = new GraphicsDeviceManager(this);
  20. if (Assembly.GetCallingAssembly().GetName().Name.Contains("DirectX"))
  21. {
  22. _graphics.GraphicsProfile = GraphicsProfile.HiDef;
  23. Memory.CurrentGraphicMode = Memory.GraphicModes.DirectX;
  24. }
  25. else Memory.CurrentGraphicMode = Memory.GraphicModes.OpenGL;
  26. if (Content != null) Content.RootDirectory = "Content";
  27. else throw new NullReferenceException($"{nameof(Game1)}::{nameof(Content)} Maybe running linux build on windows. As is null.");
  28. _graphics.PreferredBackBufferWidth = Memory.PreferredViewportWidth;
  29. _graphics.PreferredBackBufferHeight = Memory.PreferredViewportHeight;
  30. if (Window != null) Window.AllowUserResizing = true;
  31. else throw new NullReferenceException($"{nameof(Game1)}::{nameof(Window)} Maybe running linux build on windows. As is null.");
  32. IsFixedTimeStep = false;
  33. _graphics.SynchronizeWithVerticalRetrace = false;
  34. }
  35. #endregion Constructors
  36. #region Events
  37. public static event EventHandler<TextInputEventArgs> OnTextEntered;
  38. #endregion Events
  39. #region Properties
  40. public string[] Arguments { get; set; }
  41. #endregion Properties
  42. #region Methods
  43. protected override void Draw(GameTime gameTime)
  44. {
  45. ModuleHandler.Draw(gameTime);
  46. base.Draw(gameTime);
  47. if (!Extended.bRequestedBackBuffer) return;
  48. var tex = new Texture2D(_graphics.GraphicsDevice, _graphics.GraphicsDevice.Viewport.Width, _graphics.GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color);
  49. var b = new byte[tex.Width * tex.Height * 4];
  50. _graphics.GraphicsDevice.GetBackBufferData(b);
  51. tex.SetData(b);
  52. Extended.BackBufferTexture = tex;
  53. Extended.bRequestedBackBuffer = false;
  54. Extended.bBackBufferAvailable = true;
  55. Extended.postBackBufferDelegate();
  56. }
  57. protected override void Initialize()
  58. {
  59. _imgui = new Core.ImGuiRenderer(this);
  60. _imgui.RebuildFontAtlas();
  61. Memory.ImGui = _imgui;
  62. Window.TextInput += TextEntered;
  63. Memory.Log = new Log();
  64. if (Arguments != null)
  65. {
  66. var log =
  67. Arguments.FirstOrDefault(x => x.Trim().StartsWith("log=", StringComparison.OrdinalIgnoreCase));
  68. if (!string.IsNullOrWhiteSpace((log)))
  69. {
  70. log = log.Trim();
  71. if (log.EndsWith("false", StringComparison.OrdinalIgnoreCase))
  72. {
  73. Memory.Log.Enabled = false;
  74. }
  75. if (log.EndsWith("true", StringComparison.OrdinalIgnoreCase))
  76. {
  77. Memory.Log.Enabled = true;
  78. }
  79. }
  80. }
  81. FFmpeg.AutoGen.Example.FFmpegBinariesHelper.RegisterFFmpegBinaries();
  82. //Input.Init();
  83. Memory.Input2 = new Input2();
  84. Memory.Init(_graphics, _spriteBatch, Content, Arguments);
  85. AV.Music.Init(); //this initializes the DirectAudio, it's true that it gets loaded AFTER logo, but we will do the opposite
  86. AV.Sound.Init(); //this initializes the WAVE format audio.dat
  87. Memory.Log.WriteLine($"{nameof(Game)} :: {nameof(base.Initialize)}");
  88. // FORCE FPS LIMIT HERE
  89. //this.IsFixedTimeStep = true;
  90. //this.TargetElapsedTime = TimeSpan.FromSeconds(1d / 30d);
  91. // =====================
  92. base.Initialize();
  93. }
  94. protected override void LoadContent()
  95. {
  96. _spriteBatch = new SpriteBatch(GraphicsDevice);
  97. Memory.SpriteBatch = _spriteBatch;
  98. // Memory.shadowTexture = Content.Load<Texture2D>("Shadow");
  99. GenerateShadowModel();
  100. base.LoadContent();
  101. }
  102. protected override void OnExiting(object sender, EventArgs args)
  103. {
  104. GracefullyExit();
  105. base.OnExiting(sender, args);
  106. }
  107. protected override void UnloadContent()
  108. {
  109. }
  110. protected override void Update(GameTime gameTime)
  111. {
  112. if (Memory.QuitNextUpdate) Exit();
  113. Memory.GameTime = gameTime;
  114. Memory.IsActive = IsActive;
  115. FPSCounter.Update();
  116. //it breaks the Font
  117. //Memory.PreferredViewportWidth = graphics.GraphicsDevice.Viewport.Width;
  118. //Memory.PreferredViewportHeight = graphics.GraphicsDevice.Viewport.Height;
  119. Input2.Update();
  120. Memory.Update();
  121. if (Input2.Button(FF8TextTagKey.Exit) || Input2.Button(FF8TextTagKey.ExitMenu))
  122. Exit();
  123. AV.Music.Update();
  124. ModuleHandler.Update(gameTime);
  125. base.Update(gameTime);
  126. if (Memory.SuppressDraw)
  127. {
  128. SuppressDraw();
  129. Memory.SuppressDraw = false;
  130. }
  131. IsMouseVisible = Memory.IsMouseVisible;
  132. }
  133. private static void GenerateShadowModel()
  134. {
  135. /*
  136. * X-X
  137. * X-X
  138. * X-X
  139. */
  140. var vertices = new Vector3[] //3x3
  141. {
  142. new Vector3(-10,0,10),
  143. new Vector3(0,0,10),
  144. new Vector3(0,0,0),
  145. new Vector3(-10,0,0),
  146. new Vector3(10,0,10),
  147. new Vector3(10,0,0),
  148. new Vector3(0,0,-10),
  149. new Vector3(-10,0,-10),
  150. new Vector3(10,0,-10),
  151. };
  152. var textureCoordinates = new Vector2[]
  153. {
  154. new Vector2(0.0099f, 0.9950f),
  155. new Vector2(0.0099f, 0.0189f),
  156. new Vector2(0.9777f, 0.0189f),
  157. new Vector2(0.9777f, 0.9950f),
  158. new Vector2(0.9821f, 0.9995f),
  159. new Vector2(0.0143f, 0.9995f),
  160. new Vector2(0.0143f, 0.0144f),
  161. new Vector2(0.9821f, 0.0144f)
  162. };
  163. var vpt = new VertexPositionTexture[]
  164. {
  165. //right top (should be bottom left)
  166. new VertexPositionTexture(vertices[0], textureCoordinates[6]),
  167. new VertexPositionTexture(vertices[1], textureCoordinates[7]),
  168. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  169. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  170. new VertexPositionTexture(vertices[3], textureCoordinates[5]),
  171. new VertexPositionTexture(vertices[0], textureCoordinates[6]),
  172. //top left
  173. new VertexPositionTexture(vertices[1], textureCoordinates[0]),
  174. new VertexPositionTexture(vertices[4], textureCoordinates[1]),
  175. new VertexPositionTexture(vertices[5], textureCoordinates[2]),
  176. new VertexPositionTexture(vertices[5], textureCoordinates[2]),
  177. new VertexPositionTexture(vertices[2], textureCoordinates[3]),
  178. new VertexPositionTexture(vertices[1], textureCoordinates[0]),
  179. //bottom right should be top right
  180. new VertexPositionTexture(vertices[3], textureCoordinates[7]),
  181. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  182. new VertexPositionTexture(vertices[6], textureCoordinates[5]),
  183. new VertexPositionTexture(vertices[6], textureCoordinates[5]),
  184. new VertexPositionTexture(vertices[7], textureCoordinates[6]),
  185. new VertexPositionTexture(vertices[3], textureCoordinates[7]),
  186. //bottom left should be bottom right
  187. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  188. new VertexPositionTexture(vertices[5], textureCoordinates[5]),
  189. new VertexPositionTexture(vertices[8], textureCoordinates[6]),
  190. new VertexPositionTexture(vertices[8], textureCoordinates[6]),
  191. new VertexPositionTexture(vertices[6], textureCoordinates[7]),
  192. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  193. };
  194. Memory.ShadowGeometry = vpt;
  195. }
  196. private static async void GracefullyExit()
  197. {
  198. Memory.TokenSource.Cancel(); // tell task we are done
  199. //step0. dispose stop sounds
  200. ModuleMovieTest.Reset();
  201. AV.Music.Stop();
  202. AV.Music.KillAudio();
  203. AV.Sound.KillAudio();
  204. //step1. kill init task. to prevent exceptions if exiting before fully loaded.
  205. if (Memory.InitTask != null)
  206. await Memory.InitTask; // wait for task to finish what it's doing.
  207. }
  208. private static void TextEntered(object sender, TextInputEventArgs e) => OnTextEntered?.Invoke(sender, e);
  209. #endregion Methods
  210. }
  211. }