From 4b2271dd292541fda9da15dcc548543b50cd0d21 Mon Sep 17 00:00:00 2001 From: "H. Utku Maden" Date: Mon, 15 May 2023 21:49:48 +0300 Subject: [PATCH] Change textures from interface to abstract class. --- Quik.FreeType/FreeTypeFont.cs | 4 +-- Quik.OpenTK/OpenGLTexture.cs | 31 +++++++++--------- Quik.OpenTK/OpenGLTextureManager.cs | 2 +- Quik/CommandQueue/CommandQueue.cs | 8 ++--- Quik/IQuikTexture.cs | 36 ++++++++++++++------- Quik/IQuikTextureManager.cs | 2 +- Quik/QuikStyle.cs | 4 +-- Quik/Typography/QuikFont.cs | 2 +- Quik/Typography/TextLayout.cs | 8 ++--- Quik/VertexGenerator/QuikVertexGenerator.cs | 10 +++--- QuikTestApplication/TestFont.cs | 4 +-- 11 files changed, 63 insertions(+), 48 deletions(-) diff --git a/Quik.FreeType/FreeTypeFont.cs b/Quik.FreeType/FreeTypeFont.cs index d34de5b..1c2ad34 100644 --- a/Quik.FreeType/FreeTypeFont.cs +++ b/Quik.FreeType/FreeTypeFont.cs @@ -40,7 +40,7 @@ namespace Quik.FreeType return FT.GetCharIndex(_face, (ulong)character) != 0; } - public override void GetCharacter(int character, out IQuikTexture texture, out QuikGlyph glyph) + public override void GetCharacter(int character, out QuikTexture texture, out QuikGlyph glyph) { GlyphEntry entry; @@ -102,7 +102,7 @@ namespace Quik.FreeType private class Atlas : IDisposable { - public IQuikTexture Texture; + public QuikTexture Texture; private QuikVec2 _pointer = new QuikVec2(); private float _verticalAdvance = 0; diff --git a/Quik.OpenTK/OpenGLTexture.cs b/Quik.OpenTK/OpenGLTexture.cs index 8712469..2b96ed4 100644 --- a/Quik.OpenTK/OpenGLTexture.cs +++ b/Quik.OpenTK/OpenGLTexture.cs @@ -3,17 +3,21 @@ using OpenTK.Graphics.OpenGL4; namespace Quik.OpenTK { - public class OpenGLTexture : IQuikTexture + public class OpenGLTexture : QuikTexture { public int TextureId { get; private set; } private OpenGLTextureManager Manager { get; } + private int _width; + private int _height; + private bool _mipmaps; + internal OpenGLTexture(OpenGLTextureManager manager, QuikImageFormat format, QuikVec2 size, bool mipmaps) { Manager = manager; - Mipmaps = mipmaps; - Width = (int)size.X; - Height = (int)size.Y; + _mipmaps = mipmaps; + _width = (int)size.X; + _height = (int)size.Y; TextureId = GL.GenTexture(); @@ -40,29 +44,26 @@ namespace Quik.OpenTK } /// - public bool Equals(IQuikTexture other) => + public override bool Equals(QuikTexture other) => other is OpenGLTexture && ((OpenGLTexture)other).TextureId == TextureId; private bool _isDisposed = false; - private void Dispose(bool disposing) + protected override void Dispose(bool disposing) { if (_isDisposed) return; Manager?.Reclaim(this); _isDisposed = true; } - - /// - public void Dispose() => Dispose(true); /// - public int Width { get; } + public override int Width => _width; /// - public int Height { get; } + public override int Height => _height; /// - public bool Mipmaps { get; } + public override bool Mipmaps => _mipmaps; /// - public void Image(IntPtr data, QuikImageFormat format, QuikVec2 size, int level, int alignment = 4) + public override void Image(IntPtr data, QuikImageFormat format, QuikVec2 size, int level, int alignment = 4) { GL.BindTexture(TextureTarget.Texture2D, TextureId); GL.PixelStore(PixelStoreParameter.UnpackAlignment, alignment); @@ -70,7 +71,7 @@ namespace Quik.OpenTK } /// - public void SubImage(IntPtr data, QuikImageFormat format, QuikRectangle location, int level, int alignment = 4) + public override void SubImage(IntPtr data, QuikImageFormat format, QuikRectangle location, int level, int alignment = 4) { GL.BindTexture(TextureTarget.Texture2D, TextureId); GL.PixelStore(PixelStoreParameter.UnpackAlignment, alignment); @@ -87,7 +88,7 @@ namespace Quik.OpenTK } /// - public void GenerateMipMaps() + public override void GenerateMipMaps() { GL.BindTexture(TextureTarget.Texture2D, TextureId); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); diff --git a/Quik.OpenTK/OpenGLTextureManager.cs b/Quik.OpenTK/OpenGLTextureManager.cs index 5c8374e..3ff73cc 100644 --- a/Quik.OpenTK/OpenGLTextureManager.cs +++ b/Quik.OpenTK/OpenGLTextureManager.cs @@ -9,7 +9,7 @@ namespace Quik.OpenTK private List _reclaimList = new List(); - public IQuikTexture CreateTexture(QuikVec2 size, bool mipmaps, QuikImageFormat format) + public QuikTexture CreateTexture(QuikVec2 size, bool mipmaps, QuikImageFormat format) { return new OpenGLTexture(this, format, size, mipmaps); } diff --git a/Quik/CommandQueue/CommandQueue.cs b/Quik/CommandQueue/CommandQueue.cs index 843fd72..7368e6f 100644 --- a/Quik/CommandQueue/CommandQueue.cs +++ b/Quik/CommandQueue/CommandQueue.cs @@ -199,7 +199,7 @@ namespace Quik.CommandQueue } } - public void Image(IQuikTexture texture, in QuikRectangle rectangle) + public void Image(QuikTexture texture, in QuikRectangle rectangle) { Enqueue(Command.Image); Enqueue((Frame)(int)ImageCommandFlags.Single); @@ -207,7 +207,7 @@ namespace Quik.CommandQueue Enqueue(rectangle); } - public void Image(IQuikTexture texture, in QuikRectangle rectangle, in QuikRectangle uv) + public void Image(QuikTexture texture, in QuikRectangle rectangle, in QuikRectangle uv) { Enqueue(Command.Image); Enqueue((Frame)(int)(ImageCommandFlags.Single | ImageCommandFlags.UVs)); @@ -216,7 +216,7 @@ namespace Quik.CommandQueue Enqueue(uv); } - public void Image(IQuikTexture texture, QuikRectangle[] rectangles, bool interleavedUV = false) + public void Image(QuikTexture texture, QuikRectangle[] rectangles, bool interleavedUV = false) { ImageCommandFlags flags = interleavedUV ? ImageCommandFlags.UVs : ImageCommandFlags.None; @@ -230,7 +230,7 @@ namespace Quik.CommandQueue } } - public void Image(IQuikTexture texture, QuikRectangle[] rectangles, QuikRectangle[] uvs) + public void Image(QuikTexture texture, QuikRectangle[] rectangles, QuikRectangle[] uvs) { int count = Math.Min(rectangles.Length, uvs.Length); Enqueue(Command.Image); diff --git a/Quik/IQuikTexture.cs b/Quik/IQuikTexture.cs index 98bb9ec..5533565 100644 --- a/Quik/IQuikTexture.cs +++ b/Quik/IQuikTexture.cs @@ -5,22 +5,28 @@ namespace Quik /// /// Interface for texture instances. /// - public interface IQuikTexture : IEquatable, IDisposable + public abstract class QuikTexture : IEquatable, IDisposable { /// /// Width of the texture. /// - int Width { get; } - + public abstract int Width { get; } + /// /// Height of the texture. /// - int Height { get; } - + public abstract int Height { get; } + /// /// True if the texture can have mipmaps. /// - bool Mipmaps { get; } + public abstract bool Mipmaps { get; } + + /// + /// Indicates whether this texture contains a signed distance field. + /// + /// + public bool SignedDistanceField { get; set; } /// /// Upload texture data. @@ -30,8 +36,8 @@ namespace Quik /// Size of the texture data. /// Mip level. /// Pixel alignment. Expected to be 1, 2, 4, or 8. - void Image(IntPtr data, QuikImageFormat format, QuikVec2 size, int level, int alignment = 4); - + public abstract void Image(IntPtr data, QuikImageFormat format, QuikVec2 size, int level, int alignment = 4); + /// /// Upload texture data. /// @@ -40,11 +46,19 @@ namespace Quik /// Location of the data in the texture. /// Mip level. /// Pixel alignment. Expected to be 1, 2, 4, or 8. - void SubImage(IntPtr data, QuikImageFormat format, QuikRectangle location, int level, int alignment = 4); - + public abstract void SubImage(IntPtr data, QuikImageFormat format, QuikRectangle location, int level, int alignment = 4); + /// /// Generate the mip maps for the texture. /// - void GenerateMipMaps(); + public abstract void GenerateMipMaps(); + + public virtual bool Equals(QuikTexture other) + { + return base.Equals(other); + } + + public void Dispose() => Dispose(true); + protected virtual void Dispose(bool isDisposing) { } } } \ No newline at end of file diff --git a/Quik/IQuikTextureManager.cs b/Quik/IQuikTextureManager.cs index 2439be6..64ff7ef 100644 --- a/Quik/IQuikTextureManager.cs +++ b/Quik/IQuikTextureManager.cs @@ -21,7 +21,7 @@ namespace Quik /// All parameters are hints. If there is a situation where the texture does not /// have mip levels, or format cannot be specified, ignore these parameters. /// - IQuikTexture CreateTexture(QuikVec2 size, bool mipmaps, QuikImageFormat format); + QuikTexture CreateTexture(QuikVec2 size, bool mipmaps, QuikImageFormat format); /// /// A function called on context clear. (useful for discarding old textures) diff --git a/Quik/QuikStyle.cs b/Quik/QuikStyle.cs index 9c5ba7e..5b1e436 100644 --- a/Quik/QuikStyle.cs +++ b/Quik/QuikStyle.cs @@ -108,9 +108,9 @@ namespace Quik set => this["list-marker-position"] = value; } - public IQuikTexture ListMarkerImage + public QuikTexture ListMarkerImage { - get => (IQuikTexture)this["list-marker-image"]; + get => (QuikTexture)this["list-marker-image"]; set => this["list-marker-image"] = value; } diff --git a/Quik/Typography/QuikFont.cs b/Quik/Typography/QuikFont.cs index 44636f6..27d6990 100644 --- a/Quik/Typography/QuikFont.cs +++ b/Quik/Typography/QuikFont.cs @@ -12,6 +12,6 @@ namespace Quik.Typography public abstract float Descender { get; } public abstract bool HasCharacter(int character); - public abstract void GetCharacter(int character, out IQuikTexture texture, out QuikGlyph glyph); + public abstract void GetCharacter(int character, out QuikTexture texture, out QuikGlyph glyph); } } \ No newline at end of file diff --git a/Quik/Typography/TextLayout.cs b/Quik/Typography/TextLayout.cs index 1c7490c..392399e 100644 --- a/Quik/Typography/TextLayout.cs +++ b/Quik/Typography/TextLayout.cs @@ -280,7 +280,7 @@ namespace Quik.Typography for (int i = block.Text.Length - 1; i >= 0; i--) { char chr = block.Text[i]; - block.Font.GetCharacter(chr, out IQuikTexture texture, out QuikGlyph metrics); + block.Font.GetCharacter(chr, out QuikTexture texture, out QuikGlyph metrics); group.Add( new TypesetCharacter( chr, @@ -301,7 +301,7 @@ namespace Quik.Typography for (int i = 0; i < block.Text.Length; i++) { char chr = block.Text[i]; - block.Font.GetCharacter(chr, out IQuikTexture texture, out QuikGlyph metrics); + block.Font.GetCharacter(chr, out QuikTexture texture, out QuikGlyph metrics); group.Add( new TypesetCharacter( chr, @@ -352,13 +352,13 @@ namespace Quik.Typography public struct TypesetCharacter { public int Character; - public IQuikTexture Texture; + public QuikTexture Texture; public QuikRectangle Position; public QuikRectangle UV; public TypesetCharacter( int chr, - IQuikTexture texture, + QuikTexture texture, in QuikRectangle position, in QuikRectangle uv) { diff --git a/Quik/VertexGenerator/QuikVertexGenerator.cs b/Quik/VertexGenerator/QuikVertexGenerator.cs index 4405125..d04268a 100644 --- a/Quik/VertexGenerator/QuikVertexGenerator.cs +++ b/Quik/VertexGenerator/QuikVertexGenerator.cs @@ -1411,7 +1411,7 @@ namespace Quik.VertexGenerator private void RenderCharacter(QuikCommandPutChar chr) { - Context.DefaultFont.GetCharacter(chr.Character, out IQuikTexture texture, out QuikGlyph metrics); + Context.DefaultFont.GetCharacter(chr.Character, out QuikTexture texture, out QuikGlyph metrics); QuikVertex a, b, c, d; a = b = c = d = new QuikVertex() {Color = new QuikColor(0xffffffff)}; @@ -1445,14 +1445,14 @@ namespace Quik.VertexGenerator QuikFont font = Context.DefaultFont; QuikVertex vertex = new QuikVertex() {Color = new QuikColor(0x000000ff)}; QuikVec2 pointer = text.Position; - IQuikTexture texture = null; + QuikTexture texture = null; for (int i = 0; i < text.Text.Length; i++) { int chr = text.Text[i]; QuikGlyph metrics; - IQuikTexture ntex; + QuikTexture ntex; font.GetCharacter(chr, out ntex, out metrics); if (ntex != texture && texture != null) @@ -1502,7 +1502,7 @@ namespace Quik.VertexGenerator short startElement = (short)_elementBufferPointer; TypesetGroup group = text.Group; QuikVertex vertex = new QuikVertex() { Color = new QuikColor(0x000000ff) }; - IQuikTexture texture = null; + QuikTexture texture = null; group.SortBy(TypesetGroup.SortByTexture); foreach (TypesetCharacter chr in group) @@ -1569,6 +1569,6 @@ namespace Quik.VertexGenerator public short Count; public QuikRectangle Bounds; public bool ClearStencil; - public IQuikTexture Texture; + public QuikTexture Texture; } } \ No newline at end of file diff --git a/QuikTestApplication/TestFont.cs b/QuikTestApplication/TestFont.cs index 1470e72..46196a6 100644 --- a/QuikTestApplication/TestFont.cs +++ b/QuikTestApplication/TestFont.cs @@ -20,7 +20,7 @@ namespace QuikTestApplication public static byte[] TextureData { get; private set; } = Array.Empty(); public static QuikVec2 TextureSize { get; private set; } - public IQuikTexture Texture { get; } + public QuikTexture Texture { get; } public override float Ascender => throw new NotImplementedException(); @@ -113,7 +113,7 @@ namespace QuikTestApplication return _characters.Contains((char) character); } - public override void GetCharacter(int character, out IQuikTexture texture, out QuikGlyph glyph) + public override void GetCharacter(int character, out QuikTexture texture, out QuikGlyph glyph) { if (!_glyphs.TryGetValue(character, out glyph)) {