Updated examples to use the new TextPrinter.

This commit is contained in:
the_fiddler 2008-11-25 17:25:51 +00:00
parent 5d1eda9289
commit e1beaf3744
5 changed files with 361 additions and 365 deletions

View file

@ -1,298 +1,299 @@
#region --- License --- #region --- License ---
/* Licensed under the MIT/X11 license. /* Licensed under the MIT/X11 license.
* Copyright (c) 2006-2008 the OpenTK Team. * Copyright (c) 2006-2008 the OpenTK Team.
* This notice may not be removed from any source distribution. * This notice may not be removed from any source distribution.
* See license.txt for licensing details. * See license.txt for licensing details.
*/ */
#endregion #endregion
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing;
using OpenTK;
using OpenTK.Input; using OpenTK;
using OpenTK.Math; using OpenTK.Input;
using OpenTK.Graphics; using OpenTK.Math;
using OpenTK.Graphics.OpenGL; using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
namespace Examples.Tutorial
{ namespace Examples.Tutorial
[Example("Simple FrameBuffer Object.", ExampleCategory.OpenGL)] {
public class SimpleFBO : GameWindow [Example("Simple FrameBuffer Object.", ExampleCategory.OpenGL)]
{ public class SimpleFBO : GameWindow
public SimpleFBO() {
: base(800, 600) public SimpleFBO()
{ : base(800, 600)
} {
}
TextureFont sans = new TextureFont(new System.Drawing.Font(System.Drawing.FontFamily.GenericSansSerif, 14.0f));
ITextPrinter text = new TextPrinter(); Font sans = new Font(System.Drawing.FontFamily.GenericSansSerif, 16.0f);
ITextPrinter text = new TextPrinter();
uint ColorTexture;
uint DepthTexture; uint ColorTexture;
uint FBOHandle; uint DepthTexture;
uint FBOHandle;
const int TextureSize = 512;
const int TextureSize = 512;
#region Randoms
#region Randoms
Random rnd = new Random();
public const float scale = 3f; Random rnd = new Random();
public const float scale = 3f;
/// <summary>Returns a random Float in the range [-0.5*scale..+0.5*scale]</summary>
public float GetRandom() /// <summary>Returns a random Float in the range [-0.5*scale..+0.5*scale]</summary>
{ public float GetRandom()
return (float)(rnd.NextDouble() - 0.5) * scale; {
} return (float)(rnd.NextDouble() - 0.5) * scale;
}
/// <summary>Returns a random Float in the range [0..1]</summary>
public float GetRandom0to1() /// <summary>Returns a random Float in the range [0..1]</summary>
{ public float GetRandom0to1()
return (float)rnd.NextDouble(); {
} return (float)rnd.NextDouble();
}
#endregion Randoms
#endregion Randoms
public override void OnLoad(EventArgs e)
{ public override void OnLoad(EventArgs e)
GL.Enable(EnableCap.DepthTest); {
GL.ClearDepth(1.0f); GL.Enable(EnableCap.DepthTest);
GL.DepthFunc(DepthFunction.Lequal); GL.ClearDepth(1.0f);
GL.DepthFunc(DepthFunction.Lequal);
GL.Disable(EnableCap.CullFace);
GL.PolygonMode(MaterialFace.Back, PolygonMode.Line); GL.Disable(EnableCap.CullFace);
GL.PolygonMode(MaterialFace.Back, PolygonMode.Line);
// Create Color Tex
GL.GenTextures(1, out ColorTexture); // Create Color Tex
GL.BindTexture(TextureTarget.Texture2D, ColorTexture); GL.GenTextures(1, out ColorTexture);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, TextureSize, TextureSize, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); GL.BindTexture(TextureTarget.Texture2D, ColorTexture);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, TextureSize, TextureSize, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder);
// GL.Ext.GenerateMipmap( GenerateMipmapTarget.Texture2D ); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder);
// GL.Ext.GenerateMipmap( GenerateMipmapTarget.Texture2D );
// Create Depth Tex
GL.GenTextures(1, out DepthTexture); // Create Depth Tex
GL.BindTexture(TextureTarget.Texture2D, DepthTexture); GL.GenTextures(1, out DepthTexture);
GL.TexImage2D(TextureTarget.Texture2D, 0, (PixelInternalFormat)All.DepthComponent32, TextureSize, TextureSize, 0, PixelFormat.DepthComponent, PixelType.UnsignedInt, IntPtr.Zero); GL.BindTexture(TextureTarget.Texture2D, DepthTexture);
// things go horribly wrong if DepthComponent's Bitcount does not match the main Framebuffer's Depth GL.TexImage2D(TextureTarget.Texture2D, 0, (PixelInternalFormat)All.DepthComponent32, TextureSize, TextureSize, 0, PixelFormat.DepthComponent, PixelType.UnsignedInt, IntPtr.Zero);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); // things go horribly wrong if DepthComponent's Bitcount does not match the main Framebuffer's Depth
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder);
// GL.Ext.GenerateMipmap( GenerateMipmapTarget.Texture2D ); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder);
// GL.Ext.GenerateMipmap( GenerateMipmapTarget.Texture2D );
// Create a FBO and attach the textures
GL.Ext.GenFramebuffers(1, out FBOHandle); // Create a FBO and attach the textures
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, FBOHandle); GL.Ext.GenFramebuffers(1, out FBOHandle);
GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, ColorTexture, 0); GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, FBOHandle);
GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, TextureTarget.Texture2D, DepthTexture, 0); GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, ColorTexture, 0);
GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, TextureTarget.Texture2D, DepthTexture, 0);
#region Test for Error
#region Test for Error
switch (GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt))
{ switch (GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt))
case FramebufferErrorCode.FramebufferCompleteExt: {
{ case FramebufferErrorCode.FramebufferCompleteExt:
Console.WriteLine("FBO: The framebuffer is complete and valid for rendering."); {
break; Console.WriteLine("FBO: The framebuffer is complete and valid for rendering.");
} break;
case FramebufferErrorCode.FramebufferIncompleteAttachmentExt: }
{ case FramebufferErrorCode.FramebufferIncompleteAttachmentExt:
Console.WriteLine("FBO: One or more attachment points are not framebuffer attachment complete. This could mean theres no texture attached or the format isnt renderable. For color textures this means the base format must be RGB or RGBA and for depth textures it must be a DEPTH_COMPONENT format. Other causes of this error are that the width or height is zero or the z-offset is out of range in case of render to volume."); {
break; Console.WriteLine("FBO: One or more attachment points are not framebuffer attachment complete. This could mean theres no texture attached or the format isnt renderable. For color textures this means the base format must be RGB or RGBA and for depth textures it must be a DEPTH_COMPONENT format. Other causes of this error are that the width or height is zero or the z-offset is out of range in case of render to volume.");
} break;
case FramebufferErrorCode.FramebufferIncompleteMissingAttachmentExt: }
{ case FramebufferErrorCode.FramebufferIncompleteMissingAttachmentExt:
Console.WriteLine("FBO: There are no attachments."); {
break; Console.WriteLine("FBO: There are no attachments.");
} break;
/* case FramebufferErrorCode.GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT: }
{ /* case FramebufferErrorCode.GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT:
Console.WriteLine("FBO: An object has been attached to more than one attachment point."); {
break; Console.WriteLine("FBO: An object has been attached to more than one attachment point.");
}*/ break;
case FramebufferErrorCode.FramebufferIncompleteDimensionsExt: }*/
{ case FramebufferErrorCode.FramebufferIncompleteDimensionsExt:
Console.WriteLine("FBO: Attachments are of different size. All attachments must have the same width and height."); {
break; Console.WriteLine("FBO: Attachments are of different size. All attachments must have the same width and height.");
} break;
case FramebufferErrorCode.FramebufferIncompleteFormatsExt: }
{ case FramebufferErrorCode.FramebufferIncompleteFormatsExt:
Console.WriteLine("FBO: The color attachments have different format. All color attachments must have the same format."); {
break; Console.WriteLine("FBO: The color attachments have different format. All color attachments must have the same format.");
} break;
case FramebufferErrorCode.FramebufferIncompleteDrawBufferExt: }
{ case FramebufferErrorCode.FramebufferIncompleteDrawBufferExt:
Console.WriteLine("FBO: An attachment point referenced by GL.DrawBuffers() doesnt have an attachment."); {
break; Console.WriteLine("FBO: An attachment point referenced by GL.DrawBuffers() doesnt have an attachment.");
} break;
case FramebufferErrorCode.FramebufferIncompleteReadBufferExt: }
{ case FramebufferErrorCode.FramebufferIncompleteReadBufferExt:
Console.WriteLine("FBO: The attachment point referenced by GL.ReadBuffers() doesnt have an attachment."); {
break; Console.WriteLine("FBO: The attachment point referenced by GL.ReadBuffers() doesnt have an attachment.");
} break;
case FramebufferErrorCode.FramebufferUnsupportedExt: }
{ case FramebufferErrorCode.FramebufferUnsupportedExt:
Console.WriteLine("FBO: This particular FBO configuration is not supported by the implementation."); {
break; Console.WriteLine("FBO: This particular FBO configuration is not supported by the implementation.");
} break;
default: }
{ default:
Console.WriteLine("FBO: Status unknown. (yes, this is really bad.)"); {
break; Console.WriteLine("FBO: Status unknown. (yes, this is really bad.)");
} break;
} }
}
// using FBO might have changed states, e.g. the FBO might not support stereoscopic views or double buffering
int[] queryinfo = new int[6]; // using FBO might have changed states, e.g. the FBO might not support stereoscopic views or double buffering
GL.GetInteger(GetPName.MaxColorAttachmentsExt, out queryinfo[0]); int[] queryinfo = new int[6];
GL.GetInteger(GetPName.AuxBuffers, out queryinfo[1]); GL.GetInteger(GetPName.MaxColorAttachmentsExt, out queryinfo[0]);
GL.GetInteger(GetPName.MaxDrawBuffers, out queryinfo[2]); GL.GetInteger(GetPName.AuxBuffers, out queryinfo[1]);
GL.GetInteger(GetPName.Stereo, out queryinfo[3]); GL.GetInteger(GetPName.MaxDrawBuffers, out queryinfo[2]);
GL.GetInteger(GetPName.Samples, out queryinfo[4]); GL.GetInteger(GetPName.Stereo, out queryinfo[3]);
GL.GetInteger(GetPName.Doublebuffer, out queryinfo[5]); GL.GetInteger(GetPName.Samples, out queryinfo[4]);
Console.WriteLine("max. ColorBuffers: " + queryinfo[0] + " max. AuxBuffers: " + queryinfo[1] + " max. DrawBuffers: " + queryinfo[2] + GL.GetInteger(GetPName.Doublebuffer, out queryinfo[5]);
"\nStereo: " + queryinfo[3] + " Samples: " + queryinfo[4] + " DoubleBuffer: " + queryinfo[5]); Console.WriteLine("max. ColorBuffers: " + queryinfo[0] + " max. AuxBuffers: " + queryinfo[1] + " max. DrawBuffers: " + queryinfo[2] +
"\nStereo: " + queryinfo[3] + " Samples: " + queryinfo[4] + " DoubleBuffer: " + queryinfo[5]);
Console.WriteLine("Last GL Error: " + GL.GetError());
Console.WriteLine("Last GL Error: " + GL.GetError());
#endregion Test for Error
#endregion Test for Error
GL.PushAttrib(AttribMask.ViewportBit);
{ GL.PushAttrib(AttribMask.ViewportBit);
GL.Viewport(0, 0, TextureSize, TextureSize); {
GL.Viewport(0, 0, TextureSize, TextureSize);
// clear the screen in red, to make it very obvious what the clear affected. only the FBO, not the real framebuffer
GL.ClearColor(1f, 0f, 0f, 0f); // clear the screen in red, to make it very obvious what the clear affected. only the FBO, not the real framebuffer
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.ClearColor(1f, 0f, 0f, 0f);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
// smack 50 random triangles into the FBO's textures
GL.Begin(BeginMode.Triangles); // smack 50 random triangles into the FBO's textures
{ GL.Begin(BeginMode.Triangles);
for (int i = 0; i < 50; i++) {
{ for (int i = 0; i < 50; i++)
GL.Color3(GetRandom0to1(), GetRandom0to1(), GetRandom0to1()); {
GL.Vertex3(GetRandom(), GetRandom(), GetRandom()); GL.Color3(GetRandom0to1(), GetRandom0to1(), GetRandom0to1());
GL.Color3(GetRandom0to1(), GetRandom0to1(), GetRandom0to1()); GL.Vertex3(GetRandom(), GetRandom(), GetRandom());
GL.Vertex3(GetRandom(), GetRandom(), GetRandom()); GL.Color3(GetRandom0to1(), GetRandom0to1(), GetRandom0to1());
GL.Color3(GetRandom0to1(), GetRandom0to1(), GetRandom0to1()); GL.Vertex3(GetRandom(), GetRandom(), GetRandom());
GL.Vertex3(GetRandom(), GetRandom(), GetRandom()); GL.Color3(GetRandom0to1(), GetRandom0to1(), GetRandom0to1());
} GL.Vertex3(GetRandom(), GetRandom(), GetRandom());
} }
GL.End(); }
} GL.End();
GL.PopAttrib(); }
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); // disable rendering into the FBO GL.PopAttrib();
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); // disable rendering into the FBO
GL.ClearColor(.1f, .2f, .3f, 0f);
GL.Color3(1f, 1f, 1f); GL.ClearColor(.1f, .2f, .3f, 0f);
GL.Color3(1f, 1f, 1f);
GL.Enable(EnableCap.Texture2D); // enable Texture Mapping
GL.BindTexture(TextureTarget.Texture2D, 0); // bind default texture GL.Enable(EnableCap.Texture2D); // enable Texture Mapping
} GL.BindTexture(TextureTarget.Texture2D, 0); // bind default texture
}
public override void OnUnload(EventArgs e)
{ public override void OnUnload(EventArgs e)
// Clean up what we allocated before exiting {
GL.DeleteTextures(1, ref ColorTexture); // Clean up what we allocated before exiting
GL.DeleteTextures(1, ref DepthTexture); GL.DeleteTextures(1, ref ColorTexture);
GL.Ext.DeleteFramebuffers(1, ref FBOHandle); GL.DeleteTextures(1, ref DepthTexture);
GL.Ext.DeleteFramebuffers(1, ref FBOHandle);
base.OnUnload(e);
} base.OnUnload(e);
}
protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
{ protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
GL.Viewport(0, 0, Width, Height); {
GL.MatrixMode(MatrixMode.Projection); GL.Viewport(0, 0, Width, Height);
GL.LoadIdentity(); GL.MatrixMode(MatrixMode.Projection);
Glu.Perspective(60.0, Width / (double)Height, 1.0, 5.0); GL.LoadIdentity();
Glu.Perspective(60.0, Width / (double)Height, 1.0, 5.0);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity(); GL.MatrixMode(MatrixMode.Modelview);
Glu.LookAt(0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); GL.LoadIdentity();
Glu.LookAt(0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
base.OnResize(e);
} base.OnResize(e);
}
public override void OnUpdateFrame(UpdateFrameEventArgs e)
{ public override void OnUpdateFrame(UpdateFrameEventArgs e)
base.OnUpdateFrame(e); {
base.OnUpdateFrame(e);
if (Keyboard[Key.Space])
{ if (Keyboard[Key.Space])
ErrorCode err = GL.GetError(); {
Console.WriteLine(err + " " + Glu.ErrorString((GluErrorCode)err)); ErrorCode err = GL.GetError();
} Console.WriteLine(err + " " + Glu.ErrorString((GluErrorCode)err));
}
if (Keyboard[Key.Escape])
this.Exit(); if (Keyboard[Key.Escape])
} this.Exit();
}
public override void OnRenderFrame(RenderFrameEventArgs e)
{ public override void OnRenderFrame(RenderFrameEventArgs e)
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); {
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
text.Begin(); text.Begin();
text.Draw((1.0 / e.Time).ToString("F2"), sans); text.Print((1.0 / e.Time).ToString("F2"), sans);
text.End(); text.End();
GL.PushMatrix(); GL.PushMatrix();
{ {
// Draw the Color Texture // Draw the Color Texture
GL.Translate(-1.1f, 0f, 0f); GL.Translate(-1.1f, 0f, 0f);
GL.BindTexture(TextureTarget.Texture2D, ColorTexture); GL.BindTexture(TextureTarget.Texture2D, ColorTexture);
GL.Begin(BeginMode.Quads); GL.Begin(BeginMode.Quads);
{ {
GL.TexCoord2(0f, 1f); GL.TexCoord2(0f, 1f);
GL.Vertex2(-1.0f, 1.0f); GL.Vertex2(-1.0f, 1.0f);
GL.TexCoord2(0.0f, 0.0f); GL.TexCoord2(0.0f, 0.0f);
GL.Vertex2(-1.0f, -1.0f); GL.Vertex2(-1.0f, -1.0f);
GL.TexCoord2(1.0f, 0.0f); GL.TexCoord2(1.0f, 0.0f);
GL.Vertex2(1.0f, -1.0f); GL.Vertex2(1.0f, -1.0f);
GL.TexCoord2(1.0f, 1.0f); GL.TexCoord2(1.0f, 1.0f);
GL.Vertex2(1.0f, 1.0f); GL.Vertex2(1.0f, 1.0f);
} }
GL.End(); GL.End();
// Draw the Depth Texture // Draw the Depth Texture
GL.Translate(+2.2f, 0f, 0f); GL.Translate(+2.2f, 0f, 0f);
GL.BindTexture(TextureTarget.Texture2D, DepthTexture); GL.BindTexture(TextureTarget.Texture2D, DepthTexture);
GL.Begin(BeginMode.Quads); GL.Begin(BeginMode.Quads);
{ {
GL.TexCoord2(0f, 1f); GL.TexCoord2(0f, 1f);
GL.Vertex2(-1.0f, 1.0f); GL.Vertex2(-1.0f, 1.0f);
GL.TexCoord2(0.0f, 0.0f); GL.TexCoord2(0.0f, 0.0f);
GL.Vertex2(-1.0f, -1.0f); GL.Vertex2(-1.0f, -1.0f);
GL.TexCoord2(1.0f, 0.0f); GL.TexCoord2(1.0f, 0.0f);
GL.Vertex2(1.0f, -1.0f); GL.Vertex2(1.0f, -1.0f);
GL.TexCoord2(1.0f, 1.0f); GL.TexCoord2(1.0f, 1.0f);
GL.Vertex2(1.0f, 1.0f); GL.Vertex2(1.0f, 1.0f);
} }
GL.End(); GL.End();
} }
GL.PopMatrix(); GL.PopMatrix();
this.SwapBuffers(); this.SwapBuffers();
} }
#region public static void Main() #region public static void Main()
/// <summary> /// <summary>
/// Entry point of this example. /// Entry point of this example.
/// </summary> /// </summary>
[STAThread] [STAThread]
public static void Main() public static void Main()
{ {
using (SimpleFBO example = new SimpleFBO()) using (SimpleFBO example = new SimpleFBO())
{ {
Utilities.SetWindowTitle(example); Utilities.SetWindowTitle(example);
example.Run(30.0, 0.0); example.Run(30.0, 0.0);
} }
} }
#endregion #endregion
} }
} }

