Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting window image icon #3

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Hypercube.Client/Graphics/Rendering/Renderer.Render.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions Hypercube.Client/Graphics/Rendering/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ private void OnStartup(RuntimeStartupEvent args)
break;
}

var windowIcons = _windowManager.LoadWindowIcon(_textureManager, "Resources/Icons").ToList();
_windowManager.SetWindowIcons(MainWindow, windowIcons);


InitOpenGL();

OnLoad();
Expand Down
7 changes: 7 additions & 0 deletions Hypercube.Client/Graphics/Texturing/ITextureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
public interface ITextureManager
{
ITexture Create(string path);
/// <summary>
/// Creates ITexture, allows to set flipping mode
/// </summary>
/// <param name="path">Path to image</param>
/// <param name="doFlip"><a href="https://www.youtube.com/watch?v=WQuL95_ckDo">DO FLIP</a></param>
/// <returns>ITexture</returns>
ITexture Create(string path, bool doFlip);

ITextureHandle CreateHandler(string path);
ITextureHandle CreateHandler(ITexture texture);
Expand Down
10 changes: 10 additions & 0 deletions Hypercube.Client/Graphics/Texturing/TextureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -105,7 +108,7 @@ public WindowCreateResult WindowCreate(ContextInfo? context, WindowCreateSetting
return new WindowCreateResult(WindowSetup(window), 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);
Expand All @@ -121,14 +124,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;
}

Expand Down Expand Up @@ -178,5 +184,36 @@ private bool TryGetWindow(Window* window, [NotNullWhen(true)] out GlfwWindowRegi

return null;
}


public IEnumerable<ITexture> 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<ITexture> images)
{
if (window is not GlfwWindowRegistration glfwWindow)
return;

var count = images.Count;

// ReSharper disable once SuggestVarOrType_Elsewhere
Span<GCHandle> handles = stackalloc GCHandle[count];
Span<GlfwImage> 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);
}
}
3 changes: 3 additions & 0 deletions Hypercube.Client/Graphics/Windows/Manager/IWindowManager.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<ITexture> LoadWindowIcon(ITextureManager textureMan, string resPath);
void SetWindowIcons(WindowRegistration window, List<ITexture> images);

nint GetProcAddress(string procName);
}
4 changes: 3 additions & 1 deletion Hypercube.Client/Graphics/Windows/WindowCreateSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Hypercube.Client.Graphics.Monitors;
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Shared.Math.Vector;

namespace Hypercube.Client.Graphics.Windows;
Expand All @@ -10,7 +11,8 @@ public class WindowCreateSettings

public string Title = "Hypercube Window";
public Vector2Int Size = new(1280, 720);

public ITexture[]? WindowImages = null;

public IMonitorHandle? Monitor;

public bool Resizable = true;
Expand Down
Binary file added Resources/Icons/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/Textures/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading