Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
162 lines (144 loc) · 5.7 KB
/
Copy pathProgram.cs
File metadata and controls
162 lines (144 loc) · 5.7 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
using System.Diagnostics;
using ImGui4NET;
using OpenTK.Windowing.GraphicsLibraryFramework;
namespace ImGui4NET.Template.MultiPlatforms;
internal static unsafe class Program
{
private const string Usage = "Usage: [--renderer opengl|gles|vulkan]";
[STAThread]
private static int Main(string[] args)
{
if (args is ["--help"] or ["-h"])
{
Console.WriteLine(Usage);
return 0;
}
string renderer = "opengl";
for (int i = 0; i < args.Length; i++)
{
if (args[i] == "--renderer" && i + 1 < args.Length)
renderer = args[++i];
else
return Fail($"Unrecognized or incomplete argument: {args[i]}. {Usage}", 2);
}
if (renderer is not ("opengl" or "gles" or "vulkan"))
return Fail($"Unknown renderer: {renderer}. {Usage}", 2);
if (!OperatingSystem.IsWindows() && !OperatingSystem.IsLinux() && !OperatingSystem.IsMacOS())
return Fail("This template supports Windows, Linux and macOS.", 2);
if (renderer == "gles" && OperatingSystem.IsMacOS())
return Fail("GLFW does not provide native OpenGL ES contexts on macOS. Use opengl or vulkan.", 2);
try
{
Run(renderer);
return 0;
}
catch (Exception ex)
{
return Fail($"{renderer}: {ex.Message}", 1);
}
}
private static void Run(string backend)
{
using IApplication app = new SimpleApplication();
app.Create();
if (!GLFW.Init())
throw GlfwError("Could not initialize GLFW");
try
{
ConfigureContext(backend);
string? appName = typeof(Program).Assembly.GetName().Name;
Window* window = GLFW.CreateWindow(1280, 760, $"{appName} — {backend}", null, null);
if (window == null)
throw GlfwError($"Could not create a window for {backend}");
try
{
ImGuiContext* context = ImGui.CreateContext();
try
{
ImGui.SetCurrentContext(context);
ImGui.StyleColorsDark();
ImGui.GetIO()->ConfigFlags |= (int)ImGuiConfigFlags.ImGuiConfigFlags_NavEnableKeyboard;
using var input = new GlfwInput(window);
GLFW.GetFramebufferSize(window, out int width, out int height);
using var renderer = new ApplicationRenderer(backend == "vulkan"
? new VulkanRenderer(new GlfwVulkanSurface(window), width, height)
: new GlRenderer(window, backend == "gles"), app);
RunFrames(window, renderer, input, app);
}
finally
{
ImGui.DestroyContext(context);
}
}
finally
{
GLFW.DestroyWindow(window);
}
}
finally
{
GLFW.Terminate();
}
}
private static void ConfigureContext(string backend)
{
if (backend == "vulkan")
{
GLFW.WindowHint(WindowHintClientApi.ClientApi, ClientApi.NoApi);
return;
}
bool es = backend == "gles";
GLFW.WindowHint(WindowHintClientApi.ClientApi, es ? ClientApi.OpenGlEsApi : ClientApi.OpenGlApi);
GLFW.WindowHint(WindowHintInt.ContextVersionMajor, es ? 3 : 4);
GLFW.WindowHint(WindowHintInt.ContextVersionMinor, es ? 0 : 1);
if (es && OperatingSystem.IsLinux())
GLFW.WindowHint(WindowHintContextApi.ContextCreationApi, ContextApi.EglContextApi);
if (!es)
{
GLFW.WindowHint(WindowHintOpenGlProfile.OpenGlProfile, OpenGlProfile.Core);
GLFW.WindowHint(WindowHintBool.OpenGLForwardCompat, OperatingSystem.IsMacOS());
}
}
private static void RunFrames(Window* window, ApplicationRenderer renderer, GlfwInput input, IApplication app)
{
long previous = Stopwatch.GetTimestamp();
int previousWidth = 0, previousHeight = 0;
while (!GLFW.WindowShouldClose(window))
{
GLFW.PollEvents();
input.Dispatch(app);
if (GLFW.WindowShouldClose(window))
break;
GLFW.GetWindowSize(window, out int logicalWidth, out int logicalHeight);
GLFW.GetFramebufferSize(window, out int width, out int height);
if (width <= 0 || height <= 0 || logicalWidth <= 0 || logicalHeight <= 0)
{
GLFW.WaitEventsTimeout(0.1);
continue;
}
long now = Stopwatch.GetTimestamp();
double deltaSeconds = Math.Clamp(Stopwatch.GetElapsedTime(previous, now).TotalSeconds, 0.000001, 0.1);
ImGuiIO* io = ImGui.GetIO();
io->DeltaTime = (float)deltaSeconds;
io->DisplaySize = new ImVec2 { x = logicalWidth, y = logicalHeight };
io->DisplayFramebufferScale = new ImVec2 { x = (float)width / logicalWidth, y = (float)height / logicalHeight };
previous = now;
if (width != previousWidth || height != previousHeight)
{
app.Resize(width, height);
(previousWidth, previousHeight) = (width, height);
}
renderer.Render(deltaSeconds, width, height);
}
}
private static Exception GlfwError(string message)
{
GLFW.GetError(out string description);
return new InvalidOperationException($"{message}: {description}");
}
private static int Fail(string message, int exitCode)
{
Console.Error.WriteLine(message);
return exitCode;
}
}