【发布时间】:2012-03-26 17:52:02
【问题描述】:
这种绘制瓷砖的方法似乎很慢,我不确定到底出了什么问题,它相信我的剔除方法不起作用并且在屏幕外绘制东西,但我不完全确定。这里是:
// 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?
-
这无疑是一个 SpriteBatch 问题,当我只跳过瓷砖的绘制,但加载碰撞等时,我获得了 30 fps。这让我觉得顶部的剔除方法仍然是在屏幕外绘制东西,切换图形是个问题。
-
您可能应该使用分析器,以便准确找出导致其变慢的原因。例如:SlimTune; code.google.com/p/slimtune
标签: c# performance xna culling spritebatch