module_overture_debug.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Linq;
  6. namespace FF8
  7. {
  8. internal static class Module_overture_debug
  9. {
  10. private static OvertureInternalModule internalModule = OvertureInternalModule._4Squaresoft;
  11. private static ArchiveWorker aw;
  12. private const string names = "name";
  13. private const string loops = "loop";
  14. private static Texture2D splashTex = null;
  15. private static Texture2D white=null;
  16. enum OvertureInternalModule
  17. {
  18. _0InitSound,
  19. _1WaitBeforeFirst,
  20. _2PlaySequence,
  21. _3SequenceFinishedPlayMainMenu,
  22. _4Squaresoft
  23. }
  24. private static double internalTimer;
  25. private static bool bNames = true; //by default we are starting with names
  26. private static int splashIndex, splashName = 1, splashLoop = 1;
  27. private static bool bFadingIn = true; //by default first should fade in, wait, then fire fading out and wait for finish, then loop
  28. private static bool bWaitingSplash, bFadingOut;
  29. private static float fSplashWait, Fade;
  30. internal static void Update()
  31. {
  32. if (Input.Button(Buttons.Okay) || Input.Button(Buttons.Cancel) || Input.Button(Keys.Space))
  33. {
  34. Input.ResetInputLimit();
  35. init_debugger_Audio.StopAudio();
  36. Memory.module = Memory.MODULE_MAINMENU_DEBUG;
  37. if (splashTex != null && !splashTex.IsDisposed)
  38. splashTex.Dispose();
  39. if (white != null && !white.IsDisposed)
  40. white.Dispose();
  41. }
  42. switch (internalModule)
  43. {
  44. case OvertureInternalModule._0InitSound:
  45. InitSound();
  46. break;
  47. case OvertureInternalModule._1WaitBeforeFirst:
  48. Memory.SuppressDraw = true;
  49. WaitForFirst();
  50. break;
  51. case OvertureInternalModule._2PlaySequence:
  52. SplashUpdate(ref splashIndex);
  53. break;
  54. }
  55. }
  56. public static void ResetModule()
  57. {
  58. internalModule = 0;
  59. internalTimer = 0.0f;
  60. bFadingIn = true;
  61. bWaitingSplash = false;
  62. fSplashWait = 0.0f;
  63. bFadingOut = false;
  64. Fade = 0;
  65. Memory.spriteBatch.GraphicsDevice.Clear(Color.Black);
  66. Memory.module = Memory.MODULE_OVERTURE_DEBUG;
  67. internalModule = OvertureInternalModule._4Squaresoft;
  68. Module_movie_test.ReturnState = Memory.MODULE_OVERTURE_DEBUG;
  69. aw = null; // was getting exception when running the overture again as the aw target changed.
  70. }
  71. private static void WaitForFirst()
  72. {
  73. if (internalTimer > 6.0f)
  74. {
  75. internalModule++;
  76. Console.WriteLine("MODULE_OVERTURE: DEBUG MODULE 2");
  77. }
  78. internalTimer += Memory.gameTime.ElapsedGameTime.Milliseconds / 1000.0d;
  79. }
  80. private static void InitSound()
  81. {
  82. Memory.MusicIndex = 79;//79; //Overture
  83. init_debugger_Audio.PlayMusic();
  84. Memory.MusicIndex = ushort.MaxValue; // reset pos after playing overture; will loop back to start if push next
  85. if (white != null && !white.IsDisposed)
  86. white.Dispose();
  87. white = new Texture2D(Memory.graphics.GraphicsDevice, 4, 4, false, SurfaceFormat.Color);
  88. byte[] whiteBuffer = new byte[16];
  89. for (int i = 0; i < 16; i++)
  90. {
  91. whiteBuffer[i] = 255;
  92. }
  93. internalModule++;
  94. }
  95. internal static void Draw()
  96. {
  97. switch (internalModule)
  98. {
  99. case OvertureInternalModule._0InitSound:
  100. case OvertureInternalModule._1WaitBeforeFirst:
  101. Memory.graphics.GraphicsDevice.Clear(Color.Black);
  102. break;
  103. case OvertureInternalModule._2PlaySequence:
  104. Memory.graphics.GraphicsDevice.Clear(Color.Black);
  105. DrawSplash();
  106. break; //actually this is our entry point for draw;
  107. case OvertureInternalModule._3SequenceFinishedPlayMainMenu:
  108. DrawLogo(); //after this ends, jump into main menu module
  109. break;
  110. case OvertureInternalModule._4Squaresoft:
  111. internalModule = OvertureInternalModule._0InitSound;
  112. Module_movie_test.Index = 103;//103;
  113. Module_movie_test.ReturnState = Memory.MODULE_OVERTURE_DEBUG;
  114. Memory.module = Memory.MODULE_MOVIETEST;
  115. break;
  116. }
  117. }
  118. private static void DrawLogo()
  119. {
  120. //fade to white
  121. if (!bWaitingSplash)
  122. {
  123. Memory.graphics.GraphicsDevice.Clear(Color.White);
  124. }
  125. else
  126. {
  127. Memory.graphics.GraphicsDevice.Clear(Color.Black);
  128. }
  129. Memory.SpriteBatchStartAlpha();
  130. Memory.spriteBatch.Draw(splashTex, new Microsoft.Xna.Framework.Rectangle(0, 0, Memory.graphics.GraphicsDevice.Viewport.Width, Memory.graphics.GraphicsDevice.Viewport.Height),
  131. new Microsoft.Xna.Framework.Rectangle(0, 0, splashTex.Width, splashTex.Height)
  132. , Color.White * Fade);
  133. if (bFadingIn)
  134. {
  135. Fade += Memory.gameTime.ElapsedGameTime.Milliseconds / 5000.0f;
  136. }
  137. if (bFadingOut)
  138. {
  139. Fade -= Memory.gameTime.ElapsedGameTime.Milliseconds / 2000.0f;
  140. }
  141. if (Fade < 0.0f)
  142. {
  143. bFadingIn = true;
  144. ReadSplash(true);
  145. bFadingOut = false;
  146. }
  147. if (bFadingIn && Fade > 1.0f && !bWaitingSplash)
  148. {
  149. internalTimer += Memory.gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
  150. }
  151. if (internalTimer > 5.0f)
  152. {
  153. bWaitingSplash = true;
  154. bFadingOut = true;
  155. }
  156. Memory.SpriteBatchEnd();
  157. if (bWaitingSplash && Fade < 0.0f)
  158. {
  159. init_debugger_Audio.StopAudio();
  160. Memory.module = Memory.MODULE_MAINMENU_DEBUG;
  161. if (splashTex != null && !splashTex.IsDisposed)
  162. splashTex.Dispose();
  163. if (white != null && !white.IsDisposed)
  164. white.Dispose();
  165. }
  166. }
  167. private static void DrawSplash()
  168. {
  169. if (splashTex == null)
  170. {
  171. return;
  172. }
  173. Memory.SpriteBatchStartAlpha();
  174. Memory.spriteBatch.Draw(splashTex, new Microsoft.Xna.Framework.Rectangle(0, 0, Memory.graphics.GraphicsDevice.Viewport.Width, Memory.graphics.GraphicsDevice.Viewport.Height),
  175. new Microsoft.Xna.Framework.Rectangle(0, 0, splashTex.Width, splashTex.Height)
  176. , Color.White * Fade);
  177. Memory.SpriteBatchEnd();
  178. }
  179. internal static void SplashUpdate(ref int _splashIndex)
  180. {
  181. if (aw == null)
  182. {
  183. aw = new ArchiveWorker(Memory.Archives.A_MAIN);
  184. }
  185. if (splashTex == null)
  186. {
  187. ReadSplash();
  188. }
  189. if (bFadingIn)
  190. {
  191. Fade += Memory.gameTime.ElapsedGameTime.Milliseconds / 1000.0f * 2f;
  192. if (Fade > 1.0f)
  193. {
  194. Fade = 1.0f;
  195. bFadingIn = false;
  196. bWaitingSplash = true;
  197. }
  198. }
  199. if (bFadingOut)
  200. {
  201. if (splashLoop + 1 >= 0x0F && splashName >= 0x0F)
  202. {
  203. bFadingIn = false;
  204. bFadingOut = true;
  205. bWaitingSplash = false;
  206. internalTimer = 0.0f;
  207. Fade = 1.0f;
  208. internalModule++;
  209. return;
  210. }
  211. Fade -= Memory.gameTime.ElapsedGameTime.Milliseconds / 1000.0f * 2f;
  212. if (Fade < 0.0f)
  213. {
  214. bFadingIn = true;
  215. bFadingOut = false;
  216. Fade = 0.0f;
  217. _splashIndex++;
  218. if (bNames)
  219. {
  220. splashName++;
  221. }
  222. else
  223. {
  224. splashLoop++;
  225. }
  226. if (_splashIndex > 1)
  227. {
  228. bNames = !bNames;
  229. _splashIndex = 0;
  230. }
  231. ReadSplash();
  232. }
  233. }
  234. if (bWaitingSplash)
  235. {
  236. if (bNames)
  237. {
  238. if (fSplashWait > 4.8f)
  239. {
  240. bWaitingSplash = false;
  241. bFadingOut = true;
  242. fSplashWait = 0.0f;
  243. }
  244. }
  245. else
  246. {
  247. if (fSplashWait > 6.5f)
  248. {
  249. bWaitingSplash = false;
  250. bFadingOut = true;
  251. fSplashWait = 0.0f;
  252. }
  253. }
  254. Memory.SuppressDraw = true;
  255. fSplashWait += Memory.gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
  256. }
  257. //loop 01-14 + name01-14;
  258. }
  259. //Splash is 640x400 16BPP typical TIM with palette of ggg bbbbb a rrrrr gg
  260. internal static void ReadSplash(bool bLogo = false)
  261. {
  262. if (!bLogo)
  263. {
  264. if (splashName > 0x0f)
  265. {
  266. return;
  267. }
  268. string[] lof = aw.GetListOfFiles();
  269. string fileName = bNames
  270. ? lof.First(x => x.ToLower().Contains($"{names}{splashName.ToString("D2")}"))
  271. : lof.First(x => x.ToLower().Contains($"{loops}{splashLoop.ToString("D2")}"));
  272. byte[] buffer = ArchiveWorker.GetBinaryFile(Memory.Archives.A_MAIN, fileName);
  273. uint uncompSize = BitConverter.ToUInt32(buffer, 0);
  274. buffer = buffer.Skip(4).ToArray(); //hotfix for new LZSS
  275. buffer = LZSS.DecompressAllNew(buffer);
  276. if (splashTex != null && !splashTex.IsDisposed)
  277. splashTex.Dispose();
  278. splashTex = new Texture2D(Memory.graphics.GraphicsDevice, 640, 400, false, SurfaceFormat.Color);
  279. byte[] rgbBuffer = new byte[splashTex.Width * splashTex.Height * 4];
  280. int innerBufferIndex = 0;
  281. for (int i = 0; i < rgbBuffer.Length; i += 4)
  282. {
  283. if (innerBufferIndex + 1 >= buffer.Length)
  284. {
  285. break;
  286. }
  287. ushort pixel = (ushort)((buffer[innerBufferIndex + 1] << 8) | buffer[innerBufferIndex]);
  288. byte red = (byte)((pixel) & 0x1F);
  289. byte green = (byte)((pixel >> 5) & 0x1F);
  290. byte blue = (byte)((pixel >> 10) & 0x1F);
  291. red = (byte)MathHelper.Clamp((red * 8), 0, 255);
  292. green = (byte)MathHelper.Clamp((green * 8), 0, 255);
  293. blue = (byte)MathHelper.Clamp((blue * 8), 0, 255);
  294. rgbBuffer[i] = red;
  295. rgbBuffer[i + 1] = green;
  296. rgbBuffer[i + 2] = blue;
  297. rgbBuffer[i + 3] = 255;//(byte)(((pixel >> 7) & 0x1) == 1 ? 255 : 0);
  298. innerBufferIndex += 2;
  299. }
  300. splashTex.SetData(rgbBuffer);
  301. GC.Collect();
  302. GC.WaitForPendingFinalizers();
  303. }
  304. else
  305. {
  306. string[] lof = aw.GetListOfFiles();
  307. string fileName = lof.First(x => x.ToLower().Contains($"ff8.lzs"));
  308. byte[] buffer = ArchiveWorker.GetBinaryFile(Memory.Archives.A_MAIN, fileName);
  309. uint uncompSize = BitConverter.ToUInt32(buffer, 0);
  310. buffer = LZSS.DecompressAllNew(buffer);
  311. if (splashTex != null && !splashTex.IsDisposed)
  312. splashTex.Dispose();
  313. splashTex = new Texture2D(Memory.graphics.GraphicsDevice, 640, 400, false, SurfaceFormat.Color);
  314. byte[] rgbBuffer = new byte[splashTex.Width * splashTex.Height * 4];
  315. int innerBufferIndex = 0;
  316. for (int i = 0; i < rgbBuffer.Length; i += 4)
  317. {
  318. if (innerBufferIndex + 1 >= buffer.Length)
  319. {
  320. break;
  321. }
  322. ushort pixel = (ushort)((buffer[innerBufferIndex + 1] << 8) | buffer[innerBufferIndex]);
  323. byte red = (byte)((pixel) & 0x1F);
  324. byte green = (byte)((pixel >> 5) & 0x1F);
  325. byte blue = (byte)((pixel >> 10) & 0x1F);
  326. red = (byte)MathHelper.Clamp((red * 8), 0, 255);
  327. green = (byte)MathHelper.Clamp((green * 8), 0, 255);
  328. blue = (byte)MathHelper.Clamp((blue * 8), 0, 255);
  329. rgbBuffer[i] = red;
  330. rgbBuffer[i + 1] = green;
  331. rgbBuffer[i + 2] = blue;
  332. rgbBuffer[i + 3] = 255;//(byte)(((pixel >> 7) & 0x1) == 1 ? 255 : 0);
  333. innerBufferIndex += 2;
  334. }
  335. splashTex.SetData(rgbBuffer);
  336. GC.Collect();
  337. GC.WaitForPendingFinalizers();
  338. }
  339. }
  340. }
  341. }