hack hack hack... we have working text rendering

This commit is contained in:
Chris Forbes
2010-02-20 18:31:46 +13:00
parent 877348ebe3
commit 6e91c8015c
3 changed files with 69 additions and 29 deletions

Binary file not shown.

BIN
FreeSansBold.ttf Normal file

Binary file not shown.

View File

@@ -46,12 +46,14 @@ namespace OpenRa.Graphics
public ITexture PaletteTexture; public ITexture PaletteTexture;
readonly Font fDebug, fTitle; readonly Font fDebug, fTitle;
readonly FTFontBitmap testFont; readonly FTFontGL testFont, boldFont;
Sheet textSheet; Sheet textSheet;
SpriteRenderer rgbaRenderer; SpriteRenderer rgbaRenderer;
Sprite textSprite; Sprite textSprite;
const int RenderedSize = 48;
public Size Resolution { get { return device.WindowSize; } } public Size Resolution { get { return device.WindowSize; } }
public Renderer(Size resolution, bool windowed) public Renderer(Size resolution, bool windowed)
@@ -63,12 +65,25 @@ namespace OpenRa.Graphics
RgbaSpriteShader = device.CreateShader(FileSystem.Open("chrome-rgba.fx")); RgbaSpriteShader = device.CreateShader(FileSystem.Open("chrome-rgba.fx"));
WorldSpriteShader = device.CreateShader(FileSystem.Open("chrome-shp.fx")); WorldSpriteShader = device.CreateShader(FileSystem.Open("chrome-shp.fx"));
//fDebug = new Font("Tahoma", 10.0f, FontStyle.Regular); fDebug = new Font("Tahoma", 10.0f, FontStyle.Regular);
//fTitle = new Font("Tahoma", 10, FontStyle.Bold); fTitle = new Font("Tahoma", 10, FontStyle.Bold);
int Errors; int Errors;
testFont = new FTFontBitmap("FreeSans.ttf", out Errors); testFont = new FTFontGL("FreeSans.ttf", out Errors);
testFont.ftRenderToTexture(2, 48);
testFont.FT_ALIGN = FTFontAlign.FT_ALIGN_CENTERED; if (Errors > 0)
throw new InvalidOperationException("Error(s) loading font");
testFont.ftRenderToTexture(RenderedSize, 192);
testFont.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(RenderedSize, 192);
boldFont.FT_ALIGN = FTFontAlign.FT_ALIGN_LEFT;
textSheet = new Sheet(this, new Size(256, 256)); textSheet = new Sheet(this, new Size(256, 256));
rgbaRenderer = new SpriteRenderer(this, true, RgbaSpriteShader); rgbaRenderer = new SpriteRenderer(this, true, RgbaSpriteShader);
@@ -85,7 +100,7 @@ namespace OpenRa.Graphics
throw new NotImplementedException(); throw new NotImplementedException();
} }
Bitmap RenderTextToBitmap(string s, Font f, Color c) /* Bitmap RenderTextToBitmap(string s, Font f, Color c)
{ {
Bitmap b = new Bitmap(256, 256); Bitmap b = new Bitmap(256, 256);
testFont.ftBeginFont(0.9f,0.0f,0.0f,0.5f); testFont.ftBeginFont(0.9f,0.0f,0.0f,0.5f);
@@ -98,8 +113,8 @@ namespace OpenRa.Graphics
g.DrawString(s, f, new SolidBrush(c), 0, 0); g.DrawString(s, f, new SolidBrush(c), 0, 0);
g.Flush(); g.Flush();
}*/ }*/
return b; // return b;
} // }
int2 GetTextSize(string s, Font f) int2 GetTextSize(string s, Font f)
{ {
@@ -167,36 +182,61 @@ namespace OpenRa.Graphics
PerfHistory.Increment("batches", 1); PerfHistory.Increment("batches", 1);
} }
public void DrawText(string text, int2 pos, Color c) static void CheckError()
{ {
using (new PerfSample("text")) var e = Gl.glGetError();
{ if (e != Gl.GL_NO_ERROR)
Bitmap b = RenderTextToBitmap(text, fDebug, c); throw new InvalidOperationException("GL Error: " + Gl.glGetString(e));
textSheet.Texture.SetData(b); }
rgbaRenderer.DrawSprite(textSprite, pos.ToFloat2(), "chrome");
rgbaRenderer.Flush(); const float emHeight = 14f; /* px */
void DrawTextInner(FTFontGL f, string text, int2 pos, Color c)
{
using (new PerfSample("text"))
{
pos.Y += (int)(emHeight * 2 / 3);
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 / RenderedSize, emHeight / RenderedSize, 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();
} }
} }
public void DrawText2(string text, int2 pos, Color c) public void DrawText(string text, int2 pos, Color c) { DrawTextInner(testFont, text, pos, c); }
{ public void DrawText2(string text, int2 pos, Color c) { DrawTextInner(boldFont, text, pos, c); }
using (new PerfSample("text"))
{
Bitmap b = RenderTextToBitmap(text, fTitle, c);
textSheet.Texture.SetData(b);
rgbaRenderer.DrawSprite(textSprite, pos.ToFloat2(), "chrome");
rgbaRenderer.Flush();
}
}
public int2 MeasureText(string text) public int2 MeasureText(string text)
{ {
return GetTextSize(text, fDebug); return new int2((int)(testFont.ftExtent(ref text) / 3), (int)emHeight);
} }
public int2 MeasureText2(string text) public int2 MeasureText2(string text)
{ {
return GetTextSize(text, fTitle); return new int2((int)(boldFont.ftExtent(ref text) / 3), (int)emHeight);
} }
} }
} }