From 44317203113f65ae9e56f1daf4df212fc19a22ad Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 16 Nov 2015 17:00:50 +1100 Subject: [PATCH] Optimise text input bar - don't recreate caret texture unless necessary. --- .../2D/Drawing/GdiPlusDrawer2D.Text.cs | 2 +- ClassicalSharp/2D/Drawing/IDrawer2D.cs | 8 +++----- ClassicalSharp/2D/Screens/ChatScreen.cs | 2 +- .../2D/Widgets/Chat/TextInputWidget.cs | 17 +++++++++++------ 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/ClassicalSharp/2D/Drawing/GdiPlusDrawer2D.Text.cs b/ClassicalSharp/2D/Drawing/GdiPlusDrawer2D.Text.cs index 32768ead0..3b042a8d2 100644 --- a/ClassicalSharp/2D/Drawing/GdiPlusDrawer2D.Text.cs +++ b/ClassicalSharp/2D/Drawing/GdiPlusDrawer2D.Text.cs @@ -65,7 +65,7 @@ namespace ClassicalSharp { public override Size MeasureSize( ref DrawTextArgs args ) { GetTextParts( args.Text ); - if( parts.Count == 0 ) + if( parts.Count == 0 ) return Size.Empty; SizeF total = SizeF.Empty; diff --git a/ClassicalSharp/2D/Drawing/IDrawer2D.cs b/ClassicalSharp/2D/Drawing/IDrawer2D.cs index 91efe4cb4..701060dae 100644 --- a/ClassicalSharp/2D/Drawing/IDrawer2D.cs +++ b/ClassicalSharp/2D/Drawing/IDrawer2D.cs @@ -135,7 +135,7 @@ namespace ClassicalSharp { public abstract void DisposeInstance(); /// Creates a 2D texture with origin at the specified window coordinates. - public Texture Make2DTexture( Bitmap bmp, Size used, int windowX, int windowY ) { + public Texture Make2DTexture( Bitmap bmp, Size used, int windowX, int windowY ) { int texId = graphics.CreateTexture( bmp ); return new Texture( texId, windowX, windowY, used.Width, used.Height, (float)used.Width / bmp.Width, (float)used.Height / bmp.Height ); @@ -184,11 +184,9 @@ namespace ClassicalSharp { i += partLength + 1; if( nextAnd >= 0 && nextAnd + 1 < value.Length ) { - try { - code = Utils.ParseHex( value[nextAnd + 1] ); - } catch( FormatException ) { + if( !Utils.TryParseHex( value[nextAnd + 1], out code ) ) { code = 0xF; - i--; // include the character that isn't a colour code. + i--;// include the character that isn't a colour code. } } } diff --git a/ClassicalSharp/2D/Screens/ChatScreen.cs b/ClassicalSharp/2D/Screens/ChatScreen.cs index a79e2386d..a8c3e3e66 100644 --- a/ClassicalSharp/2D/Screens/ChatScreen.cs +++ b/ClassicalSharp/2D/Screens/ChatScreen.cs @@ -163,7 +163,7 @@ namespace ClassicalSharp { announcementFont.Dispose(); normalChat.Dispose(); - textInput.Dispose(); + textInput.DisposeFully(); status.Dispose(); bottomRight.Dispose(); graphicsApi.DeleteTexture( ref announcementTex ); diff --git a/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs b/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs index c3c7557d6..ef9b576b4 100644 --- a/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs +++ b/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs @@ -21,6 +21,9 @@ namespace ClassicalSharp { defaultWidth = defSize.Width; defaultHeight = defSize.Height; Width = defaultWidth; Height = defaultHeight; + args = new DrawTextArgs( "_", boldFont, true ); + caretTex = game.Drawer2D.MakeChatTextTexture( ref args, 0, 0 ); + this.font = font; this.boldFont = boldFont; altText = new AltTextInputWidget( game, font, boldFont, this ); @@ -65,12 +68,10 @@ namespace ClassicalSharp { public override void Init() { X = 5; - DrawTextArgs args = new DrawTextArgs( "_", boldFont, true ); - caretTex = game.Drawer2D.MakeChatTextTexture( ref args, 0, 0 ); - chatInputText.WordWrap( ref parts, ref partLens, 64 ); + chatInputText.WordWrap( ref parts, ref partLens, 64 ); maxWidth = 0; - args = new DrawTextArgs( null, font, true ); + DrawTextArgs args = new DrawTextArgs( null, font, true ); for( int i = 0; i < lines; i++ ) { args.Text = parts[i]; sizes[i] = game.Drawer2D.MeasureChatSize( ref args ); @@ -150,10 +151,14 @@ namespace ClassicalSharp { Width = size.Width; } - public override void Dispose() { - graphicsApi.DeleteTexture( ref caretTex ); + public override void Dispose() { graphicsApi.DeleteTexture( ref inputTex ); } + + public void DisposeFully() { + Dispose(); + graphicsApi.DeleteTexture( ref caretTex ); + } public override void MoveTo( int newX, int newY ) { int diffX = newX - X, diffY = newY - Y;