From 660daa26d0a883d4a9a4efaa1b23486cf4d76d77 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 22 May 2015 19:38:07 +1000 Subject: [PATCH] Use power of two textures for 2D operations, replace code to draw bounds of rectangles with single method in Utils2D. --- 2D/Screens/BlockSelectScreen.cs | 13 ++++++------- 2D/Screens/ChatScreen.cs | 4 ++-- 2D/Screens/LoadingMapScreen.cs | 8 ++++---- 2D/Utils2D.cs | 31 ++++++++++++++++--------------- 2D/Widgets/BlockHotbarWidget.cs | 9 ++++----- 2D/Widgets/TextInputWidget.cs | 4 ++-- Utils/Utils.cs | 4 ++++ 7 files changed, 38 insertions(+), 35 deletions(-) diff --git a/2D/Screens/BlockSelectScreen.cs b/2D/Screens/BlockSelectScreen.cs index 520f4fe40..7f550b040 100644 --- a/2D/Screens/BlockSelectScreen.cs +++ b/2D/Screens/BlockSelectScreen.cs @@ -74,13 +74,12 @@ namespace ClassicalSharp { public override void Init() { Window.BlockPermissionsChanged += BlockPermissionsChanged; - using( Bitmap bmp = new Bitmap( blockSize, blockSize ) ) { + Size size = new Size( blockSize, blockSize ); + using( Bitmap bmp = Utils2D.CreatePow2Bitmap( size ) ) { using( Graphics g = Graphics.FromImage( bmp ) ) { - using( Pen pen = new Pen( Color.White, blockSize / 8 ) ) { - g.DrawRectangle( pen, 0, 0, blockSize, blockSize ); - } + Utils2D.DrawRectBounds( g, Color.White, blockSize / 8, 0, 0, blockSize, blockSize ); } - selectedBlock = Utils2D.Make2DTexture( GraphicsApi, bmp, 0, 0 ); + selectedBlock = Utils2D.Make2DTexture( GraphicsApi, bmp, size, 0, 0 ); } RecreateBlockTextures(); } @@ -114,12 +113,12 @@ namespace ClassicalSharp { int x = startX + ( blockSize * blocksPerRow ) / 2 - size.Width / 2; int y = startY - size.Height; - using( Bitmap bmp = new Bitmap( size.Width, size.Height ) ) { + using( Bitmap bmp = Utils2D.CreatePow2Bitmap( size ) ) { using( Graphics g = Graphics.FromImage( bmp ) ) { Utils2D.DrawRect( g, backColour, 0, 0, bmp.Width, bmp.Height ); Utils2D.DrawText( g, parts, font, 0, 0 ); } - blockInfoTexture = Utils2D.Make2DTexture( GraphicsApi, bmp, x, y ); + blockInfoTexture = Utils2D.Make2DTexture( GraphicsApi, bmp, size, x, y ); } } diff --git a/2D/Screens/ChatScreen.cs b/2D/Screens/ChatScreen.cs index 47aa1d284..5075c8265 100644 --- a/2D/Screens/ChatScreen.cs +++ b/2D/Screens/ChatScreen.cs @@ -229,13 +229,13 @@ namespace ClassicalSharp { Size size = Utils2D.MeasureSize( text, historyFont, false ); int y = normalChat.CalcUsedY() - size.Height; - using( Bitmap bmp = new Bitmap( size.Width, size.Height ) ) { + using( Bitmap bmp = Utils2D.CreatePow2Bitmap( size ) ) { using( Graphics g = Graphics.FromImage( bmp ) ) { Utils2D.DrawRect( g, backColour, 0, 0, bmp.Width, bmp.Height ); DrawTextArgs args = new DrawTextArgs( GraphicsApi, text, Color.Yellow, false ); Utils2D.DrawText( g, historyFont, ref args, 0, 0 ); } - pageTexture = Utils2D.Make2DTexture( GraphicsApi, bmp, 10, y ); + pageTexture = Utils2D.Make2DTexture( GraphicsApi, bmp, size, 10, y ); } } diff --git a/2D/Screens/LoadingMapScreen.cs b/2D/Screens/LoadingMapScreen.cs index 3ffdb2111..a744079f2 100644 --- a/2D/Screens/LoadingMapScreen.cs +++ b/2D/Screens/LoadingMapScreen.cs @@ -32,14 +32,14 @@ namespace ClassicalSharp { titleWidget = TextWidget.Create( Window, 0, 30, serverName, Docking.Centre, Docking.LeftOrTop, font ); messageWidget = TextWidget.Create( Window, 0, 60, serverMotd, Docking.Centre, Docking.LeftOrTop, font ); progX = Window.Width / 2f - progWidth / 2f; - using( Bitmap bmp = new Bitmap( progWidth, progHeight ) ) { + + Size size = new Size( progWidth, progHeight ); + using( Bitmap bmp = Utils2D.CreatePow2Bitmap( size ) ) { using( Graphics g = Graphics.FromImage( bmp ) ) { Utils2D.DrawRectBounds( g, Color.White, 5f, 0, 0, progWidth, progHeight ); } - progressBoxTexture = Utils2D.Make2DTexture( GraphicsApi, bmp, 0, 0 ); + progressBoxTexture = Utils2D.Make2DTexture( GraphicsApi, bmp, size, (int)progX, (int)progY ); } - progressBoxTexture.X1 = (int)progX; - progressBoxTexture.Y1 = (int)progY; Window.MapLoading += MapLoading; } diff --git a/2D/Utils2D.cs b/2D/Utils2D.cs index 80be40b96..5c8843d0c 100644 --- a/2D/Utils2D.cs +++ b/2D/Utils2D.cs @@ -39,6 +39,10 @@ namespace ClassicalSharp { return brush; } + public static Bitmap CreatePow2Bitmap( Size size ) { + return new Bitmap( Utils.NextPowerOf2( size.Width ), Utils.NextPowerOf2( size.Height ) ); + } + const float shadowOffset = 1.3f; public static Size MeasureSize( string text, Font font, bool shadow ) { SizeF size = measuringGraphics.MeasureString( text, font, Int32.MaxValue, format ); @@ -96,35 +100,31 @@ namespace ClassicalSharp { public static Texture MakeTextTexture( Font font, int x1, int y1, ref DrawTextArgs args ) { Size size = MeasureSize( args.Text, font, args.UseShadow ); - using( Bitmap bmp = new Bitmap( size.Width, size.Height ) ) { + using( Bitmap bmp = CreatePow2Bitmap( size ) ) { using( Graphics g = Graphics.FromImage( bmp ) ) { DrawText( g, font, ref args, 0, 0 ); } - return Make2DTexture( args.Graphics, bmp, x1, y1 ); + return Make2DTexture( args.Graphics, bmp, size, x1, y1 ); } } public static Texture MakeTextTexture( List parts, Font font, Size size, int x1, int y1 ) { if( parts.Count == 0 ) return new Texture( -1, x1, y1, 0, 0, 1, 1 ); - using( Bitmap bmp = new Bitmap( size.Width, size.Height ) ) { + using( Bitmap bmp = CreatePow2Bitmap( size ) ) { using( Graphics g = Graphics.FromImage( bmp ) ) { DrawText( g, parts, font, 0, 0 ); } - return Make2DTexture( parts[0].Graphics, bmp, x1, y1 ); + return Make2DTexture( parts[0].Graphics, bmp, size, x1, y1 ); } } - public static Texture Make2DTexture( IGraphicsApi graphics, Bitmap bmp, int x1, int y1 ) { - if( graphics.SupportsNonPowerOf2Textures ) { - int textureID = graphics.LoadTexture( bmp ); - return new Texture( textureID, x1, y1, bmp.Width, bmp.Height, 1f, 1f ); - } else { - using( Bitmap adjBmp = ResizeToPower2( bmp ) ) { - int textureID = graphics.LoadTexture( adjBmp ); - return new Texture( textureID, x1, y1, bmp.Width, bmp.Height, - (float)bmp.Width / adjBmp.Width, (float)bmp.Height / adjBmp.Height ); - } - } + public static Texture Make2DTexture( IGraphicsApi graphics, Bitmap bmp, Size used, int x1, int y1 ) { + int textureID = graphics.LoadTexture( bmp ); + if( !Utils.IsPowerOf2( bmp.Width ) || !Utils.IsPowerOf2( bmp.Height ) ) + Utils.LogWarning( "Creating a non power of two texture." ); + + return new Texture( textureID, x1, y1, used.Width, used.Height, + (float)used.Width / bmp.Width, (float)used.Height / bmp.Height ); } public static Bitmap ResizeToPower2( Bitmap bmp ) { @@ -137,6 +137,7 @@ namespace ClassicalSharp { return adjBmp; } + public static void Dispose() { measuringBmp.Dispose(); measuringGraphics.Dispose(); diff --git a/2D/Widgets/BlockHotbarWidget.cs b/2D/Widgets/BlockHotbarWidget.cs index 7753d7f16..3394c75d2 100644 --- a/2D/Widgets/BlockHotbarWidget.cs +++ b/2D/Widgets/BlockHotbarWidget.cs @@ -27,13 +27,12 @@ namespace ClassicalSharp { public override void Init() { int y = Window.Height - blockSize; - using( Bitmap bmp = new Bitmap( blockSize, blockSize ) ) { + Size size = new Size( 32, 32 ); + using( Bitmap bmp = Utils2D.CreatePow2Bitmap( size ) ) { using( Graphics g = Graphics.FromImage( bmp ) ) { - using( Pen pen = new Pen( Color.White, blockSize / 8 ) ) { - g.DrawRectangle( pen, 0, 0, blockSize, blockSize ); - } + Utils2D.DrawRectBounds( g, Color.White, blockSize / 8, 0, 0, blockSize, blockSize ); } - selectedBlock = Utils2D.Make2DTexture( GraphicsApi, bmp, 0, y ); + selectedBlock = Utils2D.Make2DTexture( GraphicsApi, bmp, size, 0, y ); } int x = Window.Width / 2 - ( blockSize * barTextures.Length ) / 2; diff --git a/2D/Widgets/TextInputWidget.cs b/2D/Widgets/TextInputWidget.cs index 6fec2aab6..4432301c9 100644 --- a/2D/Widgets/TextInputWidget.cs +++ b/2D/Widgets/TextInputWidget.cs @@ -61,13 +61,13 @@ namespace ClassicalSharp { size.Height = Math.Max( size.Height, chatCaretTexture.Height ); int y = Window.Height - ChatInputYOffset - size.Height / 2; - using( Bitmap bmp = new Bitmap( size.Width, size.Height ) ) { + using( Bitmap bmp = Utils2D.CreatePow2Bitmap( size ) ) { using( Graphics g = Graphics.FromImage( bmp ) ) { Utils2D.DrawRect( g, backColour, 0, 0, bmp.Width, bmp.Height ); DrawTextArgs args = new DrawTextArgs( GraphicsApi, value, Color.White, false ); Utils2D.DrawText( g, font, ref args, 0, 0 ); } - chatInputTexture = Utils2D.Make2DTexture( GraphicsApi, bmp, 10, y ); + chatInputTexture = Utils2D.Make2DTexture( GraphicsApi, bmp, size, 10, y ); } chatCaretTexture.Y1 = chatInputTexture.Y1; Y = y; diff --git a/Utils/Utils.cs b/Utils/Utils.cs index daa641d9c..420a61ecb 100644 --- a/Utils/Utils.cs +++ b/Utils/Utils.cs @@ -35,6 +35,10 @@ namespace ClassicalSharp { return next; } + public static bool IsPowerOf2( int value ) { + return value != 0 && ( value & ( value - 1 ) ) == 0; + } + public static bool IsUrl( string value ) { return value.StartsWith( "http://" ) || value.StartsWith( "https://" ); }