-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngine.cs
89 lines (76 loc) · 2.4 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
namespace Voxel_Engine;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
public record EventLoopRunnable
{
public record OnLoad(Action Act) : EventLoopRunnable;
public record OnRender(Action<FrameEventArgs> Act) : EventLoopRunnable;
public record OnUpdate(Action<FrameEventArgs> Act) : EventLoopRunnable;
public record OnResize(Action<ResizeEventArgs> Act) : EventLoopRunnable;
}
public class Engine
{
static public readonly GameWindow Window = new(new(), new())
{
Size = new(800, 600)
};
/// <summary>
/// Creates the game window
/// </summary>
public static void CreateWindow(params EventLoopRunnable[] items)
{
foreach (var item in items)
{
switch(item)
{
case EventLoopRunnable.OnLoad(Action act):
Window.Load += act;
break;
case EventLoopRunnable.OnRender(Action<FrameEventArgs> act):
Window.RenderFrame += act;
break;
case EventLoopRunnable.OnUpdate(Action<FrameEventArgs> act):
Window.UpdateFrame += act;
break;
case EventLoopRunnable.OnResize(Action<ResizeEventArgs> act):
Window.Resize += act;
break;
}
}
Window.Load += OnLoad;
Window.Resize += OnResize;
Window.UpdateFrame += OnUpdate;
Window.RenderFrame += OnRender;
Window.Run();
Window.Dispose();
}
static void OnResize(ResizeEventArgs e)
{
Console.WriteLine($"x:{e.Width} y:{e.Height} ");
//Window.Size = e.Size;
Camera.Main?.FitToScreen();
UI.OnResize(e);
GL.Viewport(0, 0, e.Width, e.Height);
}
static void OnUpdate(FrameEventArgs e)
{
Time.PhysicsUpdate();
Input.Update();
}
static void OnRender(FrameEventArgs e)
{
ChunkStreamer.Main?.Update();
Time.Update();
Camera.Render();
UI.Render(e);
Window.SwapBuffers();
}
static void OnLoad()
{
Utility.Debug.DebugTools.Enable();
UI.Init(Window);
Input.Initialize(Window);
GL.ClearColor(new Color4(125, 125, 255, 255));
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
}
}