Further optimisations of background drawing.

This commit is contained in:
UnknownShadow200 2016-04-01 15:41:39 +11:00
parent 22be9bbfe0
commit 0993acb4fc
2 changed files with 27 additions and 33 deletions

View File

@ -15,10 +15,6 @@ namespace Launcher {
int srcY = srcRect.Y, dstY = dstRect.Y; int srcY = srcRect.Y, dstY = dstRect.Y;
int scaleWidth = scale.Width, scaleHeight = scale.Height; 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++ ) { for( int yy = 0; yy < dstHeight; yy++ ) {
int scaledY = (yy + dstRect.Y) * srcHeight / scaleHeight; int scaledY = (yy + dstRect.Y) * srcHeight / scaleHeight;
int* srcRow = src.GetRowPtr( srcY + (scaledY % srcHeight) ); int* srcRow = src.GetRowPtr( srcY + (scaledY % srcHeight) );
@ -38,27 +34,19 @@ namespace Launcher {
} }
} }
public unsafe static void DrawScaledPixels( FastBitmap src, FastBitmap dst, Size scale, public unsafe static void DrawTiledPixels( FastBitmap src, FastBitmap dst,
Rectangle srcRect, Rectangle dstRect ) { Rectangle srcRect, Rectangle dstRect ) {
int srcWidth = srcRect.Width, dstWidth = dstRect.Width; int srcWidth = srcRect.Width, dstWidth = dstRect.Width;
int srcHeight = srcRect.Height, dstHeight = dstRect.Height; int srcHeight = srcRect.Height, dstHeight = dstRect.Height;
int srcX = srcRect.X, dstX = dstRect.X; int srcX = srcRect.X, dstX = dstRect.X;
int srcY = srcRect.Y, dstY = dstRect.Y; 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++ ) { for( int yy = 0; yy < dstHeight; yy++ ) {
int scaledY = (yy + dstRect.Y) * srcHeight / scaleHeight; int* srcRow = src.GetRowPtr( srcY + ((yy + dstRect.Y) % srcHeight) );
int* srcRow = src.GetRowPtr( srcY + (scaledY % srcHeight) );
int* dstRow = dst.GetRowPtr( dstY + yy ); int* dstRow = dst.GetRowPtr( dstY + yy );
for( int xx = 0; xx < dstWidth; xx++ ) { for( int xx = 0; xx < dstWidth; xx++ )
int scaledX = (xx + dstRect.X) * srcWidth / scaleWidth; dstRow[dstX + xx] = srcRow[srcX + ((xx + dstRect.X) % srcWidth)];
dstRow[dstX + xx] = srcRow[srcX + (scaledX % srcWidth)];
}
} }
} }
@ -93,7 +81,7 @@ namespace Launcher {
} }
} }
static bool CheckCoords( FastBitmap dst,Rectangle dstRect, out int dstX, static bool CheckCoords( FastBitmap dst, Rectangle dstRect, out int dstX,
out int dstY, out int dstWidth, out int dstHeight ) { out int dstY, out int dstWidth, out int dstHeight ) {
dstWidth = dstRect.Width; dstHeight = dstRect.Height; dstWidth = dstRect.Width; dstHeight = dstRect.Height;
dstX = dstRect.X; dstY = dstRect.Y; dstX = dstRect.X; dstY = dstRect.Y;

View File

@ -40,7 +40,7 @@ namespace Launcher {
bool useBitmappedFont; bool useBitmappedFont;
Bitmap terrainBmp; Bitmap terrainBmp;
FastBitmap terrainPixels; FastBitmap terrainPixels;
int elemSize; const int tileSize = 48;
void ProcessZipEntry( string filename, byte[] data, ZipEntry entry ) { void ProcessZipEntry( string filename, byte[] data, ZipEntry entry ) {
MemoryStream stream = new MemoryStream( data ); MemoryStream stream = new MemoryStream( data );
@ -49,17 +49,20 @@ namespace Launcher {
Drawer.SetFontBitmap( bmp ); Drawer.SetFontBitmap( bmp );
useBitmappedFont = !Options.GetBool( OptionsKey.ArialChatFont, false ); useBitmappedFont = !Options.GetBool( OptionsKey.ArialChatFont, false );
} else if( filename == "terrain.png" ) { } else if( filename == "terrain.png" ) {
FastBitmap src = new FastBitmap( bmp, true ); int elemSize = bmp.Width / 16;
elemSize = src.Width / 16; Size size = new Size( tileSize, tileSize );
terrainBmp = new Bitmap( tileSize * 2, tileSize );
terrainBmp = new Bitmap( elemSize * 2, elemSize );
terrainPixels = new FastBitmap( terrainBmp, true ); 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 // Precompute the scaled background
Rectangle rect = new Rectangle( 0, 0, elemSize, elemSize ); using( FastBitmap src = new FastBitmap( bmp, true ) ) {
Drawer2DExt.DrawScaledPixels( terrainPixels, terrainPixels, rect.Size, rect, rect, 96, 96 ); Drawer2DExt.DrawScaledPixels( src, terrainPixels, size,
new Rectangle( 2 * elemSize, 0, elemSize, elemSize ),
new Rectangle( tileSize, 0, tileSize, tileSize ), 255, 255 );
Drawer2DExt.DrawScaledPixels( src, terrainPixels, size,
new Rectangle( 1 * elemSize, 0, elemSize, elemSize ),
new Rectangle( 0, 0, tileSize, tileSize ), 96, 96 );
}
} }
} }
@ -72,7 +75,7 @@ namespace Launcher {
if( ClassicBackground ) { if( ClassicBackground ) {
using( FastBitmap dst = new FastBitmap( Framebuffer, true ) ) { using( FastBitmap dst = new FastBitmap( Framebuffer, true ) ) {
ClearTile( 0, 0, Width, 48, elemSize, 128, 64, dst, true ); ClearTile( 0, 0, Width, 48, tileSize, 128, 64, dst, true );
ClearTile( 0, 48, Width, Height - 48, 0, 96, 96, dst, false ); ClearTile( 0, 48, Width, Height - 48, 0, 96, 96, dst, false );
} }
} else { } else {
@ -113,8 +116,7 @@ namespace Launcher {
void ClearTile( int x, int y, int width, int height, int srcX, void ClearTile( int x, int y, int width, int height, int srcX,
byte scaleA, byte scaleB, FastBitmap dst, bool scale ) { byte scaleA, byte scaleB, FastBitmap dst, bool scale ) {
if( x >= Width || y >= Height ) return; if( x >= Width || y >= Height ) return;
Rectangle srcRect = new Rectangle( srcX, 0, elemSize, elemSize ); Rectangle srcRect = new Rectangle( srcX, 0, tileSize, tileSize );
const int tileSize = 48;
Size size = new Size( tileSize, tileSize ); Size size = new Size( tileSize, tileSize );
int xOrig = x, xMax = x + width, yMax = y + height; int xOrig = x, xMax = x + width, yMax = y + height;
@ -124,11 +126,15 @@ namespace Launcher {
int x2 = Math.Min( x + tileSize, Math.Min( xMax, Width ) ); int x2 = Math.Min( x + tileSize, Math.Min( xMax, Width ) );
int y2 = Math.Min( y + tileSize, Math.Min( yMax, Height ) ); int y2 = Math.Min( y + tileSize, Math.Min( yMax, Height ) );
Rectangle dstRect = new Rectangle( x, y, x2 - x, y2 - y ); Rectangle area = new Rectangle( x, y, x2 - x, y2 - y );
if( area.X >= dst.Width || area.Y >= dst.Height ) continue;
area.Width = Math.Min( area.X + area.Width, dst.Width ) - area.X;
area.Height = Math.Min( area.Y + area.Height, dst.Height ) - area.Y;
if( scale ) if( scale )
Drawer2DExt.DrawScaledPixels( terrainPixels, dst, size, srcRect, dstRect, scaleA, scaleB ); Drawer2DExt.DrawScaledPixels( terrainPixels, dst, size, srcRect, area, scaleA, scaleB );
else else
Drawer2DExt.DrawScaledPixels( terrainPixels, dst, size, srcRect, dstRect ); Drawer2DExt.DrawTiledPixels( terrainPixels, dst, srcRect, area );
} }
} }
} }