下面是一個使用WriteableBitmap的示例,其性能主要與整個地圖的大小有關,而您的原始方法更多地依賴於tile的數量。你可以改變它在瓷磚之間有一個alpha混合的邊界,但是在它們之間留下一個空隙會更容易和更高效。你不需要隨機化代碼的代碼,但你應該有一些髒標誌,所以你只能在位圖改變時重新繪製位圖。
你可能也想看看我的答案和其他question。這就是說你沒有那麼多的項目,32x32的使用你的方法對我來說並不慢。
<local:Map x:Name="map" />
<RepeatButton Click="Button_Click" Content="Change" />
private void Button_Click(object sender, RoutedEventArgs e)
{
map.seed++;
map.InvalidateVisual();
}
public class Map : FrameworkElement
{
private int[][] _mapTiles;
public Map()
{
_mapTiles = Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg").Select(x =>
{
var b = new BitmapImage(new Uri(x));
var transform = new TransformedBitmap(b, new ScaleTransform((1.0/b.PixelWidth)*tileSize,(1.0/b.PixelHeight)*tileSize));
var conv = new FormatConvertedBitmap(transform, PixelFormats.Pbgra32, null, 0);
int[] data = new int[tileSize * tileSize];
conv.CopyPixels(data, tileSize * 4, 0);
return data;
}).ToArray();
bmp = new WriteableBitmap(w * tileSize, h * tileSize, 96, 96, PixelFormats.Pbgra32, null);
destData = new int[bmp.PixelWidth * bmp.PixelHeight];
}
const int w = 64, h = 64, tileSize = 8;
public int seed = 72141;
private int oldSeed = -1;
private WriteableBitmap bmp;
int[] destData;
protected override void OnRender(DrawingContext dc)
{
if(seed != oldSeed)
{
oldSeed = seed;
int startX = 0, endX = w;
int startY = 0, endY = h;
Random rnd = new Random(seed);
for(int x = startX; x < endX; x++)
{
for(int y = startY; y < endY; y++)
{
var tile = _mapTiles[rnd.Next(_mapTiles.Length)];
var rect = new Int32Rect(x * tileSize, y * tileSize, tileSize, tileSize);
for(int sourceY = 0; sourceY < tileSize; sourceY++)
{
int destY = ((rect.Y + sourceY) * (w * tileSize)) + rect.X;
Array.Copy(tile, sourceY * tileSize, destData, destY, tileSize);
}
}
}
bmp.WritePixels(new Int32Rect(0, 0, w * tileSize, h * tileSize), destData, w * tileSize * 4, 0);
}
dc.DrawImage(bmp,new Rect(0,0,w*tileSize,h*tileSize));
}
}
對於32x32網格,我發現你的方法很快,我可以使用WriteableBitmap發佈一個解決方案,該方法適用於像發佈的代碼中的8px方格那樣的小塊。 GameWorldObject.GameMap [x,y] .GraphicsTile.TileStartPoint'的代碼只是一個簡單的訪問嗎?它是一個32x32網格,8px大小的瓷磚表現緩慢? – Kris 2011-06-02 17:17:52
_maptiles是一個包含所有可能位圖的字典。它是一個32x32的網格,我從原來的尺寸縮小了。瓷磚實際上是64x64。我想知道縮小單個圖像後是否會更快,而不是每個圖像縮小 – 2011-06-03 21:42:22