Skip to content

Commit

Permalink
Merge pull request #4 from JerryImMouse/resource-manager
Browse files Browse the repository at this point in the history
Resource Manager impl
  • Loading branch information
Tornado-Technology authored Jul 11, 2024
2 parents be8a0f7 + e36f3d6 commit 78578a9
Show file tree
Hide file tree
Showing 18 changed files with 565 additions and 25 deletions.
4 changes: 4 additions & 0 deletions Hypercube.Client/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Hypercube.Shared.Entities.Realisation.EventBus;
using Hypercube.Shared.Entities.Realisation.Manager;
using Hypercube.Shared.EventBus;
using Hypercube.Shared.Resources.Manager;
using Hypercube.Shared.Timing;

namespace Hypercube.Client;
Expand All @@ -31,6 +32,9 @@ public static void Register(DependenciesContainer rootContainer)
rootContainer.Register<IInputHandler, InputHandler>();
rootContainer.Register<IInputManager, InputManager>();

// Resources
rootContainer.Register<IResourceManager, ResourceManager>();

// Texturing
rootContainer.Register<ITextureManager, TextureManager>();

Expand Down
11 changes: 8 additions & 3 deletions Hypercube.Client/Graphics/Rendering/Renderer.Render.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Hypercube.Client.Graphics.Shading;
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Client.Graphics.Viewports;
using Hypercube.Client.Graphics.Windows;
using Hypercube.Shared.Math;
using Hypercube.Shared.Math.Box;
using Hypercube.Shared.Runtimes.Loop.Event;
Expand Down Expand Up @@ -31,8 +30,14 @@ public sealed partial class Renderer

private void OnLoad()
{
_baseShader = new Shader("Resources/Shaders/base");
_baseTexture = _textureManager.CreateHandler("Resources/Textures/icon.png");
// mount directories
_resourceManager.MountContentFolder(".", "/");
_resourceManager.MountContentFolder("Resources", "/");
_resourceManager.MountContentFolder("Resources/Textures", "/");
_resourceManager.MountContentFolder("Resources/Shaders", "/");

_baseShader = new Shader("/base", _resourceManager);
_baseTexture = _textureManager.CreateHandler("/icon.png");
_baseTexture.Bind();

_vbo = new BufferObject(BufferTarget.ArrayBuffer);
Expand Down
4 changes: 3 additions & 1 deletion Hypercube.Client/Graphics/Rendering/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Hypercube.Shared.Dependency;
using Hypercube.Shared.EventBus;
using Hypercube.Shared.Logging;
using Hypercube.Shared.Resources.Manager;
using Hypercube.Shared.Runtimes.Event;
using Hypercube.Shared.Runtimes.Loop.Event;
using Hypercube.Shared.Timing;
Expand All @@ -21,6 +22,7 @@ public sealed partial class Renderer : IRenderer, IPostInject
[Dependency] private readonly IEventBus _eventBus = default!;
[Dependency] private readonly ITextureManager _textureManager = default!;
[Dependency] private readonly ITiming _timing = default!;
[Dependency] private readonly IResourceManager _resourceManager = default!;

private readonly ILogger _logger = LoggingManager.GetLogger("renderer");
private readonly ILogger _loggerOpenGL = LoggingManager.GetLogger("open_gl")!;
Expand Down Expand Up @@ -93,7 +95,7 @@ private void OnStartup(RuntimeStartupEvent args)
break;
}

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


Expand Down
21 changes: 13 additions & 8 deletions Hypercube.Client/Graphics/Shading/Shader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Hypercube.Shared.Math.Vector;
using Hypercube.Shared.Resources;
using Hypercube.Shared.Resources.Manager;
using OpenToolkit.Graphics.OpenGL4;
using Vector2 = Hypercube.Shared.Math.Vector.Vector2;

Expand All @@ -8,15 +10,19 @@ public class Shader : IShader
{
public readonly int _handle;
private readonly Dictionary<string, int> _uniformLocations = new();

public Shader(string path) : this($"{path}.vert", $"{path}.frag")

public Shader(string path, IResourceManager manager) : this(new ResourcePath($"{path}.vert"), new ResourcePath($"{path}.frag"),
manager)
{
}

private Shader(string vertPath, string fragPath)
private Shader(ResourcePath vertPath, ResourcePath fragPath, IResourceManager resourceManager)
{
var vertexShader = CreateShader(vertPath, ShaderType.VertexShader);
var fragmentShader = CreateShader(fragPath, ShaderType.FragmentShader);
var vertSource = resourceManager.ReadFileContentAllText(vertPath);
var fragSource = resourceManager.ReadFileContentAllText(fragPath);

var vertexShader = CreateShader(vertSource, ShaderType.VertexShader);
var fragmentShader = CreateShader(fragSource, ShaderType.FragmentShader);

_handle = GL.CreateProgram();

Expand Down Expand Up @@ -75,9 +81,8 @@ public void SetUniform(string name, Vector2 value)
GL.Uniform2(_uniformLocations[name], value.X, value.Y);
}

private int CreateShader(string path, ShaderType type)
private int CreateShader(string source, ShaderType type)
{
var source = File.ReadAllText(path);
var shader = GL.CreateShader(type);

GL.ShaderSource(shader, source);
Expand Down
10 changes: 6 additions & 4 deletions Hypercube.Client/Graphics/Texturing/ITextureManager.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
namespace Hypercube.Client.Graphics.Texturing;
using Hypercube.Shared.Resources;

namespace Hypercube.Client.Graphics.Texturing;

public interface ITextureManager
{
ITexture Create(string path);
ITexture Create(ResourcePath 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);
ITexture Create(ResourcePath path, bool doFlip);

ITextureHandle CreateHandler(string path);
ITextureHandle CreateHandler(ResourcePath path);
ITextureHandle CreateHandler(ITexture texture);
}
16 changes: 10 additions & 6 deletions Hypercube.Client/Graphics/Texturing/TextureManager.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using StbImageSharp;
using Hypercube.Shared.Dependency;
using Hypercube.Shared.Resources;
using Hypercube.Shared.Resources.Manager;
using StbImageSharp;

namespace Hypercube.Client.Graphics.Texturing;

public sealed class TextureManager : ITextureManager
{
[Dependency] private readonly IResourceManager _resourceManager = default!;

public TextureManager()
{
StbImage.stbi_set_flip_vertically_on_load(1);
}

public ITexture Create(string path)
public ITexture Create(ResourcePath path)
{
using var stream = File.OpenRead(path);
return Create(ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha));
return Create(ImageResult.FromStream(_resourceManager.ReadFileContent(path) ?? throw new FileNotFoundException(), ColorComponents.RedGreenBlueAlpha));
}

public ITexture Create(string path, bool doFlip)
public ITexture Create(ResourcePath path, bool doFlip)
{
if (doFlip)
StbImage.stbi_set_flip_vertically_on_load(0);
Expand All @@ -30,7 +34,7 @@ public ITextureHandle CreateHandler(ITexture texture)
return new TextureHandle(texture);
}

public ITextureHandle CreateHandler(string path)
public ITextureHandle CreateHandler(ResourcePath path)
{
return CreateHandler(Create(path));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Client.Graphics.Windows.Manager.Registrations;
using Hypercube.Client.Utilities;
using Hypercube.Shared.Resources;
using Hypercube.Shared.Resources.Manager;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.GraphicsLibraryFramework;
using GlfwImage = OpenTK.Windowing.GraphicsLibraryFramework.Image;
Expand Down Expand Up @@ -185,9 +187,9 @@ private bool TryGetWindow(Window* window, [NotNullWhen(true)] out GlfwWindowRegi
return null;
}

public IEnumerable<ITexture> LoadWindowIcon(ITextureManager textureMan, string resPath)
public IEnumerable<ITexture> LoadWindowIcon(ITextureManager textureMan, IResourceManager resourceManager, ResourcePath path)
{
var files = Directory.EnumerateFiles(resPath, "*.png");
var files = resourceManager.FindContentFiles(path);

foreach (var file in files)
{
Expand Down
4 changes: 3 additions & 1 deletion Hypercube.Client/Graphics/Windows/Manager/IWindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Hypercube.Client.Graphics.OpenGL;
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Shared.Math.Vector;
using Hypercube.Shared.Resources;
using Hypercube.Shared.Resources.Manager;

namespace Hypercube.Client.Graphics.Windows.Manager;

Expand Down Expand Up @@ -30,7 +32,7 @@ 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);
IEnumerable<ITexture> LoadWindowIcon(ITextureManager textureManager, IResourceManager resourceManager, ResourcePath resPath);
void SetWindowIcons(WindowRegistration window, List<ITexture> images);

nint GetProcAddress(string procName);
Expand Down
64 changes: 64 additions & 0 deletions Hypercube.Shared/Resources/DirRoot/DirContentRoot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Diagnostics.CodeAnalysis;
using Hypercube.Shared.Logging;
using Hypercube.Shared.Utilities.Helpers;

namespace Hypercube.Shared.Resources.DirRoot;

public class DirContentRoot : IContentRoot
{
private readonly DirectoryInfo _directory;
private Logger _logger;

public DirContentRoot(DirectoryInfo directory, Logger logger)
{
_directory = directory;
_logger = logger;
}

public bool TryGetFile(ResourcePath path, [NotNullWhen(true)] out Stream? stream)
{
if (!FileExists(path))
{
stream = null;
return false;
}

try
{
stream = File.Open(GetPath(path), FileMode.Open, FileAccess.Read);
return true;
}
catch (Exception ex)
{
_logger.Error(ex.Message);
stream = null;
}
return false;
}

private bool FileExists(ResourcePath relPath)
{
var path = GetPath(relPath);
return File.Exists(path);
}

private string GetPath(ResourcePath path)
{
return Path.GetFullPath(Path.Combine(_directory.FullName, path));
}

public IEnumerable<ResourcePath> FindFiles(ResourcePath path)
{
var fullPath = GetPath(path);
if (!Directory.Exists(fullPath))
yield break;

var paths = PathHelpers.GetFiles(fullPath);

foreach (var filePath in paths)
{
var relPath = filePath.Substring(_directory.FullName.Length);
yield return ResourcePath.FromRelativeSystemPath(relPath, OperatingSystem.IsWindows() ? '\\' : '/');
}
}
}
9 changes: 9 additions & 0 deletions Hypercube.Shared/Resources/IContentRoot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Diagnostics.CodeAnalysis;

namespace Hypercube.Shared.Resources;

public interface IContentRoot
{
bool TryGetFile(ResourcePath path, [NotNullWhen(true)] out Stream? stream);
IEnumerable<ResourcePath> FindFiles(ResourcePath path);
}
14 changes: 14 additions & 0 deletions Hypercube.Shared/Resources/Manager/IResourceManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Diagnostics.CodeAnalysis;

namespace Hypercube.Shared.Resources.Manager;

public interface IResourceManager
{
StreamReader WrapStream(Stream stream);
void AddRoot(ResourcePath prefix, IContentRoot root);
void MountContentFolder(string file, ResourcePath? prefix = null);
Stream? ReadFileContent(ResourcePath path);
bool TryReadFileContent(ResourcePath path, [NotNullWhen(true)] out Stream? fileStream);
IEnumerable<ResourcePath> FindContentFiles(ResourcePath? path);
string ReadFileContentAllText(ResourcePath path);
}
Loading

0 comments on commit 78578a9

Please sign in to comment.