Skip to content

Commit

Permalink
Merge pull request #3 from JerryImMouse/do-flip
Browse files Browse the repository at this point in the history
Setting window image icon
  • Loading branch information
Tornado-Technology authored Jul 10, 2024
2 parents b3eb798 + 1234317 commit 62e1646
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 6 deletions.
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.

0 comments on commit 62e1646

Please sign in to comment.