View file

@ -55,7 +55,7 @@ namespace Examples.Tutorial
// Text drawing (for fps) // Text drawing (for fps)
TextPrinter printer = new TextPrinter(); TextPrinter printer = new TextPrinter();
TextureFont font = new TextureFont(new Font(FontFamily.GenericSansSerif, 14.0f)); Font font = new Font(FontFamily.GenericSansSerif, 16.0f);
#endregion private Fields #endregion private Fields
@ -290,7 +290,7 @@ namespace Examples.Tutorial
GL.UseProgram(0); GL.UseProgram(0);
printer.Begin(); printer.Begin();
GL.Color3(Color.PaleGoldenrod); GL.Color3(Color.PaleGoldenrod);
printer.Draw((1 / e.Time).ToString("F2"), font); printer.Print((1 / e.Time).ToString("F2"), font, TextPrinterOptions.NoCache);
printer.End(); printer.End();
SwapBuffers(); SwapBuffers();

View file

@ -20,7 +20,7 @@ namespace Examples.Tests
[Example("GameWindow states", ExampleCategory.Test)] [Example("GameWindow states", ExampleCategory.Test)]
public class GameWindowStates : GameWindow public class GameWindowStates : GameWindow
{ {
TextureFont font = new TextureFont(new Font(FontFamily.GenericSansSerif, 16.0f)); Font font = new Font(FontFamily.GenericSansSerif, 16.0f);
TextPrinter printer = new TextPrinter(); TextPrinter printer = new TextPrinter();
#region GetNext and GetPrevious methods for enums. #region GetNext and GetPrevious methods for enums.
@ -118,12 +118,12 @@ namespace Examples.Tests
printer.Begin(); printer.Begin();
printer.Draw("Instructions:", font); GL.Translate(0, font.Height, 0); printer.Print("Instructions:", font); GL.Translate(0, font.Height, 0);
printer.Draw(String.Format("1 - cycle through window styles (current: {0}).", this.WindowState), font); printer.Print(String.Format("1 - cycle through window styles (current: {0}).", this.WindowState), font);
GL.Translate(0, font.Height, 0); GL.Translate(0, font.Height, 0);
printer.Draw(String.Format("2 - cycle through window borders (current: {0}).", this.WindowBorder), font); printer.Print(String.Format("2 - cycle through window borders (current: {0}).", this.WindowBorder), font);
GL.Translate(0, font.Height, 0); GL.Translate(0, font.Height, 0);
printer.Draw(String.Format("3 - toggle fullscreen (current: {0}).", printer.Print(String.Format("3 - toggle fullscreen (current: {0}).",
this.WindowState == WindowState.Fullscreen ? "enabled" : "disabled"), font); this.WindowState == WindowState.Fullscreen ? "enabled" : "disabled"), font);

View file

@ -33,52 +33,50 @@ namespace Examples.Tutorial
ITextPrinter printer = new TextPrinter(); ITextPrinter printer = new TextPrinter();
const string text = "Hello, world!"; const string text = "Hello, world!";
TextHandle[] handles; // Used to cache the strings we want to print.
// Load some different TextureFont sizes to compare their quality. // Load some different TextureFont sizes to compare their quality.
// You'll never need to load that many fonts in your application, // You'll never need to load that many fonts in your application,
// 3 or 4 should be more than enough. // 3 or 4 should be more than enough.
TextureFont[] fonts = new TextureFont[] Font[] fonts = new Font[]
{ {
new TextureFont(new Font(FontFamily.GenericSerif, 8.0f)), new Font(FontFamily.GenericSerif, 8.0f),
new TextureFont(new Font(FontFamily.GenericSerif, 10.0f)), new Font(FontFamily.GenericSerif, 10.0f),
new TextureFont(new Font(FontFamily.GenericSerif, 12.0f)), new Font(FontFamily.GenericSerif, 12.0f),
new TextureFont(new Font(FontFamily.GenericSerif, 14.0f)), new Font(FontFamily.GenericSerif, 14.0f),
new TextureFont(new Font(FontFamily.GenericSerif, 16.0f)), new Font(FontFamily.GenericSerif, 16.0f),
new TextureFont(new Font(FontFamily.GenericSerif, 18.0f)), new Font(FontFamily.GenericSerif, 18.0f),
new TextureFont(new Font(FontFamily.GenericSerif, 20.0f)), new Font(FontFamily.GenericSerif, 20.0f),
new TextureFont(new Font(FontFamily.GenericSerif, 22.0f)), new Font(FontFamily.GenericSerif, 22.0f),
new TextureFont(new Font(FontFamily.GenericSerif, 24.0f, FontStyle.Bold)), new Font(FontFamily.GenericSerif, 24.0f, FontStyle.Bold),
new TextureFont(new Font(FontFamily.GenericSerif, 26.0f)), new Font(FontFamily.GenericSerif, 26.0f),
new TextureFont(new Font(FontFamily.GenericSerif, 28.0f)), new Font(FontFamily.GenericSerif, 28.0f),
new TextureFont(new Font(FontFamily.GenericSerif, 30.0f)), new Font(FontFamily.GenericSerif, 30.0f),
new TextureFont(new Font(FontFamily.GenericSerif, 32.0f, FontStyle.Italic)), new Font(FontFamily.GenericSerif, 32.0f, FontStyle.Italic),
new TextureFont(new Font(FontFamily.GenericSerif, 34.0f)), new Font(FontFamily.GenericSerif, 34.0f),
new TextureFont(new Font(FontFamily.GenericSerif, 36.0f)), new Font(FontFamily.GenericSerif, 36.0f),
new TextureFont(new Font(FontFamily.GenericSerif, 38.0f)), new Font(FontFamily.GenericSerif, 38.0f),
new TextureFont(new Font(FontFamily.GenericSansSerif, 8.0f)), new Font(FontFamily.GenericSansSerif, 8.0f),
new TextureFont(new Font(FontFamily.GenericSansSerif, 10.0f)), new Font(FontFamily.GenericSansSerif, 10.0f),
new TextureFont(new Font(FontFamily.GenericSansSerif, 12.0f)), new Font(FontFamily.GenericSansSerif, 12.0f),
new TextureFont(new Font(FontFamily.GenericSansSerif, 14.0f)), new Font(FontFamily.GenericSansSerif, 14.0f),
new TextureFont(new Font(FontFamily.GenericSansSerif, 16.0f)), new Font(FontFamily.GenericSansSerif, 16.0f),
new TextureFont(new Font(FontFamily.GenericSansSerif, 18.0f)), new Font(FontFamily.GenericSansSerif, 18.0f),
new TextureFont(new Font(FontFamily.GenericSansSerif, 20.0f)), new Font(FontFamily.GenericSansSerif, 20.0f),
new TextureFont(new Font(FontFamily.GenericSansSerif, 22.0f)), new Font(FontFamily.GenericSansSerif, 22.0f),
new TextureFont(new Font(FontFamily.GenericSansSerif, 24.0f, FontStyle.Bold)), new Font(FontFamily.GenericSansSerif, 24.0f, FontStyle.Bold),
new TextureFont(new Font(FontFamily.GenericSansSerif, 26.0f)), new Font(FontFamily.GenericSansSerif, 26.0f),
new TextureFont(new Font(FontFamily.GenericSansSerif, 28.0f)), new Font(FontFamily.GenericSansSerif, 28.0f),
new TextureFont(new Font(FontFamily.GenericSansSerif, 30.0f)), new Font(FontFamily.GenericSansSerif, 30.0f),
new TextureFont(new Font(FontFamily.GenericSansSerif, 32.0f, FontStyle.Italic)), new Font(FontFamily.GenericSansSerif, 32.0f, FontStyle.Italic),
new TextureFont(new Font(FontFamily.GenericSansSerif, 34.0f)), new Font(FontFamily.GenericSansSerif, 34.0f),
new TextureFont(new Font(FontFamily.GenericSansSerif, 36.0f)), new Font(FontFamily.GenericSansSerif, 36.0f),
new TextureFont(new Font(FontFamily.GenericSansSerif, 38.0f)), new Font(FontFamily.GenericSansSerif, 38.0f),
}; };
#endregion #endregion
@ -99,10 +97,6 @@ namespace Examples.Tutorial
public override void OnLoad(EventArgs e) public override void OnLoad(EventArgs e)
{ {
GL.ClearColor(Color.SteelBlue); GL.ClearColor(Color.SteelBlue);
handles = new TextHandle[fonts.Length];
for (int i = handles.Length; --i >= 0; )
printer.Prepare(text, fonts[i], out handles[i]);
} }
#endregion #endregion
@ -117,9 +111,7 @@ namespace Examples.Tutorial
/// <param name="e"></param> /// <param name="e"></param>
public override void OnUnload(EventArgs e) public override void OnUnload(EventArgs e)
{ {
foreach (TextHandle h in handles) foreach (Font f in fonts)
h.Dispose();
foreach (TextureFont f in fonts)
f.Dispose(); f.Dispose();
} }
@ -168,21 +160,21 @@ namespace Examples.Tutorial
printer.Begin(); printer.Begin();
// Print using the first font. // Print using the first font.
for (int i = 0; i < handles.Length / 2; i++) for (int i = 0; i < fonts.Length / 2; i++)
{ {
printer.Draw(handles[i]); printer.Print(text, fonts[i]);
GL.Translate(0, fonts[i].Height, 0); GL.Translate(0, fonts[i].Height, 0);
} }
// Move to the right, and print using the second font. // Move to the right, and print using the second font.
//float width, height; //float width, height;
//fonts[handles.Length / 2 - 1].MeasureString(text, out width, out height); //fonts[handles.Length / 2 - 1].MeasureString(text, out width, out height);
RectangleF rect = fonts[handles.Length / 2 - 1].MeasureText(text); RectangleF rect = printer.Measure(text, fonts[fonts.Length / 2 - 1]).BoundingBox;
GL.LoadIdentity(); GL.LoadIdentity();
GL.Translate(rect.Width + 32.0f, 0, 0); GL.Translate(rect.Width + 32.0f, 0, 0);
for (int i = handles.Length / 2; i < handles.Length; i++) for (int i = fonts.Length / 2; i < fonts.Length; i++)
{ {
printer.Draw(handles[i]); printer.Print(text, fonts[i]);
GL.Translate(0, fonts[i].Height, 0); GL.Translate(0, fonts[i].Height, 0);
} }

View file

@ -23,13 +23,12 @@ namespace Examples.Tutorial
[Example("Text", ExampleCategory.Tutorial, 4)] [Example("Text", ExampleCategory.Tutorial, 4)]
public class Text : GameWindow public class Text : GameWindow
{ {
Font serif2 = new Font(FontFamily.GenericSerif, 16.0f); Font serif = new Font(FontFamily.GenericSerif, 16.0f);
TextureFont serif = new TextureFont(new Font(FontFamily.GenericSerif, 12.0f)); Font sans = new Font(FontFamily.GenericSansSerif, 48.0f, FontStyle.Italic);
TextureFont sans = new TextureFont(new Font(FontFamily.GenericSansSerif, 14.0f));
TextHandle poem_handle;
TextPrinter text = new TextPrinter(); TextPrinter text = new TextPrinter();
string poem = new StreamReader("Data/Poem.txt").ReadToEnd(); //string poem = new StreamReader("Data/Poem.txt").ReadToEnd();
string poem = "The quick brown fox jumped over the lazy dogs!\n\nKerning: Wo\nLigatures: ffi, fft";
int lines; // How many lines the poem contains. int lines; // How many lines the poem contains.
float scroll_speed; float scroll_speed;
@ -49,7 +48,6 @@ namespace Examples.Tutorial
current_position = initial_position; current_position = initial_position;
scroll_speed = -1.0f; scroll_speed = -1.0f;
text.Prepare(poem, serif, out poem_handle);
// Count the amount of lines in the text, to find out the correct // Count the amount of lines in the text, to find out the correct
// warparound position. We want the text to scroll until the last // warparound position. We want the text to scroll until the last
@ -69,8 +67,10 @@ namespace Examples.Tutorial
public override void OnUnload(EventArgs e) public override void OnUnload(EventArgs e)
{ {
if (poem_handle != null) poem_handle.Dispose(); if (serif != null)
if (serif != null) serif.Dispose(); serif.Dispose();
if (sans != null)
sans.Dispose();
} }
#endregion #endregion
@ -126,14 +126,17 @@ namespace Examples.Tutorial
// used in 2d graphics, and is necessary for achieving pixel-perfect glyph rendering. // used in 2d graphics, and is necessary for achieving pixel-perfect glyph rendering.
// TextPrinter.End() restores your previous projection/modelview matrices. // TextPrinter.End() restores your previous projection/modelview matrices.
text.Begin(); text.Begin();
//GL.Color3(Color.LightBlue);
//text.Draw((1.0 / e.Time).ToString("F2"), sans); // Print FPS counter. Since the counter changes per frame,
// it shouldn't be cached (TextPrinterOptions.NoCache).
GL.Color3(Color.LightYellow);
text.Print((1.0 / e.Time).ToString("F2"), sans, TextPrinterOptions.NoCache);
// Print the actual text.
GL.Translate(0.0f, current_position, 0.0f); GL.Translate(0.0f, current_position, 0.0f);
GL.Color3(Color.White); GL.Color3(Color.White);
//text.Draw(poem_handle); text.Print(poem, serif);
//text.Draw(poem, serif);
//GL.BindTexture(TextureTarget.Texture2D, 1);
text.Print(poem, serif2);
text.End(); text.End();
SwapBuffers(); SwapBuffers();