Skip to content

Commit 6a9d18e

Browse files
chore - Add command line arguments for overriding certain configurations (#886)
1 parent ac8c21a commit 6a9d18e

File tree

5 files changed

+909
-865
lines changed

5 files changed

+909
-865
lines changed

FDK/src/01.Framework/Core/Game.cs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,20 @@ namespace FDK;
4040
public abstract class Game : IDisposable {
4141
public static GL Gl { get; private set; }
4242
public static Silk.NET.Core.Contexts.IGLContext Context { get; private set; }
43+
44+
private static string[] parameters;
45+
private static string GetParameterValue(string parameter = "", string parameter_full = "") {
46+
if (parameters.Length == 0) return "";
47+
int index = parameters.Contains(parameter) || parameters.Contains(parameter_full)
48+
? Array.FindIndex(parameters, x => x.Equals(parameter) || x.Equals(parameter_full))
49+
: -1;
50+
return index > -1 && parameters.Length > index ? parameters[index + 1] : "";
51+
}
4352

4453
public static ImGuiController ImGuiController { get; private set; }
4554
public static ImGuiIOPtr ImGuiIO { get; private set; }
4655
private static CTexture ImGuiFontAtlas;
4756

48-
static string _test = "";
4957
public static void InitImGuiController(IView window, IInputContext context) {
5058
if (ImGuiController != null) return;
5159

@@ -265,8 +273,9 @@ private RawImage GetIconData(string fileName) {
265273
/// <summary>
266274
/// Initializes a new instance of the <see cref="Game"/> class.
267275
/// </summary>
268-
protected Game(string iconFileName) {
276+
protected Game(string iconFileName, params string[] args) {
269277
strIconFileName = iconFileName;
278+
parameters = args;
270279

271280
MainThreadID = Thread.CurrentThread.ManagedThreadId;
272281
Configuration();
@@ -287,15 +296,27 @@ protected Game(string iconFileName) {
287296
options.WindowBorder = WindowBorder.Resizable;
288297
options.Title = Text;
289298

299+
#region Override Windowing
300+
string windowing_override = GetParameterValue("-w", "--windowing");
301+
#endregion
290302

291303
// Use SDL on Linux with Wayland, otherwise use GLFW for everything else
292-
if (OperatingSystem.IsLinux() && Environment.GetEnvironmentVariable("XDG_SESSION_TYPE") == "wayland") {
293-
Silk.NET.Windowing.Sdl.SdlWindowing.Use();
304+
if ((OperatingSystem.IsLinux() && Environment.GetEnvironmentVariable("XDG_SESSION_TYPE") == "wayland" && windowing_override != "glfw")
305+
|| windowing_override == "sdl") {
306+
Silk.NET.Windowing.Sdl.SdlWindowing.Use();
307+
Console.WriteLine("SDL selected for Windowing");
294308
} else {
295-
Silk.NET.Windowing.Glfw.GlfwWindowing.Use();
309+
Silk.NET.Windowing.Glfw.GlfwWindowing.Use();
310+
Console.WriteLine("GLFW selected for Windowing");
296311
}
297312

298-
Window_ = Window.Create(options);
313+
try {
314+
Window_ = Window.Create(options);
315+
}
316+
catch {
317+
Console.WriteLine("The window failed to be created.\nYou can attempt to fix this by overriding the default windowing.\nTry launching OpenTaiko with args, using '-w glfw' to force GLFW or '-w sdl' to force SDL.");
318+
throw;
319+
}
299320

300321
ViewPortSize.X = Window_.Size.X;
301322
ViewPortSize.Y = Window_.Size.Y;
@@ -416,7 +437,20 @@ public void Window_Load() {
416437

417438
Context = Window_.GLContext;
418439
} else {
419-
Context = new AngleContext(GraphicsDeviceType_, Window_);
440+
#region Override Platform
441+
GraphicsDeviceType_ = GetParameterValue("-p", "--platform") switch {
442+
"opengl" => AnglePlatformType.OpenGL,
443+
"opengles" => AnglePlatformType.OpenGLES,
444+
"d3d9" => AnglePlatformType.D3D9,
445+
"d3d11" => AnglePlatformType.D3D11,
446+
"vulkan" => AnglePlatformType.Vulkan,
447+
"metal" => AnglePlatformType.Metal,
448+
_ => GraphicsDeviceType_
449+
};
450+
Console.WriteLine("Platform set to " + GraphicsDeviceType_);
451+
#endregion
452+
453+
Context = new AngleContext(GraphicsDeviceType_, Window_, GetParameterValue("-f", "--flag"));
420454

421455
Context.MakeCurrent();
422456
}

FDK/src/01.Framework/Rendering/Angle/AngleContext.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,41 @@ public class AngleContext : IGLContext {
1111
private nint Context;
1212
private nint Surface;
1313

14-
public AngleContext(AnglePlatformType anglePlatformType, IWindow window) {
14+
public AngleContext(AnglePlatformType anglePlatformType, IWindow window, string flag_override = "") {
1515
nint windowHandle;
1616
nint display;
1717
NativeWindowFlags selectedflag; // For logging
1818

19-
if (window.Native.Kind.HasFlag(NativeWindowFlags.Win32)) {
19+
bool flag_has_override = !string.IsNullOrWhiteSpace(flag_override);
20+
if ((window.Native.Kind.HasFlag(NativeWindowFlags.Win32) && !flag_has_override) || flag_override == "win32") {
2021
selectedflag = NativeWindowFlags.Win32;
2122
windowHandle = window.Native.Win32.Value.Hwnd;
2223
display = window.Native.Win32.Value.HDC;
23-
} else if (window.Native.Kind.HasFlag(NativeWindowFlags.X11)) {
24+
Console.WriteLine("Handle set to Win32");
25+
} else if ((window.Native.Kind.HasFlag(NativeWindowFlags.X11) && !flag_has_override) || flag_override == "x11") {
2426
selectedflag = NativeWindowFlags.X11;
2527
windowHandle = (nint)window.Native.X11.Value.Window;
2628
// Temporary fix for the segfaults
2729
// Note than X11 Display number is NOT always 0, it can be 1, 2 and so on for example in cases of user switching
2830
display = 0;// Egl.GetDisplay(window.Native.X11.Value.Display);
29-
} else if (window.Native.Kind.HasFlag(NativeWindowFlags.Cocoa)) {
31+
Console.WriteLine("Handle set to X11");
32+
} else if ((window.Native.Kind.HasFlag(NativeWindowFlags.Cocoa) && !flag_has_override) || flag_override == "cocoa") {
3033
selectedflag = NativeWindowFlags.Cocoa;
3134
windowHandle = window.Native.Cocoa.Value;
3235
display = 0;
33-
} else if (window.Native.Kind.HasFlag(NativeWindowFlags.Wayland)) {
36+
Console.WriteLine("Handle set to Cocoa");
37+
} else if ((window.Native.Kind.HasFlag(NativeWindowFlags.Wayland) && !flag_has_override) || flag_override == "wayland") {
3438
selectedflag = NativeWindowFlags.Wayland;
3539
windowHandle = window.Native.Wayland.Value.Surface;
3640
display = window.Native.Wayland.Value.Display;
41+
Console.WriteLine("Handle set to Wayland");
3742
} else {
43+
if (flag_has_override) throw new Exception("Override flag provided is invalid, please check for spelling errors or remove your argument.");
3844
throw new Exception("Window not found");
3945
}
4046

4147
Source = window;
42-
48+
4349
int platform = 0;
4450
switch (anglePlatformType) {
4551
case AnglePlatformType.OpenGL:

0 commit comments

Comments
 (0)