/
Main.cs
349 lines (304 loc) · 8.83 KB
/
Main.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
#region File Description
//-------------------------------------------------------------------------------------------------
// VolumetricRenderer.cs
//
// Base class and entry point for the VolumetricRenderer game.
//-------------------------------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Renderer.Diagnostics;
using Renderer.Graphics.Camera;
using Renderer.Graphics.Screen;
using Renderer.Input;
#endregion
namespace Renderer
{
/// <summary>
/// Base class for the game.
/// </summary>
public class VolumetricRendererGame : Microsoft.Xna.Framework.Game
{
private delegate void UpdateMethod(GameTime gameTime);
private delegate void DrawMethod(GameTime gameTime);
#region Fields
private GraphicsDeviceManager graphicsDM;
private ScreenManager screenManager;
private InputState input;
private Camera camera;
private volatile GameTime gameTime;
public Thread mainThread; // Should be private, but 'Warnings as Errors' is turned on.
public Thread updateThread; // Should be private, but 'Warnings as Errors' is turned on.
public Thread drawThread; // Should be private, but 'Warnings as Errors' is turned on.
public Thread handleAssertThread; // Should be private, but 'Warnings as Errors' is turned on.
public object updateLock; // Should be private, but 'Warnings as Errors' is turned on.
public object drawLock; // Should be private, but 'Warnings as Errors' is turned on.
private UpdateMethod updateMethod;
private DrawMethod drawMethod;
private bool assertHandled;
#endregion
#region Properties
/// <summary>
/// Gets the GraphicsDeviceManager instance for this game.
/// </summary>
public GraphicsDeviceManager GraphicsDM
{
get
{ return graphicsDM; }
}
/// <summary>
/// Gets the main ScreenManager instance for this game.
/// </summary>
public ScreenManager ScreenManager
{
get
{ return screenManager; }
}
/// <summary>
/// Gets the InputState instance for this game.
/// </summary>
public InputState Input
{
get
{ return input; }
}
/// <summary>
/// Gets the Camera instance for this game.
/// </summary>
public Camera Camera
{
get
{ return camera; }
}
#endregion
#region Initialization
public VolumetricRendererGame()
{
Debug.OnAssert += new EventHandler<DebugEventArgs>(HandleAssert);
//Debug.BlockAfterAssert = true;
// Set up and run initial threads.
//mainThread = Thread.CurrentThread;
//mainThread.Name = "Main";
//updateLock = new object();
//updateThread = new Thread(new ThreadStart(UpdateThread));
//updateThread.Name = "Update";
//updateThread.IsBackground = true;
//updateThread.Start();
//drawLock = new object();
//drawThread = new Thread(new ThreadStart(DrawThread));
//drawThread.Name = "Draw";
//drawThread.IsBackground = true;
//drawThread.Start();
// Set up the game.
updateMethod = Update_Normal;
drawMethod = Draw_Normal;
assertHandled = false;
Content.RootDirectory = "Content";
graphicsDM = new GraphicsDeviceManager(this);
graphicsDM.PreferredBackBufferWidth = 1280;
graphicsDM.PreferredBackBufferHeight = 960;
graphicsDM.PreferMultiSampling = false;
graphicsDM.SynchronizeWithVerticalRetrace = false;
graphicsDM.IsFullScreen = false;
input = new InputState();
screenManager = new ScreenManager(this, input);
Components.Add(screenManager);
IsMouseVisible = true;
}
protected override void Initialize()
{
base.Initialize();
MenuScreen.LoadSharedContent();
screenManager.AddScreen(new BackgroundScreen());
screenManager.AddScreen(new MainMenuScreen());
camera = new Camera(graphicsDM.GraphicsDevice.Viewport);
}
protected override void UnloadContent()
{
base.UnloadContent();
MenuScreen.UnloadSharedContent();
}
#endregion
#region Update
protected override void Update(GameTime gameTime)
{
this.gameTime = gameTime;
input.Update();
camera.Update(gameTime);
//updateMethod(gameTime);
base.Update(gameTime);
}
/// <summary>
/// Normal update method to run, which signals the update thread, as long as there's no assert.
/// </summary>
private void Update_Normal(GameTime gameTime)
{
lock (updateLock)
{
Monitor.Pulse(updateLock);
Monitor.Wait(updateLock);
}
}
/// <summary>
/// Update method that we switch to after an assert, so the Update cycle can still be run
/// after the update thread has been killed off.
/// </summary>
private void Update_Assert(GameTime gameTime)
{
while (!assertHandled)
Thread.Sleep(TimeSpan.FromSeconds(1));
base.Update(gameTime);
}
/// <summary>
/// Thread for running the Update cycle, so it can be killed after an assert.
/// </summary>
private void UpdateThread()
{
while (true)
{
lock (updateLock)
{
try
{
Monitor.Wait(updateLock);
base.Update(gameTime);
}
finally
{
Monitor.Pulse(updateLock);
}
}
}
}
#endregion
#region Draw
protected override void Draw(GameTime gameTime)
{
this.gameTime = gameTime;
//drawMethod(gameTime);
GraphicsDevice.Clear(Color.Black);
base.Draw(gameTime);
}
/// <summary>
/// Normal draw method to run, which signals the draw thread, as long as there's no assert.
/// </summary>
private void Draw_Normal(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
lock (drawLock)
{
Monitor.Pulse(drawLock);
Monitor.Wait(drawLock);
}
}
/// <summary>
/// Draw method that we switch to after an assert, so the Draw cycle can still be run
/// after the draw thread has been killed off.
/// </summary>
private void Draw_Assert(GameTime gameTime)
{
if (assertHandled)
base.Draw(gameTime);
}
/// <summary>
/// Thread for running the Draw cycle, so it can be killed after an assert.
/// </summary>
private void DrawThread()
{
while (true)
{
lock (drawLock)
{
try
{
Monitor.Wait(drawLock);
base.Draw(gameTime);
}
finally
{
Monitor.Pulse(drawLock);
}
}
}
}
#endregion
#region Methods
/// <summary>
/// Event handler for an assert.
/// </summary>
/// <param name="sender">Because Debug is a static class, this is always NULL.</param>
/// <param name="args">Info about the assert.</param>
private void HandleAssert(object sender, DebugEventArgs args)
{
updateMethod = Update_Assert;
drawMethod = Draw_Assert;
// This method is running on the thread that asserted, so start a new thread for the
// handler so it can kill off all the worker threads without killing itself.
//handleAssertThread = new Thread(new ParameterizedThreadStart(HandleAssertThread));
//handleAssertThread.Name = "HandleAssert";
//handleAssertThread.IsBackground = true;
//handleAssertThread.Start(args);
HandleAssertThread(args);
}
/// <summary>
/// Thread for handling an assert, so it can kill off other threads without being affected.
/// </summary>
/// <param name="obj">DebugEventArgs instance.</param>
private void HandleAssertThread(object obj)
{
DebugEventArgs args = (DebugEventArgs)obj;
// Abort all worker threads, and give them plenty of time to do so.
//updateThread.Abort();
//drawThread.Abort();
//Thread.Sleep(TimeSpan.FromSeconds(2));
Components.Clear();
Components.Add(screenManager);
screenManager.RemoveAllScreens();
screenManager.AddScreen(new AssertScreen(args));
assertHandled = true;
//Debug.UnblockAssert();
}
#endregion
}
/// <summary>
/// Entry point class for the game.
/// </summary>
public static class VolumetricRenderer
{
#region Fields
private static VolumetricRendererGame game;
#endregion
#region Properties
/// <summary>
/// Gets the VolumetricRendererGame instance.
/// </summary>
public static VolumetricRendererGame Game
{
get
{ return game; }
}
#endregion
#region Entry Point
/// <summary>
/// The main entry point for the game.
/// </summary>
/// <param name="args">Command-line arguments passed into the game.</param>
public static void Main(string[] args)
{
Debug.SetRootDirName("VolumetricRenderer");
try
{
game = new VolumetricRendererGame();
game.Run();
}
finally
{
game.Dispose();
}
}
#endregion
}
}