Player.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using OpenVIII.AV;
  7. namespace OpenVIII.Movie
  8. {
  9. public class Player : IDisposable
  10. {
  11. #region Fields
  12. public static readonly int[] LetterBox = { 101, 103, 104 };
  13. private static Files _files;
  14. private State _state;
  15. private Audio _audio;
  16. private bool _disposedValue;
  17. private bool _suppressDraw;
  18. private Texture2D _texture;
  19. private Video _video;
  20. #endregion Fields
  21. #region Destructors
  22. // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
  23. ~Player()
  24. {
  25. // // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  26. Dispose(false);
  27. }
  28. #endregion Destructors
  29. #region Events
  30. public event EventHandler<State> StateChanged;
  31. #endregion Events
  32. #region Properties
  33. public int Id { get; set; }
  34. // To detect redundant calls
  35. public bool IsDisposed => _disposedValue;
  36. public State State
  37. {
  38. get => _state; private set
  39. {
  40. _state = value;
  41. StateChanged?.Invoke(this, value);
  42. }
  43. }
  44. #endregion Properties
  45. #region Methods
  46. public static Player Load(int id, bool overlayingModels = false)
  47. {
  48. Player player;
  49. if(_files == null)
  50. _files = Files.Instance;
  51. if (File.Exists(_files[id]))
  52. {
  53. player = new Player
  54. {
  55. Id = id,
  56. State = State.Load,
  57. _video = Video.Load(_files[id]),
  58. _audio = Audio.Load(_files[id]),
  59. _suppressDraw = !overlayingModels
  60. };
  61. }
  62. else if (_files.ZZZ)
  63. {
  64. return null; // doesn't work.
  65. //ArchiveZzz a = (ArchiveZzz)ArchiveZzz.Load(Memory.Archives.ZZZ_OTHER);
  66. //var fd = a.ArchiveMap.GetFileData(Files[ID]);
  67. //AV.Audio ffccAudioFromZZZ = AV.Audio.Load(
  68. // new AV.BufferData
  69. // {
  70. // DataSeekLoc = fd.Value.Offset,
  71. // DataSize = fd.Value.UncompressedSize,
  72. // HeaderSize = 0,
  73. // Target = AV.BufferData.TargetFile.other_zzz
  74. // },
  75. // null, -1);
  76. //AV.Video ffccVideoFromZZZ = AV.Video.Load(
  77. // new AV.BufferData
  78. // {
  79. // DataSeekLoc = fd.Value.Offset,
  80. // DataSize = fd.Value.UncompressedSize,
  81. // HeaderSize = 0,
  82. // Target = AV.BufferData.TargetFile.other_zzz
  83. // },
  84. // null, -1);
  85. ////ffcc.Play(volume, pitch, pan);
  86. //Player = new Player()
  87. //{
  88. // ID = ID,
  89. // STATE = STATE.LOAD,
  90. // Video = ffccVideoFromZZZ,
  91. // Audio = ffccAudioFromZZZ,
  92. // SuppressDraw = !OverlayingModels
  93. //};
  94. }
  95. else
  96. return null;
  97. player.State++;
  98. if (player._video == null && player._audio == null)
  99. return null;
  100. return player;
  101. }
  102. // This code added to correctly implement the disposable pattern.
  103. public void Dispose() =>
  104. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  105. Dispose(true);
  106. public void Draw()
  107. {
  108. switch (State)
  109. {
  110. case State.Load:
  111. break;
  112. case State.Clear:
  113. State++;
  114. ClearScreen();
  115. break;
  116. case State.StartPlay:
  117. case State.Playing:
  118. PlayingDraw();
  119. break;
  120. case State.Paused:
  121. break;
  122. case State.Finished:
  123. State++;
  124. PlayingDraw();
  125. break;
  126. case State.Reset:
  127. break;
  128. case State.Return:
  129. break;
  130. default:
  131. throw new ArgumentOutOfRangeException();
  132. }
  133. }
  134. public void PlayingDraw()
  135. {
  136. if (_texture == null)
  137. {
  138. return;
  139. }
  140. //draw frame;
  141. Memory.SpriteBatchStartStencil(ss: SamplerState.AnisotropicClamp);//by default xna filters all textures SamplerState.PointClamp disables that. so video is being filtered why playing.
  142. ClearScreen();
  143. var dst = new Rectangle(new Point(0), (new Vector2(_texture.Width, _texture.Height) * Memory.Scale(_texture.Width, _texture.Height, LetterBox.Contains(Id) ? ScaleMode.FitHorizontal : ScaleMode.FitBoth)).ToPoint());
  144. dst.Offset(Memory.Center.X - dst.Center.X, Memory.Center.Y - dst.Center.Y);
  145. Memory.SpriteBatch.Draw(_texture, dst, Color.White);
  146. Memory.SpriteBatchEnd();
  147. }
  148. public void Stop()
  149. {
  150. _audio.Dispose();
  151. _video.Dispose();
  152. State = State.Return;
  153. }
  154. public void Update()
  155. {
  156. switch (State)
  157. {
  158. case State.Load:
  159. break;
  160. case State.Clear:
  161. break;
  162. case State.StartPlay:
  163. State++;
  164. if (_audio != null)
  165. {
  166. if (Memory.Threaded)
  167. _audio.PlayInTask();
  168. else
  169. _audio.Play();
  170. }
  171. _video?.Play();
  172. break;
  173. case State.Playing:
  174. if (_audio != null && !Memory.Threaded)
  175. {
  176. _audio.NextLoop();
  177. }
  178. if (_video == null)
  179. State = State.Finished;
  180. else if (_video.Behind)
  181. {
  182. if (_video.Next() < 0)
  183. {
  184. State = State.Finished;
  185. //Memory.SuppressDraw = true;
  186. break;
  187. }
  188. if (_texture != null)
  189. {
  190. _texture.Dispose();
  191. GC.Collect();
  192. GC.WaitForPendingFinalizers();
  193. _texture = null;
  194. }
  195. }
  196. else
  197. {
  198. //Memory next frame is skipped.
  199. Memory.SuppressDraw = _suppressDraw;
  200. }
  201. if (_texture == null)
  202. {
  203. if (_video != null)
  204. {
  205. if (Memory.State?.FieldVars != null)
  206. Memory.State.FieldVars.FMVFrames = (ulong)_video.CurrentFrameNum;
  207. _texture = _video.Texture2D();
  208. }
  209. }
  210. break;
  211. case State.Paused:
  212. //todo add a function to pause sound
  213. //pausing the stopwatch will cause the video to pause because it calculates the current frame based on time.
  214. break;
  215. case State.Finished:
  216. break;
  217. case State.Reset:
  218. break;
  219. }
  220. }
  221. protected virtual void Dispose(bool disposing)
  222. {
  223. if (_disposedValue) return;
  224. if (disposing)
  225. {
  226. // TODO: dispose managed state (managed objects).
  227. }
  228. // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
  229. // TODO: set large fields to null.
  230. if (!(_video?.IsDisposed ?? true))
  231. _video.Dispose();
  232. if (!(_audio?.IsDisposed ?? true))
  233. _audio.Dispose();
  234. if (_texture != null && !_texture.IsDisposed)
  235. _texture.Dispose();
  236. _disposedValue = true;
  237. }
  238. private static void ClearScreen() => Memory.SpriteBatch.GraphicsDevice.Clear(Color.Black);
  239. #endregion Methods
  240. // TODO: uncomment the following line if the finalizer is overridden above.// GC.SuppressFinalize(this);
  241. }
  242. }