// MonoGame - Copyright (C) The MonoGame Team // This file is subject to the terms and conditions defined in // file 'LICENSE.txt', which is part of this source code package. using System; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input.Touch; namespace Microsoft.Xna.Framework { abstract partial class GamePlatform : IDisposable { #region Fields protected TimeSpan _inactiveSleepTime = TimeSpan.FromMilliseconds(20.0); protected bool _needsToResetElapsedTime = false; bool disposed; protected bool InFullScreenMode = false; protected bool IsDisposed { get { return disposed; } } #endregion #region Construction/Destruction protected GamePlatform(Game game) { if (game == null) throw new ArgumentNullException("game"); Game = game; } ~GamePlatform() { Dispose(false); } #endregion Construction/Destruction #region Public Properties /// /// When implemented in a derived class, reports the default /// GameRunBehavior for this platform. /// public abstract GameRunBehavior DefaultRunBehavior { get; } /// /// Gets the Game instance that owns this GamePlatform instance. /// public Game Game { get; private set; } private bool _isActive; public bool IsActive { get { return _isActive; } internal set { if (_isActive != value) { _isActive = value; EventHelpers.Raise(this, _isActive ? Activated : Deactivated, EventArgs.Empty); } } } private bool _isMouseVisible; public bool IsMouseVisible { get { return _isMouseVisible; } set { if (_isMouseVisible != value) { _isMouseVisible = value; OnIsMouseVisibleChanged(); } } } private GameWindow _window; public GameWindow Window { get { return _window; } protected set { if (_window == null) { Mouse.PrimaryWindow = value; TouchPanel.PrimaryWindow = value; } _window = value; } } #endregion #region Events public event EventHandler AsyncRunLoopEnded; public event EventHandler Activated; public event EventHandler Deactivated; /// /// Raises the AsyncRunLoopEnded event. This method must be called by /// derived classes when the asynchronous run loop they start has /// stopped running. /// protected void RaiseAsyncRunLoopEnded() { EventHelpers.Raise(this, AsyncRunLoopEnded, EventArgs.Empty); } #endregion Events #region Methods /// /// Gives derived classes an opportunity to do work before any /// components are initialized. Note that the base implementation sets /// IsActive to true, so derived classes should either call the base /// implementation or set IsActive to true by their own means. /// public virtual void BeforeInitialize() { IsActive = true; } /// /// Gives derived classes an opportunity to do work just before the /// run loop is begun. Implementations may also return false to prevent /// the run loop from starting. /// /// public virtual bool BeforeRun() { return true; } /// /// When implemented in a derived, ends the active run loop. /// public abstract void Exit(); /// /// When implemented in a derived, starts the run loop and blocks /// until it has ended. /// public abstract void RunLoop(); /// /// When implemented in a derived, starts the run loop and returns /// immediately. /// public abstract void StartRunLoop(); /// /// Gives derived classes an opportunity to do work just before Update /// is called for all IUpdatable components. Returning false from this /// method will result in this round of Update calls being skipped. /// /// /// public abstract bool BeforeUpdate(GameTime gameTime); /// /// Gives derived classes an opportunity to do work just before Draw /// is called for all IDrawable components. Returning false from this /// method will result in this round of Draw calls being skipped. /// /// /// public abstract bool BeforeDraw(GameTime gameTime); /// /// When implemented in a derived class, causes the game to enter /// full-screen mode. /// public abstract void EnterFullScreen(); /// /// When implemented in a derived class, causes the game to exit /// full-screen mode. /// public abstract void ExitFullScreen(); /// /// Gives derived classes an opportunity to modify /// Game.TargetElapsedTime before it is set. /// /// The proposed new value of TargetElapsedTime. /// The new value of TargetElapsedTime that will be set. public virtual TimeSpan TargetElapsedTimeChanging(TimeSpan value) { return value; } /// /// Starts a device transition (windowed to full screen or vice versa). /// /// /// Specifies whether the device will be in full-screen mode upon completion of the change. /// public abstract void BeginScreenDeviceChange ( bool willBeFullScreen ); /// /// Completes a device transition. /// /// /// Screen device name. /// /// /// The new width of the game's client window. /// /// /// The new height of the game's client window. /// public abstract void EndScreenDeviceChange ( string screenDeviceName, int clientWidth, int clientHeight ); /// /// Gives derived classes an opportunity to take action after /// Game.TargetElapsedTime has been set. /// public virtual void TargetElapsedTimeChanged() {} /// /// MSDN: Use this method if your game is recovering from a slow-running state, and ElapsedGameTime is too large to be useful. /// Frame timing is generally handled by the Game class, but some platforms still handle it elsewhere. Once all platforms /// rely on the Game class's functionality, this method and any overrides should be removed. /// public virtual void ResetElapsedTime() {} public virtual void Present() { } protected virtual void OnIsMouseVisibleChanged() {} /// /// Called by the GraphicsDeviceManager to notify the platform /// that the presentation parameters have changed. /// /// The new presentation parameters. internal virtual void OnPresentationChanged(PresentationParameters pp) { } #endregion Methods #region IDisposable implementation /// /// Performs application-defined tasks associated with freeing, /// releasing, or resetting unmanaged resources. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!disposed) { Mouse.PrimaryWindow = null; TouchPanel.PrimaryWindow = null; disposed = true; } } /// /// Log the specified Message. /// /// /// /// [System.Diagnostics.Conditional("DEBUG")] public virtual void Log(string Message) {} #endregion } }