First optimisation of classic mode background drawing.

This commit is contained in:
UnknownShadow200 2016-04-01 15:12:35 +11:00
parent a47118e652
commit 22be9bbfe0
4 changed files with 41 additions and 10 deletions

View File

@ -30,7 +30,7 @@ namespace ClassicalSharp {
if( !args.SkipPartsCheck )
GetTextParts( args.Text );
Brush shadowBrush = GetOrCreateBrush( Color.Black );
Brush shadowBrush = GetOrCreateBrush( FastColour.Black );
float textX = x;
for( int i = 0; i < parts.Count; i++ ) {
TextPart part = parts[i];
@ -47,7 +47,7 @@ namespace ClassicalSharp {
if( !args.SkipPartsCheck )
GetTextParts( args.Text );
Brush shadowBrush = GetOrCreateBrush( Color.Black );
Brush shadowBrush = GetOrCreateBrush( FastColour.Black );
StringFormatFlags flags = format.FormatFlags;
format.FormatFlags |= StringFormatFlags.NoWrap;
format.Trimming = StringTrimming.EllipsisCharacter;

View File

@ -92,13 +92,13 @@ namespace ClassicalSharp {
DisposeBitmappedText();
}
SolidBrush GetOrCreateBrush( Color color ) {
int key = color.ToArgb();
SolidBrush GetOrCreateBrush( FastColour col ) {
int key = col.ToArgb();
SolidBrush brush;
if( brushCache.TryGetValue( key, out brush ) )
return brush;
brush = new SolidBrush( color );
brush = new SolidBrush( col );
brushCache[key] = brush;
return brush;
}

View File

@ -38,6 +38,30 @@ namespace Launcher {
}
}
public unsafe static void DrawScaledPixels( FastBitmap src, FastBitmap dst, Size scale,
Rectangle srcRect, Rectangle dstRect ) {
int srcWidth = srcRect.Width, dstWidth = dstRect.Width;
int srcHeight = srcRect.Height, dstHeight = dstRect.Height;
int srcX = srcRect.X, dstX = dstRect.X;
int srcY = srcRect.Y, dstY = dstRect.Y;
int scaleWidth = scale.Width, scaleHeight = scale.Height;
if( dstX >= dst.Width || dstY >= dst.Height ) return;
dstWidth = Math.Min( dstX + dstWidth, dst.Width ) - dstX;
dstHeight = Math.Min( dstY + dstHeight, dst.Height ) - dstY;
for( int yy = 0; yy < dstHeight; yy++ ) {
int scaledY = (yy + dstRect.Y) * srcHeight / scaleHeight;
int* srcRow = src.GetRowPtr( srcY + (scaledY % srcHeight) );
int* dstRow = dst.GetRowPtr( dstY + yy );
for( int xx = 0; xx < dstWidth; xx++ ) {
int scaledX = (xx + dstRect.X) * srcWidth / scaleWidth;
dstRow[dstX + xx] = srcRow[srcX + (scaledX % srcWidth)];
}
}
}
public unsafe static void DrawNoise( FastBitmap dst, Rectangle dstRect, FastColour col, int variation ) {
int dstX, dstY, dstWidth, dstHeight;
if( !CheckCoords( dst, dstRect, out dstX, out dstY, out dstWidth, out dstHeight ) )

View File

@ -56,6 +56,10 @@ namespace Launcher {
terrainPixels = new FastBitmap( terrainBmp, true );
FastBitmap.MovePortion( elemSize * 1, 0, elemSize * 0, 0, src, terrainPixels, elemSize );
FastBitmap.MovePortion( elemSize * 2, 0, elemSize * 1, 0, src, terrainPixels, elemSize );
// Precompute the darkened stone background
Rectangle rect = new Rectangle( 0, 0, elemSize, elemSize );
Drawer2DExt.DrawScaledPixels( terrainPixels, terrainPixels, rect.Size, rect, rect, 96, 96 );
}
}
@ -68,8 +72,8 @@ namespace Launcher {
if( ClassicBackground ) {
using( FastBitmap dst = new FastBitmap( Framebuffer, true ) ) {
ClearTile( 0, 0, Width, 48, elemSize, 128, 64, dst );
ClearTile( 0, 48, Width, Height - 48, 0, 96, 96, dst );
ClearTile( 0, 0, Width, 48, elemSize, 128, 64, dst, true );
ClearTile( 0, 48, Width, Height - 48, 0, 96, 96, dst, false );
}
} else {
ClearArea( 0, 0, Width, Height );
@ -99,7 +103,7 @@ namespace Launcher {
public void ClearArea( int x, int y, int width, int height, FastBitmap dst ) {
if( ClassicBackground ) {
ClearTile( x, y, width, height, 0, 96, 96, dst );
ClearTile( x, y, width, height, 0, 96, 96, dst, false );
} else {
FastColour col = LauncherSkin.BackgroundCol;
Drawer2DExt.DrawNoise( dst, new Rectangle( x, y, width, height ), col, 6 );
@ -107,7 +111,7 @@ namespace Launcher {
}
void ClearTile( int x, int y, int width, int height, int srcX,
byte scaleA, byte scaleB, FastBitmap dst ) {
byte scaleA, byte scaleB, FastBitmap dst, bool scale ) {
if( x >= Width || y >= Height ) return;
Rectangle srcRect = new Rectangle( srcX, 0, elemSize, elemSize );
const int tileSize = 48;
@ -121,7 +125,10 @@ namespace Launcher {
int y2 = Math.Min( y + tileSize, Math.Min( yMax, Height ) );
Rectangle dstRect = new Rectangle( x, y, x2 - x, y2 - y );
if( scale )
Drawer2DExt.DrawScaledPixels( terrainPixels, dst, size, srcRect, dstRect, scaleA, scaleB );
else
Drawer2DExt.DrawScaledPixels( terrainPixels, dst, size, srcRect, dstRect );
}
}
}