Skip to content

Commit

Permalink
add icon
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesGameDev committed Apr 11, 2024
1 parent f54b4c1 commit a333768
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
4 changes: 4 additions & 0 deletions Fushigi/Fushigi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PackageReference Include="Silk.NET.Input" Version="2.19.0" />
<PackageReference Include="Silk.NET.Input.Extensions" Version="2.19.0" />
<PackageReference Include="Silk.NET.OpenGL.Extensions.ImGui" Version="2.19.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.3" />
<PackageReference Include="StbImageSharp" Version="2.27.13" />
<PackageReference Include="ZstdSharp.Port" Version="0.7.3" />
</ItemGroup>
Expand Down Expand Up @@ -67,6 +68,9 @@
<None Update="res\Font.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="res\Icon.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="res\imgui.ini">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Binary file added Fushigi/res/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 42 additions & 28 deletions Fushigi/windowing/WindowManager.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
using Fushigi.util;
using ImGuiNET;
using Silk.NET.Core;
using Silk.NET.Core.Contexts;
using Silk.NET.GLFW;
using Silk.NET.Input;
using Silk.NET.Maths;
using Silk.NET.OpenGL;
using Silk.NET.Windowing;
using Silk.NET.Windowing.Glfw;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
using System.Runtime.InteropServices;

namespace Fushigi.windowing
{
internal static class WindowManager
{
public static IGLContext? SharedContext { get; private set; } = null;

private static GL? s_gl = null;
private static GL? sGL = null;

private record struct WindowResources(ImGuiController ImguiController, IInputContext Input, GL Gl, bool HasRenderDelegate);

private static bool s_isRunning = false;
private static bool sIsRunning = false;

private static readonly List<IWindow> s_pendingInits = [];
private static readonly List<(IWindow window, WindowResources res)> s_windows = [];
private static readonly List<IWindow> sPendingInits = [];
private static readonly List<(IWindow window, WindowResources res)> sWindows = [];

public static void CreateWindow(out IWindow window, Vector2D<int>? initialWindowSize = null, Action? onConfigureIO = null)
public static unsafe void CreateWindow(out IWindow window, Vector2D<int>? initialWindowSize = null, Action? onConfigureIO = null)
{
var options = WindowOptions.Default;
options.Title = $"Fushigi {Program.Version}";
Expand All @@ -45,33 +46,46 @@ public static void CreateWindow(out IWindow window, Vector2D<int>? initialWindow

window.Load += () =>
{
if(s_gl == null)
s_gl = _window.CreateOpenGL();
sGL ??= _window.CreateOpenGL();

//initialization
if (_window.Native!.Win32.HasValue)
WindowsDarkmodeUtil.SetDarkmodeAware(_window.Native.Win32.Value.Hwnd);

var input = _window.CreateInput();
var imguiController = new ImGuiController(s_gl, _window, input, onConfigureIO);
var imguiController = new ImGuiController(sGL, _window, input, onConfigureIO);

//update
_window.Update += ds => imguiController.Update((float)ds);

s_windows.Add((_window, new WindowResources(imguiController, input, s_gl, false)));
Logger.Logger.LogMessage("WindowManager", "Loading icon");
using var image = SixLabors.ImageSharp.Image.Load<Rgba32>(Path.Combine("res", "Icon.png"));
var memoryGroup = image.GetPixelMemoryGroup();
Memory<byte> array = new byte[memoryGroup.TotalLength * sizeof(Rgba32)];
var block = MemoryMarshal.Cast<byte, Rgba32>(array.Span);
foreach (var memory in memoryGroup)
{
memory.Span.CopyTo(block);
block = block[memory.Length..];
}

var icon = new RawImage(image.Width, image.Height, array);
_window.SetWindowIcon(ref icon);

sWindows.Add((_window, new WindowResources(imguiController, input, sGL, false)));
};

s_pendingInits.Add(window);
sPendingInits.Add(window);
}

public static void RegisterRenderDelegate(IWindow window, Action<GL, double, ImGuiController> renderGLDelegate)
{
int idx = s_windows.FindIndex(x => x.window == window);
int idx = sWindows.FindIndex(x => x.window == window);

if (idx == -1)
throw new Exception($"window was not created using the {nameof(WindowManager)} class");

var res = s_windows[idx].res;
var res = sWindows[idx].res;

if(res.HasRenderDelegate)
throw new Exception("window has already registered a render delegate");
Expand All @@ -91,32 +105,32 @@ public static void RegisterRenderDelegate(IWindow window, Action<GL, double, ImG
};

res.HasRenderDelegate = true;
s_windows[idx] = (window, res);
sWindows[idx] = (window, res);
}

public static void Run()
{
if (s_isRunning)
if (sIsRunning)
return;

s_isRunning = true;
sIsRunning = true;

while (s_windows.Count > 0 || s_pendingInits.Count > 0)
while (sWindows.Count > 0 || sPendingInits.Count > 0)
{
if (s_pendingInits.Count > 0)
if (sPendingInits.Count > 0)
{
foreach (var window in s_pendingInits)
foreach (var window in sPendingInits)
{
window.Initialize();
SharedContext ??= window.GLContext;
}

s_pendingInits.Clear();
sPendingInits.Clear();
}

for (int i = 0; i < s_windows.Count; i++)
for (int i = 0; i < sWindows.Count; i++)
{
var (window, res) = s_windows[i];
var (window, res) = sWindows[i];

window.DoEvents();
if (!window.IsClosing)
Expand All @@ -127,10 +141,10 @@ public static void Run()

if (window.IsClosing)
{
s_windows.RemoveAt(i);
sWindows.RemoveAt(i);

if (window.GLContext == SharedContext && s_windows.Count > 0)
SharedContext = s_windows[0].window.GLContext;
if (window.GLContext == SharedContext && sWindows.Count > 0)
SharedContext = sWindows[0].window.GLContext;

res.Input.Dispose();
res.ImguiController.Dispose();
Expand All @@ -143,7 +157,7 @@ public static void Run()
}
}

s_gl?.Dispose();
sGL?.Dispose();
}
}
}

0 comments on commit a333768

Please sign in to comment.