mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-01-23 17:43:08 -05:00
Use power of two textures for 2D operations, replace code to draw bounds of rectangles with single method in Utils2D.
This commit is contained in:
parent
b0b7285953
commit
660daa26d0
7 changed files with 38 additions and 35 deletions
|
@ -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 );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<DrawTextArgs> 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();
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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://" );
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue