Skip to content

Commit

Permalink
Merge pull request #22 from Tornado-Technology/texture-rework
Browse files Browse the repository at this point in the history
Texture Manager rework
  • Loading branch information
JerryImMouse authored Jul 13, 2024
2 parents 6cd7df5 + d88cffc commit dfbf30f
Show file tree
Hide file tree
Showing 16 changed files with 267 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Hypercube.Client/Entities/Systems/Sprite/SpriteSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ private void OnRenderDrawing(ref RenderDrawingEvent ev)

public void Render(Entity<SpriteComponent> entity, Transform2 transform)
{
_drawing.DrawTexture(entity.Component.TextureHandle ??= _textureManager.GetHandler(entity.Component.TexturePath), Vector2.Zero, entity.Component.Color, transform.Matrix * entity.Component.Transform.Matrix);
_drawing.DrawTexture(entity.Component.TextureHandle ??= _textureManager.GetTextureHandle(entity.Component.TexturePath), Vector2.Zero, entity.Component.Color, transform.Matrix * entity.Component.Transform.Matrix);
}
}
2 changes: 1 addition & 1 deletion Hypercube.Client/Graphics/Drawing/RenderDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void DrawTexture(ITexture texture, Box2 quad, Box2 uv)

public void DrawTexture(ITexture texture, Box2 quad, Box2 uv, Color color)
{
_renderer.DrawTexture(_textureManager.GetHandler(texture), quad, uv, color);
_renderer.DrawTexture(_textureManager.GetTextureHandle(texture, new Texture2DCreationSettings()), quad, uv, color);
}

public void DrawTexture(ITextureHandle texture, Vector2 position, Color color, Matrix4X4 model)
Expand Down
10 changes: 4 additions & 6 deletions Hypercube.Client/Graphics/Rendering/Renderer.Render.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using Hypercube.Client.Graphics.Event;
using Hypercube.Client.Graphics.Shading;
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Client.Graphics.Texturing.TextureSettings;
using Hypercube.Client.Graphics.Viewports;
using Hypercube.Shared.Math;
using Hypercube.Shared.Math.Box;
using Hypercube.Client.Graphics.Windows;
using Hypercube.Shared.Math.Matrix;
using Hypercube.Shared.Runtimes.Loop.Event;
using OpenToolkit.Graphics.OpenGL4;
using HTexTarget = Hypercube.Client.Graphics.Texturing.TextureSettings.TextureParameters.TextureTarget;


namespace Hypercube.Client.Graphics.Rendering;

