Use UnsafeString in BlockSelectScreen, fix not disposing Bitmap in TerrainAtlas2D.LoadTextureElement.

This commit is contained in:
UnknownShadow200 2015-05-27 18:36:23 +10:00
parent d65ba5b226
commit f5a55c50b8
5 changed files with 44 additions and 39 deletions

View File

@ -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<DrawTextArgs> parts = Utils2D.SplitText( GraphicsApi, text, true );
UpdateBlockInfoString( block );
List<DrawTextArgs> 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;

View File

@ -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 );

View File

@ -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;

View File

@ -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;

View File

@ -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 );