Moved Begin/End to ITextOutputProvider.

Fixed text location.
This commit is contained in:
the_fiddler 2008-11-29 17:45:43 +00:00
parent 3dd42ada72
commit 2abc0461b0
4 changed files with 73 additions and 63 deletions

View file

@ -25,11 +25,8 @@
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using OpenTK.Math;
namespace OpenTK.Graphics.Text
@ -41,6 +38,7 @@ 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];
#endregion
@ -55,27 +53,12 @@ namespace OpenTK.Graphics.Text
#region ITextOutputProvider Members
#region Print
public void Print(TextBlock block, PointF location, Color color, IGlyphRasterizer rasterizer, GlyphCache cache)
{
using (TextExtents extents = rasterizer.MeasureText(block, location))
{
//GL.BindTexture(TextureTarget.Texture2D, 2);
//GL.Begin(BeginMode.Quads);
//GL.TexCoord2(0, 0);
//GL.Vertex2(0, 0);
//GL.TexCoord2(1, 0);
//GL.Vertex2(256, 0);
//GL.TexCoord2(1, 1);
//GL.Vertex2(256, 256);
//GL.TexCoord2(0, 1);
//GL.Vertex2(0, 256);
//GL.End();
//GL.Translate(0, 256, 0);
// Build layout
int current = 0;
foreach (Glyph glyph in block)
@ -124,6 +107,9 @@ namespace OpenTK.Graphics.Text
List<Vector2> list = active_lists[key];
key.Bind();
GL.Translate(location.X, location.Y, 0);
GL.Begin(BeginMode.Triangles);
for (int i = 0; i < list.Count; i += 2)
@ -146,5 +132,61 @@ namespace OpenTK.Graphics.Text
}
#endregion
#region Begin
public void Begin()
{
if (GraphicsContext.CurrentContext == null)
throw new GraphicsContextException("No GraphicsContext is current in the calling thread.");
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();
GL.PushAttrib(AttribMask.TextureBit | AttribMask.EnableBit | AttribMask.ColorBufferBit);
//GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)TextureEnvMode.Modulate);
//GL.Enable(EnableCap.ColorMaterial);
GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.Blend);
//GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); // For grayscale
GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcColor); // For subpixel
//GL.BlendFunc(BlendingFactorSrc.ConstantColorExt, BlendingFactorDest.OneMinusSrcColor); // For subpixel with color
GL.Disable(EnableCap.DepthTest);
}
#endregion
#region End
public void End()
{
GL.PopAttrib();
GL.MatrixMode(MatrixMode.Modelview);
GL.PopMatrix();
GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix();
}
#endregion
#endregion
}
}

View file

@ -136,7 +136,7 @@ namespace OpenTK.Graphics.Text
return block_cache[block];
// If this block is not cached, we have to measure it and (potentially) place it in the cache.
TextExtents extents = MeasureTextExtents(block, location);
TextExtents extents = MeasureTextExtents(block);
if ((block.Options & TextPrinterOptions.NoCache) == 0)
block_cache.Add(block, extents);
@ -185,7 +185,7 @@ namespace OpenTK.Graphics.Text
#region MeasureTextExtents
TextExtents MeasureTextExtents(TextBlock block, PointF location)
TextExtents MeasureTextExtents(TextBlock block)
{
// Todo: Parse layout options:
//StringFormat format = default_string_format;
@ -193,7 +193,7 @@ namespace OpenTK.Graphics.Text
TextExtents extents = text_extents_pool.Acquire();
RectangleF rect = new RectangleF(location, block.Bounds);
RectangleF rect = new RectangleF(PointF.Empty, block.Bounds);
// Work around Mono/GDI+ bug, which causes incorrect
// text wraping when block.Bounds == SizeF.Empty.
if (block.Bounds == SizeF.Empty)

View file

@ -35,5 +35,7 @@ namespace OpenTK.Graphics.Text
interface ITextOutputProvider
{
void Print(TextBlock block, PointF location, Color color, IGlyphRasterizer rasterizer, GlyphCache cache);
void Begin();
void End();
}
}

View file

@ -34,8 +34,6 @@ namespace OpenTK.Graphics
IGlyphRasterizer glyph_rasterizer;
ITextOutputProvider text_output;
float[] viewport = new float[4];
#endregion
#region Constructors
@ -70,39 +68,10 @@ namespace OpenTK.Graphics
/// <summary>
/// Sets up OpenGL state for drawing text.
/// </summary>
[Obsolete]
public void Begin()
{
if (GraphicsContext.CurrentContext == null)
throw new GraphicsContextException("No GraphicsContext is current in the calling thread.");
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();
GL.PushAttrib(AttribMask.TextureBit | AttribMask.EnableBit | AttribMask.ColorBufferBit);
//GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)TextureEnvMode.Modulate);
//GL.Enable(EnableCap.ColorMaterial);
GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.Blend);
//GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); // For grayscale
GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcColor); // For subpixel
//GL.BlendFunc(BlendingFactorSrc.ConstantColorExt, BlendingFactorDest.OneMinusSrcColor); // For subpixel with color
GL.Disable(EnableCap.DepthTest);
text_output.Begin();
}
#endregion
@ -112,15 +81,10 @@ namespace OpenTK.Graphics
/// <summary>
/// Restores OpenGL state.
/// </summary>
[Obsolete]
public void End()
{
GL.PopAttrib();
GL.MatrixMode(MatrixMode.Modelview);
GL.PopMatrix();
GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix();
text_output.End();
}
#endregion
@ -145,7 +109,9 @@ namespace OpenTK.Graphics
if (font == null)
throw new ArgumentNullException("font");
text_output.Begin();
text_output.Print(new TextBlock(text, font, options, layoutRectangle.Size), layoutRectangle.Location, color, glyph_rasterizer, glyph_cache);
text_output.End();
}
#endregion