Game1.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  7. namespace FF8
  8. {
  9. public class Game1 : Game
  10. {
  11. GraphicsDeviceManager graphics;
  12. SpriteBatch spriteBatch;
  13. SpriteBatch spriteRender;
  14. private float scale = 1f;
  15. RenderTarget2D rt;
  16. public Game1()
  17. {
  18. graphics = new GraphicsDeviceManager(this);
  19. Content.RootDirectory = "Content";
  20. graphics.PreferredBackBufferWidth = Memory.PreferredViewportWidth;
  21. graphics.PreferredBackBufferHeight = Memory.PreferredViewportHeight;
  22. Window.AllowUserResizing = true;
  23. }
  24. protected override void Initialize()
  25. {
  26. FFmpeg.AutoGen.Example.FFmpegBinariesHelper.RegisterFFmpegBinaries();
  27. Memory.Init(graphics, spriteBatch, Content);
  28. Memory.graphics = graphics;
  29. Memory.spriteBatch = spriteBatch;
  30. Memory.content = Content;
  31. spriteRender = new SpriteBatch(GraphicsDevice);
  32. init_debugger_Audio.DEBUG(); //this initializes the DirectAudio, it's true that it gets loaded AFTER logo, but we will do the opposite
  33. init_debugger_Audio.DEBUG_SoundAudio(); //this initalizes the WAVE format audio.dat
  34. Init_debugger_fields.DEBUG(); //this initializes the field module, it's worth to have this at the beginning
  35. Init_debugger_battle.DEBUG(); //this initializes the encounters
  36. Saves.Init(); //loads all savegames from steam or cd2000 directories. first come first serve.
  37. Memory.font = new Font(); //this initializes the fonts and drawing system- holds fonts in-memory
  38. ArchiveWorker aw = new ArchiveWorker(Memory.Archives.A_MENU);
  39. //TEX tex = new TEX(ArchiveWorker.GetBinaryFile(Memory.Archives.A_MENU,
  40. // aw.GetListOfFiles().First(x => x.ToLower().Contains("icon.tex"))));
  41. //Memory.iconsTex = new Texture2D[tex.TextureData.NumOfPalettes];
  42. //for (int i = 0; i < Memory.iconsTex.Length; i++)
  43. // Memory.iconsTex[i] = tex.GetTexture(i);
  44. Memory.FieldHolder.FieldMemory = new int[1024];
  45. Memory.Cards = new Cards();
  46. Memory.Faces = new Faces();
  47. Memory.Icons = new Icons();
  48. rt = new RenderTarget2D(GraphicsDevice, (int)(GraphicsDevice.Viewport.Width * scale), (int)(GraphicsDevice.Viewport.Height * scale), false, SurfaceFormat.Color, DepthFormat.Depth24);
  49. base.Initialize();
  50. //ArchiveSearch s = new ArchiveSearch("Zell\0");//used to find file a string is in. disable if not using.
  51. }
  52. protected override void LoadContent()
  53. {
  54. spriteBatch = new SpriteBatch(GraphicsDevice);
  55. Memory.spriteBatch = spriteBatch;
  56. Memory.shadowTexture = Content.Load<Texture2D>("Shadow");
  57. GenerateShadowModel();
  58. }
  59. private void GenerateShadowModel()
  60. {
  61. /*
  62. * X-X
  63. * X-X
  64. * X-X
  65. */
  66. Vector3[] vertices = new Vector3[] //3x3
  67. {
  68. new Vector3(-10,0,10),
  69. new Vector3(0,0,10),
  70. new Vector3(0,0,0),
  71. new Vector3(-10,0,0),
  72. new Vector3(10,0,10),
  73. new Vector3(10,0,0),
  74. new Vector3(0,0,-10),
  75. new Vector3(-10,0,-10),
  76. new Vector3(10,0,-10),
  77. };
  78. Vector2[] textureCoordinates = new Vector2[]
  79. {
  80. new Vector2(0.0099f, 0.9950f),
  81. new Vector2(0.0099f, 0.0189f),
  82. new Vector2(0.9777f, 0.0189f),
  83. new Vector2(0.9777f, 0.9950f),
  84. new Vector2(0.9821f, 0.9995f),
  85. new Vector2(0.0143f, 0.9995f),
  86. new Vector2(0.0143f, 0.0144f),
  87. new Vector2(0.9821f, 0.0144f)
  88. };
  89. VertexPositionTexture[] vpt = new VertexPositionTexture[]
  90. {
  91. //righttop (should be bottom left)
  92. new VertexPositionTexture(vertices[0], textureCoordinates[6]),
  93. new VertexPositionTexture(vertices[1], textureCoordinates[7]),
  94. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  95. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  96. new VertexPositionTexture(vertices[3], textureCoordinates[5]),
  97. new VertexPositionTexture(vertices[0], textureCoordinates[6]),
  98. //top left
  99. new VertexPositionTexture(vertices[1], textureCoordinates[0]),
  100. new VertexPositionTexture(vertices[4], textureCoordinates[1]),
  101. new VertexPositionTexture(vertices[5], textureCoordinates[2]),
  102. new VertexPositionTexture(vertices[5], textureCoordinates[2]),
  103. new VertexPositionTexture(vertices[2], textureCoordinates[3]),
  104. new VertexPositionTexture(vertices[1], textureCoordinates[0]),
  105. //bottom right should be top right
  106. new VertexPositionTexture(vertices[3], textureCoordinates[7]),
  107. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  108. new VertexPositionTexture(vertices[6], textureCoordinates[5]),
  109. new VertexPositionTexture(vertices[6], textureCoordinates[5]),
  110. new VertexPositionTexture(vertices[7], textureCoordinates[6]),
  111. new VertexPositionTexture(vertices[3], textureCoordinates[7]),
  112. //bottom left should be bottom right
  113. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  114. new VertexPositionTexture(vertices[5], textureCoordinates[5]),
  115. new VertexPositionTexture(vertices[8], textureCoordinates[6]),
  116. new VertexPositionTexture(vertices[8], textureCoordinates[6]),
  117. new VertexPositionTexture(vertices[6], textureCoordinates[7]),
  118. new VertexPositionTexture(vertices[2], textureCoordinates[4]),
  119. };
  120. Memory.shadowGeometry = vpt;
  121. }
  122. protected override void UnloadContent()
  123. {
  124. }
  125. protected override void Update(GameTime gameTime)
  126. {
  127. Memory.gameTime = gameTime;
  128. Memory.IsActive = IsActive;
  129. //it breaks the Font
  130. //Memory.PreferredViewportWidth = graphics.GraphicsDevice.Viewport.Width;
  131. //Memory.PreferredViewportHeight = graphics.GraphicsDevice.Viewport.Height;
  132. Input.Update();
  133. if (Input.Button(Buttons.Exit))
  134. GracefullyExit();
  135. init_debugger_Audio.Update();
  136. ModuleHandler.Update(gameTime);
  137. base.Update(gameTime);
  138. if (Memory.SuppressDraw)
  139. {
  140. SuppressDraw();
  141. Memory.SuppressDraw = false;
  142. }
  143. IsMouseVisible = Memory.IsMouseVisible;
  144. }
  145. protected override void Draw(GameTime gameTime)
  146. {
  147. this.Window.Title = $"OpenVIII - Debug showcase of resolution scaling: {(scale*100).ToString("F02")}%";
  148. if (Input.GetInputDelayed(Keys.NumPad1))
  149. {
  150. scale += 0.25f;
  151. rt = new RenderTarget2D(GraphicsDevice, (int)(GraphicsDevice.Viewport.Width * scale), (int)(GraphicsDevice.Viewport.Height * scale), false, SurfaceFormat.Color, DepthFormat.Depth24);
  152. }
  153. if (Input.GetInputDelayed(Keys.NumPad2))
  154. {
  155. rt = new RenderTarget2D(GraphicsDevice, (int)(GraphicsDevice.Viewport.Width * scale), (int)(GraphicsDevice.Viewport.Height * scale), false, SurfaceFormat.Color, DepthFormat.Depth24);
  156. scale -= 0.25f;
  157. }
  158. //RESOLUTION SCALING IMPLEMENTATION - TO USE WITH TestBranch
  159. GraphicsDevice.SetRenderTarget(rt);
  160. ModuleHandler.Draw(gameTime);
  161. base.Draw(gameTime);
  162. GraphicsDevice.SetRenderTarget(null);
  163. if (rt != null)
  164. {
  165. spriteRender.Begin(rasterizerState: RasterizerState.CullCounterClockwise, depthStencilState: DepthStencilState.Default, samplerState: SamplerState.PointClamp);
  166. spriteRender.Draw(rt, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
  167. spriteRender.End();
  168. }
  169. base.Draw(gameTime);
  170. if (Input.GetInputDelayed(Keys.F1)) //SCREENSHOT CAPABILITIES WIP; I'm leaving it as-is for now. I'll be probably using that for battle transitions (or not)
  171. {
  172. Texture2D tex = new Texture2D(graphics.GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color);
  173. byte[] b = new byte[tex.Width * tex.Height * 4];
  174. graphics.GraphicsDevice.GetBackBufferData<byte>(b);
  175. tex.SetData(b);
  176. tex.SaveAsJpeg(new System.IO.FileStream("D:/test.jpg", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite), tex.Width, tex.Height);
  177. }
  178. }
  179. private void GracefullyExit()
  180. {
  181. //step1. dispose DirectMusic as it's unmanaged
  182. init_debugger_Audio.KillAudio();
  183. Exit();
  184. }
  185. }
  186. }