module_overture_debug.cs 11 KB

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