-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEngine.cs
95 lines (70 loc) · 2.68 KB
/
Engine.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
using Core;
using Core.CustomSerialize;
using Core.Primitive;
using Core.Tickable;
using OpenTK.Mathematics;
using System.Diagnostics;
using System.DirectoryServices;
using System.Threading;
using System.Threading.Tasks;
using Core.Asset;
using ZeroFormatter.Formatters;
using DirectXTexWrapper;
using Engine.Scene;
using FreeTypeGLWrapper;
using Engine;
namespace Engine
{
public class Engine : Singleton<Engine>
{
protected int MainThreadId = 0;
protected Stopwatch stopwatch = new Stopwatch();
protected bool bFirstTick = true;
protected bool bInitialized = false;
public bool IsInitialized => bInitialized;
// private SceneBase mCurrentScene = new FontTestScene();
// private SceneBase mCurrentScene = new GLTFTestScene();
private SceneBase mCurrentScene = new SphereScene();
public SceneBase CurrentScene => mCurrentScene;
public async Task Initialize()
{
// register resolver
Formatter<DefaultResolver, Vector3>.Register(new Vector3Formatter<DefaultResolver>());
Formatter<DefaultResolver, Vector2>.Register(new Vector2Formatter<DefaultResolver>());
Formatter<DefaultResolver, Vector4>.Register(new Vector4Formatter<DefaultResolver>());
MainThreadId = Thread.CurrentThread.ManagedThreadId;
OpenGLContext.Instance.SetMainThreadId(MainThreadId);
await AssetManager.Instance.ImportStaticMeshesAsync();
await mCurrentScene.InitializeScene();
bInitialized = true;
}
public void Tick()
{
// frametime in milliseconds
long frameMilliseconds = stopwatch.ElapsedMilliseconds;
// gamethread 120fps
float frameCap = (1.0f / 240.0f) * 1000;
if (stopwatch.ElapsedMilliseconds < frameCap)
{
int sleeptime = (int)(frameCap - frameMilliseconds);
Thread.Sleep(sleeptime);
}
TickableObjectManager.Tick(stopwatch.ElapsedMilliseconds * 0.001f);
SceneObjectManager.Instance.Tick(stopwatch.ElapsedMilliseconds * 0.001f);
GameThreadDone.Set();
stopwatch.Reset();
stopwatch.Start();
}
public void WaitForGameThread(int miliseconds=30)
{
GameThreadDone.WaitOne(miliseconds);
}
public void RequestExit()
{
bIsRequestExit = true;
}
public bool IsRequestExit => bIsRequestExit;
protected bool bIsRequestExit = false;
public AutoResetEvent GameThreadDone = new AutoResetEvent(false);
}
}