-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
97 lines (77 loc) · 2.43 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
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
using System;
using System.Diagnostics;
using Glfw3;
using SFML.System;
namespace Tiler
{
public abstract class Program : IUpdatable
{
private static Stopwatch runtimeSW = new Stopwatch();
public Window Window;
public static TimeSpan ElapsedTime { get => runtimeSW.Elapsed; }
static Program()
{
//Glfw.ConfigureNativesDirectory(Path.GetFullPath(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)) + "\\thirdparty\\Glfw3");
string LibDir = IntPtr.Size == sizeof(long) ? CrossPlatform.Platform.GetX64() : CrossPlatform.Platform.GetX86();
if (CrossPlatform.Platform.SetLibraryDir(LibDir))
Console.WriteLine("Running as {0}", IntPtr.Size == sizeof(long) ? "64 bit" : "32 bit");
else
Environment.Exit(2);
if (!Glfw.Init())
Environment.Exit(1);
runtimeSW.Start();
}
public Program()
{
Window = new Window(1024, 768, "Tiler Program");
Window.Activate();
Window.Closed += (s, e) => ((Window)s).Close();
Window.KeyPressed += (s, e) => GUI.State.HandleKeyPressed(e);
Window.KeyReleased += (s, e) => GUI.State.HandleKeyReleased(e);
Window.UnicodeInput += (s, e) => GUI.State.HandleTextInput(e);
Window.MousePressed += (s, e) => GUI.State.HandleMousePressed(e);
Window.MouseReleased += (s, e) => GUI.State.HandleMouseReleased(e);
Window.MouseScrolled += (s, e) =>
{
Input.Manager.MouseWheelDeltas = new System.Numerics.Vector2((float)e.X, (float)e.Y);
GUI.State.HandleMouseScroll();
};
Window.Resized += (s, e) =>
{
var rw = ((Window)s).RenderWindow;
var view = rw.GetView();
view.Size = new Vector2f(e.Width, e.Height);
rw.SetView(view);
};
Input.Manager.Window = Window;
}
public void Run()
{
var frameTime = Stopwatch.StartNew();
float deltaTime;
while (Window.IsOpen)
{
while (frameTime.ElapsedMilliseconds / 1000.0f < (1.0f / 120.0f))
;
deltaTime = frameTime.ElapsedMilliseconds / 1000.0f;
frameTime.Restart();
Glfw.PollEvents();
Update(TimeSpan.FromSeconds(deltaTime));
Window.Clear();
OnDraw();
Window.RenderWindow.SetView(Window.RenderWindow.DefaultView);
GUI.State.Draw(Window);
Window.Display();
}
Glfw.Terminate();
}
public void Update(TimeSpan deltaTime)
{
Input.Manager.Update(deltaTime);
GUI.State.Update(deltaTime);
OnUpdate(deltaTime);
}
public abstract void OnDraw();
public abstract void OnUpdate(TimeSpan deltaTime);
}
}