VIS2 SS2013 CVD DVR
 All Classes Namespaces Functions Enumerations Properties
VisGraphicsDevice.cs
1 #region Using Statements
2 using System;
3 using System.Threading;
4 using Microsoft.Xna.Framework;
5 using Microsoft.Xna.Framework.Graphics;
6 #endregion
7 
8 #pragma warning disable 67
9 
10 namespace visLU
11 {
16  class VisGraphicsDevice: IGraphicsDeviceService
17  {
18 
19  #region Variables
20 
21  //GraphicsDevice instance
22  static VisGraphicsDevice instance;
23  //how many controls are sharing the graphics device
24  static int referenceCount;
25 
26  GraphicsDevice graphicsDevice;
27  // Store the current device settings.
28  PresentationParameters parameters;
29  //GraphicsDeviceManager graphics;//todo: do we need this? + setGraphicsDeviceManager(); ->(Game1.cs)
30 
31  //IGraphicsDeviceService events
32  public event EventHandler<EventArgs> DeviceCreated;
33  public event EventHandler<EventArgs> DeviceDisposing;
34  public event EventHandler<EventArgs> DeviceReset;
35  public event EventHandler<EventArgs> DeviceResetting;
36 
37  #endregion
38 
39  #region Properties
44  {
45  get { return graphicsDevice; }
46  }
47 
48 
49  #endregion
50 
51  #region Constructor
52 
53  VisGraphicsDevice(IntPtr windowHandle, int width, int height)
54  {
55  parameters = new PresentationParameters();
56 
57  parameters.BackBufferWidth = Math.Max(width, 1);
58  parameters.BackBufferHeight = Math.Max(height, 1);
59  parameters.BackBufferFormat = SurfaceFormat.Rgba64;
60  parameters.DepthStencilFormat = DepthFormat.Depth24;
61  parameters.DeviceWindowHandle = windowHandle;
62  //parameters.MultiSampleCount = 8;
63  parameters.PresentationInterval = PresentInterval.Default;
64  parameters.IsFullScreen = false;
65  parameters.MultiSampleCount = 0;
66 
67  graphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter,
68  GraphicsProfile.HiDef,
69  parameters);
70  }
71 
72  #endregion
73 
78  public static VisGraphicsDevice Add(IntPtr windowHandle, int width, int height)
79  {
80 
81  if (Interlocked.Increment(ref referenceCount) == 1)
82  {
83  instance = new VisGraphicsDevice(windowHandle, width, height);
84  }
85 
86  return instance;
87  }
88 
89 
93  public void Release(bool disposing)
94  {
95  //Decrement the control references count to the VisGraphicsDevice instance
96  if (Interlocked.Decrement(ref referenceCount) == 0)
97  {
98  // Dispose the VisGraphicsDevice instance if no more control references
99  if (disposing)
100  {
101  if (DeviceDisposing != null)
102  DeviceDisposing(this, EventArgs.Empty);
103 
104  graphicsDevice.Dispose();
105  }
106 
107  graphicsDevice = null;
108  }
109  }
110 
111 
117  public void ResetDevice(int width, int height)
118  {
119  if (DeviceResetting != null)
120  DeviceResetting(this, EventArgs.Empty);
121 
122  parameters.BackBufferWidth = Math.Max(parameters.BackBufferWidth, width);
123  parameters.BackBufferHeight = Math.Max(parameters.BackBufferHeight, height);
124 
125  graphicsDevice.Reset(parameters);
126 
127  if (DeviceReset != null)
128  DeviceReset(this, EventArgs.Empty);
129  }
130 
131 
132  }
133 
134 }