-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
70 lines (62 loc) · 2.03 KB
/
Program.cs
File metadata and controls
70 lines (62 loc) · 2.03 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
using System;
using System.Threading.Tasks;
namespace MatrixScreen
{
/// <summary>
/// This class will provide the entry point to the application, manage its
/// lifecycle, as well as provide the timing for, and run, the update/draw loops.
/// All of the work runs in a single worker thread, allowing the user to exit by pressing any key.
/// </summary>
internal class Program
{
private long _millisecondsPerFrame = 16; // 16ms = 60fps
private long _previousFrameTime = 0;
private ColumnsManager _columnsManager;
static void Main(string[] args)
{
Program instance = new Program();
instance.StartLoop();
Console.ReadKey(); // Pressing a key will kill the thread and exit the program.
}
private async void StartLoop()
{
try
{
_columnsManager = new ColumnsManager();
await Task.Factory.StartNew(RunLoop);
}
catch(Exception e)
{
Console.WriteLine($"There was an unfortunate crash, luckily nobody was hurt: {e.ToString()}");
}
}
private async Task RunLoop()
{
long now;
long elapsed;
while (true)
{
now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
elapsed = now - _previousFrameTime;
if (elapsed >= _millisecondsPerFrame)
{
Update(elapsed);
Draw(elapsed);
_previousFrameTime = now;
}
else
{
await Task.Delay(1);
}
}
}
private void Update(long elapsedTime)
{
_columnsManager.Update(elapsedTime);
}
private void Draw(long elapsedTime)
{
_columnsManager.Draw(elapsedTime);
}
}
}