| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using System;
- using System.Linq;
- namespace OpenVIII.Movie
- {
- public class Player : IDisposable
- {
- #region Fields
- public static readonly int[] LetterBox = new int[] { 101, 103, 104 };
- private static Files Files;
- private STATE _state;
- private AV.Audio Audio;
- private bool disposedValue = false;
- private bool SuppressDraw;
- private Texture2D Texture;
- private AV.Video Video;
- #endregion Fields
- #region Destructors
- // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
- ~Player()
- {
- // // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
- Dispose(false);
- }
- #endregion Destructors
- #region Events
- public event EventHandler<STATE> StateChanged;
- #endregion Events
- #region Properties
- public int ID { get; private set; }
- // To detect redundant calls
- public bool IsDisposed => disposedValue;
- public STATE STATE
- {
- get => _state; private set
- {
- _state = value;
- StateChanged?.Invoke(this, value);
- }
- }
- #endregion Properties
- #region Methods
- public static Player Load(int ID, bool OverlayingModels = false)
- {
- if (Files.Exists(ID))
- {
- Player Player = new Player()
- {
- ID = ID,
- STATE = STATE.LOAD,
- Video = AV.Video.Load(Files[ID]),
- Audio = AV.Audio.Load(Files[ID]),
- SuppressDraw = !OverlayingModels
- };
- Player.STATE++;
- return Player;
- }
- return null;
- }
- // This code added to correctly implement the disposable pattern.
- public void Dispose() =>
- // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
- Dispose(true);
- public void Draw()
- {
- switch (STATE)
- {
- case STATE.LOAD:
- break;
- case STATE.CLEAR:
- STATE++;
- ClearScreen();
- break;
- case STATE.STARTPLAY:
- case STATE.PLAYING:
- PlayingDraw();
- break;
- case STATE.PAUSED:
- break;
- case STATE.FINISHED:
- STATE++;
- PlayingDraw();
- break;
- case STATE.RESET:
- break;
- case STATE.RETURN:
- default:
- break;
- }
- }
- public void PlayingDraw()
- {
- if (Texture == null)
- {
- return;
- }
- //draw frame;
- Viewport vp = Memory.graphics.GraphicsDevice.Viewport;
- Memory.SpriteBatchStartStencil(ss: SamplerState.AnisotropicClamp);//by default xna filters all textures SamplerState.PointClamp disables that. so video is being filtered why playing.
- ClearScreen();
- Rectangle dst = new Rectangle(new Point(0), (new Vector2(Texture.Width, Texture.Height) * Memory.Scale(Texture.Width, Texture.Height, LetterBox.Contains(ID) ? Memory.ScaleMode.FitHorizontal : Memory.ScaleMode.FitBoth)).ToPoint());
- dst.Offset(Memory.Center.X - dst.Center.X, Memory.Center.Y - dst.Center.Y);
- Memory.spriteBatch.Draw(Texture, dst, Microsoft.Xna.Framework.Color.White);
- Memory.SpriteBatchEnd();
- }
- public void STOP()
- {
- Audio.Dispose();
- Video.Dispose();
- STATE = STATE.RETURN;
- }
- public void Update()
- {
- switch (STATE)
- {
- case STATE.LOAD:
- break;
- case STATE.CLEAR:
- break;
- case STATE.STARTPLAY:
- STATE++;
- if (Audio != null)
- {
- Audio.PlayInTask();
- }
- if (Video != null)
- {
- Video.Play();
- }
- break;
- case STATE.PLAYING:
- //if (Audio != null && !Audio.Ahead)
- //{
- // // if we are behind the timer get the next frame of audio.
- // Audio.Next();
- //}
- if (Video == null)
- STATE = STATE.FINISHED;
- else if (Video.Behind)
- {
- if (Video.Next() < 0)
- {
- STATE = STATE.FINISHED;
- //Memory.SuppressDraw = true;
- break;
- }
- else if (Texture != null)
- {
- Texture.Dispose();
- GC.Collect();
- GC.WaitForPendingFinalizers();
- Texture = null;
- }
- }
- else
- {
- //Memory next frame is skipped.
- Memory.SuppressDraw = SuppressDraw;
- }
- if (Texture == null)
- {
- if (Video != null)
- {
- if (Memory.State?.Fieldvars != null)
- Memory.State.Fieldvars.FMVFrames = (ulong)Video.CurrentFrameNum;
- Texture = Video.Texture2D();
- }
- }
- break;
- case STATE.PAUSED:
- //todo add a function to pause sound
- //pausing the stopwatch will cause the video to pause because it calcs the current frame based on time.
- break;
- case STATE.FINISHED:
- break;
- case STATE.RESET:
- break;
- case STATE.RETURN:
- default:
- break;
- }
- }
- protected virtual void Dispose(bool disposing)
- {
- if (!disposedValue)
- {
- if (disposing)
- {
- // TODO: dispose managed state (managed objects).
- }
- // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
- // TODO: set large fields to null.
- if (!Video.IsDisposed)
- Video.Dispose();
- if (!Audio.IsDisposed)
- Audio.Dispose();
- if (Texture != null && !Texture.IsDisposed)
- Texture.Dispose();
- disposedValue = true;
- }
- }
- private static void ClearScreen() => Memory.spriteBatch.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black);
- #endregion Methods
- // TODO: uncomment the following line if the finalizer is overridden above.// GC.SuppressFinalize(this);
- }
- }
|