Backport TextLabel optimizations

This commit is contained in:
spherallic 2023-04-25 16:45:52 +02:00
parent b54d7a48cc
commit 7e488497ef

View file

@ -94,6 +94,11 @@ namespace CodeImp.DoomBuilder.Rendering
// Disposing
private bool isdisposed;
// ano - static stuff to prevent often alloc/dealloc performance hits
private static StringFormat strFormat;
private static SolidBrush brush;
private static Pen pen;
#endregion
#region ================== Properties
@ -157,6 +162,8 @@ namespace CodeImp.DoomBuilder.Rendering
this.updateneeded = true;
this.textureupdateneeded = true; //mxd
InitializeStatics();
// Register as resource
General.Map.Graphics.RegisterResource(this);
@ -181,6 +188,8 @@ namespace CodeImp.DoomBuilder.Rendering
this.updateneeded = true;
this.textureupdateneeded = true;
InitializeStatics();
// Register as resource
General.Map.Graphics.RegisterResource(this);
@ -210,6 +219,27 @@ namespace CodeImp.DoomBuilder.Rendering
#region ================== Methods
// ano - share resources instead of constantly alloc/dealloc
public void InitializeStatics()
{
if (strFormat == null)
{
strFormat = new StringFormat();
strFormat.FormatFlags = StringFormatFlags.FitBlackBox | StringFormatFlags.NoWrap;
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
}
if (brush == null)
{
// if we actually see magenta, know we made a mistake somewhere
brush = new SolidBrush(System.Drawing.Color.Magenta);
}
if (pen == null)
{
pen = new Pen(System.Drawing.Color.Magenta);
}
}
// This updates the text if needed
public void Update(float translatex, float translatey, float scalex, float scaley)
{
@ -347,21 +377,14 @@ namespace CodeImp.DoomBuilder.Rendering
private static Bitmap CreateLabelImage(string text, Font font, PixelColor color, PixelColor backcolor, bool drawbg, RectangleF textrect, RectangleF bgrect, Size texturesize, PointF textorigin)
{
Bitmap result = new Bitmap(texturesize.Width, texturesize.Height);
using(Graphics g = Graphics.FromImage(result))
using (Graphics g = Graphics.FromImage(result))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
g.CompositingQuality = CompositingQuality.HighQuality;
// Draw text
using(StringFormat sf = new StringFormat())
{
sf.FormatFlags = StringFormatFlags.FitBlackBox | StringFormatFlags.NoWrap;
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
// Draw text with BG
if(drawbg)
if (drawbg)
{
GraphicsPath p = new GraphicsPath();
float radius = textorigin.X;
@ -388,32 +411,34 @@ namespace CodeImp.DoomBuilder.Rendering
p.AddArc(pathrect.Left, pathrect.Bottom - radius, radius, radius, 90, 90);
// Fill'n'draw bg
using(SolidBrush brush = new SolidBrush(color.ToColor()))
brush.Color = color.ToColor();
g.FillPath(brush, p);
using(Pen pen = new Pen(backcolor.ToColor(), outlinewidth))
pen.Color = backcolor.ToColor();
g.DrawPath(pen, p);
// Draw text
textrect.Inflate(4, 2);
using(SolidBrush brush = new SolidBrush(backcolor.ToColor()))
g.DrawString(text, font, brush, textrect, sf);
brush.Color = backcolor.ToColor();
g.DrawString(text, font, brush, textrect, strFormat);
}
// Draw plain text
else
{
RectangleF plainbgrect = textrect;
if(text.Length > 1) plainbgrect.Inflate(6, 2);
if (text.Length > 1) plainbgrect.Inflate(6, 2);
RectangleF plaintextrect = textrect;
plaintextrect.Inflate(6, 4);
using(SolidBrush brush = new SolidBrush(backcolor.ToColor()))
brush.Color = backcolor.ToColor();
g.FillRectangle(brush, plainbgrect);
using(SolidBrush brush = new SolidBrush(color.ToColor()))
g.DrawString(text, font, brush, plaintextrect, sf);
}
brush.Color = color.ToColor();
g.DrawString(text, font, brush, plaintextrect, strFormat);
}
}