moved text rendering into renderer dll.

This commit is contained in:
Bob
2010-02-20 21:10:02 +13:00
parent b519f2bace
commit 7e28f28fae
5 changed files with 90 additions and 69 deletions

View File

@@ -25,7 +25,7 @@ namespace OpenRa.FileFormats.Graphics
IVertexBuffer<Vertex> CreateVertexBuffer( int length );
IIndexBuffer CreateIndexBuffer( int length );
ITexture CreateTexture( Bitmap bitmap );
IFont CreateFont( string filename );
IShader CreateShader( Stream stream );
Size WindowSize { get; }
@@ -67,6 +67,13 @@ namespace OpenRa.FileFormats.Graphics
void SetData( Bitmap bitmap );
}
public interface IFont
{
void DrawText( string text, int2 pos, Color c );
int2 Measure( string text );
}
public enum PrimitiveType
{
PointList,

View File

@@ -28,7 +28,6 @@ using OpenRa.FileFormats.Graphics;
using OpenRa.Support;
using System.IO;
using ISE;
using Tao.OpenGl;
namespace OpenRa.Graphics
{
@@ -45,8 +44,7 @@ namespace OpenRa.Graphics
public ITexture PaletteTexture;
readonly FTFontGL regularFont, boldFont;
const int RenderedFontSize = 48;
readonly IFont regularFont, boldFont;
public Size Resolution { get { return device.WindowSize; } }
@@ -59,21 +57,8 @@ namespace OpenRa.Graphics
RgbaSpriteShader = device.CreateShader(FileSystem.Open("shaders/chrome-rgba.fx"));
WorldSpriteShader = device.CreateShader(FileSystem.Open("shaders/chrome-shp.fx"));
int Errors;
regularFont = new FTFontGL("FreeSans.ttf", out Errors);
if (Errors > 0)
throw new InvalidOperationException("Error(s) loading font");
regularFont.ftRenderToTexture(RenderedFontSize, 192);
regularFont.FT_ALIGN = FTFontAlign.FT_ALIGN_LEFT;
boldFont = new FTFontGL("FreeSansBold.ttf", out Errors);
if (Errors > 0)
throw new InvalidOperationException("Error(s) loading font");
boldFont.ftRenderToTexture(RenderedFontSize, 192);
boldFont.FT_ALIGN = FTFontAlign.FT_ALIGN_LEFT;
regularFont = device.CreateFont( "FreeSans.ttf" );
boldFont = device.CreateFont( "FreeSansBold.ttf" );
}
IGraphicsDevice CreateDevice( Assembly rendererDll, int width, int height, bool windowed, bool vsync )
@@ -144,61 +129,25 @@ namespace OpenRa.Graphics
PerfHistory.Increment("batches", 1);
}
static void CheckError()
{
var e = Gl.glGetError();
if (e != Gl.GL_NO_ERROR)
throw new InvalidOperationException("GL Error: " + Gl.glGetString(e));
}
const float emHeight = 14f; /* px */
void DrawTextInner(FTFontGL f, string text, int2 pos, Color c)
public void DrawText( string text, int2 pos, Color c )
{
using (new PerfSample("text"))
{
pos.Y += (int)(emHeight);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glPushMatrix();
Gl.glLoadIdentity();
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glPushMatrix();
Gl.glLoadIdentity();
Gl.glOrtho(0, Resolution.Width, 0, Resolution.Height, 0, 1);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glTranslatef(pos.X, Resolution.Height - pos.Y, 0);
Gl.glScalef(emHeight / RenderedFontSize, emHeight / RenderedFontSize, 1);
f.ftBeginFont(false);
Gl.glColor4f(c.R / 255f, c.G / 255f, c.B / 255f, c.A / 255f);
f.ftWrite(text);
f.ftEndFont();
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glPopMatrix();
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glPopMatrix();
CheckError();
}
regularFont.DrawText( text, pos, c );
}
public void DrawText2( string text, int2 pos, Color c )
{
using (new PerfSample("text"))
boldFont.DrawText( text, pos, c );
}
public void DrawText(string text, int2 pos, Color c) { DrawTextInner(regularFont, text, pos, c); }
public void DrawText2(string text, int2 pos, Color c) { DrawTextInner(boldFont, text, pos, c); }
public int2 MeasureText(string text)
{
return new int2((int)(regularFont.ftExtent(ref text) / 3), (int)emHeight);
return regularFont.Measure( text );
}
public int2 MeasureText2(string text)
{
return new int2((int)(boldFont.ftExtent(ref text) / 3), (int)emHeight);
return boldFont.Measure( text );
}
}
}

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -66,10 +66,6 @@
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Tao.OpenAl, Version=1.1.0.1, Culture=neutral, PublicKeyToken=a7579dda88828311, processorArchitecture=MSIL" />
<Reference Include="Tao.OpenGl, Version=2.1.0.12, Culture=neutral, PublicKeyToken=1ca010269a4501ef">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\thirdparty\Tao\Tao.OpenGl.dll</HintPath>
</Reference>
<Reference Include="ISE.FreeType, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1b34ab585684d5ea">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\thirdparty\ISE.FreeType.dll</HintPath>

View File

@@ -28,6 +28,7 @@ using Tao.Cg;
using Tao.OpenGl;
using OpenRa.FileFormats.Graphics;
using Tao.Sdl;
using ISE;
[assembly: Renderer( typeof( OpenRa.GlRenderer.GraphicsDevice ))]
@@ -231,6 +232,11 @@ namespace OpenRa.GlRenderer
return new Shader( this, stream );
}
public IFont CreateFont( string filename )
{
return new Font( this, filename );
}
#endregion
}
@@ -414,4 +420,63 @@ namespace OpenRa.GlRenderer
bitmap.UnlockBits(bits);
}
}
class Font : IFont
{
const int RenderedFontSize = 48;
const float emHeight = 14f; /* px */
GraphicsDevice dev;
FTFontGL font;
public Font( GraphicsDevice dev, string filename )
{
this.dev = dev;
int Errors;
font = new FTFontGL(filename, out Errors);
if (Errors > 0)
throw new InvalidOperationException("Error(s) loading font");
font.ftRenderToTexture(RenderedFontSize, 192);
font.FT_ALIGN = FTFontAlign.FT_ALIGN_LEFT;
}
public void DrawText( string text, int2 pos, Color c )
{
pos.Y += (int)(emHeight);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glPushMatrix();
Gl.glLoadIdentity();
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glPushMatrix();
Gl.glLoadIdentity();
Gl.glOrtho(0, dev.WindowSize.Width, 0, dev.WindowSize.Height, 0, 1);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glTranslatef(pos.X, dev.WindowSize.Height - pos.Y, 0);
Gl.glScalef(emHeight / RenderedFontSize, emHeight / RenderedFontSize, 1);
font.ftBeginFont(false);
Gl.glColor4f(c.R / 255f, c.G / 255f, c.B / 255f, c.A / 255f);
font.ftWrite(text);
font.ftEndFont();
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glPopMatrix();
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glPopMatrix();
GraphicsDevice.CheckGlError();
}
public int2 Measure( string text )
{
return new int2((int)(font.ftExtent(ref text) / 3), (int)emHeight);
}
}
}

View File

@@ -31,6 +31,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="ISE.FreeType, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1b34ab585684d5ea, processorArchitecture=x86">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\thirdparty\ISE.FreeType.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>