diff --git a/OpenRa.FileFormats/Graphics/IGraphicsDevice.cs b/OpenRa.FileFormats/Graphics/IGraphicsDevice.cs index 470cc8aca0..e826fa2269 100755 --- a/OpenRa.FileFormats/Graphics/IGraphicsDevice.cs +++ b/OpenRa.FileFormats/Graphics/IGraphicsDevice.cs @@ -25,7 +25,7 @@ namespace OpenRa.FileFormats.Graphics IVertexBuffer 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, diff --git a/OpenRa.Game/Graphics/Renderer.cs b/OpenRa.Game/Graphics/Renderer.cs index 97269ac643..8b8db3e15c 100644 --- a/OpenRa.Game/Graphics/Renderer.cs +++ b/OpenRa.Game/Graphics/Renderer.cs @@ -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 ); } } } diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index 2f12f4c531..5aaa4041ee 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -1,4 +1,4 @@ - + Debug @@ -66,10 +66,6 @@ - - False - ..\thirdparty\Tao\Tao.OpenGl.dll - False ..\thirdparty\ISE.FreeType.dll diff --git a/OpenRa.Gl/GraphicsDevice.cs b/OpenRa.Gl/GraphicsDevice.cs index 9f085d9177..73d10503b5 100644 --- a/OpenRa.Gl/GraphicsDevice.cs +++ b/OpenRa.Gl/GraphicsDevice.cs @@ -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); + } + } } diff --git a/OpenRa.Gl/OpenRa.Gl.csproj b/OpenRa.Gl/OpenRa.Gl.csproj index 5ca4f36177..26652c5fd8 100644 --- a/OpenRa.Gl/OpenRa.Gl.csproj +++ b/OpenRa.Gl/OpenRa.Gl.csproj @@ -31,6 +31,10 @@ 4 + + False + ..\thirdparty\ISE.FreeType.dll + 3.5