Game1.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using OpenVIII.Encoding.Tags;
  4. using System;
  5. using System.Reflection;
  6. namespace OpenVIII
  7. {
  8. public class Game1 : Game
  9. {
  10. private GraphicsDeviceManager graphics;
  11. private SpriteBatch spriteBatch;
  12. public Game1()
  13. {
  14. graphics = new GraphicsDeviceManager(this);
  15. if (Assembly.GetCallingAssembly().GetName().Name.Contains("DirectX"))
  16. {
  17. graphics.GraphicsProfile = GraphicsProfile.HiDef;
  18. Memory.currentGraphicMode = Memory.GraphicModes.DirectX;
  19. }
  20. else Memory.currentGraphicMode = Memory.GraphicModes.OpenGL;
  21. Content.RootDirectory = "Content";
  22. graphics.PreferredBackBufferWidth = Memory.PreferredViewportWidth;
  23. graphics.PreferredBackBufferHeight = Memory.PreferredViewportHeight;
  24. Window.AllowUserResizing = true;
  25. IsFixedTimeStep = false;
  26. graphics.SynchronizeWithVerticalRetrace = false;
  27. }
  28. protected override void Initialize()
  29. {
  30. FFmpeg.AutoGen.Example.FFmpegBinariesHelper.RegisterFFmpegBinaries();
  31. //Input.Init();
  32. Memory.Input2 = new Input2();
  33. Memory.Init(graphics, spriteBatch, Content);
  34. init_debugger_Audio.Init(); //this initializes the DirectAudio, it's true that it gets loaded AFTER logo, but we will do the opposite
  35. init_debugger_Audio.Init_SoundAudio(); //this initalizes the WAVE format audio.dat
  36. Fields.Initializer.Init(); //this initializes the field module, it's worth to have this at the beginning
  37. Init_debugger_battle.Init(); //this initializes the encounters
  38. base.Initialize();
  39. }
  40. protected override void LoadContent()
  41. {
  42. spriteBatch = new SpriteBatch(GraphicsDevice);
  43. Memory.spriteBatch = spriteBatch;
  44. // Memory.shadowTexture = Content.Load<Texture2D>("Shadow");
  45. GenerateShadowModel();
  46. base.LoadContent();
  47. }
  48. protected override void OnExiting(object sender, EventArgs args)
  49. {
  50. GracefullyExit();
  51. base.OnExiting(sender, args);
  52. }
  53. private void GenerateShadowModel()
  54. {
  55. /*
  56. * X-X
  57. * X-X
  58. * X-X
  59. */
  60. Vector3[] vertices = new Vector3[] //3x3
  61. {
  62. new Vector3(-10,0,10),
  63. new Vector3(0,0,10),
  64. new Vector3(0,0,0),
  65. new Vector3(-10,0,0),
  66. new Vector3(10,0,10),
  67. new Vector3(10,0,0),
  68. new Vector3(0,0,-10),
  69. new Vector3(-10,0,-10),
  70. new Vector3(10,0,-10),
  71. };
  72. Vector2[] textureCoordinates = new Vector2[]
  73. {
  74. new Vector2(0.0099f, 0.9950f),
  75. new Vector2(0.0099f, 0.0189f),
  76. new Vector2(0.9777f, 0.0189f),
  77. new Vector2(0.9777f, 0.9950f),
  78. new Vector2(0.9821f, 0.9995f),
  79. new Vector2(0.0143f, 0.9995f),
  80. new Vector2(0.0143f, 0.0144f),
  81. new Vector2(0.9821f, 0.0144f)
  82. };
  83. VertexPositionTexture[] vpt = new VertexPositionTexture[]
  84. {
  85. //righttop (should be bottom left)
  86. new VertexPositionTexture(vertices[0], textureCoordinates[6]),
  87. new VertexPositionTexture(vertices[1], textureCoordinates[7]),
  88. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  89. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  90. new VertexPositionTexture(vertices[3], textureCoordinates[5]),
  91. new VertexPositionTexture(vertices[0], textureCoordinates[6]),
  92. //top left
  93. new VertexPositionTexture(vertices[1], textureCoordinates[0]),
  94. new VertexPositionTexture(vertices[4], textureCoordinates[1]),
  95. new VertexPositionTexture(vertices[5], textureCoordinates[2]),
  96. new VertexPositionTexture(vertices[5], textureCoordinates[2]),
  97. new VertexPositionTexture(vertices[2], textureCoordinates[3]),
  98. new VertexPositionTexture(vertices[1], textureCoordinates[0]),
  99. //bottom right should be top right
  100. new VertexPositionTexture(vertices[3], textureCoordinates[7]),
  101. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  102. new VertexPositionTexture(vertices[6], textureCoordinates[5]),
  103. new VertexPositionTexture(vertices[6], textureCoordinates[5]),
  104. new VertexPositionTexture(vertices[7], textureCoordinates[6]),
  105. new VertexPositionTexture(vertices[3], textureCoordinates[7]),
  106. //bottom left should be bottom right
  107. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  108. new VertexPositionTexture(vertices[5], textureCoordinates[5]),
  109. new VertexPositionTexture(vertices[8], textureCoordinates[6]),
  110. new VertexPositionTexture(vertices[8], textureCoordinates[6]),
  111. new VertexPositionTexture(vertices[6], textureCoordinates[7]),
  112. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  113. };
  114. Memory.shadowGeometry = vpt;
  115. }
  116. protected override void UnloadContent()
  117. {
  118. }
  119. protected override void Update(GameTime gameTime)
  120. {
  121. Memory.gameTime = gameTime;
  122. Memory.IsActive = IsActive;
  123. //it breaks the Font
  124. //Memory.PreferredViewportWidth = graphics.GraphicsDevice.Viewport.Width;
  125. //Memory.PreferredViewportHeight = graphics.GraphicsDevice.Viewport.Height;
  126. Input2.Update();
  127. Memory.Update();
  128. if (Input2.Button(FF8TextTagKey.Exit) || Input2.Button(FF8TextTagKey.ExitMenu))
  129. Exit();
  130. init_debugger_Audio.Update();
  131. ModuleHandler.Update(gameTime);
  132. base.Update(gameTime);
  133. if (Memory.SuppressDraw)
  134. {
  135. SuppressDraw();
  136. Memory.SuppressDraw = false;
  137. }
  138. IsMouseVisible = Memory.IsMouseVisible;
  139. }
  140. protected override void Draw(GameTime gameTime)
  141. {
  142. ModuleHandler.Draw(gameTime);
  143. base.Draw(gameTime);
  144. if(Extended.bRequestedBackBuffer)
  145. {
  146. Texture2D tex = new Texture2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color);
  147. byte[] b = new byte[tex.Width * tex.Height * 4];
  148. graphics.GraphicsDevice.GetBackBufferData<byte>(b);
  149. tex.SetData(b);
  150. Extended.BackBufferTexture = tex;
  151. Extended.bRequestedBackBuffer = false;
  152. if (tex != null)
  153. {
  154. Extended.bBackBufferAvailable = true;
  155. Extended.postBackBufferDelegate();
  156. }
  157. }
  158. }
  159. private async void GracefullyExit()
  160. {
  161. Memory.TokenSource.Cancel(); // tell task we are done
  162. //step0. dispose stop sounds
  163. Module_movie_test.Reset();
  164. init_debugger_Audio.StopMusic();
  165. init_debugger_Audio.KillAudio();
  166. //step1. kill init task. to prevent exceptions if exiting before fully loaded.
  167. await Memory.InitTask; // wait for task to finish what it's doing.
  168. }
  169. }
  170. }