-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphicsDeviceService.cs
90 lines (77 loc) · 3.3 KB
/
GraphicsDeviceService.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
using Microsoft.Xna.Framework.Graphics;
namespace Xnb.Exporter
{
public class GraphicsDeviceService : IGraphicsDeviceService
{
static GraphicsDeviceService singletonInstance;
static int referenceCount;
public GraphicsDevice GraphicsDevice { get; private set; }
public event EventHandler<EventArgs> DeviceCreated;
public event EventHandler<EventArgs> DeviceDisposing;
public event EventHandler<EventArgs> DeviceReset;
public event EventHandler<EventArgs> DeviceResetting;
/**
* @brief Initializes a new instance of the `GraphicsDeviceService` class.
*
* @param windowHandle The handle of the window associated with the `GraphicsDevice`.
* @param width The width of the back buffer.
* @param height The height of the back buffer.
*/
public GraphicsDeviceService(IntPtr windowHandle, int width, int height)
{
var parameters = new PresentationParameters
{
BackBufferWidth = Math.Max(width, 1),
BackBufferHeight = Math.Max(height, 1),
BackBufferFormat = SurfaceFormat.Color,
DepthStencilFormat = DepthFormat.Depth24,
DeviceWindowHandle = windowHandle,
PresentationInterval = PresentInterval.Immediate,
IsFullScreen = false
};
GraphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.Reach, parameters);
}
/**
* @brief Adds a reference to the singleton instance of the `GraphicsDeviceService`.
*
* If the singleton instance does not exist, it is created with the specified window handle,
* width, and height. The reference count is incremented.
*
* @param windowHandle The handle of the window associated with the `GraphicsDevice`.
* @param width The width of the back buffer.
* @param height The height of the back buffer.
* @return The singleton instance of the `GraphicsDeviceService`.
*/
public static GraphicsDeviceService AddRef(IntPtr windowHandle, int width, int height)
{
if (referenceCount == 0)
{
singletonInstance = new GraphicsDeviceService(windowHandle, width, height);
}
referenceCount++;
return singletonInstance;
}
/**
* @brief Releases a reference to the singleton instance of the `GraphicsDeviceService`.
*
* The reference count is decremented, and if it reaches zero, the `GraphicsDevice` is disposed
* if disposing is true. The `GraphicsDevice` property is set to null.
*
* @param disposing Indicates whether to dispose the `GraphicsDevice`.
*/
public void Release(bool disposing)
{
referenceCount--;
if (referenceCount == 0)
{
if (disposing)
{
if (DeviceDisposing != null)
DeviceDisposing(this, EventArgs.Empty);
GraphicsDevice.Dispose();
}
GraphicsDevice = null;
}
}
}
}