| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using System;
- using System.Windows;
- using System.Windows.Interop;
- using Microsoft.Xna.Framework.Graphics;
- using SharpDX.Direct3D9;
- namespace MonoGame.WpfCore.MonoGameControls
- {
- public class MonoGameGraphicsDeviceService : IGraphicsDeviceService, IDisposable
- {
- public MonoGameGraphicsDeviceService()
- {
- }
- public void Dispose()
- {
- DeviceDisposing?.Invoke(this, EventArgs.Empty);
- GraphicsDevice.Dispose();
- Direct3DDevice?.Dispose();
- Direct3DContext?.Dispose();
- }
- public Direct3DEx Direct3DContext { get; private set; }
- public DeviceEx Direct3DDevice { get; private set; }
- public event EventHandler<EventArgs> DeviceCreated;
- public event EventHandler<EventArgs> DeviceDisposing;
- public event EventHandler<EventArgs> DeviceReset;
- public event EventHandler<EventArgs> DeviceResetting;
- public void StartDirect3D(Window window)
- {
- Direct3DContext = new Direct3DEx();
- var presentParameters = new PresentParameters
- {
- Windowed = true,
- SwapEffect = SwapEffect.Discard,
- DeviceWindowHandle = new WindowInteropHelper(window).Handle,
- PresentationInterval = SharpDX.Direct3D9.PresentInterval.Default
- };
- Direct3DDevice = new DeviceEx(Direct3DContext, 0, DeviceType.Hardware, IntPtr.Zero,
- CreateFlags.HardwareVertexProcessing | CreateFlags.Multithreaded | CreateFlags.FpuPreserve,
- presentParameters);
- // Create the device using the main window handle, and a placeholder size (1,1).
- // The actual size doesn't matter because whenever we render using this GraphicsDevice,
- // we will make sure the back buffer is large enough for the window we're rendering into.
- // Also, the handle doesn't matter because we call GraphicsDevice.Present(...) with the
- // actual window handle to render into.
- GraphicsDevice = CreateGraphicsDevice(new WindowInteropHelper(window).Handle, 1, 1, 8);
- DeviceCreated?.Invoke(this, EventArgs.Empty);
- }
- // Store the current device settings.
- private PresentationParameters _parameters;
- public GraphicsDevice GraphicsDevice { get; private set; }
- public GraphicsDevice CreateGraphicsDevice(IntPtr windowHandle, int width, int height, int multisampling = 1)
- {
- _parameters = new PresentationParameters
- {
- BackBufferWidth = Math.Max(width, 1),
- BackBufferHeight = Math.Max(height, 1),
- BackBufferFormat = SurfaceFormat.Color,
- DepthStencilFormat = DepthFormat.Depth24,
- DeviceWindowHandle = windowHandle,
- MultiSampleCount = multisampling,
- PresentationInterval = Microsoft.Xna.Framework.Graphics.PresentInterval.Immediate,
- IsFullScreen = false
- };
- return new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.HiDef, _parameters);
- }
- /// <summary>
- /// Resets the graphics device to whichever is bigger out of the specified
- /// resolution or its current size. This behavior means the device will
- /// demand-grow to the largest of all its GraphicsDeviceControl clients.
- /// </summary>
- public void ResetDevice(int width, int height)
- {
- var newWidth = Math.Max(_parameters.BackBufferWidth, width);
- var newHeight = Math.Max(_parameters.BackBufferHeight, height);
- if (newWidth != _parameters.BackBufferWidth || newHeight != _parameters.BackBufferHeight)
- {
- DeviceResetting?.Invoke(this, EventArgs.Empty);
- _parameters.BackBufferWidth = newWidth;
- _parameters.BackBufferHeight = newHeight;
- GraphicsDevice.Reset(_parameters);
- DeviceReset?.Invoke(this, EventArgs.Empty);
- }
- }
- }
- }
|