mirror of
https://github.com/Ryujinx/Opentk.git
synced 2025-01-11 22:35:45 +00:00
Added SansSerif font.
This commit is contained in:
parent
04585824dc
commit
7c187e705e
|
@ -19,12 +19,16 @@ using OpenTK.Math;
|
||||||
|
|
||||||
namespace Examples.Tutorial
|
namespace Examples.Tutorial
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Tests Font loading and rendering.
|
||||||
|
/// </summary>
|
||||||
class Fonts : GameWindow, IExample
|
class Fonts : GameWindow, IExample
|
||||||
{
|
{
|
||||||
public Fonts() : base(new DisplayMode(800, 600), String.Format("OpenTK | Tutorial {0}: Fonts", order))
|
public Fonts() : base(new DisplayMode(800, 600), String.Format("OpenTK | Tutorial {0}: Fonts", order))
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
ITextPrinter text = new TextPrinter();
|
ITextPrinter printer = new TextPrinter();
|
||||||
|
const string text = "Hello, world!";
|
||||||
|
|
||||||
// Load some different TextureFont sizes to compare their quality.
|
// Load some different TextureFont sizes to compare their quality.
|
||||||
TextureFont[] fonts = new TextureFont[]
|
TextureFont[] fonts = new TextureFont[]
|
||||||
|
@ -39,25 +43,70 @@ namespace Examples.Tutorial
|
||||||
new TextureFont(new Font(FontFamily.GenericSerif, 20.0f)),
|
new TextureFont(new Font(FontFamily.GenericSerif, 20.0f)),
|
||||||
new TextureFont(new Font(FontFamily.GenericSerif, 22.0f)),
|
new TextureFont(new Font(FontFamily.GenericSerif, 22.0f)),
|
||||||
|
|
||||||
new TextureFont(new Font(FontFamily.GenericSerif, 24.0f)),
|
new TextureFont(new Font(FontFamily.GenericSerif, 24.0f, FontStyle.Bold)),
|
||||||
new TextureFont(new Font(FontFamily.GenericSerif, 26.0f)),
|
new TextureFont(new Font(FontFamily.GenericSerif, 26.0f)),
|
||||||
new TextureFont(new Font(FontFamily.GenericSerif, 28.0f)),
|
new TextureFont(new Font(FontFamily.GenericSerif, 28.0f)),
|
||||||
new TextureFont(new Font(FontFamily.GenericSerif, 30.0f)),
|
new TextureFont(new Font(FontFamily.GenericSerif, 30.0f)),
|
||||||
|
|
||||||
new TextureFont(new Font(FontFamily.GenericSerif, 32.0f)),
|
new TextureFont(new Font(FontFamily.GenericSerif, 32.0f, FontStyle.Italic)),
|
||||||
new TextureFont(new Font(FontFamily.GenericSerif, 34.0f)),
|
new TextureFont(new Font(FontFamily.GenericSerif, 34.0f)),
|
||||||
new TextureFont(new Font(FontFamily.GenericSerif, 36.0f)),
|
new TextureFont(new Font(FontFamily.GenericSerif, 36.0f)),
|
||||||
new TextureFont(new Font(FontFamily.GenericSerif, 38.0f)),
|
new TextureFont(new Font(FontFamily.GenericSerif, 38.0f)),
|
||||||
|
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 8.0f)),
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 10.0f)),
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 12.0f)),
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 14.0f)),
|
||||||
|
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 16.0f)),
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 18.0f)),
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 20.0f)),
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 22.0f)),
|
||||||
|
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 24.0f, FontStyle.Bold)),
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 26.0f)),
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 28.0f)),
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 30.0f)),
|
||||||
|
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 32.0f, FontStyle.Italic)),
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 34.0f)),
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 36.0f)),
|
||||||
|
new TextureFont(new Font(FontFamily.GenericSansSerif, 38.0f)),
|
||||||
};
|
};
|
||||||
TextHandle[] handles; // Used to cache the strings we want to print.
|
TextHandle[] handles; // Used to cache the strings we want to print.
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// To maintain high rendering performance, we need to cache the text
|
||||||
|
/// we wish to draw, for each font we want to draw it with. To achieve
|
||||||
|
/// this, we use the TextPrinter.Prepare() method, which returns a
|
||||||
|
/// TextHandle - i.e. a handle to the cached text.
|
||||||
|
/// <para>
|
||||||
|
/// We can use these TextHandles with the TextPrinter.Draw() method
|
||||||
|
/// to draw text with very high performance.
|
||||||
|
/// </para>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="e"></param>
|
||||||
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];
|
handles = new TextHandle[fonts.Length];
|
||||||
for (int i = handles.Length; --i >= 0; )
|
for (int i = handles.Length; --i >= 0; )
|
||||||
text.Prepare("Hello, world!", fonts[i], out handles[i]);
|
printer.Prepare(text, fonts[i], out handles[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// It is important that we need to call the Dispose() methods to reclaim of
|
||||||
|
/// each and every TextHandle and TextureFont to reclaim the unamanged
|
||||||
|
/// (OpenGL) resources they are using.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
public override void OnUnload(EventArgs e)
|
||||||
|
{
|
||||||
|
foreach (TextHandle h in handles)
|
||||||
|
h.Dispose();
|
||||||
|
foreach (TextureFont f in fonts)
|
||||||
|
f.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
|
protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
|
||||||
|
@ -71,6 +120,21 @@ namespace Examples.Tutorial
|
||||||
this.Exit();
|
this.Exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// To render pixel-perfect text, we have to setup a 2d display projection
|
||||||
|
/// with a width/height that corresponds to our current Viewport.
|
||||||
|
/// In OpenGL this can be easily achieved using the GL.Ortho method.
|
||||||
|
/// <para>
|
||||||
|
/// It is still possible to render text in a 3d projection, but it will
|
||||||
|
/// be very hard to achieve pixel-perfect precision.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// To achieve the highest possible performance, render your 3d scene first.
|
||||||
|
/// At the end of the RenderFrame call, setup a 2d projection and render all
|
||||||
|
/// text in one go.
|
||||||
|
/// </para>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="e"></param>
|
||||||
public override void OnRenderFrame(RenderFrameEventArgs e)
|
public override void OnRenderFrame(RenderFrameEventArgs e)
|
||||||
{
|
{
|
||||||
GL.Clear(ClearBufferMask.ColorBufferBit);
|
GL.Clear(ClearBufferMask.ColorBufferBit);
|
||||||
|
@ -82,10 +146,21 @@ namespace Examples.Tutorial
|
||||||
GL.MatrixMode(MatrixMode.Modelview);
|
GL.MatrixMode(MatrixMode.Modelview);
|
||||||
GL.LoadIdentity();
|
GL.LoadIdentity();
|
||||||
|
|
||||||
// Font size test:
|
// Print using the first font.
|
||||||
for (int i = 0; i < handles.Length; i++)
|
for (int i = 0; i < handles.Length / 2; i++)
|
||||||
{
|
{
|
||||||
text.Draw(handles[i]);
|
printer.Draw(handles[i]);
|
||||||
|
GL.Translate(0, fonts[i].Height, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move to the right, and print using the second font.
|
||||||
|
float width, height;
|
||||||
|
fonts[handles.Length / 2 - 1].MeasureString(text, out width, out height);
|
||||||
|
GL.LoadIdentity();
|
||||||
|
GL.Translate(width + 32.0f, 0, 0);
|
||||||
|
for (int i = handles.Length / 2; i < handles.Length; i++)
|
||||||
|
{
|
||||||
|
printer.Draw(handles[i]);
|
||||||
GL.Translate(0, fonts[i].Height, 0);
|
GL.Translate(0, fonts[i].Height, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue