mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-17 11:35:08 -04:00
Minimise DrawTextArgs allocations.
This commit is contained in:
parent
05621acd92
commit
d8b8e58656
@ -4,7 +4,7 @@ using ClassicalSharp.GraphicsAPI;
|
|||||||
|
|
||||||
namespace ClassicalSharp {
|
namespace ClassicalSharp {
|
||||||
|
|
||||||
public class DrawTextArgs {
|
public struct DrawTextArgs {
|
||||||
|
|
||||||
public Color TextColour;
|
public Color TextColour;
|
||||||
public string Text;
|
public string Text;
|
||||||
|
@ -109,7 +109,7 @@ namespace ClassicalSharp {
|
|||||||
|
|
||||||
Block block = blocksTable[selectedIndex].BlockId;
|
Block block = blocksTable[selectedIndex].BlockId;
|
||||||
string text = GetBlockInfo( block );
|
string text = GetBlockInfo( block );
|
||||||
List<DrawTextArgs> parts = Utils.SplitText( GraphicsApi, text, true );
|
List<DrawTextArgs> parts = Utils2D.SplitText( GraphicsApi, text, true );
|
||||||
Size size = Utils2D.MeasureSize( parts, font, true );
|
Size size = Utils2D.MeasureSize( parts, font, true );
|
||||||
int x = startX + ( blockSize * blocksPerRow ) / 2 - size.Width / 2;
|
int x = startX + ( blockSize * blocksPerRow ) / 2 - size.Width / 2;
|
||||||
int y = startY - size.Height;
|
int y = startY - size.Height;
|
||||||
|
@ -142,7 +142,7 @@ namespace ClassicalSharp {
|
|||||||
void UpdateAnnouncement( string text ) {
|
void UpdateAnnouncement( string text ) {
|
||||||
announcementDisplayTime = DateTime.UtcNow;
|
announcementDisplayTime = DateTime.UtcNow;
|
||||||
if( !String.IsNullOrEmpty( text ) ) {
|
if( !String.IsNullOrEmpty( text ) ) {
|
||||||
List<DrawTextArgs> parts = Utils.SplitText( GraphicsApi, text, true );
|
List<DrawTextArgs> parts = Utils2D.SplitText( GraphicsApi, text, true );
|
||||||
Size size = Utils2D.MeasureSize( Utils.StripColours( text ), announcementFont, true );
|
Size size = Utils2D.MeasureSize( Utils.StripColours( text ), announcementFont, true );
|
||||||
int x = Window.Width / 2 - size.Width / 2;
|
int x = Window.Width / 2 - size.Width / 2;
|
||||||
int y = Window.Height / 4 - size.Height / 2;
|
int y = Window.Height / 4 - size.Height / 2;
|
||||||
@ -232,7 +232,7 @@ namespace ClassicalSharp {
|
|||||||
using( Graphics g = Graphics.FromImage( bmp ) ) {
|
using( Graphics g = Graphics.FromImage( bmp ) ) {
|
||||||
Utils2D.DrawRect( g, backColour, 0, 0, bmp.Width, bmp.Height );
|
Utils2D.DrawRect( g, backColour, 0, 0, bmp.Width, bmp.Height );
|
||||||
DrawTextArgs args = new DrawTextArgs( GraphicsApi, text, Color.Yellow, false );
|
DrawTextArgs args = new DrawTextArgs( GraphicsApi, text, Color.Yellow, false );
|
||||||
Utils2D.DrawText( g, historyFont, args, 0, 0 );
|
Utils2D.DrawText( g, historyFont, ref args, 0, 0 );
|
||||||
}
|
}
|
||||||
pageTexture = Utils2D.Make2DTexture( GraphicsApi, bmp, 10, y );
|
pageTexture = Utils2D.Make2DTexture( GraphicsApi, bmp, 10, y );
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ namespace ClassicalSharp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Texture ModifyText( int yOffset, string text, Font font ) {
|
Texture ModifyText( int yOffset, string text, Font font ) {
|
||||||
List<DrawTextArgs> parts = Utils.SplitText( GraphicsApi, text, true );
|
List<DrawTextArgs> parts = Utils2D.SplitText( GraphicsApi, text, true );
|
||||||
Size size = Utils2D.MeasureSize( Utils.StripColours( text ), font, true );
|
Size size = Utils2D.MeasureSize( Utils.StripColours( text ), font, true );
|
||||||
int x = Window.Width / 2 - size.Width / 2;
|
int x = Window.Width / 2 - size.Width / 2;
|
||||||
int y = Window.Height / 2 - yOffset;
|
int y = Window.Height / 2 - yOffset;
|
||||||
|
@ -57,7 +57,7 @@ namespace ClassicalSharp {
|
|||||||
return Size.Ceiling( total );
|
return Size.Ceiling( total );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void DrawText( Graphics g, Font font, DrawTextArgs args, float x, float y ) {
|
public static void DrawText( Graphics g, Font font, ref DrawTextArgs args, float x, float y ) {
|
||||||
Brush textBrush = GetOrCreateBrush( args.TextColour );
|
Brush textBrush = GetOrCreateBrush( args.TextColour );
|
||||||
Brush shadowBrush = GetOrCreateBrush( args.ShadowColour );
|
Brush shadowBrush = GetOrCreateBrush( args.ShadowColour );
|
||||||
g.TextRenderingHint = TextRenderingHint.AntiAlias;
|
g.TextRenderingHint = TextRenderingHint.AntiAlias;
|
||||||
@ -71,7 +71,7 @@ namespace ClassicalSharp {
|
|||||||
public static void DrawText( Graphics g, List<DrawTextArgs> parts, Font font, float x, float y ) {
|
public static void DrawText( Graphics g, List<DrawTextArgs> parts, Font font, float x, float y ) {
|
||||||
for( int i = 0; i < parts.Count; i++ ) {
|
for( int i = 0; i < parts.Count; i++ ) {
|
||||||
DrawTextArgs part = parts[i];
|
DrawTextArgs part = parts[i];
|
||||||
DrawText( g, font, part, x, y );
|
DrawText( g, font, ref part, x, y );
|
||||||
SizeF partSize = g.MeasureString( part.Text, font, Int32.MaxValue, format );
|
SizeF partSize = g.MeasureString( part.Text, font, Int32.MaxValue, format );
|
||||||
x += partSize.Width;
|
x += partSize.Width;
|
||||||
}
|
}
|
||||||
@ -88,11 +88,11 @@ namespace ClassicalSharp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Texture MakeTextTexture( Font font, int x1, int y1, DrawTextArgs args ) {
|
public static Texture MakeTextTexture( Font font, int x1, int y1, ref DrawTextArgs args ) {
|
||||||
Size size = MeasureSize( args.Text, font, args.UseShadow );
|
Size size = MeasureSize( args.Text, font, args.UseShadow );
|
||||||
using( Bitmap bmp = new Bitmap( size.Width, size.Height ) ) {
|
using( Bitmap bmp = new Bitmap( size.Width, size.Height ) ) {
|
||||||
using( Graphics g = Graphics.FromImage( bmp ) ) {
|
using( Graphics g = Graphics.FromImage( bmp ) ) {
|
||||||
DrawText( g, font, args, 0, 0 );
|
DrawText( g, font, ref args, 0, 0 );
|
||||||
}
|
}
|
||||||
return Make2DTexture( args.Graphics, bmp, x1, y1 );
|
return Make2DTexture( args.Graphics, bmp, x1, y1 );
|
||||||
}
|
}
|
||||||
@ -138,5 +138,42 @@ namespace ClassicalSharp {
|
|||||||
pair.Value.Dispose();
|
pair.Value.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Color[] colours = new Color[] {
|
||||||
|
Color.FromArgb( 0, 0, 0 ), // black
|
||||||
|
Color.FromArgb( 0, 0, 191 ), // dark blue
|
||||||
|
Color.FromArgb( 0, 191, 0 ), // dark green
|
||||||
|
Color.FromArgb( 0, 191, 191 ), // dark teal
|
||||||
|
Color.FromArgb( 191, 0, 0 ), // dark red
|
||||||
|
Color.FromArgb( 191, 0, 191 ), // purple
|
||||||
|
Color.FromArgb( 191, 191, 0 ), // gold
|
||||||
|
Color.FromArgb( 191, 191, 191 ), // gray
|
||||||
|
Color.FromArgb( 64, 64, 64 ), // dark gray
|
||||||
|
Color.FromArgb( 64, 64, 255 ), // blue
|
||||||
|
Color.FromArgb( 64, 255, 64 ), // lime
|
||||||
|
Color.FromArgb( 64, 255, 255 ), // teal
|
||||||
|
Color.FromArgb( 255, 64, 64 ), // red
|
||||||
|
Color.FromArgb( 255, 64, 255 ), // pink
|
||||||
|
Color.FromArgb( 255, 255, 64 ), // yellow
|
||||||
|
Color.FromArgb( 255, 255, 255 ), // white
|
||||||
|
};
|
||||||
|
|
||||||
|
static List<DrawTextArgs> parts = new List<DrawTextArgs>( 64 );
|
||||||
|
public static List<DrawTextArgs> SplitText( IGraphicsApi graphics, string value, bool shadow ) {
|
||||||
|
int code = 15;
|
||||||
|
parts.Clear();
|
||||||
|
for( int i = 0; i < value.Length; i++ ) {
|
||||||
|
int nextAnd = value.IndexOf( '&', i );
|
||||||
|
int partLength = nextAnd == -1 ? value.Length - i : nextAnd - i;
|
||||||
|
|
||||||
|
if( partLength > 0 ) {
|
||||||
|
string part = value.Substring( i, partLength );
|
||||||
|
parts.Add( new DrawTextArgs( graphics, part, colours[code], shadow ) );
|
||||||
|
}
|
||||||
|
i += partLength + 1;
|
||||||
|
code = nextAnd == -1 ? 0 : Utils.ParseHex( value[nextAnd + 1] );
|
||||||
|
}
|
||||||
|
return parts;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ namespace ClassicalSharp {
|
|||||||
NameId = p.NameId;
|
NameId = p.NameId;
|
||||||
GroupName = p.GroupName;
|
GroupName = p.GroupName;
|
||||||
GroupRank = p.GroupRank;
|
GroupRank = p.GroupRank;
|
||||||
List<DrawTextArgs> parts = Utils.SplitText( graphics, Name, true );
|
List<DrawTextArgs> parts = Utils2D.SplitText( graphics, Name, true );
|
||||||
Size size = Utils2D.MeasureSize( Utils.StripColours( Name ), font, true );
|
Size size = Utils2D.MeasureSize( Utils.StripColours( Name ), font, true );
|
||||||
Texture = Utils2D.MakeTextTexture( parts, font, size, 0, 0 );
|
Texture = Utils2D.MakeTextTexture( parts, font, size, 0, 0 );
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ namespace ClassicalSharp {
|
|||||||
public PlayerInfo( IGraphicsApi graphics, Player p, Font font ) {
|
public PlayerInfo( IGraphicsApi graphics, Player p, Font font ) {
|
||||||
Name = p.DisplayName;
|
Name = p.DisplayName;
|
||||||
PlayerId = p.ID;
|
PlayerId = p.ID;
|
||||||
List<DrawTextArgs> parts = Utils.SplitText( graphics, Name, true );
|
List<DrawTextArgs> parts = Utils2D.SplitText( graphics, Name, true );
|
||||||
Size size = Utils2D.MeasureSize( Utils.StripColours( Name ), font, true );
|
Size size = Utils2D.MeasureSize( Utils.StripColours( Name ), font, true );
|
||||||
Texture = Utils2D.MakeTextTexture( parts, font, size, 0, 0 );
|
Texture = Utils2D.MakeTextTexture( parts, font, size, 0, 0 );
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ namespace ClassicalSharp {
|
|||||||
List<DrawTextArgs> parts = null;
|
List<DrawTextArgs> parts = null;
|
||||||
Size size = new Size( 0, defaultHeight );
|
Size size = new Size( 0, defaultHeight );
|
||||||
if( !String.IsNullOrEmpty( text ) ) {
|
if( !String.IsNullOrEmpty( text ) ) {
|
||||||
parts = Utils.SplitText( GraphicsApi, text, true );
|
parts = Utils2D.SplitText( GraphicsApi, text, true );
|
||||||
size = Utils2D.MeasureSize( Utils.StripColours( text ), font, true );
|
size = Utils2D.MeasureSize( Utils.StripColours( text ), font, true );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ namespace ClassicalSharp {
|
|||||||
public override void Init() {
|
public override void Init() {
|
||||||
X = 10;
|
X = 10;
|
||||||
DrawTextArgs caretArgs = new DrawTextArgs( GraphicsApi, "_", Color.White, false );
|
DrawTextArgs caretArgs = new DrawTextArgs( GraphicsApi, "_", Color.White, false );
|
||||||
chatCaretTexture = Utils2D.MakeTextTexture( boldFont, 0, 0, caretArgs );
|
chatCaretTexture = Utils2D.MakeTextTexture( boldFont, 0, 0, ref caretArgs );
|
||||||
|
|
||||||
if( chatInputText.Length == 0 ) {
|
if( chatInputText.Length == 0 ) {
|
||||||
caretPos = -1;
|
caretPos = -1;
|
||||||
@ -61,7 +61,7 @@ namespace ClassicalSharp {
|
|||||||
using( Graphics g = Graphics.FromImage( bmp ) ) {
|
using( Graphics g = Graphics.FromImage( bmp ) ) {
|
||||||
Utils2D.DrawRect( g, backColour, 0, 0, bmp.Width, bmp.Height );
|
Utils2D.DrawRect( g, backColour, 0, 0, bmp.Width, bmp.Height );
|
||||||
DrawTextArgs args = new DrawTextArgs( GraphicsApi, chatInputText, Color.White, false );
|
DrawTextArgs args = new DrawTextArgs( GraphicsApi, chatInputText, Color.White, false );
|
||||||
Utils2D.DrawText( g, font, args, 0, 0 );
|
Utils2D.DrawText( g, font, ref args, 0, 0 );
|
||||||
}
|
}
|
||||||
chatInputTexture = Utils2D.Make2DTexture( GraphicsApi, bmp, 10, y );
|
chatInputTexture = Utils2D.Make2DTexture( GraphicsApi, bmp, 10, y );
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ namespace ClassicalSharp {
|
|||||||
|
|
||||||
List<DrawTextArgs> parts = null;
|
List<DrawTextArgs> parts = null;
|
||||||
Size size = new Size( 0, defaultHeight );
|
Size size = new Size( 0, defaultHeight );
|
||||||
parts = Utils.SplitText( GraphicsApi, text, true );
|
parts = Utils2D.SplitText( GraphicsApi, text, true );
|
||||||
size = Utils2D.MeasureSize( Utils.StripColours( text ), font, true );
|
size = Utils2D.MeasureSize( Utils.StripColours( text ), font, true );
|
||||||
|
|
||||||
X = CalcAdjOffset( XOffset, Window.Width, size.Width, HorizontalDocking );
|
X = CalcAdjOffset( XOffset, Window.Width, size.Width, HorizontalDocking );
|
||||||
|
@ -23,7 +23,7 @@ namespace ClassicalSharp.Renderers {
|
|||||||
Graphics = window.Graphics;
|
Graphics = window.Graphics;
|
||||||
|
|
||||||
using( Font font = new Font( "Arial", 14 ) ) {
|
using( Font font = new Font( "Arial", 14 ) ) {
|
||||||
List<DrawTextArgs> parts = Utils.SplitText( Graphics, player.DisplayName, true );
|
List<DrawTextArgs> parts = Utils2D.SplitText( Graphics, player.DisplayName, true );
|
||||||
Size size = Utils2D.MeasureSize( Utils.StripColours( player.DisplayName ), font, true );
|
Size size = Utils2D.MeasureSize( Utils.StripColours( player.DisplayName ), font, true );
|
||||||
nameTexture = Utils2D.MakeTextTexture( parts, font, size, 0, 0 );
|
nameTexture = Utils2D.MakeTextTexture( parts, font, size, 0, 0 );
|
||||||
nameWidth = size.Width;
|
nameWidth = size.Width;
|
||||||
|
@ -54,42 +54,6 @@ namespace ClassicalSharp {
|
|||||||
return new String( output, 0, usedChars );
|
return new String( output, 0, usedChars );
|
||||||
}
|
}
|
||||||
|
|
||||||
static Color[] colours = new Color[] {
|
|
||||||
Color.FromArgb( 0, 0, 0 ), // black
|
|
||||||
Color.FromArgb( 0, 0, 191 ), // dark blue
|
|
||||||
Color.FromArgb( 0, 191, 0 ), // dark green
|
|
||||||
Color.FromArgb( 0, 191, 191 ), // dark teal
|
|
||||||
Color.FromArgb( 191, 0, 0 ), // dark red
|
|
||||||
Color.FromArgb( 191, 0, 191 ), // purple
|
|
||||||
Color.FromArgb( 191, 191, 0 ), // gold
|
|
||||||
Color.FromArgb( 191, 191, 191 ), // gray
|
|
||||||
Color.FromArgb( 64, 64, 64 ), // dark gray
|
|
||||||
Color.FromArgb( 64, 64, 255 ), // blue
|
|
||||||
Color.FromArgb( 64, 255, 64 ), // lime
|
|
||||||
Color.FromArgb( 64, 255, 255 ), // teal
|
|
||||||
Color.FromArgb( 255, 64, 64 ), // red
|
|
||||||
Color.FromArgb( 255, 64, 255 ), // pink
|
|
||||||
Color.FromArgb( 255, 255, 64 ), // yellow
|
|
||||||
Color.FromArgb( 255, 255, 255 ), // white
|
|
||||||
};
|
|
||||||
|
|
||||||
public static List<DrawTextArgs> SplitText( IGraphicsApi graphics, string value, bool shadow ) {
|
|
||||||
int code = 15;
|
|
||||||
List<DrawTextArgs> parts = new List<DrawTextArgs>();
|
|
||||||
for( int i = 0; i < value.Length; i++ ) {
|
|
||||||
int nextAnd = value.IndexOf( '&', i );
|
|
||||||
int partLength = nextAnd == -1 ? value.Length - i : nextAnd - i;
|
|
||||||
|
|
||||||
if( partLength > 0 ) {
|
|
||||||
string part = value.Substring( i, partLength );
|
|
||||||
parts.Add( new DrawTextArgs( graphics, part, colours[code], shadow ) );
|
|
||||||
}
|
|
||||||
i += partLength + 1;
|
|
||||||
code = nextAnd == -1 ? 0 : ParseHex( value[nextAnd + 1] );
|
|
||||||
}
|
|
||||||
return parts;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string ToHexString( byte[] array ) {
|
public static string ToHexString( byte[] array ) {
|
||||||
int len = array.Length;
|
int len = array.Length;
|
||||||
char[] hexadecimal = new char[len * 2];
|
char[] hexadecimal = new char[len * 2];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user