這種繪製我的瓷磚的方法似乎很慢,我不確定究竟是什麼錯誤,它相信我的剔除方法不工作,並正在繪製屏幕外的東西,但我不完全確定。那就是:XNA - 剔除性能問題
// Calculate the visible range of tiles.
int left = (int)Math.Floor(cameraPosition.X/16);
int right = left + spriteBatch.GraphicsDevice.Viewport.Width/16;
right = Math.Min(right, Width) + 1; // Width -1 originally - didn't look good as tiles drawn on screen
if (right > tiles.GetUpperBound(0))
right = tiles.GetUpperBound(0) + 1; // adding 1 to get the last right tile drawn
int top = (int)Math.Floor(cameraPosition.Y/16);
int bottom = left + spriteBatch.GraphicsDevice.Viewport.Height/ 16;
bottom = Math.Min(bottom, Height) + 1; // Height -1 originally - didn't look good as tiles drawn on screen
if (bottom > tiles.GetUpperBound(1))
bottom = tiles.GetUpperBound(1) + 1; // adding 1 to get the last bottom tile drawn
// For each tile position
for (int y = top; y < bottom; ++y)
{
for (int x = left; x < right; ++x)
{
// If there is a visible tile in that position, draw it
if (tiles[x, y].BlockType.Name != "Blank")
{
Texture2D texture = tileContent["DirtBlock_" + getTileSetType(tiles,x,y)];
spriteBatch.Draw(texture, new Vector2(x * 16, y * 16), Color.White);
if (isMinimap)
spriteBatch.Draw(pixel, new Vector2(30+x, 30+y), Color.White);
}
}
}
GetTileSetTypes是一個函數來獲取什麼磚周圍,爲不同的紋理,像DirtBlock_North,DirtBlock_Center等
瓷磚的內容僅僅是一類與我的塊紋理。
你是怎麼確定這是代碼慢的?一般每幀有多少塊瓷磚?你在哪裏調用SpriteBatch.Begin和.End? – 2012-03-26 19:27:26
它明顯是一個SpriteBatch問題,當我跳過瓷磚的繪製,但加載碰撞等,我獲得30幀/秒。這導致我認爲頂部的剔除方法仍然在屏幕上繪畫,切換圖形是一個問題。 – Cyral 2012-03-26 20:18:49
你應該使用一個分析器,這樣你就可以精確地確定是什麼導致它變慢了。例如:SlimTune; http://code.google.com/p/slimtune/ – 2012-03-26 21:50:15