using System; using System.Drawing; using OpenTK; using OpenTK.Graphics; using System.Collections.Generic; namespace Example { public class TKFontTest : GameWindow { List textureFontList = new List(); TextHandle textHandle; ITextPrinter textPrinter = new TextPrinter(); string[] text = new string[] { "ABCD abcd To whom it may concern", "Triangles", }; public TKFontTest() : base(new DisplayMode(400, 600), "TKFontTest") { for (int i = 6; i < 10; i++) { textureFontList.Add(new TextureFont(new Font(FontFamily.GenericSansSerif, i))); } } public override void OnLoad(EventArgs e) { GL.ClearColor(Color.Black); } public override void OnUnload(EventArgs e) { textHandle.Dispose(); foreach (TextureFont textureFont in textureFontList) { textureFont.Dispose(); } } float[] viewport = new float[6]; protected override void OnResize(OpenTK.Platform.ResizeEventArgs e) { GL.Viewport(0, 0, Width, Height); GL.GetFloat(GetPName.Viewport, viewport); GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.Ortho(viewport[0], viewport[2], viewport[3], viewport[1], 0.0, 1.0); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); } public override void OnRenderFrame(RenderFrameEventArgs e) { GL.Clear(ClearBufferMask.ColorBufferBit); GL.LoadIdentity(); textPrinter.Begin(); GL.Translate(5, 5, 0); foreach (string str in text) { foreach (TextureFont textureFont in textureFontList) { textPrinter.Prepare(str, textureFont, out textHandle); RectangleF measureTextRectangle = textureFont.MeasureText(str); GL.Color3(0.25f, 0.25f, 0.25f); GL.Disable(EnableCap.Texture2D); GL.Rect(0, 0, measureTextRectangle.Width, measureTextRectangle.Height); GL.Enable(EnableCap.Texture2D); GL.Color3(Color.White); GL.PushMatrix(); //GL.Translate(-measureTextRectangle.X, -measureTextRectangle.Y, 0.0f); textPrinter.Draw(textHandle); GL.PopMatrix(); GL.Translate(0, measureTextRectangle.Height + 5, 0); } } textPrinter.End(); SwapBuffers(); } [STAThread] public static void Main() { try { using (TKFontTest tkFontTest = new TKFontTest()) { tkFontTest.Run(30.0, 0.0); } } catch{} } } }