module_overture_debug.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using OpenVIII.Encoding.Tags;
  5. using System;
  6. using System.IO;
  7. using System.Linq;
  8. namespace OpenVIII
  9. {
  10. public static class Module_overture_debug
  11. {
  12. #region Fields
  13. private const float speed = 1f;
  14. private static bool bFadingIn = true;
  15. private static bool bNames = true;
  16. //by default first should fade in, wait, then fire fading out and wait for finish, then loop
  17. private static bool bWaitingSplash, bFadingOut;
  18. private static float fSplashWait, Fade;
  19. private static OverturepublicModule publicModule = OverturepublicModule._4Squaresoft;
  20. private static double publicTimer;
  21. private static int splashIndex, splashName = 1, splashLoop = 1;
  22. private static Splash splashTex = null;
  23. #endregion Fields
  24. #region Enums
  25. private enum OverturepublicModule
  26. {
  27. _0InitSound,
  28. _1WaitBeforeFirst,
  29. _2PlaySequence,
  30. _3SequenceFinishedPlayMainMenu,
  31. _4Squaresoft
  32. }
  33. #endregion Enums
  34. #region Properties
  35. private static float Fadespd1 => (float)(Memory.gameTime.ElapsedGameTime.TotalMilliseconds / 500f) * speed;
  36. private static float Fadespd2 => (float)Fadespd5;
  37. private static float Fadespd3 => (float)(Memory.gameTime.ElapsedGameTime.TotalMilliseconds / 5000.0f) * speed;
  38. private static float Fadespd4 => (float)(Memory.gameTime.ElapsedGameTime.TotalMilliseconds / 2000.0f) * speed;
  39. private static double Fadespd5 => (Memory.gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0d) * speed;
  40. #endregion Properties
  41. #region Methods
  42. public static void Draw()
  43. {
  44. switch (publicModule)
  45. {
  46. case OverturepublicModule._0InitSound:
  47. case OverturepublicModule._1WaitBeforeFirst:
  48. Memory.graphics.GraphicsDevice.Clear(Color.Black);
  49. break;
  50. case OverturepublicModule._2PlaySequence:
  51. Memory.graphics.GraphicsDevice.Clear(Color.Black);
  52. DrawSplash();
  53. break; //actually this is our entry point for draw;
  54. case OverturepublicModule._3SequenceFinishedPlayMainMenu:
  55. DrawLogo(); //after this ends, jump into main menu module
  56. break;
  57. case OverturepublicModule._4Squaresoft:
  58. publicModule = OverturepublicModule._0InitSound;
  59. Module_movie_test.Index = 104;//104 is SE logo in steam release.
  60. Module_movie_test.ReturnState = MODULE.OVERTURE_DEBUG;
  61. Memory.Module = MODULE.MOVIETEST;
  62. break;
  63. }
  64. }
  65. public static void ResetModule()
  66. {
  67. publicModule = 0;
  68. publicTimer = 0.0f;
  69. bFadingIn = true;
  70. bWaitingSplash = false;
  71. fSplashWait = 0.0f;
  72. bFadingOut = false;
  73. Fade = 0;
  74. splashIndex = 0;
  75. splashName = splashLoop = 1;
  76. Memory.spriteBatch.GraphicsDevice.Clear(Color.Black);
  77. Memory.Module = MODULE.OVERTURE_DEBUG;
  78. publicModule = OverturepublicModule._4Squaresoft;
  79. Module_movie_test.ReturnState = MODULE.OVERTURE_DEBUG;
  80. }
  81. public static void SplashUpdate(ref int _splashIndex)
  82. {
  83. if (splashTex == null)
  84. {
  85. ReadSplash();
  86. }
  87. if (bFadingIn)
  88. {
  89. Fade += Fadespd1;
  90. if (Fade > 1.0f)
  91. {
  92. Fade = 1.0f;
  93. bFadingIn = false;
  94. bWaitingSplash = true;
  95. }
  96. }
  97. if (bFadingOut)
  98. {
  99. if (splashLoop + 1 >= 0x0F && splashName >= 0x0F)
  100. {
  101. bFadingIn = false;
  102. bFadingOut = true;
  103. bWaitingSplash = false;
  104. publicTimer = 0.0f;
  105. Fade = 1.0f;
  106. publicModule++;
  107. return;
  108. }
  109. Fade -= Fadespd1;
  110. if (Fade < 0.0f)
  111. {
  112. bFadingIn = true;
  113. bFadingOut = false;
  114. Fade = 0.0f;
  115. _splashIndex++;
  116. if (bNames)
  117. {
  118. splashName++;
  119. }
  120. else
  121. {
  122. splashLoop++;
  123. }
  124. if (_splashIndex > 1)
  125. {
  126. bNames = !bNames;
  127. _splashIndex = 0;
  128. }
  129. ReadSplash();
  130. }
  131. }
  132. if (bWaitingSplash)
  133. {
  134. if (bNames)
  135. {
  136. if (fSplashWait > 4.8f)
  137. {
  138. bWaitingSplash = false;
  139. bFadingOut = true;
  140. fSplashWait = 0.0f;
  141. }
  142. }
  143. else
  144. {
  145. if (fSplashWait > 6.5f)
  146. {
  147. bWaitingSplash = false;
  148. bFadingOut = true;
  149. fSplashWait = 0.0f;
  150. }
  151. }
  152. Memory.SuppressDraw = true;
  153. fSplashWait += Fadespd2;
  154. }
  155. //loop 01-14 + name01-14;
  156. }
  157. public static void Update()
  158. {
  159. if (Input2.DelayedButton(FF8TextTagKey.Confirm) || Input2.DelayedButton(FF8TextTagKey.Cancel) || Input2.DelayedButton(Keys.Space))
  160. {
  161. init_debugger_Audio.StopMusic();
  162. Memory.Module = MODULE.MAINMENU_DEBUG;
  163. }
  164. switch (publicModule)
  165. {
  166. case OverturepublicModule._0InitSound:
  167. InitSound();
  168. break;
  169. case OverturepublicModule._1WaitBeforeFirst:
  170. Memory.SuppressDraw = true;
  171. WaitForFirst();
  172. break;
  173. case OverturepublicModule._2PlaySequence:
  174. SplashUpdate(ref splashIndex);
  175. break;
  176. }
  177. }
  178. private static void DrawLogo()
  179. {
  180. if (splashTex != null)
  181. {
  182. if (!bWaitingSplash)
  183. {
  184. Memory.graphics.GraphicsDevice.Clear(Color.White);
  185. Memory.SpriteBatchStartAlpha(ss: SamplerState.AnisotropicClamp);
  186. }
  187. else
  188. {
  189. Memory.SpriteBatchStartStencil(ss: SamplerState.AnisotropicClamp);
  190. }
  191. Memory.spriteBatch.Draw(splashTex,
  192. new Rectangle(0, 0, Memory.graphics.GraphicsDevice.Viewport.Width, Memory.graphics.GraphicsDevice.Viewport.Height),
  193. new Rectangle(0, 0, splashTex.Width, splashTex.Height)
  194. , Color.White * Fade);
  195. if (bFadingIn)
  196. {
  197. Fade += Fadespd3;
  198. }
  199. if (bFadingOut)
  200. {
  201. Fade -= Fadespd4;
  202. }
  203. if (Fade < 0.0f)
  204. {
  205. bFadingIn = true;
  206. ReadSplash(true);
  207. bFadingOut = false;
  208. }
  209. if (bFadingIn && Fade > 1.0f && !bWaitingSplash)
  210. {
  211. publicTimer += Fadespd5;
  212. }
  213. if (publicTimer > 5.0f)
  214. {
  215. bWaitingSplash = true;
  216. bFadingOut = true;
  217. }
  218. Memory.SpriteBatchEnd();
  219. if (bWaitingSplash && Fade < 0.0f)
  220. {
  221. Memory.Module = MODULE.MAINMENU_DEBUG;
  222. }
  223. }
  224. }
  225. private static void DrawSplash()
  226. {
  227. if (splashTex == null)
  228. {
  229. return;
  230. }
  231. Memory.SpriteBatchStartStencil(ss: SamplerState.AnisotropicClamp);
  232. Memory.spriteBatch.Draw(splashTex,
  233. new Rectangle(0, 0, Memory.graphics.GraphicsDevice.Viewport.Width, Memory.graphics.GraphicsDevice.Viewport.Height),
  234. new Rectangle(0, 0, splashTex.Width, splashTex.Height)
  235. , Color.White * Fade);
  236. Memory.SpriteBatchEnd();
  237. }
  238. private static void InitSound()
  239. {
  240. init_debugger_Audio.PlayMusic(79, loop: false);
  241. Memory.MusicIndex = ushort.MaxValue; // reset pos after playing overture; will loop back to start if push next
  242. publicModule++;
  243. }
  244. private static void ReadSplash(bool bLogo = false) => splashTex = new Splash(bNames ? splashName : splashLoop, bNames, bLogo);
  245. private static void WaitForFirst()
  246. {
  247. if (publicTimer > 6.0f)
  248. {
  249. publicModule++;
  250. Console.WriteLine("MODULE_OVERTURE: DEBUG MODULE 2");
  251. }
  252. publicTimer += Fadespd5;
  253. }
  254. #endregion Methods
  255. }
  256. public class Splash : IDisposable
  257. {
  258. #region Fields
  259. private const string loops = "loop";
  260. private const string names = "name";
  261. private ArchiveWorker aw;
  262. private string filename;
  263. #endregion Fields
  264. #region Constructors
  265. public Splash(int splashNum, bool bNames = true, bool bLogo = false)
  266. {
  267. if (splashNum > 0x0f)
  268. {
  269. return;
  270. }
  271. aw = new ArchiveWorker(Memory.Archives.A_MAIN);
  272. GetName(splashNum, bNames, bLogo);
  273. ReadSplash();
  274. }
  275. #endregion Constructors
  276. #region Destructors
  277. #endregion Destructors
  278. #region Properties
  279. public Texture2D tex { get; private set; }
  280. public int Height => tex?.Height ?? 0;
  281. public int Width => tex?.Width ?? 0;
  282. #endregion Properties
  283. #region Methods
  284. public static implicit operator Texture2D(Splash s) => s.tex;
  285. public override string ToString() => filename;
  286. private void GetName(int splashNum, bool bNames, bool bLogo)
  287. {
  288. string[] lof = aw.GetListOfFiles();
  289. filename = !bLogo
  290. ? bNames
  291. ? lof.First(x => x.ToLower().Contains($"{names}{splashNum.ToString("D2")}"))
  292. : lof.First(x => x.ToLower().Contains($"{loops}{splashNum.ToString("D2")}"))
  293. : lof.First(x => x.ToLower().Contains($"ff8.lzs"));
  294. }
  295. //Splash is 640x400 16BPP typical TIM with palette of ggg bbbbb a rrrrr gg
  296. private void ReadSplash()
  297. {
  298. byte[] buffer = ArchiveWorker.GetBinaryFile(Memory.Archives.A_MAIN, filename);
  299. string fn = Path.GetFileNameWithoutExtension(filename);
  300. uint uncompSize = BitConverter.ToUInt32(buffer, 0);
  301. buffer = buffer.Skip(4).ToArray(); //hotfix for new LZSS
  302. buffer = LZSS.DecompressAllNew(buffer);
  303. TIM_OVERTURE tim = new TIM_OVERTURE(buffer);
  304. if ((fn.Equals("ff8", StringComparison.OrdinalIgnoreCase))||(fn.IndexOf("loop", StringComparison.OrdinalIgnoreCase)>=0))
  305. {
  306. tim.IgnoreAlpha = true;
  307. }
  308. tex = (Texture2D)TextureHandler.Create(fn, tim, 0);//TIM2.Overture(buffer);
  309. //using (FileStream fs = File.Create(Path.Combine("D:\\main", Path.GetFileNameWithoutExtension(filename) + ".png")))
  310. // splashTex.SaveAsPng(fs, splashTex.Width, splashTex.Height);
  311. GC.Collect();
  312. GC.WaitForPendingFinalizers();
  313. }
  314. #region IDisposable Support
  315. private bool disposedValue = false; // To detect redundant calls
  316. protected virtual void Dispose(bool disposing)
  317. {
  318. if (!disposedValue)
  319. {
  320. if (disposing)
  321. {
  322. // TODO: dispose managed state (managed objects).
  323. }
  324. // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
  325. // TODO: set large fields to null.
  326. tex.Dispose();
  327. disposedValue = true;
  328. }
  329. }
  330. // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
  331. ~Splash()
  332. {
  333. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  334. Dispose(false);
  335. }
  336. // This code added to correctly implement the disposable pattern.
  337. public void Dispose()
  338. {
  339. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  340. Dispose(true);
  341. // TODO: uncomment the following line if the finalizer is overridden above.
  342. GC.SuppressFinalize(this);
  343. }
  344. #endregion
  345. #endregion Methods
  346. }
  347. }