Text rendering to bitmap using System.Drawing. Currently broken.

This commit is contained in:
Matthew Bowra-Dean
2010-02-16 14:03:26 +13:00
parent 25dbd72e43
commit c6684a7436

View File

@@ -3,6 +3,7 @@ using System.Windows.Forms;
using Ijw.DirectX; using Ijw.DirectX;
using OpenRa.FileFormats; using OpenRa.FileFormats;
using OpenRa.Support; using OpenRa.Support;
using System.Drawing.Drawing2D;
namespace OpenRa.Graphics namespace OpenRa.Graphics
{ {
@@ -20,7 +21,11 @@ namespace OpenRa.Graphics
public Texture PaletteTexture; public Texture PaletteTexture;
readonly SpriteHelper sh; readonly SpriteHelper sh;
readonly FontHelper fhDebug, fhTitle; readonly Font fDebug, fTitle;
Sheet textSheet;
SpriteRenderer rgbaRenderer;
Sprite textSprite;
public Renderer(Control host, Size resolution, bool windowed) public Renderer(Control host, Size resolution, bool windowed)
{ {
@@ -37,9 +42,29 @@ namespace OpenRa.Graphics
WorldSpriteShader = new Shader(device, FileSystem.Open("chrome-shp.fx")); WorldSpriteShader = new Shader(device, FileSystem.Open("chrome-shp.fx"));
WorldSpriteShader.Quality = ShaderQuality.High; WorldSpriteShader.Quality = ShaderQuality.High;
sh = new SpriteHelper(device); //sh = new SpriteHelper(device);
fhDebug = new FontHelper(device, "Tahoma", 10, false); fDebug = new Font("Tahoma", 10, FontStyle.Regular);
fhTitle = new FontHelper(device, "Tahoma", 10, true); fTitle = new Font("Tahoma", 10, FontStyle.Bold);
textSheet = new Sheet(this, new Size(256, 256));
rgbaRenderer = new SpriteRenderer(this, true, RgbaSpriteShader);
textSprite = new Sprite(textSheet, new Rectangle(0, 0, 256, 256), TextureChannel.Alpha);
}
Bitmap RenderTextToBitmap(string s, Font f, Color c)
{
Bitmap b = new Bitmap(256, 256);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b);
g.DrawString(s, f, new SolidBrush(c), 0, 0);
g.Flush();
g.Dispose();
return b;
}
int2 GetTextSize(string s, Font f)
{
Bitmap b = new Bitmap(1,1);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b);
return new int2(g.MeasureString(s, f).ToSize());
} }
public GraphicsDevice Device { get { return device; } } public GraphicsDevice Device { get { return device; } }
@@ -96,26 +121,26 @@ namespace OpenRa.Graphics
public void DrawText(string text, int2 pos, Color c) public void DrawText(string text, int2 pos, Color c)
{ {
sh.Begin(); Bitmap b = RenderTextToBitmap(text, fDebug, c);
fhDebug.Draw(sh, text, pos.X, pos.Y, c.ToArgb()); textSheet.Texture.SetData(b);
sh.End(); rgbaRenderer.DrawSprite(textSprite, pos.ToFloat2(), "chrome");
} }
public void DrawText2(string text, int2 pos, Color c) public void DrawText2(string text, int2 pos, Color c)
{ {
sh.Begin(); Bitmap b = RenderTextToBitmap(text, fTitle, c);
fhTitle.Draw(sh, text, pos.X, pos.Y, c.ToArgb()); textSheet.Texture.SetData(b);
sh.End(); rgbaRenderer.DrawSprite(textSprite, pos.ToFloat2(), "chrome");
} }
public int2 MeasureText(string text) public int2 MeasureText(string text)
{ {
return new int2(fhDebug.MeasureText(sh, text)); return GetTextSize(text, fDebug);
} }
public int2 MeasureText2(string text) public int2 MeasureText2(string text)
{ {
return new int2(fhTitle.MeasureText(sh, text)); return GetTextSize(text, fTitle);
} }
} }
} }