Skip to content

Commit

Permalink
hack hack hack... we have working text rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisforbes committed Feb 20, 2010
1 parent 877348e commit 6e91c80
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 27 deletions.
Binary file modified FreeSans.ttf
Binary file not shown.
Binary file added FreeSansBold.ttf
Binary file not shown.
94 changes: 67 additions & 27 deletions OpenRa.Game/Graphics/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ internal class Renderer
public ITexture PaletteTexture;

readonly Font fDebug, fTitle;
readonly FTFontBitmap testFont;
readonly FTFontGL testFont, boldFont;

Sheet textSheet;
SpriteRenderer rgbaRenderer;
Sprite textSprite;

const int RenderedSize = 48;

public Size Resolution { get { return device.WindowSize; } }

public Renderer(Size resolution, bool windowed)
Expand All @@ -63,12 +65,25 @@ public Renderer(Size resolution, bool windowed)
RgbaSpriteShader = device.CreateShader(FileSystem.Open("chrome-rgba.fx"));
WorldSpriteShader = device.CreateShader(FileSystem.Open("chrome-shp.fx"));

//fDebug = new Font("Tahoma", 10.0f, FontStyle.Regular);
//fTitle = new Font("Tahoma", 10, FontStyle.Bold);
fDebug = new Font("Tahoma", 10.0f, FontStyle.Regular);
fTitle = new Font("Tahoma", 10, FontStyle.Bold);

int Errors;
testFont = new FTFontBitmap("FreeSans.ttf", out Errors);
testFont.ftRenderToTexture(2, 48);
testFont.FT_ALIGN = FTFontAlign.FT_ALIGN_CENTERED;
testFont = new FTFontGL("FreeSans.ttf", out Errors);

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));
rgbaRenderer = new SpriteRenderer(this, true, RgbaSpriteShader);
Expand All @@ -85,7 +100,7 @@ IGraphicsDevice CreateDevice( Assembly rendererDll, int width, int height, bool
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);
testFont.ftBeginFont(0.9f,0.0f,0.0f,0.5f);
Expand All @@ -98,8 +113,8 @@ Bitmap RenderTextToBitmap(string s, Font f, Color c)
g.DrawString(s, f, new SolidBrush(c), 0, 0);
g.Flush();
}*/
return b;
}
// return b;
// }

int2 GetTextSize(string s, Font f)
{
Expand Down Expand Up @@ -167,36 +182,61 @@ public void DrawBatch<T>(IVertexBuffer<T> vertices, IIndexBuffer indices,
PerfHistory.Increment("batches", 1);
}

public void DrawText(string text, int2 pos, Color c)
{
using (new PerfSample("text"))
{
Bitmap b = RenderTextToBitmap(text, fDebug, c);
textSheet.Texture.SetData(b);
rgbaRenderer.DrawSprite(textSprite, pos.ToFloat2(), "chrome");
rgbaRenderer.Flush();
}
static void CheckError()
{
var e = Gl.glGetError();
if (e != Gl.GL_NO_ERROR)
throw new InvalidOperationException("GL Error: " + Gl.glGetString(e));
}

public void DrawText2(string text, int2 pos, Color c)
{
using (new PerfSample("text"))
const float emHeight = 14f; /* px */

void DrawTextInner(FTFontGL f, string text, int2 pos, Color c)
{
using (new PerfSample("text"))
{
Bitmap b = RenderTextToBitmap(text, fTitle, c);
textSheet.Texture.SetData(b);
rgbaRenderer.DrawSprite(textSprite, pos.ToFloat2(), "chrome");
rgbaRenderer.Flush();
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 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); }

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)
{
return GetTextSize(text, fTitle);
return new int2((int)(boldFont.ftExtent(ref text) / 3), (int)emHeight);
}
}
}

0 comments on commit 6e91c80

Please sign in to comment.