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 // Disposing
private bool isdisposed; 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 #endregion
#region ================== Properties #region ================== Properties
@ -157,6 +162,8 @@ namespace CodeImp.DoomBuilder.Rendering
this.updateneeded = true; this.updateneeded = true;
this.textureupdateneeded = true; //mxd this.textureupdateneeded = true; //mxd
InitializeStatics();
// Register as resource // Register as resource
General.Map.Graphics.RegisterResource(this); General.Map.Graphics.RegisterResource(this);
@ -181,6 +188,8 @@ namespace CodeImp.DoomBuilder.Rendering
this.updateneeded = true; this.updateneeded = true;
this.textureupdateneeded = true; this.textureupdateneeded = true;
InitializeStatics();
// Register as resource // Register as resource
General.Map.Graphics.RegisterResource(this); General.Map.Graphics.RegisterResource(this);
@ -210,6 +219,27 @@ namespace CodeImp.DoomBuilder.Rendering
#region ================== Methods #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 // This updates the text if needed
public void Update(float translatex, float translatey, float scalex, float scaley) public void Update(float translatex, float translatey, float scalex, float scaley)
{ {
@ -353,13 +383,6 @@ namespace CodeImp.DoomBuilder.Rendering
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
g.CompositingQuality = CompositingQuality.HighQuality; 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 // Draw text with BG
if (drawbg) if (drawbg)
{ {
@ -388,16 +411,19 @@ namespace CodeImp.DoomBuilder.Rendering
p.AddArc(pathrect.Left, pathrect.Bottom - radius, radius, radius, 90, 90); p.AddArc(pathrect.Left, pathrect.Bottom - radius, radius, radius, 90, 90);
// Fill'n'draw bg // Fill'n'draw bg
using(SolidBrush brush = new SolidBrush(color.ToColor())) brush.Color = color.ToColor();
g.FillPath(brush, p); g.FillPath(brush, p);
using(Pen pen = new Pen(backcolor.ToColor(), outlinewidth)) pen.Color = backcolor.ToColor();
g.DrawPath(pen, p); g.DrawPath(pen, p);
// Draw text // Draw text
textrect.Inflate(4, 2); textrect.Inflate(4, 2);
using(SolidBrush brush = new SolidBrush(backcolor.ToColor())) brush.Color = backcolor.ToColor();
g.DrawString(text, font, brush, textrect, sf);
g.DrawString(text, font, brush, textrect, strFormat);
} }
// Draw plain text // Draw plain text
else else
@ -408,12 +434,11 @@ namespace CodeImp.DoomBuilder.Rendering
RectangleF plaintextrect = textrect; RectangleF plaintextrect = textrect;
plaintextrect.Inflate(6, 4); plaintextrect.Inflate(6, 4);
using(SolidBrush brush = new SolidBrush(backcolor.ToColor())) brush.Color = backcolor.ToColor();
g.FillRectangle(brush, plainbgrect); g.FillRectangle(brush, plainbgrect);
using(SolidBrush brush = new SolidBrush(color.ToColor())) brush.Color = color.ToColor();
g.DrawString(text, font, brush, plaintextrect, sf); g.DrawString(text, font, brush, plaintextrect, strFormat);
}
} }
} }