Expand All @@ -35,8 +33,8 @@ public sealed partial class Renderer
private void OnLoad()
{
_baseShader = new Shader("/base", _resourceManager);
_baseTexture = _textureManager.GetHandler("/icon.png");
_baseTexture.Bind();
_baseTexture = _textureManager.GetTextureHandle("/icon.png");
_baseTexture.Bind(HTexTarget.Texture2D);

_cameraManager.SetMainCamera(_cameraManager.CreateCamera2D(MainWindow.Size));

Expand Down
18 changes: 17 additions & 1 deletion Hypercube.Client/Graphics/Rendering/Renderer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Frozen;
using Hypercube.Client.Graphics.OpenGL;
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Client.Graphics.Texturing.Events;
using Hypercube.Client.Graphics.Viewports;
using Hypercube.Client.Graphics.Windows;
using Hypercube.Client.Graphics.Windows.Manager;
Expand Down Expand Up @@ -69,12 +70,25 @@ public sealed partial class Renderer : IRenderer, IPostInject, IEventSubscriber

public void PostInject()
{
_eventBus.Subscribe<TexturesPreloadEvent>(this, OnTexturesPreload);
_eventBus.Subscribe<HandlesPreloadEvent>(this, OnHandlesPreload);
_eventBus.Subscribe<RuntimeInitializationEvent>(this, OnInitialization);
_eventBus.Subscribe<RuntimeStartupEvent>(this, OnStartup);
_eventBus.Subscribe<UpdateFrameEvent>(this, OnFrameUpdate);
_eventBus.Subscribe<RenderFrameEvent>(this, OnFrameRender);
}

private void OnTexturesPreload(ref TexturesPreloadEvent args)
{
args.Textures.Add("/Icons/image.png");
args.Textures.Add("/icon.png");
}

private void OnHandlesPreload(ref HandlesPreloadEvent args)
{
args.Handles.Add("/icon.png");
}

private void OnInitialization(ref RuntimeInitializationEvent args)
{
_windowManager = CreateWindowManager();
Expand Down Expand Up @@ -102,7 +116,9 @@ private void OnStartup(ref RuntimeStartupEvent args)


InitOpenGL();


_textureManager.CacheHandles();

OnLoad();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Hypercube.Shared.EventBus.Events;
using Hypercube.Shared.Resources;

namespace Hypercube.Client.Graphics.Texturing.Events;

public record struct HandlesPreloadEvent(HashSet<ResourcePath> Handles) : IEventArgs;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Hypercube.Shared.EventBus.Events;
using Hypercube.Shared.Resources;

namespace Hypercube.Client.Graphics.Texturing.Events;

public record struct TexturesPreloadEvent(HashSet<ResourcePath> Textures) : IEventArgs;
2 changes: 2 additions & 0 deletions Hypercube.Client/Graphics/Texturing/ITexture.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Hypercube.Shared.Math.Box;
using Hypercube.Shared.Math.Vector;
using Hypercube.Shared.Resources;

namespace Hypercube.Client.Graphics.Texturing;

public interface ITexture
{
ResourcePath Path { get; }
int Width { get; }
int Height { get; }
byte[] Data { get; }
Expand Down
9 changes: 6 additions & 3 deletions Hypercube.Client/Graphics/Texturing/ITextureHandle.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
namespace Hypercube.Client.Graphics.Texturing;
using Hypercube.Client.Graphics.Texturing.TextureSettings.TextureParameters;

public interface ITextureHandle
namespace Hypercube.Client.Graphics.Texturing;

public interface ITextureHandle : IDisposable
{
int Handle { get; }
ITexture Texture { get; }

void Bind();
void Bind(TextureTarget target);
void Unbind(TextureTarget target);
}
19 changes: 7 additions & 12 deletions Hypercube.Client/Graphics/Texturing/ITextureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ namespace Hypercube.Client.Graphics.Texturing;

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

ITextureHandle CreateHandler(ResourcePath path, ITextureCreationSettings settings);
ITextureHandle CreateHandler(ITexture texture, ITextureCreationSettings settings);
ITextureHandle GetHandler(ResourcePath path);
ITextureHandle GetHandler(ITexture texture);
ITextureHandle GetTextureHandle(ResourcePath path, ITextureCreationSettings settings);
ITextureHandle GetTextureHandle(ResourcePath path);

ITextureHandle GetTextureHandle(ITexture texture, ITextureCreationSettings settings);
ITextureHandle GetTextureHandle(ITexture texture);
}
15 changes: 7 additions & 8 deletions Hypercube.Client/Graphics/Texturing/Texture.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using Hypercube.Shared.Math.Box;
using Hypercube.Shared.Math.Vector;
using Hypercube.Shared.Resources;

namespace Hypercube.Client.Graphics.Texturing;

public readonly struct Texture(Vector2Int size, byte[] data) : ITexture
public readonly struct Texture(ResourcePath path, Vector2Int size, byte[] data) : ITexture
{
public readonly Vector2Int Size = size;

public int Width => size.X;
public int Height => size.Y;
public byte[] Data => data;

public ResourcePath Path { get; } = path;
public int Width { get; } = size.X;
public int Height { get; } = size.Y;
public byte[] Data { get; } = data;
public Box2 QuadCrateTranslated(Vector2 position)
{
return new Box2(position, position + (Vector2)Size);
return new Box2(position, position + (Vector2)size);
}
}
29 changes: 20 additions & 9 deletions Hypercube.Client/Graphics/Texturing/TextureHandle.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
using Hypercube.Client.Graphics.Texturing.TextureSettings;
using Hypercube.Client.Utilities;
using OpenToolkit.Graphics.OpenGL4;
using TextureTarget = Hypercube.Client.Graphics.Texturing.TextureSettings.TextureParameters.TextureTarget;

namespace Hypercube.Client.Graphics.Texturing;

public sealed class TextureHandle : ITextureHandle
{
public int Handle { get; init; }
public ITexture Texture { get; init; }

public TextureHandle(ITexture texture, ITextureCreationSettings settings)
{
Handle = GL.GenTexture();
Texture = texture;

var target = settings.TextureTarget.ToOpenToolkit();

GL.BindTexture(target, Handle);

foreach (var param in settings.Parameters)
Expand All @@ -23,7 +22,7 @@ public TextureHandle(ITexture texture, ITextureCreationSettings settings)
}

GL.TexImage2D(
settings.TextureTarget.ToOpenToolkit(),
target,
settings.Level,
settings.PixelInternalFormat.ToOpenToolkit(),
texture.Width, texture.Height,
Expand All @@ -32,11 +31,23 @@ public TextureHandle(ITexture texture, ITextureCreationSettings settings)
settings.PixelType.ToOpenToolkit(),
texture.Data);

GL.GenerateMipmap((GenerateMipmapTarget)settings.TextureTarget);
GL.GenerateMipmap((GenerateMipmapTarget)target);
}

public void Bind()

public int Handle { get; }
public ITexture Texture { get; }
public void Bind(TextureTarget target)
{
GL.BindTexture(target.ToOpenToolkit(), Handle);
}

public void Unbind(TextureTarget target)
{
GL.BindTexture(target.ToOpenToolkit(), 0);
}

public void Dispose()
{
GL.BindTexture(TextureTarget.Texture2D, Handle);
GL.DeleteTexture(Handle);
}
}
Loading

0 comments on commit dfbf30f

Please sign in to comment.