forked from BlinkSun/ParticleLifeSimulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
45 lines (41 loc) · 1.55 KB
/
Program.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
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ParticleLifeSimulation;
public static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
Stopwatch stopwatch = new();
ApplicationConfiguration.Initialize();
Application.Idle += new((s, ev) =>
{
while (AppStillIdle)
{
long deltaTime = stopwatch.ElapsedMilliseconds;
stopwatch.Restart();
foreach (IFormLoop form in Application.OpenForms.OfType<IFormLoop>())
{
// Render a frame during idle time (no messages are waiting)
form.UpdateEnvironment(deltaTime);
form.RenderEnvironment(deltaTime);
}
}
});
Application.Run(new FrmSimulation());
}
[System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
private static bool AppStillIdle => !PeekMessage(out _, IntPtr.Zero, 0, 0, 0);
}
internal interface IFormLoop
{
public void UpdateEnvironment(long deltaTime);
public void RenderEnvironment(long deltaTime);
}