Added Clear() method to all classes that contain caches.

Implemented IDisposable interface on the TextPrinter and all relevant classes.
This commit is contained in:
the_fiddler 2009-02-12 17:41:09 +00:00
parent e7e5e1453f
commit 64dfa91678
9 changed files with 161 additions and 83 deletions

View file

@ -15,7 +15,7 @@ namespace OpenTK.Graphics
/// <summary>
/// Defines the interface for a TextPrinter.
/// </summary>
public interface ITextPrinter
public interface ITextPrinter : IDisposable
{
void Begin();
void End();

View file

@ -39,8 +39,8 @@ namespace OpenTK.Graphics.Text
// Triangle lists, sorted by texture.
Dictionary<Texture2D, List<Vector2>> active_lists = new Dictionary<Texture2D, List<Vector2>>();
Queue<List<Vector2>> inactive_lists = new Queue<List<Vector2>>();
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
}
}

View file

@ -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

View file

@ -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<T> : GlyphCache where T : Texture2D
sealed class GlyphCache<T> : GlyphCache where T : Texture2D
{
#region Fields
@ -53,6 +57,8 @@ namespace OpenTK.Graphics.Text
Dictionary<Glyph, CachedGlyphInfo> cached_glyphs = new Dictionary<Glyph, CachedGlyphInfo>();
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
}
}

View file

@ -32,13 +32,15 @@ using System.Drawing;
namespace OpenTK.Graphics.Text
{
class GlyphSheet<T> where T : Texture2D
class GlyphSheet<T> : 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
}
}

View file

@ -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();
}
}

View file

@ -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();
}
}

View file

@ -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();
}
}

View file

@ -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()
/// <summary>
/// Sets up OpenGL state for drawing text.
/// </summary>
[Obsolete]
public void Begin()
{
TextOutput.Begin();
}
#endregion
#region public void End()
/// <summary>
/// Restores OpenGL state.
/// </summary>
[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
/// <summary>
/// Sets up OpenGL state for drawing text.
/// </summary>
[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();
}
/// <summary>
/// Restores OpenGL state.
/// </summary>
[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
}
}