From 64dfa91678b7d419f0eabeb9d9c8b202e4206d56 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Thu, 12 Feb 2009 17:41:09 +0000 Subject: [PATCH] Added Clear() method to all classes that contain caches. Implemented IDisposable interface on the TextPrinter and all relevant classes. --- Source/Utilities/Graphics/ITextPrinter.cs | 2 +- .../Graphics/Text/GL1TextOutputProvider.cs | 72 +++++------- .../Graphics/Text/GdiPlusGlyphRasterizer .cs | 7 +- Source/Utilities/Graphics/Text/GlyphCache.cs | 28 ++++- Source/Utilities/Graphics/Text/GlyphSheet.cs | 21 +++- Source/Utilities/Graphics/Text/IGlyphCache.cs | 5 +- .../Graphics/Text/IGlyphRasterizer.cs | 1 + .../Graphics/Text/ITextOutputProvider.cs | 5 +- Source/Utilities/Graphics/TextPrinter.cs | 103 +++++++++++++----- 9 files changed, 161 insertions(+), 83 deletions(-) diff --git a/Source/Utilities/Graphics/ITextPrinter.cs b/Source/Utilities/Graphics/ITextPrinter.cs index 882e2e50..d8f3ca8e 100644 --- a/Source/Utilities/Graphics/ITextPrinter.cs +++ b/Source/Utilities/Graphics/ITextPrinter.cs @@ -15,7 +15,7 @@ namespace OpenTK.Graphics /// /// Defines the interface for a TextPrinter. /// - public interface ITextPrinter + public interface ITextPrinter : IDisposable { void Begin(); void End(); diff --git a/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs b/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs index f37877c7..c93177a2 100644 --- a/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs +++ b/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs @@ -39,8 +39,8 @@ namespace OpenTK.Graphics.Text // Triangle lists, sorted by texture. Dictionary> active_lists = new Dictionary>(); Queue> inactive_lists = new Queue>(); - float[] viewport = new float[4]; - bool legacy_mode = false; + + bool disposed; #endregion @@ -118,10 +118,12 @@ namespace OpenTK.Graphics.Text key.Bind(); - if (!legacy_mode) - { - GL.Scale(2.0 / (viewport[2] - viewport[0]), -2.0 / (viewport[3] - viewport[1]), 1); - } + //if (!legacy_mode) + //{ + // GL.PushMatrix(); + // GL.GetFloat(GetPName.Viewport, viewport); + // GL.Scale(2.0 / (viewport[2] - viewport[0]), -2.0 / (viewport[3] - viewport[1]), 1); + //} SetColor(color); @@ -134,6 +136,9 @@ namespace OpenTK.Graphics.Text } GL.End(); + + //if (!legacy_mode) + // GL.PopMatrix(); } // Clean layout @@ -150,47 +155,11 @@ namespace OpenTK.Graphics.Text #endregion - #region Begin + #region Clear - [Obsolete] - public void Begin() + public void Clear() { - GraphicsContext.Assert(); - - legacy_mode = true; - - GL.GetFloat(GetPName.Viewport, viewport); - - // Prepare to draw text. We want pixel perfect precision, so we setup a 2D mode, - // with size equal to the window (in pixels). - // While we could also render text in 3D mode, it would be very hard to get - // pixel-perfect precision. - GL.MatrixMode(MatrixMode.Projection); - GL.PushMatrix(); - GL.LoadIdentity(); - GL.Ortho(viewport[0], viewport[2], viewport[3], viewport[1], -1.0, 1.0); - - GL.MatrixMode(MatrixMode.Modelview); - GL.PushMatrix(); - GL.LoadIdentity(); - } - - #endregion - - #region End - - [Obsolete] - public void End() - { - GraphicsContext.Assert(); - - legacy_mode = false; - - GL.MatrixMode(MatrixMode.Modelview); - GL.PopMatrix(); - - GL.MatrixMode(MatrixMode.Projection); - GL.PopMatrix(); + Cache.Clear(); } #endregion @@ -220,5 +189,18 @@ namespace OpenTK.Graphics.Text } #endregion + + #region IDisposable Members + + public void Dispose() + { + if (!disposed) + { + Cache.Dispose(); + disposed = true; + } + } + + #endregion } } diff --git a/Source/Utilities/Graphics/Text/GdiPlusGlyphRasterizer .cs b/Source/Utilities/Graphics/Text/GdiPlusGlyphRasterizer .cs index 19d31bc8..80f16652 100644 --- a/Source/Utilities/Graphics/Text/GdiPlusGlyphRasterizer .cs +++ b/Source/Utilities/Graphics/Text/GdiPlusGlyphRasterizer .cs @@ -35,7 +35,7 @@ using OpenTK.Platform; namespace OpenTK.Graphics.Text { - class GdiPlusGlyphRasterizer : IGlyphRasterizer + sealed class GdiPlusGlyphRasterizer : IGlyphRasterizer { #region Fields @@ -129,6 +129,11 @@ namespace OpenTK.Graphics.Text #endregion + public void Clear() + { + block_cache.Clear(); + } + #endregion #region Private Members diff --git a/Source/Utilities/Graphics/Text/GlyphCache.cs b/Source/Utilities/Graphics/Text/GlyphCache.cs index 090f9a9f..9497d27c 100644 --- a/Source/Utilities/Graphics/Text/GlyphCache.cs +++ b/Source/Utilities/Graphics/Text/GlyphCache.cs @@ -41,10 +41,14 @@ namespace OpenTK.Graphics.Text public abstract CachedGlyphInfo this[Glyph glyph] { get; } + public abstract void Clear(); + + public abstract void Dispose(); + #endregion } - class GlyphCache : GlyphCache where T : Texture2D + sealed class GlyphCache : GlyphCache where T : Texture2D { #region Fields @@ -53,6 +57,8 @@ namespace OpenTK.Graphics.Text Dictionary cached_glyphs = new Dictionary(); + bool disposed; + const int SheetWidth = 512, SheetHeight = 512; #endregion @@ -106,6 +112,13 @@ namespace OpenTK.Graphics.Text return cached_glyphs[glyph]; } } + public override void Clear() + { + for (int i = 0; i < sheets.Count; i++) + sheets[i].Dispose(); + + sheets.Clear(); + } #endregion @@ -125,5 +138,18 @@ namespace OpenTK.Graphics.Text } #endregion + + #region IDisposable Members + + public override void Dispose() + { + if (!disposed) + { + Clear(); + disposed = true; + } + } + + #endregion } } diff --git a/Source/Utilities/Graphics/Text/GlyphSheet.cs b/Source/Utilities/Graphics/Text/GlyphSheet.cs index 655638c4..bc1cce1b 100644 --- a/Source/Utilities/Graphics/Text/GlyphSheet.cs +++ b/Source/Utilities/Graphics/Text/GlyphSheet.cs @@ -32,13 +32,15 @@ using System.Drawing; namespace OpenTK.Graphics.Text { - class GlyphSheet where T : Texture2D + class GlyphSheet : IDisposable where T : Texture2D { #region Fields readonly T texture; readonly GlyphPacker packer; + bool disposed; + #endregion #region Constructors @@ -46,7 +48,9 @@ namespace OpenTK.Graphics.Text public GlyphSheet(int width, int height) { texture = (T)typeof(T).GetConstructor(new Type[] { typeof(int), typeof(int) }).Invoke(new object[] { width, height }); - packer = new GlyphPacker(width, height); + //texture.MagnificationFilter = TextureMagFilter.Nearest; + //texture.MinificationFilter = TextureMinFilter.Nearest; + packer = new GlyphPacker(width, height); } #endregion @@ -64,5 +68,18 @@ namespace OpenTK.Graphics.Text } #endregion + + #region IDisposable Members + + public void Dispose() + { + if (!disposed) + { + texture.Dispose(); + disposed = true; + } + } + + #endregion } } diff --git a/Source/Utilities/Graphics/Text/IGlyphCache.cs b/Source/Utilities/Graphics/Text/IGlyphCache.cs index d18a7dd7..fe5a60f6 100644 --- a/Source/Utilities/Graphics/Text/IGlyphCache.cs +++ b/Source/Utilities/Graphics/Text/IGlyphCache.cs @@ -25,12 +25,15 @@ // #endregion +using System; + namespace OpenTK.Graphics.Text { - interface IGlyphCache + interface IGlyphCache : IDisposable { void Add(Glyph glyph, IGlyphRasterizer rasterizer, TextQuality quality); bool Contains(Glyph glyph); CachedGlyphInfo this[Glyph glyph] { get; } + void Clear(); } } diff --git a/Source/Utilities/Graphics/Text/IGlyphRasterizer.cs b/Source/Utilities/Graphics/Text/IGlyphRasterizer.cs index e5d60594..54e311e7 100644 --- a/Source/Utilities/Graphics/Text/IGlyphRasterizer.cs +++ b/Source/Utilities/Graphics/Text/IGlyphRasterizer.cs @@ -40,5 +40,6 @@ namespace OpenTK.Graphics.Text Bitmap Rasterize(Glyph glyph, TextQuality quality); TextExtents MeasureText(TextBlock block); TextExtents MeasureText(TextBlock block, TextQuality quality); + void Clear(); } } diff --git a/Source/Utilities/Graphics/Text/ITextOutputProvider.cs b/Source/Utilities/Graphics/Text/ITextOutputProvider.cs index fe0902ef..ce1aaf93 100644 --- a/Source/Utilities/Graphics/Text/ITextOutputProvider.cs +++ b/Source/Utilities/Graphics/Text/ITextOutputProvider.cs @@ -32,10 +32,9 @@ using System.Drawing; namespace OpenTK.Graphics.Text { - interface ITextOutputProvider + interface ITextOutputProvider : IDisposable { void Print(TextBlock block, Color color, IGlyphRasterizer rasterizer); - void Begin(); - void End(); + void Clear(); } } diff --git a/Source/Utilities/Graphics/TextPrinter.cs b/Source/Utilities/Graphics/TextPrinter.cs index c285ec0e..156d954b 100644 --- a/Source/Utilities/Graphics/TextPrinter.cs +++ b/Source/Utilities/Graphics/TextPrinter.cs @@ -34,6 +34,8 @@ namespace OpenTK.Graphics ITextOutputProvider text_output; TextQuality text_quality; + bool disposed; + #endregion #region Constructors @@ -58,32 +60,6 @@ namespace OpenTK.Graphics #region ITextPrinter Members - #region public void Begin() - - /// - /// Sets up OpenGL state for drawing text. - /// - [Obsolete] - public void Begin() - { - TextOutput.Begin(); - } - - #endregion - - #region public void End() - - /// - /// Restores OpenGL state. - /// - [Obsolete] - public void End() - { - TextOutput.End(); - } - - #endregion - #region Print public void Print(string text, Font font, Color color) @@ -98,10 +74,13 @@ namespace OpenTK.Graphics public void Print(string text, Font font, Color color, SizeF size, TextPrinterOptions options) { + if (disposed) + throw new ObjectDisposedException(this.GetType().ToString()); + if (!ValidateParameters(text, font, size)) return; - text_output.Print(new TextBlock(text, font, options, size), color, Rasterizer); + TextOutput.Print(new TextBlock(text, font, options, size), color, Rasterizer); } #endregion @@ -120,6 +99,9 @@ namespace OpenTK.Graphics public TextExtents Measure(string text, Font font, SizeF size, TextPrinterOptions options) { + if (disposed) + throw new ObjectDisposedException(this.GetType().ToString()); + if (!ValidateParameters(text, font, size)) return TextExtents.Empty; @@ -132,14 +114,64 @@ namespace OpenTK.Graphics public void Clear() { - //glyph_cache.Clear(); - throw new NotImplementedException(); + if (disposed) + throw new ObjectDisposedException(this.GetType().ToString()); + + TextOutput.Clear(); + Rasterizer.Clear(); } #endregion #region Obsolete + /// + /// Sets up OpenGL state for drawing text. + /// + [Obsolete] + public void Begin() + { + if (disposed) + throw new ObjectDisposedException(this.GetType().ToString()); + + GraphicsContext.Assert(); + + float[] viewport = new float[4]; + + GL.GetFloat(GetPName.Viewport, viewport); + + // Prepare to draw text. We want pixel perfect precision, so we setup a 2D mode, + // with size equal to the window (in pixels). + // While we could also render text in 3D mode, it would be very hard to get + // pixel-perfect precision. + GL.MatrixMode(MatrixMode.Projection); + GL.PushMatrix(); + GL.LoadIdentity(); + GL.Ortho(viewport[0], viewport[2], viewport[3], viewport[1], -1.0, 1.0); + + GL.MatrixMode(MatrixMode.Modelview); + GL.PushMatrix(); + GL.LoadIdentity(); + } + + /// + /// Restores OpenGL state. + /// + [Obsolete] + public void End() + { + if (disposed) + throw new ObjectDisposedException(this.GetType().ToString()); + + GraphicsContext.Assert(); + + GL.MatrixMode(MatrixMode.Modelview); + GL.PopMatrix(); + + GL.MatrixMode(MatrixMode.Projection); + GL.PopMatrix(); + } + [Obsolete("Use TextPrinter.Print instead")] public void Draw(TextHandle handle) { @@ -204,5 +236,18 @@ namespace OpenTK.Graphics } #endregion + + #region IDisposable Members + + public void Dispose() + { + if (!disposed) + { + TextOutput.Dispose(); + disposed = true; + } + } + + #endregion } }