This repository was archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.cs
416 lines (338 loc) · 12.8 KB
/
Game.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using MoreLinq;
using SDL2;
namespace HatlessEngine
{
public static class Game
{
internal static IntPtr WindowHandle;
internal static IntPtr RendererHandle;
internal static QuadTree QuadTree;
internal static bool RenderframeReady;
private static bool _running;
private static int _ticksPerStep;
private static int _ticksPerDraw;
/// <summary>
/// If true the game will force to run steps at max frequency until it has caught up with the required speed.
/// If not it will just keep going at regular speed after it goes out of sync.
/// </summary>
public static bool CatchUpSteps;
/// <summary>
/// Gets or sets the desired amount of steps per second.
/// </summary>
public static float StepsPerSecond
{
get { return Stopwatch.Frequency / _ticksPerStep; }
set
{
if (value > 0)
_ticksPerStep = (int)(Stopwatch.Frequency / value);
else
throw new ArgumentOutOfRangeException("StepsPerSecond", "StepsPerSecond must be positive and nonzero. (" + value + " given)");
}
}
/// <summary>
/// Gets or sets the limit of frames per second.
/// If 0 no limit is enforced.
/// </summary>
public static float FPSLimit
{
get
{
if (_ticksPerDraw == 0)
return 0;
return Stopwatch.Frequency / _ticksPerDraw;
}
set
{
if (value > 0)
_ticksPerDraw = (int)(Stopwatch.Frequency / value);
else if (value == 0f)
_ticksPerDraw = 0;
else
throw new ArgumentOutOfRangeException("FPSLimit", "FpsLimit must be positive or zero. (" + value + " given)");
}
}
static Game()
{
AppDomain.CurrentDomain.UnhandledException += ExceptionHandler;
}
/// <summary>
/// Sets up a window and enters the gameloop. (Code after this call won't run until the game has exited.)
/// </summary>
public static void Run(float speed = 100f)
{
SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING);
SDL_image.IMG_Init(SDL_image.IMG_InitFlags.IMG_INIT_JPG
| SDL_image.IMG_InitFlags.IMG_INIT_PNG
| SDL_image.IMG_InitFlags.IMG_INIT_WEBP
| SDL_image.IMG_InitFlags.IMG_INIT_TIF);
SDL_mixer.Mix_Init(SDL_mixer.MIX_InitFlags.MIX_INIT_OGG
| SDL_mixer.MIX_InitFlags.MIX_INIT_MP3
| SDL_mixer.MIX_InitFlags.MIX_INIT_FLAC
| SDL_mixer.MIX_InitFlags.MIX_INIT_MOD
| SDL_mixer.MIX_InitFlags.MIX_INIT_FLUIDSYNTH);
SDL_ttf.TTF_Init();
//open window
SDL.SDL_SetHint(SDL.SDL_HINT_RENDER_VSYNC, "1");
SDL.SDL_SetHint(SDL.SDL_HINT_RENDER_SCALE_QUALITY, "2");
WindowHandle = SDL.SDL_CreateWindow("HatlessEngine", SDL.SDL_WINDOWPOS_UNDEFINED, SDL.SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN | SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE | SDL.SDL_WindowFlags.SDL_WINDOW_INPUT_FOCUS);
RendererHandle = SDL.SDL_CreateRenderer(WindowHandle, -1, (uint)(SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC));
SDL.SDL_SetRenderDrawBlendMode(RendererHandle, SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND);
Window.SetIcon();
//add default view that spans the current window
new View("default", new Rectangle(Point.Zero, Window.Size), new Rectangle(Point.Zero , new Point(1f, 1f)));
//initialize audio system and let Resources handle sound expiration
SDL_mixer.Mix_OpenAudio(44100, SDL.AUDIO_S16SYS, 2, 4096);
SDL_mixer.Mix_AllocateChannels((int)(speed * 2)); //might want to dynamically create and remove channels during runtime
SDL_mixer.Mix_ChannelFinished(Resources.SoundChannelFinished);
SDL_mixer.Mix_HookMusicFinished(Resources.MusicFinished);
//initialize loop
StepsPerSecond = speed;
_running = true;
if (Started != null)
Started(null, EventArgs.Empty);
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
long lastStepTick = 0;
long lastDrawTick = 0;
while (_running)
{
//perform step when needed
if (stopWatch.ElapsedTicks >= lastStepTick + _ticksPerStep)
{
Profiler.Start("step");
if (CatchUpSteps)
lastStepTick = lastStepTick + _ticksPerStep;
else
lastStepTick = stopWatch.ElapsedTicks;
Step();
Profiler.Stop();
}
//perform draw when ready for a new one
if (!RenderframeReady && _running && stopWatch.ElapsedTicks >= lastDrawTick + _ticksPerDraw)
{
Profiler.Start("draw");
lastDrawTick = lastDrawTick + _ticksPerDraw;
Draw();
Profiler.Stop();
}
}
//cleanup and uninitialization
Resources.UnloadAllExternalResources();
Log.CloseAllStreams();
Input.CloseGamepads();
SDL.SDL_DestroyWindow(WindowHandle);
WindowHandle = IntPtr.Zero;
SDL.SDL_DestroyRenderer(RendererHandle);
RendererHandle = IntPtr.Zero;
SDL_mixer.Mix_CloseAudio();
SDL.SDL_Quit();
SDL_image.IMG_Quit();
SDL_mixer.Mix_Quit();
SDL_ttf.TTF_Quit();
}
private static void Step()
{
//push input state
Input.PushState();
//process all SDL.SDL_events
SDL.SDL_Event e;
while (SDL.SDL_PollEvent(out e) == 1)
{
switch (e.type)
{
//let Input handle input related events
case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN:
case SDL.SDL_EventType.SDL_MOUSEBUTTONUP:
case SDL.SDL_EventType.SDL_MOUSEWHEEL:
case SDL.SDL_EventType.SDL_MOUSEMOTION:
case SDL.SDL_EventType.SDL_KEYDOWN:
case SDL.SDL_EventType.SDL_KEYUP:
case SDL.SDL_EventType.SDL_TEXTINPUT:
case SDL.SDL_EventType.SDL_CONTROLLERDEVICEADDED:
case SDL.SDL_EventType.SDL_CONTROLLERDEVICEREMOVED:
case SDL.SDL_EventType.SDL_CONTROLLERBUTTONDOWN:
case SDL.SDL_EventType.SDL_CONTROLLERBUTTONUP:
case SDL.SDL_EventType.SDL_CONTROLLERAXISMOTION:
Input.InputEvent(e);
break;
//let Window handle window related events
case SDL.SDL_EventType.SDL_WINDOWEVENT:
Window.WindowEvent(e);
break;
//global quit, not only the window's exit button
case SDL.SDL_EventType.SDL_QUIT:
Exit();
break;
}
}
Input.UpdateMousePosition();
Input.ApplyButtonMaps();
foreach (GameObject obj in Resources.Objects)
{
if (!obj.Destroyed)
obj.Step();
}
//collision time!
Profiler.Start("collision");
float minX = float.PositiveInfinity;
float minY = float.PositiveInfinity;
float maxX = float.NegativeInfinity;
float maxY = float.NegativeInfinity;
foreach(PhysicalObject obj in Resources.PhysicalObjects)
{
obj.UpdateCoverableArea();
if (obj.CoverableArea.Position.X < minX)
minX = obj.CoverableArea.Position.X;
if (obj.CoverableArea.Position2.X > maxX)
maxX = obj.CoverableArea.Position2.X;
if (obj.CoverableArea.Position.Y < minY)
minY = obj.CoverableArea.Position.Y;
if (obj.CoverableArea.Position2.Y > maxY)
maxY = obj.CoverableArea.Position2.Y;
//set before the actual collision check phase
obj.SpeedLeft = 1f;
obj.CollisionCandidates = null;
}
//create and fill quadtree for this step
QuadTree = new QuadTree(new Rectangle(minX, minY, maxX - minX, maxY - minY));
//create list of objects to process and calculate all first collision speedfractions for those objects
List<PhysicalObject> processingObjects = new List<PhysicalObject>(Resources.PhysicalObjects);
foreach (PhysicalObject obj in Resources.PhysicalObjects)
{
if (obj.Speed == Point.Zero)
continue;
processingObjects.Add(obj);
obj.CalculateClosestCollision();
}
while (processingObjects.Count > 0)
{
//get closest collision, process it/the pair of objects
PhysicalObject obj = processingObjects.MinBy(o => o.ClosestCollisionSpeedFraction + 1 - o.SpeedLeft);
obj.PerformClosestCollision();
//remove/recalculate collisions
if (obj.SpeedLeft == 0f)
processingObjects.Remove(obj);
else
obj.CalculateClosestCollision();
//recalculate for all possibly influenced objects (if needed)
if (obj.CollisionCandidates != null)
{
foreach (PhysicalObject influencedObj in obj.CollisionCandidates)
influencedObj.CalculateClosestCollision();
}
}
Profiler.Stop();
Resources.ObjectAdditionAndRemoval();
Resources.CleanupFontTextures();
}
private static void Draw()
{
//collect drawjobs
foreach (GameObject obj in Resources.Objects)
{
if (!obj.Destroyed)
obj.Draw();
}
DrawX.DrawJobs = DrawX.DrawJobs.OrderBy(job => job.Depth).ToList();
SDL.SDL_SetRenderDrawColor(RendererHandle, DrawX.BackgroundColor.R, DrawX.BackgroundColor.G, DrawX.BackgroundColor.B, DrawX.BackgroundColor.A);
SDL.SDL_RenderClear(RendererHandle);
foreach (View view in Resources.Get<View>())
{
if (!view.Active)
continue;
Rectangle absoluteGameArea = view.GetAbsoluteGameArea();
Rectangle absoluteViewport = view.GetAbsoluteViewport();
Point scale = view.GetScale();
SDL.SDL_RenderSetScale(RendererHandle, scale.X, scale.Y);
//viewport is affected by scale for whatever reason, correct it
Rectangle scaledViewport = new Rectangle(absoluteViewport);
scaledViewport.Position /= scale;
scaledViewport.Size /= scale;
SDL.SDL_Rect sdlViewport = (SDL.SDL_Rect)scaledViewport;
SDL.SDL_RenderSetViewport(RendererHandle, ref sdlViewport);
//get all jobs that will draw inside this view
foreach (IDrawJob job in DrawX.GetDrawJobsByArea(absoluteGameArea))
{
Type jobType = job.GetType();
if (jobType == typeof(TextureDrawJob)) //draw a texture
{
TextureDrawJob textureDrawJob = (TextureDrawJob)job;
SDL.SDL_Rect sourceRect = (SDL.SDL_Rect)textureDrawJob.SourceRect;
SDL.SDL_Rect destRect = (SDL.SDL_Rect)new Rectangle(textureDrawJob.DestRect.Position - absoluteGameArea.Position - textureDrawJob.DestRect.Origin, textureDrawJob.DestRect.Size);
if (textureDrawJob.DestRect.Rotation == 0f)
SDL.SDL_RenderCopy(RendererHandle, textureDrawJob.Texture, ref sourceRect, ref destRect);
else
{
SDL.SDL_Point origin = (SDL.SDL_Point)textureDrawJob.DestRect.Origin;
SDL.SDL_RenderCopyEx(RendererHandle, textureDrawJob.Texture, ref sourceRect, ref destRect, textureDrawJob.DestRect.Rotation, ref origin, SDL.SDL_RendererFlip.SDL_FLIP_NONE);
}
}
else if (jobType == typeof(LineDrawJob)) //draw line(s)
{
LineDrawJob lineDrawJob = (LineDrawJob)job;
//transform all points according to view and cast em
SDL.SDL_Point[] sdlPoints = Array.ConvertAll(lineDrawJob.Points, point => (SDL.SDL_Point)(point - absoluteGameArea.Position));
SDL.SDL_SetRenderDrawColor(RendererHandle, lineDrawJob.Color.R, lineDrawJob.Color.G, lineDrawJob.Color.B, lineDrawJob.Color.A);
SDL.SDL_RenderDrawLines(RendererHandle, sdlPoints, lineDrawJob.PointCount);
}
else //draw filledrect
{
FilledRectDrawJob rectDrawJob = (FilledRectDrawJob)job;
SDL.SDL_Rect rect = (SDL.SDL_Rect)(rectDrawJob.Area - absoluteGameArea.Position);
SDL.SDL_SetRenderDrawColor(RendererHandle, rectDrawJob.Color.R, rectDrawJob.Color.G, rectDrawJob.Color.B, rectDrawJob.Color.A);
SDL.SDL_RenderFillRect(RendererHandle, ref rect);
}
}
}
DrawX.DrawJobs.Clear();
//threadpool should take care of actually swapping the frames (RenderPresent may wait for things like Fraps or VSync)
RenderframeReady = true;
ThreadPool.QueueUserWorkItem(PresentRender);
}
/// <summary>
/// Game will finish it's current loop (Step or Draw) and exit the Running method.
/// </summary>
public static void Exit()
{
_running = false;
}
public static event EventHandler Started;
private static void PresentRender(object o)
{
SDL.SDL_RenderPresent(RendererHandle);
RenderframeReady = false;
}
private static void ExceptionHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception)args.ExceptionObject;
string eTypeString = e.GetType().ToString();
StreamWriter writer = new StreamWriter(File.Open("latestcrashlog.txt", FileMode.Create, FileAccess.Write, FileShare.Read));
writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm zzz"));
writer.WriteLine(eTypeString);
writer.WriteLine(e.Message);
writer.WriteLine(e.StackTrace);
writer.Close();
if (Misc.ExceptionErrorMessageEnabled)
{
string message = "The game encountered an unhandled " + eTypeString + " and has to exit.";
message += "\nA crashlog has been written to latestcrashlog.txt.";
SDL.SDL_ShowSimpleMessageBox(SDL.SDL_MessageBoxFlags.SDL_MESSAGEBOX_ERROR, "Game Crash", message, WindowHandle);
}
}
/// <summary>
/// Converts a per-second value to a per-step value based on the game's speed, so you can use relative values.
/// Basically all this does is take your value and divide it by the game's speed, but it's more logical and less bug prone if using this method.
/// </summary>
public static float ValuePerStep(float valuePerSecond)
{
return valuePerSecond / StepsPerSecond;
}
}
}