From f5a55c50b844544f1995bcc31f97364610693c94 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 27 May 2015 18:36:23 +1000 Subject: [PATCH] Use UnsafeString in BlockSelectScreen, fix not disposing Bitmap in TerrainAtlas2D.LoadTextureElement. --- 2D/Screens/BlockSelectScreen.cs | 36 ++++++++++++++++++++++++--------- Commands/DefaultCommands.cs | 13 +++++------- Utils/TerrainAtlas2D.cs | 13 ++++++------ Utils/UnsafeString.cs | 5 +++++ Utils/Utils.cs | 16 --------------- 5 files changed, 44 insertions(+), 39 deletions(-) diff --git a/2D/Screens/BlockSelectScreen.cs b/2D/Screens/BlockSelectScreen.cs index de43f0e70..3a066fc47 100644 --- a/2D/Screens/BlockSelectScreen.cs +++ b/2D/Screens/BlockSelectScreen.cs @@ -28,6 +28,7 @@ namespace ClassicalSharp { int rows; int startX, startY; readonly Font font; + UnsafeString buffer = new UnsafeString( 96 ); public override void Render( double delta ) { GraphicsApi.Texturing = true; @@ -93,13 +94,30 @@ namespace ClassicalSharp { } static readonly Color backColour = Color.FromArgb( 120, 60, 60, 60 ); - - string GetBlockInfo( Block block ) { - return String.Format( - "&f{0} (can place: {1}&f, can delete: {2}&f)", - Utils.GetSpacedBlockName( block ), - Window.CanPlace[(int)block] ? "&aYes" : "&cNo", - Window.CanDelete[(int)block] ? "&aYes" : "&cNo" ); + static readonly string[] blockNames = Enum.GetNames( typeof( Block ) ); + unsafe void UpdateBlockInfoString( Block block ) { + fixed( char* ptr = buffer.value ) { + char* ptr2 = ptr; + buffer.Clear( ptr ); + buffer.Append( ref ptr2, "&f" ); + if( block == Block.TNT ) { + buffer.Append( ref ptr2, "TNT" ); + } else { + string value = blockNames[(int)block]; + for( int i = 0; i < value.Length; i++ ) { + char c = value[i]; + if( Char.IsUpper( c ) && i > 0 ) { + buffer.Append( ref ptr2, ' ' ); + } + buffer.Append( ref ptr2, c ); + } + } + buffer.Append( ref ptr2, " (can place: " ); + buffer.Append( ref ptr2, Window.CanPlace[(int)block] ? "&aYes" : "&cNo" ); + buffer.Append( ref ptr2, "&f, can delete: " ); + buffer.Append( ref ptr2, Window.CanDelete[(int)block] ? "&aYes" : "&cNo" ); + buffer.Append( ref ptr2, "&f)" ); + } } void RecreateBlockInfoTexture() { @@ -107,8 +125,8 @@ namespace ClassicalSharp { if( selectedIndex == -1 ) return; Block block = blocksTable[selectedIndex].BlockId; - string text = GetBlockInfo( block ); - List parts = Utils2D.SplitText( GraphicsApi, text, true ); + UpdateBlockInfoString( block ); + List parts = Utils2D.SplitText( GraphicsApi, buffer.value, true ); Size size = Utils2D.MeasureSize( parts, font, true ); int x = startX + ( blockSize * blocksPerRow ) / 2 - size.Width / 2; int y = startY - size.Height; diff --git a/Commands/DefaultCommands.cs b/Commands/DefaultCommands.cs index 44227d635..801dd3f82 100644 --- a/Commands/DefaultCommands.cs +++ b/Commands/DefaultCommands.cs @@ -152,11 +152,10 @@ namespace ClassicalSharp.Commands { get { return new [] { "&a/client info [property]", - "&bproperties: &epos, target, dimensions, jumpheight", + "&bproperties: &epos, targetpos, dimensions, jumpheight", }; } - } - + } public override void Execute( CommandReader reader ) { string property = reader.Next(); @@ -165,14 +164,12 @@ namespace ClassicalSharp.Commands { } else if( property == "pos" ) { Window.AddChat( "Feet: " + Window.LocalPlayer.Position ); Window.AddChat( "Eye: " + Window.LocalPlayer.EyePosition ); - } else if( property == "target" ) { + } else if( property == "targetpos" ) { PickedPos pos = Window.SelectedPos; if( !pos.Valid ) { - Window.AddChat( "no target pos" ); + Window.AddChat( "Currently not targeting a block" ); } else { - Block block = (Block)Window.Map.GetBlock( pos.BlockPos ); - Window.AddChat( "target pos: " + pos.BlockPos ); - Window.AddChat( "target block: " + Utils.GetSpacedBlockName( block ) ); + Window.AddChat( "Currently targeting: " + pos.BlockPos ); } } else if( property == "dimensions" ) { Window.AddChat( "map width: " + Window.Map.Width ); diff --git a/Utils/TerrainAtlas2D.cs b/Utils/TerrainAtlas2D.cs index 570fe1523..f91cbfe08 100644 --- a/Utils/TerrainAtlas2D.cs +++ b/Utils/TerrainAtlas2D.cs @@ -19,7 +19,7 @@ namespace ClassicalSharp { public const float invElementSize = 0.0625f; public readonly int UsedRowsCount = 5; public Bitmap AtlasBitmap; - public int elementSize; + public int elementSize; public IGraphicsApi GraphicsApi; public int TexId; @@ -36,10 +36,11 @@ namespace ClassicalSharp { int x = index & 0x0F; int y = index >> 4; using( FastBitmap atlas = new FastBitmap( AtlasBitmap, true ) ) { - Bitmap bmp = new Bitmap( elementSize, elementSize ); - using( FastBitmap dst = new FastBitmap( bmp, true ) ) { - Utils.MovePortion( x * elementSize, y * elementSize, 0, 0, atlas, dst, elementSize ); - return GraphicsApi.LoadTexture( dst ); + using( Bitmap bmp = new Bitmap( elementSize, elementSize ) ) { + using( FastBitmap dst = new FastBitmap( bmp, true ) ) { + Utils.MovePortion( x * elementSize, y * elementSize, 0, 0, atlas, dst, elementSize ); + return GraphicsApi.LoadTexture( dst ); + } } } } @@ -57,7 +58,7 @@ namespace ClassicalSharp { GraphicsApi.DeleteTexture( ref TexId ); } - static ushort[] rowFlags = { 0xFFFF, 0xFFEE, 0xFFE0, 0xFFE0, 0xFFFF, 0xFA00 }; + static ushort[] rowFlags = { 0xFFFF, 0xFFEE, 0xFFE0, 0xFFE0, 0xFFFF, 0xFA00 }; void MakeOptimisedTexture( FastBitmap atlas ) { int srcIndex = 0, destIndex = 0; diff --git a/Utils/UnsafeString.cs b/Utils/UnsafeString.cs index d5425d44f..910c8087e 100644 --- a/Utils/UnsafeString.cs +++ b/Utils/UnsafeString.cs @@ -36,6 +36,11 @@ namespace ClassicalSharp { return this; } + public UnsafeString Append( ref char* ptr, char c) { + *ptr++ = c; + return this; + } + static char[] numBuffer = new char[20]; public UnsafeString AppendNum( ref char* ptr, long num ) { int index = 0; diff --git a/Utils/Utils.cs b/Utils/Utils.cs index 8b4baa0c8..3d0f2419e 100644 --- a/Utils/Utils.cs +++ b/Utils/Utils.cs @@ -142,22 +142,6 @@ namespace ClassicalSharp { return new Vector3( (float)x, (float)y, (float)z ); } - static string SplitUppercase( string value ) { - StringBuilder buffer = new StringBuilder( value.Length + 3 ); - for( int i = 0; i < value.Length; i++ ) { - char c = value[i]; - if( Char.IsUpper( c ) && buffer.Length > 0 ) { - buffer.Append( ' ' ); - } - buffer.Append( c ); - } - return buffer.ToString(); - } - - public static string GetSpacedBlockName( Block block ) { - return block == Block.TNT ? "TNT" : SplitUppercase( block.ToString() ); - } - public static void LogWarning( string text ) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine( text );