Texture.cs - Add IDisposable interface

This commit is contained in:
miku-666
2025-02-12 08:21:19 +01:00
parent 905765863b
commit b79188b4c2
2 changed files with 13 additions and 13 deletions

View File

@@ -6,9 +6,9 @@ using OpenTK.Graphics.OpenGL;
namespace PckStudio.Rendering.Texture
{
internal class Texture
internal abstract class Texture : IDisposable
{
protected readonly int _id;
protected readonly int _GL_Id;
protected readonly TextureTarget Target;
@@ -73,7 +73,7 @@ namespace PckStudio.Rendering.Texture
protected Texture(TextureTarget target)
{
_id = GL.GenTexture();
_GL_Id = GL.GenTexture();
Target = target;
}
@@ -85,7 +85,7 @@ namespace PckStudio.Rendering.Texture
public void Bind(int slot = 0)
{
GL.ActiveTexture(TextureUnit.Texture0 + slot);
GL.BindTexture(Target, _id);
GL.BindTexture(Target, _GL_Id);
}
public void Unbind()
@@ -99,5 +99,11 @@ namespace PckStudio.Rendering.Texture
GL.TexParameter(Target, parameterName, value);
Debug.WriteLineIf(GL.GetError() != ErrorCode.NoError, $"{Target}: {parameterName} = {value}");
}
public void Dispose()
{
Unbind();
GL.DeleteTexture(_GL_Id);
}
}
}

View File

@@ -23,23 +23,17 @@ namespace PckStudio.Rendering.Texture
public override void SetTexture(Image image)
{
Bind();
var bitmap = new Bitmap(image);
var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var bitmap = new Bitmap(image);
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, InternalPixelFormat, bitmap.Width, bitmap.Height, 0, PixelFormat, PixelType.UnsignedByte, data.Scan0);
bitmap.UnlockBits(data);
Unbind();
}
public void Dispose()
{
Unbind();
GL.DeleteTexture(_id);
}
public void AttachToFramebuffer(FrameBuffer frameBuffer, FramebufferAttachment attachment)
{
frameBuffer.Bind();
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, attachment, Target, _id, 0);
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, attachment, Target, _GL_Id, 0);
}
}
}