From 0f87b7feb539da80605329da221337b725a1ec4e Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 21 Jan 2016 21:19:23 +1100 Subject: [PATCH] Specify whether the FastBitmap instance is readonly or not. (Mainly for android) --- .../2D/Drawing/GdiPlusDrawer2D.Text.cs | 2 +- ClassicalSharp/2D/Drawing/IDrawer2D.TextMC.cs | 2 +- ClassicalSharp/2D/Utils/FastBitmap.cs | 29 ++++++++++++------- ClassicalSharp/Blocks/BlockInfo.BoundsBox.cs | 10 ++++--- ClassicalSharp/Entities/Player.cs | 2 +- ClassicalSharp/GraphicsAPI/OpenGLApi.cs | 10 +++---- .../Network/NetworkProcessor.CPE.cs | 6 ++-- ClassicalSharp/Platform/IPlatformWindow.cs | 4 ++- ClassicalSharp/TexturePack/Animations.cs | 4 +-- ClassicalSharp/TexturePack/TerrainAtlas1D.cs | 20 ++++++------- ClassicalSharp/TexturePack/TerrainAtlas2D.cs | 24 +++++++-------- 11 files changed, 60 insertions(+), 53 deletions(-) diff --git a/ClassicalSharp/2D/Drawing/GdiPlusDrawer2D.Text.cs b/ClassicalSharp/2D/Drawing/GdiPlusDrawer2D.Text.cs index c5aa537fb..3b6dfdaa0 100644 --- a/ClassicalSharp/2D/Drawing/GdiPlusDrawer2D.Text.cs +++ b/ClassicalSharp/2D/Drawing/GdiPlusDrawer2D.Text.cs @@ -71,7 +71,7 @@ namespace ClassicalSharp { if( !args.SkipPartsCheck ) GetTextParts( args.Text ); - using( FastBitmap fastBmp = new FastBitmap( curBmp, true ) ) + using( FastBitmap fastBmp = new FastBitmap( curBmp, true, false ) ) DrawBitmapTextImpl( fastBmp, ref args, x, y ); } diff --git a/ClassicalSharp/2D/Drawing/IDrawer2D.TextMC.cs b/ClassicalSharp/2D/Drawing/IDrawer2D.TextMC.cs index 8d23f796a..a914cf9e9 100644 --- a/ClassicalSharp/2D/Drawing/IDrawer2D.TextMC.cs +++ b/ClassicalSharp/2D/Drawing/IDrawer2D.TextMC.cs @@ -18,7 +18,7 @@ namespace ClassicalSharp { public void SetFontBitmap( Bitmap bmp ) { FontBitmap = bmp; boxSize = FontBitmap.Width / 16; - fontPixels = new FastBitmap( FontBitmap, true ); + fontPixels = new FastBitmap( FontBitmap, true, true ); CalculateTextWidths(); } diff --git a/ClassicalSharp/2D/Utils/FastBitmap.cs b/ClassicalSharp/2D/Utils/FastBitmap.cs index bf835347a..c4de26f3e 100644 --- a/ClassicalSharp/2D/Utils/FastBitmap.cs +++ b/ClassicalSharp/2D/Utils/FastBitmap.cs @@ -11,22 +11,28 @@ namespace ClassicalSharp { /// Wrapper around a bitmap that allows extremely fast manipulation of 32bpp images. public unsafe class FastBitmap : IDisposable { - public FastBitmap( Bitmap bmp, bool lockBits ) { - Bitmap = bmp; - if( lockBits ) { - LockBits(); - } + [Obsolete( "You should always specify whether the bitmap is readonly or not." )] + public FastBitmap( Bitmap bmp, bool lockBits ) : this( bmp, lockBits, false ) { } - public FastBitmap( int width, int height, int stride, IntPtr scan0 ) { + public FastBitmap( Bitmap bmp, bool lockBits, bool readOnly ) { + Bitmap = bmp; + if( lockBits ) + LockBits(); + ReadOnly = readOnly; + } + + public FastBitmap( int width, int height, int stride, IntPtr scan0, bool readOnly ) { Width = width; Height = height; Stride = stride; Scan0 = scan0; scan0Byte = (byte*)scan0; + ReadOnly = readOnly; } public Bitmap Bitmap; + public bool ReadOnly; BitmapData data; byte* scan0Byte; @@ -51,15 +57,15 @@ namespace ClassicalSharp { for( int y = 0; y < size; y++ ) { int* srcRow = src.GetRowPtr( srcY + y ); int* dstRow = dst.GetRowPtr( dstY + y ); - for( int x = 0; x < size; x++ ) { + for( int x = 0; x < size; x++ ) dstRow[dstX + x] = srcRow[srcX + x]; - } } } public void Dispose() { UnlockBits(); } #if !ANDROID + public void LockBits() { if( Bitmap == null ) throw new InvalidOperationException( "Underlying bitmap is null." ); if( data != null ) return; @@ -70,8 +76,8 @@ namespace ClassicalSharp { Rectangle rec = new Rectangle( 0, 0, Bitmap.Width, Bitmap.Height ); data = Bitmap.LockBits( rec, ImageLockMode.ReadWrite, format ); - scan0Byte = (byte*)data.Scan0; Scan0 = data.Scan0; + scan0Byte = (byte*)Scan0; Stride = data.Stride; Width = data.Width; Height = data.Height; @@ -95,8 +101,8 @@ namespace ClassicalSharp { data = ByteBuffer.AllocateDirect( Bitmap.Width * Bitmap.Height * 4 ); Bitmap.CopyPixelsToBuffer( data ); - scan0Byte = (byte*)data.GetDirectBufferAddress(); Scan0 = data.GetDirectBufferAddress(); + scan0Byte = (byte*)Scan0; Stride = Bitmap.Width * 4; Width = Bitmap.Width; Height = Bitmap.Height; @@ -106,7 +112,8 @@ namespace ClassicalSharp { if( Bitmap == null || data == null ) return; data.Rewind(); - Bitmap.CopyPixelsFromBuffer( data ); // TODO: Only if not readonly + if( !ReadOnly ) + Bitmap.CopyPixelsFromBuffer( data ); data.Dispose(); data = null; diff --git a/ClassicalSharp/Blocks/BlockInfo.BoundsBox.cs b/ClassicalSharp/Blocks/BlockInfo.BoundsBox.cs index df2eea218..b283c83f1 100644 --- a/ClassicalSharp/Blocks/BlockInfo.BoundsBox.cs +++ b/ClassicalSharp/Blocks/BlockInfo.BoundsBox.cs @@ -45,10 +45,12 @@ namespace ClassicalSharp { internal void RecalculateBB( int block, FastBitmap fastBmp ) { int elemSize = fastBmp.Width / 16; int texId = GetTextureLoc( (byte)block, TileSide.Top ); - float topY = GetSpriteBB_TopY( elemSize, texId & 0x0F, texId >> 4, fastBmp ); - float bottomY = GetSpriteBB_BottomY( elemSize, texId & 0x0F, texId >> 4, fastBmp ); - float leftX = GetSpriteBB_LeftX( elemSize, texId & 0x0F, texId >> 4, fastBmp ); - float rightX = GetSpriteBB_RightX( elemSize, texId & 0x0F, texId >> 4, fastBmp ); + int texX = texId & 0x0F, texY = texId >> 4; + + float topY = GetSpriteBB_TopY( elemSize, texX, texY, fastBmp ); + float bottomY = GetSpriteBB_BottomY( elemSize, texX, texY, fastBmp ); + float leftX = GetSpriteBB_LeftX( elemSize, texX, texY, fastBmp ); + float rightX = GetSpriteBB_RightX( elemSize, texX, texY, fastBmp ); MinBB[block] = Utils.RotateY( leftX - 0.5f, bottomY, 0, angle ) + centre; MaxBB[block] = Utils.RotateY( rightX - 0.5f, topY, 0, angle ) + centre; diff --git a/ClassicalSharp/Entities/Player.cs b/ClassicalSharp/Entities/Player.cs index 48c8f92ef..7eef5454b 100644 --- a/ClassicalSharp/Entities/Player.cs +++ b/ClassicalSharp/Entities/Player.cs @@ -87,7 +87,7 @@ namespace ClassicalSharp { } unsafe static void ClearHat( Bitmap bmp, SkinType skinType ) { - using( FastBitmap fastBmp = new FastBitmap( bmp, true ) ) { + using( FastBitmap fastBmp = new FastBitmap( bmp, true, false ) ) { int sizeX = (bmp.Width / 64) * 32; int yScale = skinType == SkinType.Type64x32 ? 32 : 64; int sizeY = (bmp.Height / yScale) * 16; diff --git a/ClassicalSharp/GraphicsAPI/OpenGLApi.cs b/ClassicalSharp/GraphicsAPI/OpenGLApi.cs index cf4df8f69..aece26c84 100644 --- a/ClassicalSharp/GraphicsAPI/OpenGLApi.cs +++ b/ClassicalSharp/GraphicsAPI/OpenGLApi.cs @@ -363,14 +363,14 @@ namespace ClassicalSharp.GraphicsAPI { #endregion - public override void BeginFrame( GameWindow game ) { + public override void BeginFrame( Game game ) { } - public override void EndFrame( GameWindow game ) { - game.SwapBuffers(); + public override void EndFrame( Game game ) { + game.window.SwapBuffers(); } - public override void SetVSync( GameWindow game, bool value ) { + public override void SetVSync( Game game, bool value ) { game.VSync = value; } @@ -414,7 +414,7 @@ namespace ClassicalSharp.GraphicsAPI { } } - public override void OnWindowResize( GameWindow game ) { + public override void OnWindowResize( Game game ) { GL.Viewport( 0, 0, game.Width, game.Height ); } diff --git a/ClassicalSharp/Network/NetworkProcessor.CPE.cs b/ClassicalSharp/Network/NetworkProcessor.CPE.cs index 27a382490..3509b7f15 100644 --- a/ClassicalSharp/Network/NetworkProcessor.CPE.cs +++ b/ClassicalSharp/Network/NetworkProcessor.CPE.cs @@ -412,10 +412,8 @@ namespace ClassicalSharp { HandleCpeDefineBlockCommonEnd( block ); // Update sprite BoundingBox if necessary if( info.IsSprite[block] ) { - using( FastBitmap fastBmp = - new FastBitmap( game.TerrainAtlas.AtlasBitmap, true ) ) { - info.RecalculateBB( block, fastBmp ); - } + using( FastBitmap dst = new FastBitmap( game.TerrainAtlas.AtlasBitmap, true, true ) ) + info.RecalculateBB( block, dst ); } } diff --git a/ClassicalSharp/Platform/IPlatformWindow.cs b/ClassicalSharp/Platform/IPlatformWindow.cs index fe02c7245..070d4c0aa 100644 --- a/ClassicalSharp/Platform/IPlatformWindow.cs +++ b/ClassicalSharp/Platform/IPlatformWindow.cs @@ -40,7 +40,9 @@ namespace ClassicalSharp { void Run(); - void Exit(); + void SwapBuffers(); + + void Exit(); event EventHandler KeyPress; } diff --git a/ClassicalSharp/TexturePack/Animations.cs b/ClassicalSharp/TexturePack/Animations.cs index 5a1ee58c9..14904c9f3 100644 --- a/ClassicalSharp/TexturePack/Animations.cs +++ b/ClassicalSharp/TexturePack/Animations.cs @@ -28,7 +28,7 @@ namespace ClassicalSharp.TexturePack { public void SetAtlas( Bitmap bmp ) { Dispose(); this.bmp = bmp; - fastBmp = new FastBitmap( bmp, true ); + fastBmp = new FastBitmap( bmp, true, true ); } /// Runs through all animations and if necessary updates the terrain atlas. @@ -102,7 +102,7 @@ namespace ClassicalSharp.TexturePack { int size = data.FrameSize; byte* temp = stackalloc byte[size * size * 4]; - FastBitmap part = new FastBitmap( size, size, size * 4, (IntPtr)temp ); + FastBitmap part = new FastBitmap( size, size, size * 4, (IntPtr)temp, false ); FastBitmap.MovePortion( data.FrameX + data.CurrentState * size, data.FrameY, 0, 0, fastBmp, part, size ); api.UpdateTexturePart( atlas.TexIds[index], 0, rowNum * game.TerrainAtlas.elementSize, part ); } diff --git a/ClassicalSharp/TexturePack/TerrainAtlas1D.cs b/ClassicalSharp/TexturePack/TerrainAtlas1D.cs index c46e4c5dc..5147b3af8 100644 --- a/ClassicalSharp/TexturePack/TerrainAtlas1D.cs +++ b/ClassicalSharp/TexturePack/TerrainAtlas1D.cs @@ -49,12 +49,12 @@ namespace ClassicalSharp { invElementSize = 1f / elementsPerBitmap; } - void Convert2DTo1D( TerrainAtlas2D atlas2D, int atlasesCount, int atlas1DHeight ) { + void Convert2DTo1D( TerrainAtlas2D atlas2D, int atlasesCount, int atlas1DHeight ) { TexIds = new int[atlasesCount]; Utils.LogDebug( "Loaded new atlas: {0} bmps, {1} per bmp", atlasesCount, elementsPerAtlas1D ); int index = 0; - using( FastBitmap atlas = new FastBitmap( atlas2D.AtlasBitmap, true ) ) { + using( FastBitmap atlas = new FastBitmap( atlas2D.AtlasBitmap, true, true ) ) { for( int i = 0; i < TexIds.Length; i++ ) Make1DTexture( i, atlas, atlas2D, atlas1DHeight, ref index ); } @@ -62,15 +62,15 @@ namespace ClassicalSharp { void Make1DTexture( int i, FastBitmap atlas, TerrainAtlas2D atlas2D, int atlas1DHeight, ref int index ) { int elemSize = atlas2D.elementSize; - using( Bitmap atlas1d = new Bitmap( atlas2D.elementSize, atlas1DHeight ) ) { - using( FastBitmap dst = new FastBitmap( atlas1d, true ) ) { - for( int index1D = 0; index1D < elementsPerAtlas1D; index1D++ ) { - FastBitmap.MovePortion( (index & 0x0F) * elemSize, (index >> 4) * elemSize, - 0, index1D * elemSize, atlas, dst, elemSize ); - index++; - } - TexIds[i] = graphics.CreateTexture( dst ); + using( Bitmap atlas1d = new Bitmap( atlas2D.elementSize, atlas1DHeight ) ) + using( FastBitmap dst = new FastBitmap( atlas1d, true, false ) ) + { + for( int index1D = 0; index1D < elementsPerAtlas1D; index1D++ ) { + FastBitmap.MovePortion( (index & 0x0F) * elemSize, (index >> 4) * elemSize, + 0, index1D * elemSize, atlas, dst, elemSize ); + index++; } + TexIds[i] = graphics.CreateTexture( dst ); } } diff --git a/ClassicalSharp/TexturePack/TerrainAtlas2D.cs b/ClassicalSharp/TexturePack/TerrainAtlas2D.cs index ef6348b4c..e122eb1f4 100644 --- a/ClassicalSharp/TexturePack/TerrainAtlas2D.cs +++ b/ClassicalSharp/TexturePack/TerrainAtlas2D.cs @@ -43,7 +43,7 @@ namespace ClassicalSharp { AtlasBitmap = bmp; elementSize = bmp.Width >> 4; - using( FastBitmap fastBmp = new FastBitmap( bmp, true ) ) { + using( FastBitmap fastBmp = new FastBitmap( bmp, true, true ) ) { info.RecalculateSpriteBB( fastBmp ); TexId = graphics.CreateTexture( fastBmp ); } @@ -51,27 +51,25 @@ namespace ClassicalSharp { /// Creates a new texture that contains the tile at the specified index. public int LoadTextureElement( int index ) { - int size = elementSize; - using( FastBitmap atlas = new FastBitmap( AtlasBitmap, true ) ) { - using( Bitmap bmp = new Bitmap( size, size ) ) { - - using( FastBitmap dst = new FastBitmap( bmp, true ) ) { - FastBitmap.MovePortion( (index & 0x0F) * size, (index >> 4) * - size, 0, 0, atlas, dst, size ); - return graphics.CreateTexture( dst ); - } - } + int size = elementSize; + using( FastBitmap atlas = new FastBitmap( AtlasBitmap, true, true ) ) + using( Bitmap bmp = new Bitmap( size, size ) ) + using( FastBitmap dst = new FastBitmap( bmp, true, false ) ) + { + FastBitmap.MovePortion( (index & 0x0F) * size, (index >> 4) * + size, 0, 0, atlas, dst, size ); + return graphics.CreateTexture( dst ); } } - /// Gets a rectangle that describes the UV coordinates for + /// Gets a rectangle that describes the UV coordinates for /// the tile at the specified index. public TextureRec GetTexRec( int index ) { return new TextureRec( (index & 0x0F) * invElementSize, (index >> 4) * invElementSize, invElementSize, invElementSize ); } - /// Gets a rectangle that describes the UV coordinates for + /// Gets a rectangle that describes the UV coordinates for /// the tile at the specified index, adjusted to work for AMD/ATI cards. public TextureRec GetAdjTexRec( int index ) { // Adjust coords to be slightly inside - fixes issues with AMD/ATI cards.