This repository was archived by the owner on Nov 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathGraphicsDeviceService.cs
More file actions
212 lines (171 loc) · 7.26 KB
/
GraphicsDeviceService.cs
File metadata and controls
212 lines (171 loc) · 7.26 KB
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
#region File Description
//-----------------------------------------------------------------------------
// GraphicsDeviceService.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Threading;
using System.Windows.Forms;
#endregion
// The IGraphicsDeviceService interface requires a DeviceCreated event, but we
// always just create the device inside our constructor, so we have no place to
// raise that event. The C# compiler warns us that the event is never used, but
// we don't care so we just disable this warning.
#pragma warning disable 67
namespace PasiveRadar
{
/// <summary>
/// Helper class responsible for creating and managing the GraphicsDevice.
/// All GraphicsDeviceControl instances share the same GraphicsDeviceService,
/// so even though there can be many controls, there will only ever be a single
/// underlying GraphicsDevice. This implements the standard IGraphicsDeviceService
/// interface, which provides notification events for when the device is reset
/// or disposed.
/// </summary>
public class GraphicsDeviceService : IGraphicsDeviceService, IDisposable
{
#region Fields
// Singleton device service instance.
static GraphicsDeviceService singletonInstance;
// Keep track of how many controls are sharing the singletonInstance.
static int referenceCount;
PresentationParameters presentation_parameters;
#endregion
/// <summary>
/// Constructor is private, because this is a singleton class:
/// client controls should use the public AddRef method instead.
/// </summary>
public GraphicsDeviceService(IntPtr windowHandle, int width, int height)
{
CreateDevice(windowHandle, width, height);
}
public void CreateDevice(IntPtr windowHandle, int width, int height)
{
// Create Presentation Parameters
presentation_parameters = new PresentationParameters();
// pp.BackBufferCount = 1;
presentation_parameters.IsFullScreen = false;
// pp.SwapEffect = SwapEffect.Discard;
presentation_parameters.BackBufferWidth = Math.Max(width, 1);
presentation_parameters.BackBufferHeight = Math.Max(height, 1);
// pp.AutoDepthStencilFormat = DepthFormat.Depth24Stencil8;
// pp.EnableAutoDepthStencil = true;
presentation_parameters.PresentationInterval = PresentInterval.Default;
presentation_parameters.BackBufferFormat = SurfaceFormat.Color;
// pp.MultiSampleType = MultiSampleType.None;
presentation_parameters.DepthStencilFormat = DepthFormat.Depth24Stencil8;
presentation_parameters.DeviceWindowHandle = windowHandle;
presentation_parameters.PresentationInterval = PresentInterval.Immediate;
// Create device
graphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.Reach,
// this.panelViewport.Handle,
// CreateOptions.HardwareVertexProcessing,
presentation_parameters);
}
/// <summary>
/// Gets a reference to the singleton instance.
/// </summary>
public static GraphicsDeviceService AddRef(IntPtr windowHandle,
int width, int height)
{
// Increment the "how many controls sharing the device" reference count.
if (Interlocked.Increment(ref referenceCount) == 1)
{
// If this is the first control to start using the
// device, we must create the singleton instance.
singletonInstance = new GraphicsDeviceService(windowHandle,
width, height);
}
return singletonInstance;
}
/// <summary>
/// Releases a reference to the singleton instance.
/// </summary>
public void Release(bool disposing)
{
// Decrement the "how many controls sharing the device" reference count.
if (Interlocked.Decrement(ref referenceCount) == 0)
{
// If this is the last control to finish using the
// device, we should dispose the singleton instance.
if (disposing)
{
if (DeviceDisposing != null)
DeviceDisposing(this, EventArgs.Empty);
graphicsDevice.Dispose();
}
graphicsDevice = null;
}
}
/// <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)
{
ResetingDevice();
if (presentation_parameters == null)
MessageBox.Show("Presentation parameters is null.", "Important Message");
try
{
graphicsDevice.Reset(presentation_parameters);
}
catch
{
Release(true);
ResetingDevice();
DeviceReset(this, EventArgs.Empty);
//graphicsDevice.Reset(presentation_parameters);
}
presentation_parameters.BackBufferWidth = Math.Max(presentation_parameters.BackBufferWidth, width);
presentation_parameters.BackBufferHeight = Math.Max(presentation_parameters.BackBufferHeight, height);
if (DeviceReset != null)
try
{
DeviceReset(this, EventArgs.Empty);
}
catch (Exception e)
{
MessageBox.Show("Reset device:" + e, "Important Message");
}
}
public void ResetingDevice()
{
if (DeviceResetting != null)
DeviceResetting(this, EventArgs.Empty);
}
/// <summary>
/// Gets the current graphics device.
/// </summary>
public GraphicsDevice GraphicsDevice
{
get { return graphicsDevice; }
}
GraphicsDevice graphicsDevice;
// IGraphicsDeviceService events.
public event EventHandler<System.EventArgs> DeviceCreated;
public event EventHandler<System.EventArgs> DeviceDisposing;
public event EventHandler<System.EventArgs> DeviceReset;
public event EventHandler<System.EventArgs> DeviceResetting;
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// dispose managed resources
graphicsDevice.Dispose();
}
// free native resources
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}