From 8624ad6d48dcc89a46a87ad6e249ce59d8f577a6 Mon Sep 17 00:00:00 2001 From: JerryImMouse Date: Thu, 11 Jul 2024 02:37:05 +0500 Subject: [PATCH] DO A FCKN FLIP! --- .../Graphics/Rendering/Renderer.Render.cs | 2 +- .../Graphics/Rendering/Renderer.cs | 4 ++ .../Graphics/Texturing/ITextureManager.cs | 7 +++ .../Graphics/Texturing/TextureManager.cs | 10 ++++ .../Manager/GlfwWindowManager.Window.cs | 47 ++++++++++++++++-- .../Windows/Manager/IWindowManager.cs | 3 ++ .../Graphics/Windows/WindowCreateSettings.cs | 2 + Resources/Icons/image.png | Bin 0 -> 1184 bytes Resources/Textures/icon.png | Bin 0 -> 1680 bytes 9 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 Resources/Icons/image.png create mode 100644 Resources/Textures/icon.png diff --git a/Hypercube.Client/Graphics/Rendering/Renderer.Render.cs b/Hypercube.Client/Graphics/Rendering/Renderer.Render.cs index bbfb1fd..4ba1f23 100644 --- a/Hypercube.Client/Graphics/Rendering/Renderer.Render.cs +++ b/Hypercube.Client/Graphics/Rendering/Renderer.Render.cs @@ -32,7 +32,7 @@ public sealed partial class Renderer private void OnLoad() { _baseShader = new Shader("Resources/Shaders/base"); - _baseTexture = _textureManager.CreateHandler("Resources/Textures/opengl_logo.png"); + _baseTexture = _textureManager.CreateHandler("Resources/Textures/icon.png"); _baseTexture.Bind(); _vbo = new BufferObject(BufferTarget.ArrayBuffer); diff --git a/Hypercube.Client/Graphics/Rendering/Renderer.cs b/Hypercube.Client/Graphics/Rendering/Renderer.cs index 057a1e5..4d55791 100644 --- a/Hypercube.Client/Graphics/Rendering/Renderer.cs +++ b/Hypercube.Client/Graphics/Rendering/Renderer.cs @@ -93,6 +93,10 @@ private void OnStartup(RuntimeStartupEvent args) break; } + var windowIcons = _windowManager.LoadWindowIcon(_textureManager, "Resources/Icons").ToList(); + _windowManager.SetWindowIcons(MainWindow, windowIcons); + + InitOpenGL(); OnLoad(); diff --git a/Hypercube.Client/Graphics/Texturing/ITextureManager.cs b/Hypercube.Client/Graphics/Texturing/ITextureManager.cs index b6b7f9e..75cc0ce 100644 --- a/Hypercube.Client/Graphics/Texturing/ITextureManager.cs +++ b/Hypercube.Client/Graphics/Texturing/ITextureManager.cs @@ -3,6 +3,13 @@ public interface ITextureManager { ITexture Create(string path); + /// + /// Creates ITexture, allows to set flipping mode + /// + /// Path to image + /// DO FLIP + /// ITexture + ITexture Create(string path, bool doFlip); ITextureHandle CreateHandler(string path); ITextureHandle CreateHandler(ITexture texture); diff --git a/Hypercube.Client/Graphics/Texturing/TextureManager.cs b/Hypercube.Client/Graphics/Texturing/TextureManager.cs index 7420b4e..b241759 100644 --- a/Hypercube.Client/Graphics/Texturing/TextureManager.cs +++ b/Hypercube.Client/Graphics/Texturing/TextureManager.cs @@ -15,6 +15,16 @@ public ITexture Create(string path) return Create(ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha)); } + public ITexture Create(string path, bool doFlip) + { + if (doFlip) + StbImage.stbi_set_flip_vertically_on_load(0); + var texture = Create(path); + + StbImage.stbi_set_flip_vertically_on_load(1); + return texture; + } + public ITextureHandle CreateHandler(ITexture texture) { return new TextureHandle(texture); diff --git a/Hypercube.Client/Graphics/Windows/Manager/GlfwWindowManager.Window.cs b/Hypercube.Client/Graphics/Windows/Manager/GlfwWindowManager.Window.cs index 74dc1c1..3f22442 100644 --- a/Hypercube.Client/Graphics/Windows/Manager/GlfwWindowManager.Window.cs +++ b/Hypercube.Client/Graphics/Windows/Manager/GlfwWindowManager.Window.cs @@ -1,9 +1,12 @@ using System.Diagnostics.CodeAnalysis; +using System.Runtime.InteropServices; using Hypercube.Client.Graphics.OpenGL; +using Hypercube.Client.Graphics.Texturing; using Hypercube.Client.Graphics.Windows.Manager.Registrations; using Hypercube.Client.Utilities; using OpenTK.Windowing.Common; using OpenTK.Windowing.GraphicsLibraryFramework; +using GlfwImage = OpenTK.Windowing.GraphicsLibraryFramework.Image; using Monitor = OpenTK.Windowing.GraphicsLibraryFramework.Monitor; namespace Hypercube.Client.Graphics.Windows.Manager; @@ -103,10 +106,10 @@ public WindowCreateResult WindowCreate(ContextInfo? context, WindowCreateSetting if (settings.NoTitleBar) GLFW.WindowHint(WindowHintBool.Decorated, false); - return new WindowCreateResult(WindowSetup(window), null); + return new WindowCreateResult(WindowSetup(window, settings), null); } - private GlfwWindowRegistration WindowSetup(Window* window) + private GlfwWindowRegistration WindowSetup(Window* window, WindowCreateSettings settings) { GLFWHelper.GetFramebufferSize(window, out var framebufferSize); GLFWHelper.GetWindowSize(window, out var size); @@ -122,14 +125,17 @@ private GlfwWindowRegistration WindowSetup(Window* window) }; registration.Handle = new WindowHandle(_renderer, registration); - + + // Setting icons + if (settings.WindowImages != null) + SetWindowIcons(registration, settings.WindowImages.ToList()); + // Setting callbacks GLFW.SetKeyCallback(window, OnWindowKeyHandled); GLFW.SetWindowCloseCallback(window, OnWindowClosed); GLFW.SetWindowSizeCallback(window, OnWindowResized); GLFW.SetWindowFocusCallback(window, OnWindowFocusChanged); - return registration; } @@ -179,5 +185,36 @@ private bool TryGetWindow(Window* window, [NotNullWhen(true)] out GlfwWindowRegi return null; } - + + public IEnumerable LoadWindowIcon(ITextureManager textureMan, string resPath) + { + var files = Directory.EnumerateFiles(resPath, "*.png"); + + foreach (var file in files) + { + yield return textureMan.Create(file, true); + } + } + + public void SetWindowIcons(WindowRegistration window, List images) + { + if (window is not GlfwWindowRegistration glfwWindow) + return; + + var count = images.Count; + + // ReSharper disable once SuggestVarOrType_Elsewhere + Span handles = stackalloc GCHandle[count]; + Span glfwImages = stackalloc GlfwImage[count]; + + for (var i = 0; i < count; i++) + { + var image = images[i]; + handles[i] = GCHandle.Alloc(image.Data, GCHandleType.Pinned); + var addrOfPinnedObject = (byte*) handles[i].AddrOfPinnedObject(); + glfwImages[i] = new GlfwImage(image.Width, image.Height, addrOfPinnedObject); + } + + GLFW.SetWindowIcon(glfwWindow.Pointer, glfwImages); + } } \ No newline at end of file diff --git a/Hypercube.Client/Graphics/Windows/Manager/IWindowManager.cs b/Hypercube.Client/Graphics/Windows/Manager/IWindowManager.cs index 52b9992..b9a4b3d 100644 --- a/Hypercube.Client/Graphics/Windows/Manager/IWindowManager.cs +++ b/Hypercube.Client/Graphics/Windows/Manager/IWindowManager.cs @@ -1,5 +1,6 @@ using Hypercube.Client.Graphics.Monitors; using Hypercube.Client.Graphics.OpenGL; +using Hypercube.Client.Graphics.Texturing; using Hypercube.Shared.Math.Vector; namespace Hypercube.Client.Graphics.Windows.Manager; @@ -29,6 +30,8 @@ public interface IWindowManager : IDisposable void WindowSetVisible(WindowRegistration registration, bool visible); void WindowSetSize(WindowRegistration registration, Vector2Int size); void WindowSwapBuffers(WindowRegistration window); + IEnumerable LoadWindowIcon(ITextureManager textureMan, string resPath); + void SetWindowIcons(WindowRegistration window, List images); nint GetProcAddress(string procName); } \ No newline at end of file diff --git a/Hypercube.Client/Graphics/Windows/WindowCreateSettings.cs b/Hypercube.Client/Graphics/Windows/WindowCreateSettings.cs index 813a230..eea9672 100644 --- a/Hypercube.Client/Graphics/Windows/WindowCreateSettings.cs +++ b/Hypercube.Client/Graphics/Windows/WindowCreateSettings.cs @@ -1,4 +1,5 @@ using Hypercube.Client.Graphics.Monitors; +using Hypercube.Client.Graphics.Texturing; using Hypercube.Shared.Math.Vector; namespace Hypercube.Client.Graphics.Windows; @@ -15,6 +16,7 @@ public class WindowCreateSettings public Vector2Int Size = new(1280, 720); public WindowStyles Styles; public Version Version; + public ITexture[]? WindowImages = null; public IMonitorHandle? Monitor; diff --git a/Resources/Icons/image.png b/Resources/Icons/image.png new file mode 100644 index 0000000000000000000000000000000000000000..f3df425b1f74cfe6d5c660a950e0fedc028025bb GIT binary patch literal 1184 zcmV;R1Yi4!P)Px#1ZP1_K>z@;j|==^1poj9FiAu~RCr$PnY~WqKoo_YkQInffdmqfpnwM6fd@c5 z00k9vq@|?Z5=@y=Pz8vooG&y10hPqJn*@!Wgv@y)MW zQ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