【发布时间】:2014-02-09 22:48:04
【问题描述】:
我的游戏中有一个棋盘类。这个 Board 类负责绘制许多 Zone 对象,目前它们都具有相同的纹理。
所以我的绘图调用如下:Game1.Draw 调用 Board.Draw,它为每个区域对象调用 Zone.Draw,它调用 Spritebatch.Draw
Game1的绘制方法
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, cam.Transform);
board.Draw(gameTime);
spriteBatch.End();
}
Board类中的Draw方法(Board.Draw)
public void Draw(GameTime gameTime)
{
if (zones != null)
{
for (int x = 1; x < xbound-1; x++)
{
for (int y = 1; y < ybound-1; y++)
{
zones[x, y].Draw(gameTime);
}
}
}
}
区域中的绘制方法(Zone.Draw)
public void Draw(GameTime gameTime)
{
if (Tile != null)
{
Game1.spriteBatch.Draw(Tile, new Rectangle((int)pos.X, (int)pos.Y, size, size), Color.White);
//Game1.spriteBatch.DrawString(font2, pos.X.ToString() + "," + pos.Y.ToString(), pos, Color.Blue);
}
}
在我开始绘制字符串之前一切正常。取消注释 DrawString 调用会导致灾难性的 FPS 从一致的 60 下降到 20-25。
但这是奇怪的部分。我创建了一种不同的方法来绘制字符串,而不是在 Zone's Draw 中进行。我在 Board.Draw 绘制完区域后调用了这个方法。
绘制字符串的新单独方法
public void DrawNumbers(GameTime gameTime)
{
if (zones != null)
{
for (int x = 1; x < xbound - 1; x++)
{
for (int y = 1; y < ybound - 1; y++)
{
Game1.spriteBatch.DrawString(font2, x.ToString() + "," + y.ToString(), zones[x, y].getPos(), Color.Blue);
}
}
}
}
现在 Game1 的 Draw 是这样的
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, cam.Transform);
board.Draw(gameTime);
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, cam.Transform);
board.DrawNumbers(gameTime);
spriteBatch.End();
}
这样做不会影响性能!我再次获得恒定的 60FPS,这对我来说听起来违反直觉......
知道为什么要这样做吗?为什么在 Draw 调用之后立即调用 DrawString 似乎如此昂贵?
如果问题不清楚,请编辑或询问。
提前致谢。
【问题讨论】: