From 40b074179ee80045219bf067ec1bf26b984f9f67 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Sun, 13 Apr 2008 18:29:36 +0000 Subject: [PATCH] Fixed dynamic text rendering when no text is precached at all. --- Source/Utilities/Fonts/TextPrinter.cs | 63 +++++++++++++++------------ 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/Source/Utilities/Fonts/TextPrinter.cs b/Source/Utilities/Fonts/TextPrinter.cs index 83f2b1d5..9809e246 100644 --- a/Source/Utilities/Fonts/TextPrinter.cs +++ b/Source/Utilities/Fonts/TextPrinter.cs @@ -27,46 +27,52 @@ namespace OpenTK.Graphics { //static Regex break_point = new Regex("[ .,/*-+?\\!=]", RegexOptions.Compiled | RegexOptions.IgnoreCase); //static char[] split_chars = new char[] { ' ', '\n', '\t', ',', '.', '/', '?', '!', ';', '\\', '-', '+', '*', '=' }; - static bool functionality_checked = false; static ITextPrinterImplementation printer; float[] viewport = new float[4]; - // 8 chars by default - Vector2[] vertices = new Vector2[8 * 8]; // Interleaved, vertex, texcoord, vertex, etc... + Vector2[] vertices = new Vector2[8 * 8]; // Interleaved, vertex, texcoord, vertex, etc... Starts with 8 chars, will expand as needed. ushort[] indices = new ushort[6 * 8]; #region --- Constructor --- /// - /// Constructs a new DefaultLayoutProvider object. + /// Constructs a new TextPrinter object. /// public TextPrinter() { } #endregion - #region static void CheckNeededFunctionality() + #region --- Private Members --- + + #region static ITextPrinterImplementation Printer /// /// Checks the machine's capabilities and selects the fastest method to print text. /// - static void CheckNeededFunctionality() + static ITextPrinterImplementation Printer { - printer = (ITextPrinterImplementation)new DisplayListTextPrinter(); - /* - GL.SupportsExtension("VERSION_1_5") ? - (ITextPrinterImplementation)new VboTextPrinter() : - GL.SupportsExtension("ARB_vertex_buffer_object") ? null : - GL.SupportsExtension("VERSION_1_1") ? null : null; - */ - if (printer == null) - throw new NotSupportedException("TextPrinter requires at least OpenGL 1.1 support."); - - functionality_checked = true; + get + { + if (printer == null) + { - Debug.Print("Using {0} for font printing.", printer); + printer = (ITextPrinterImplementation)new DisplayListTextPrinter(); + //GL.SupportsExtension("VERSION_1_5") ? + //(ITextPrinterImplementation)new VboTextPrinter() : + //GL.SupportsExtension("ARB_vertex_buffer_object") ? null : + //GL.SupportsExtension("VERSION_1_1") ? null : null; + if (printer == null) + throw new NotSupportedException("TextPrinter requires at least OpenGL 1.1 support."); + + Debug.Print("Using {0} for font printing.", printer); + } + return printer; + } } #endregion + #endregion + #region --- ITextPrinter Members --- #region public void Prepare(string text, TextureFont font, out TextHandle handle) @@ -138,14 +144,11 @@ namespace OpenTK.Graphics /// Occurs when OpenGL 1.1 is not supported. public void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp, StringAlignment alignment, bool rightToLeft) { - if (!functionality_checked) - CheckNeededFunctionality(); - int num_indices; PerformLayout(text, font, width, wordWarp, alignment, rightToLeft, ref vertices, ref indices, out num_indices); - handle = printer.Load(vertices, indices, num_indices); + handle = Printer.Load(vertices, indices, num_indices); handle.font = font; } @@ -261,8 +264,8 @@ namespace OpenTK.Graphics public void Draw(TextHandle handle) { GL.BindTexture(TextureTarget.Texture2D, handle.font.Texture); - - printer.Draw(handle); + + Printer.Draw(handle); } #endregion @@ -278,7 +281,10 @@ namespace OpenTK.Graphics { int num_indices; PerformLayout(text, font, 0, false, StringAlignment.Near, false, ref vertices, ref indices, out num_indices); - printer.Draw(vertices, indices, num_indices); + + GL.BindTexture(TextureTarget.Texture2D, font.Texture); + + Printer.Draw(vertices, indices, num_indices); } #endregion @@ -290,6 +296,9 @@ namespace OpenTK.Graphics /// 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, @@ -305,9 +314,7 @@ namespace OpenTK.Graphics GL.PushMatrix(); GL.LoadIdentity(); - GL.PushAttrib(AttribMask.TextureBit); - GL.PushAttrib(AttribMask.EnableBit); - GL.PushAttrib(AttribMask.ColorBufferBit); + GL.PushAttrib(AttribMask.TextureBit | AttribMask.EnableBit | AttribMask.ColorBufferBit); GL.Enable(EnableCap.Texture2D); GL.Enable(EnableCap.Blend);