forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharedGraphicsDeviceManager.cs
133 lines (101 loc) · 4.41 KB
/
SharedGraphicsDeviceManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Windows.UI.Xaml.Controls;
namespace Microsoft.Xna.Framework
{
public class SharedGraphicsDeviceManager : IGraphicsDeviceService, IDisposable
{
public static readonly int DefaultBackBufferHeight;
public static readonly int DefaultBackBufferWidth;
static SharedGraphicsDeviceManager()
{
DefaultBackBufferWidth = 1024;
DefaultBackBufferHeight = 768;
}
private GameTimer _timer;
public SharedGraphicsDeviceManager()
{
if (Current != null)
throw new InvalidOperationException("Only one device manager can be created per process!");
GraphicsProfile = GraphicsProfile.HiDef;
PreferredBackBufferFormat = SurfaceFormat.Color;
PreferredBackBufferWidth = DefaultBackBufferWidth;
PreferredBackBufferHeight = DefaultBackBufferHeight;
PreferredDepthStencilFormat = DepthFormat.Depth24;
PresentationInterval = PresentInterval.One;
SynchronizeWithVerticalRetrace = true;
Current = this;
}
public GraphicsDevice GraphicsDevice { get; private set; }
public GraphicsProfile GraphicsProfile { get; set; }
public int MultiSampleCount { get; set; }
public SurfaceFormat PreferredBackBufferFormat { get; set; }
public int PreferredBackBufferWidth { get; set; }
public int PreferredBackBufferHeight { get; set; }
public DepthFormat PreferredDepthStencilFormat { get; set; }
public PresentInterval PresentationInterval { get; set; }
public RenderTargetUsage RenderTargetUsage { get; set; }
public bool SynchronizeWithVerticalRetrace { get; set; }
#if WINDOWS_UAP
[CLSCompliant(false)]
public SwapChainPanel SwapChainPanel { get; set; }
#else
[CLSCompliant(false)]
public SwapChainBackgroundPanel SwapChainBackgroundPanel { get; set; }
#endif
public event EventHandler<EventArgs> DeviceCreated;
public event EventHandler<EventArgs> DeviceDisposing;
public event EventHandler<EventArgs> DeviceReset;
public event EventHandler<EventArgs> DeviceResetting;
public static SharedGraphicsDeviceManager Current { get; private set; }
public void ApplyChanges()
{
var createDevice = GraphicsDevice == null;
var presentationParameters = createDevice ? new PresentationParameters() : GraphicsDevice.PresentationParameters;
presentationParameters.BackBufferWidth = PreferredBackBufferWidth;
presentationParameters.BackBufferHeight = PreferredBackBufferHeight;
presentationParameters.BackBufferFormat = PreferredBackBufferFormat;
presentationParameters.DepthStencilFormat = PreferredDepthStencilFormat;
presentationParameters.MultiSampleCount = MultiSampleCount;
presentationParameters.PresentationInterval = PresentationInterval;
presentationParameters.IsFullScreen = false;
#if WINDOWS_UAP
presentationParameters.SwapChainPanel = this.SwapChainPanel;
#else
presentationParameters.SwapChainBackgroundPanel = this.SwapChainBackgroundPanel;
#endif
if (createDevice)
{
GraphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile, presentationParameters);
}
else
{
GraphicsDevice.CreateSizeDependentResources();
GraphicsDevice.ApplyRenderTargets(null);
}
// Set the new display size on the touch panel.
//
// TODO: In XNA this seems to be done as part of the
// GraphicsDevice.DeviceReset event... we need to get
// those working.
//
TouchPanel.DisplayWidth = GraphicsDevice.PresentationParameters.BackBufferWidth;
TouchPanel.DisplayHeight = GraphicsDevice.PresentationParameters.BackBufferHeight;
}
public void Dispose()
{
if (_timer != null)
{
_timer.Dispose();
_timer = null;
}
if ( GraphicsDevice != null )
{
GraphicsDevice.Dispose();
GraphicsDevice = null;
}
Current = null;
}
}
}