#region CPL License /* Nuclex Framework Copyright (C) 2002-2009 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #endregion using System; using System.Collections.Generic; using System.Reflection; using System.Runtime.InteropServices; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using DeviceEventHandler = System.EventHandler; namespace Nuclex.Graphics { /// Provides supporting functions for the graphics device service public static class GraphicsDeviceServiceHelper { #region class DummyGraphicsDeviceService /// Dummy graphics device service using an existing graphics device private class DummyGraphicsDeviceService : IGraphicsDeviceService, IDisposable { /// Triggered when the graphics device has been created public event DeviceEventHandler DeviceCreated { add { } remove { } } /// Triggered when the graphics device is about to be disposed public event DeviceEventHandler DeviceDisposing; /// Triggered after the graphics device has completed a reset public event DeviceEventHandler DeviceReset; /// Triggered when the graphcis device is about to be reset public event DeviceEventHandler DeviceResetting; /// Initializes a new dummy graphics device service /// Graphics device the service will use public DummyGraphicsDeviceService(GraphicsDevice graphicsDevice) { this.graphicsDevice = graphicsDevice; this.graphicsDeviceResettingDelegate = new DeviceEventHandler( graphicsDeviceResetting ); this.graphicsDeviceResetDelegate = new DeviceEventHandler( graphicsDeviceReset ); this.graphicsDeviceDisposingDelegate = new DeviceEventHandler( graphicsDeviceDisposing ); graphicsDevice.DeviceResetting += this.graphicsDeviceResettingDelegate; graphicsDevice.DeviceReset += this.graphicsDeviceResetDelegate; graphicsDevice.Disposing += this.graphicsDeviceDisposingDelegate; } /// Immediately releases all resouces owned by the instance public void Dispose() { if(this.graphicsDevice != null) { graphicsDeviceDisposing(this.graphicsDevice, EventArgs.Empty); this.graphicsDevice.Disposing -= this.graphicsDeviceDisposingDelegate; this.graphicsDevice.DeviceReset -= this.graphicsDeviceResetDelegate; this.graphicsDevice.DeviceResetting -= this.graphicsDeviceResettingDelegate; this.graphicsDevice = null; } } /// Graphics device provided by the service public GraphicsDevice GraphicsDevice { get { return this.graphicsDevice; } } /// Called when the graphics device is about to reset /// Graphics device that is started a reset /// Not used private void graphicsDeviceResetting(object sender, EventArgs arguments) { if(DeviceResetting != null) { DeviceResetting(this, EventArgs.Empty); } } /// Called when the graphics device has completed a reset /// Graphics device that has completed its reset /// Not used private void graphicsDeviceReset(object sender, EventArgs arguments) { if(DeviceReset != null) { DeviceReset(this, EventArgs.Empty); } } /// Called when the graphics device is being disposed /// Graphics device that is being disposed /// Not used private void graphicsDeviceDisposing(object sender, EventArgs arguments) { if(DeviceDisposing != null) { DeviceDisposing(this, EventArgs.Empty); } } /// Graphics device the dummy service is being created for private GraphicsDevice graphicsDevice; /// Delegate for the graphicsDeviceResetting() method private DeviceEventHandler graphicsDeviceResettingDelegate; /// Delegate for the graphicsDeviceReset() method private DeviceEventHandler graphicsDeviceResetDelegate; /// Delegate for the graphicsDeviceDisposing() method private DeviceEventHandler graphicsDeviceDisposingDelegate; } #endregion // class DummyGraphicsDeviceService /// /// Creates a service provider containing only the graphics device service /// /// /// Graphics device service that will be provided by the service provider /// /// /// A new service provider that provides the graphics device service /// public static IServiceProvider MakePrivateServiceProvider( IGraphicsDeviceService graphicsDeviceService ) { GameServiceContainer serviceContainer = new GameServiceContainer(); serviceContainer.AddService( typeof(IGraphicsDeviceService), graphicsDeviceService ); return serviceContainer; } /// /// Creates a dummy graphics device service for the provided graphics device /// /// /// Graphics device the dummy service is created around /// /// A new dummy service for the provided graphics device /// /// The dummy graphics device service is in all terms equal to the real thing, /// except that it will trigger the service's events *after* the graphics device /// might have already notified other subscribers. /// public static IGraphicsDeviceService MakeDummyGraphicsDeviceService( GraphicsDevice graphicsDevice ) { return new DummyGraphicsDeviceService(graphicsDevice); } } } // namespace Nuclex.Graphics