-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.cs
More file actions
167 lines (147 loc) · 5.06 KB
/
Core.cs
File metadata and controls
167 lines (147 loc) · 5.06 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;
namespace TextMeshRender;
public static class Core
{
private static readonly GameWindow Window = new(
GameWindowSettings.Default,
new NativeWindowSettings
{
NumberOfSamples = 4,
}
);
private static string _vertexShaderPath = null!;
private static string _fragmentShaderPath = null!;
public static Vector2i WindowSize {
get => Window.Size;
set => Window.Size = value;
}
public static string VertexShaderPath {
get => _vertexShaderPath;
set => _vertexShaderPath = value;
}
public static string FragmentShaderPath {
get => _fragmentShaderPath;
set => _fragmentShaderPath = value;
}
public static string Title {
get => Window.Title;
set => Window.Title = value;
}
public static VSyncMode VSync {
get => Window.VSync;
set => Window.VSync = value;
}
private static readonly List<Object2DMesh> Objects = [];
private static Rect2D _target = null!;
private static Text2D _fpsText = null!;
private static Text2D _inputText = null!;
private static Shader _shader = null!;
private static void WindowOnLoad()
{
var vertexSource = File.ReadAllText(_vertexShaderPath);
var fragmentSource = File.ReadAllText(_fragmentShaderPath);
_shader = new Shader(vertexSource, fragmentSource);
Object2DRenderer.RectMesh = new Mesh2D([
// Top Left
new Mesh2D.Vertex(new Vector2(0.0f,0.0f), new Vector2(0.0f, 0.0f)),
// Top Right
new Mesh2D.Vertex(new Vector2(1.0f,0.0f), new Vector2(1.0f, 0.0f)),
// Bottom Left
new Mesh2D.Vertex(new Vector2(0.0f,1.0f), new Vector2(0.0f, 1.0f)),
// Bottom Right
new Mesh2D.Vertex(new Vector2(1.0f,1.0f), new Vector2(1.0f, 1.0f)),
], [
0, 1, 3,
0, 2, 3
]);
Object2DRenderer.TextShader = _shader;
{
var rect = new Rect2D(new Vector2(0.0f, 0.0f), new Vector2(100.0f, 100.0f), 0.0f)
{
Color = new Vector3(1.0f, 0.0f, 0.0f)
};
Objects.Add(rect);
_target = rect;
}
var fontMesh = new FontMesh("fonts/Axiforma-Regular.ttf");
{
var tex = new Text2D(new Vector2(45.0f, 45.0f), new Vector2(1.0f, 1.0f), 0.0f, fontMesh)
{
Color = new Vector3(1.0f, 0.0f, 0.0f),
Text = "The_Text",
};
Objects.Add(tex);
_inputText = tex;
}
{
var tex = new Text2D(new Vector2(45.0f, 400.0f), new Vector2(1.0f, 1.0f), 0.0f, fontMesh)
{
Color = new Vector3(1.0f, 0.0f, 0.0f),
Text = "<FPS>",
};
Objects.Add(tex);
_fpsText = tex;
}
GL.ClearColor(0.1f,0.1f,0.1f,1.0f);
GL.Enable(EnableCap.Multisample);
GL.Enable(EnableCap.LineSmooth);
GL.Enable(EnableCap.PolygonSmooth);
GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest);
GL.Hint(HintTarget.PolygonSmoothHint, HintMode.Nicest);
}
private static void WindowOnRenderFrame(FrameEventArgs obj)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
foreach (var object2D in Objects)
{
Object2DRenderer.Render(object2D, _shader, Window.ClientSize.X, Window.ClientSize.Y);
}
Window.SwapBuffers();
}
private static void WindowOnUpdateFrame(FrameEventArgs obj)
{
_target.Position = new Vector2(Window.MousePosition.X, Window.MousePosition.Y);
_target.Rotation += (float)obj.Time * 20.0f;
_fpsText.Text = $"FPS: {1.0f / obj.Time}";
}
private static void WindowOnResize(ResizeEventArgs obj)
{
GL.Viewport(0, 0, Window.ClientSize.X, Window.ClientSize.Y);
}
private static void WindowOnKeyDown(KeyboardKeyEventArgs obj)
{
switch (obj.Key)
{
case Keys.Backspace:
if (!string.IsNullOrEmpty(_inputText.Text))
{
_inputText.Text = _inputText.Text[..^1];
}
break;
case Keys.Enter:
_inputText.Text += '\n';
break;
case Keys.Escape:
break;
}
}
private static void WindowOnTextInput(TextInputEventArgs e)
{
var ch = e.AsString;
_inputText.Text += ch;
}
public static void Start()
{
Window.Load += WindowOnLoad;
Window.RenderFrame += WindowOnRenderFrame;
Window.Resize += WindowOnResize;
Window.UpdateFrame += WindowOnUpdateFrame;
Window.KeyDown += WindowOnKeyDown;
Window.TextInput += WindowOnTextInput;
Window.Run();
}
}