forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphicsDeviceManager.cs
508 lines (446 loc) · 18.1 KB
/
GraphicsDeviceManager.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// 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.Touch;
namespace Microsoft.Xna.Framework
{
/// <summary>
/// Used to initialize and control the presentation of the graphics device.
/// </summary>
public partial class GraphicsDeviceManager : IGraphicsDeviceService, IDisposable, IGraphicsDeviceManager
{
private readonly Game _game;
private GraphicsDevice _graphicsDevice;
private int _preferredBackBufferHeight;
private int _preferredBackBufferWidth;
private SurfaceFormat _preferredBackBufferFormat;
private DepthFormat _preferredDepthStencilFormat;
private bool _preferMultiSampling;
private DisplayOrientation _supportedOrientations;
private bool _synchronizedWithVerticalRetrace = true;
private bool _drawBegun;
private bool _disposed;
private bool _hardwareModeSwitch = true;
private bool _wantFullScreen;
/// <summary>
/// The default back buffer width.
/// </summary>
public static readonly int DefaultBackBufferWidth = 800;
/// <summary>
/// The default back buffer height.
/// </summary>
public static readonly int DefaultBackBufferHeight = 480;
/// <summary>
/// Optional override for platform specific defaults.
/// </summary>
partial void PlatformConstruct();
/// <summary>
/// Associates this graphics device manager to a game instances.
/// </summary>
/// <param name="game">The game instance to attach.</param>
public GraphicsDeviceManager(Game game)
{
if (game == null)
throw new ArgumentNullException("game", "Game cannot be null.");
_game = game;
_supportedOrientations = DisplayOrientation.Default;
_preferredBackBufferFormat = SurfaceFormat.Color;
_preferredDepthStencilFormat = DepthFormat.Depth24;
_synchronizedWithVerticalRetrace = true;
// Assume the window client size as the default back
// buffer resolution in the landscape orientation.
var clientBounds = _game.Window.ClientBounds;
if (clientBounds.Width >= clientBounds.Height)
{
_preferredBackBufferWidth = clientBounds.Width;
_preferredBackBufferHeight = clientBounds.Height;
}
else
{
_preferredBackBufferWidth = clientBounds.Height;
_preferredBackBufferHeight = clientBounds.Width;
}
// Default to windowed mode... this is ignored on platforms that don't support it.
_wantFullScreen = false;
// XNA would read this from the manifest, but it would always default
// to reach unless changed. So lets mimic that without the manifest bit.
GraphicsProfile = GraphicsProfile.Reach;
// Let the plaform optionally overload construction defaults.
PlatformConstruct();
if (_game.Services.GetService(typeof(IGraphicsDeviceManager)) != null)
throw new ArgumentException("A graphics device manager is already registered. The graphics device manager cannot be changed once it is set.");
_game.Services.AddService(typeof(IGraphicsDeviceManager), this);
_game.Services.AddService(typeof(IGraphicsDeviceService), this);
}
~GraphicsDeviceManager()
{
Dispose(false);
}
public void CreateDevice()
{
if (_graphicsDevice != null)
return;
try
{
Initialize();
}
catch (NoSuitableGraphicsDeviceException)
{
throw;
}
catch (Exception ex)
{
throw new NoSuitableGraphicsDeviceException("Failed to create graphics device!", ex);
}
OnDeviceCreated(EventArgs.Empty);
}
public bool BeginDraw()
{
if (_graphicsDevice == null)
return false;
_drawBegun = true;
return true;
}
public void EndDraw()
{
if (_graphicsDevice != null && _drawBegun)
{
_drawBegun = false;
_graphicsDevice.Present();
}
}
#region IGraphicsDeviceService Members
public event EventHandler<EventArgs> DeviceCreated;
public event EventHandler<EventArgs> DeviceDisposing;
public event EventHandler<EventArgs> DeviceReset;
public event EventHandler<EventArgs> DeviceResetting;
public event EventHandler<PreparingDeviceSettingsEventArgs> PreparingDeviceSettings;
// FIXME: Why does the GraphicsDeviceManager not know enough about the
// GraphicsDevice to raise these events without help?
internal void OnDeviceDisposing(EventArgs e)
{
Raise(DeviceDisposing, e);
}
// FIXME: Why does the GraphicsDeviceManager not know enough about the
// GraphicsDevice to raise these events without help?
internal void OnDeviceResetting(EventArgs e)
{
Raise(DeviceResetting, e);
}
// FIXME: Why does the GraphicsDeviceManager not know enough about the
// GraphicsDevice to raise these events without help?
internal void OnDeviceReset(EventArgs e)
{
Raise(DeviceReset, e);
}
// FIXME: Why does the GraphicsDeviceManager not know enough about the
// GraphicsDevice to raise these events without help?
internal void OnDeviceCreated(EventArgs e)
{
Raise(DeviceCreated, e);
}
private void Raise<TEventArgs>(EventHandler<TEventArgs> handler, TEventArgs e)
where TEventArgs : EventArgs
{
if (handler != null)
handler(this, e);
}
#endregion
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
if (_graphicsDevice != null)
{
_graphicsDevice.Dispose();
_graphicsDevice = null;
}
}
_disposed = true;
}
}
#endregion
partial void PlatformApplyChanges();
private void PreparePresentationParameters(PresentationParameters presentationParameters)
{
presentationParameters.BackBufferFormat = _preferredBackBufferFormat;
presentationParameters.BackBufferWidth = _preferredBackBufferWidth;
presentationParameters.BackBufferHeight = _preferredBackBufferHeight;
presentationParameters.DepthStencilFormat = _preferredDepthStencilFormat;
presentationParameters.IsFullScreen = _wantFullScreen;
presentationParameters.PresentationInterval = _synchronizedWithVerticalRetrace ? PresentInterval.One : PresentInterval.Immediate;
presentationParameters.DisplayOrientation = _game.Window.CurrentOrientation;
presentationParameters.DeviceWindowHandle = _game.Window.Handle;
if (_preferMultiSampling)
{
if (_graphicsDevice == null)
{
// We can't determine the multisampling level by calling PlatformSetMultiSamplingToMaximum yet.
// Once the device initializes, it will call CreateSizeDependentResources which will perform
// a call to PlatformSetMultiSamplingToMaximum.
presentationParameters.MultiSampleCount = 32;
}
else
{
int quality;
_graphicsDevice.PlatformSetMultiSamplingToMaximum(presentationParameters, out quality);
}
}
else
{
presentationParameters.MultiSampleCount = 0;
}
}
/// <summary>
/// Applies any pending property changes to the graphics device.
/// </summary>
public void ApplyChanges()
{
// If the device hasn't been created then create it now.
if (_graphicsDevice == null)
CreateDevice();
_game.Window.SetSupportedOrientations(_supportedOrientations);
PreparePresentationParameters(_graphicsDevice.PresentationParameters);
// TODO: Should this trigger some sort of device reset?
_graphicsDevice.GraphicsProfile = GraphicsProfile;
// Allow for optional platform specific behavior.
PlatformApplyChanges();
// Update the graphics device and then the platform window.
_graphicsDevice.OnPresentationChanged();
_game.Platform.OnPresentationChanged();
// 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;
}
partial void PlatformInitialize(PresentationParameters presentationParameters);
private void Initialize()
{
_game.Window.SetSupportedOrientations(_supportedOrientations);
var presentationParameters = new PresentationParameters();
PreparePresentationParameters(presentationParameters);
// Allow for any per-platform changes to the presentation.
PlatformInitialize(presentationParameters);
// TODO: Implement multisampling (aka anti-alising) for all platforms!
if (PreparingDeviceSettings != null)
{
var gdi = new GraphicsDeviceInformation();
gdi.GraphicsProfile = GraphicsProfile; // Microsoft defaults this to Reach.
gdi.Adapter = GraphicsAdapter.DefaultAdapter;
gdi.PresentationParameters = presentationParameters;
var pe = new PreparingDeviceSettingsEventArgs(gdi);
PreparingDeviceSettings(this, pe);
presentationParameters = pe.GraphicsDeviceInformation.PresentationParameters;
GraphicsProfile = pe.GraphicsDeviceInformation.GraphicsProfile;
}
// Create and initialize the graphics device.
_graphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile, presentationParameters);
// 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;
TouchPanel.DisplayOrientation = _graphicsDevice.PresentationParameters.DisplayOrientation;
}
/// <summary>
/// Toggles between windowed and fullscreen modes.
/// </summary>
/// <remarks>
/// Note that on platforms that do not support windowed modes this has no affect.
/// </remarks>
public void ToggleFullScreen()
{
IsFullScreen = !IsFullScreen;
ApplyChanges();
}
/// <summary>
/// The profile which determines the graphics feature level.
/// </summary>
public GraphicsProfile GraphicsProfile { get; set; }
/// <summary>
/// Returns the graphics device for this manager.
/// </summary>
public GraphicsDevice GraphicsDevice
{
get
{
return _graphicsDevice;
}
}
/// <summary>
/// Indicates the desire to switch into fullscreen mode.
/// </summary>
/// <remarks>
/// When called at startup this will automatically set fullscreen mode during initialization. If
/// set after startup you must call ApplyChanges() for the fullscreen mode to be changed.
/// Note that for some platforms that do not support windowed modes this property has no affect.
/// </remarks>
public bool IsFullScreen
{
get { return _wantFullScreen; }
set
{
_wantFullScreen = value;
}
}
/// <summary>
/// Gets or sets the boolean which defines how window switches from windowed to fullscreen state.
/// "Hard" mode(true) is slow to switch, but more effecient for performance, while "soft" mode(false) is vice versa.
/// The default value is <c>true</c>.
/// </summary>
public bool HardwareModeSwitch
{
get { return _hardwareModeSwitch;}
set
{
_hardwareModeSwitch = value;
}
}
/// <summary>
/// Indicates the desire for a multisampled back buffer.
/// </summary>
/// <remarks>
/// When called at startup this will automatically set the MSAA mode during initialization. If
/// set after startup you must call ApplyChanges() for the MSAA mode to be changed.
/// </remarks>
public bool PreferMultiSampling
{
get
{
return _preferMultiSampling;
}
set
{
_preferMultiSampling = value;
}
}
/// <summary>
/// Indicates the desired back buffer color format.
/// </summary>
/// <remarks>
/// When called at startup this will automatically set the format during initialization. If
/// set after startup you must call ApplyChanges() for the format to be changed.
/// </remarks>
public SurfaceFormat PreferredBackBufferFormat
{
get
{
return _preferredBackBufferFormat;
}
set
{
_preferredBackBufferFormat = value;
}
}
/// <summary>
/// Indicates the desired back buffer height in pixels.
/// </summary>
/// <remarks>
/// When called at startup this will automatically set the height during initialization. If
/// set after startup you must call ApplyChanges() for the height to be changed.
/// </remarks>
public int PreferredBackBufferHeight
{
get
{
return _preferredBackBufferHeight;
}
set
{
_preferredBackBufferHeight = value;
}
}
/// <summary>
/// Indicates the desired back buffer width in pixels.
/// </summary>
/// <remarks>
/// When called at startup this will automatically set the width during initialization. If
/// set after startup you must call ApplyChanges() for the width to be changed.
/// </remarks>
public int PreferredBackBufferWidth
{
get
{
return _preferredBackBufferWidth;
}
set
{
_preferredBackBufferWidth = value;
}
}
/// <summary>
/// Indicates the desired depth-stencil buffer format.
/// </summary>
/// <remarks>
/// The depth-stencil buffer format defines the scene depth precision and stencil bits available for effects during rendering.
/// When called at startup this will automatically set the format during initialization. If
/// set after startup you must call ApplyChanges() for the format to be changed.
/// </remarks>
public DepthFormat PreferredDepthStencilFormat
{
get
{
return _preferredDepthStencilFormat;
}
set
{
_preferredDepthStencilFormat = value;
}
}
/// <summary>
/// Indicates the desire for vsync when presenting the back buffer.
/// </summary>
/// <remarks>
/// Vsync limits the frame rate of the game to the monitor referesh rate to prevent screen tearing.
/// When called at startup this will automatically set the vsync mode during initialization. If
/// set after startup you must call ApplyChanges() for the vsync mode to be changed.
/// </remarks>
public bool SynchronizeWithVerticalRetrace
{
get
{
return _synchronizedWithVerticalRetrace;
}
set
{
_synchronizedWithVerticalRetrace = value;
}
}
/// <summary>
/// Indicates the desired allowable display orientations when the device is rotated.
/// </summary>
/// <remarks>
/// This property only applies to mobile platforms with automatic display rotation.
/// When called at startup this will automatically apply the supported orientations during initialization. If
/// set after startup you must call ApplyChanges() for the supported orientations to be changed.
/// </remarks>
public DisplayOrientation SupportedOrientations
{
get
{
return _supportedOrientations;
}
set
{
_supportedOrientations = value;
}
}
}
}