我正在开发 XNA 中的“落沙”游戏。当然,这意味着必须绘制大量像素!它开始很好,每个像素使用透明的蓝色纯粹用于测试算法。在失去任何帧速率(稳定的 60 FPS)之前,我能够在屏幕上绘制大约 100,000 个像素,这超出了我的需要!然而,当我最终添加了不同颜色的像素(蓝色和红色)并且没有其他变化时,我的 FPS 呈指数级下降,在 FPS 下降到大约 10 之前,只允许在屏幕上绘制大约 10,000 个像素。经过一些实验后,我得出结论精灵批次最有可能有问题,尽管我可能完全错了,如果您想到不同的东西,请告诉我,因为精灵批次会自动排序并将像素混合在一起。我认为将两种颜色混合在一起的行为是我失去速度的地方。那么,我怎样才能绕过精灵批处理的方法呢?还是我完全不在基地?
我尝试过使用 SpriteSortMode 和 BlendMode,甚至只在每个精灵批处理调用中绘制彩色像素,但到目前为止还没有运气。
此外,我在屏幕上绘制的像素从一开始就没有相同的坐标,因此不需要混合等。
//Game Draw Method
protected override void Draw(GameTime gameTime)
{
stopWatch.Start();
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
//backgroundSprite.Draw(spriteBatch);
terrainSprite.Draw(spriteBatch);
resourceMap.Draw(spriteBatch, spriteFont);
frameCounter++;
string fps = string.Format("fps: {0}", frameRate);
int totalint = 0;
for (int i = 0; i < resourceMap.activeCoordinates.Count; i++)
{
totalint += resourceMap.activeCoordinates[i].Count;
}
string total = string.Format("resource count: {0}", totalint);
spriteBatch.DrawString(spriteFont, fps, new Vector2(33, 33), Color.White);
spriteBatch.DrawString(spriteFont, total, new Vector2(33, 45), Color.White);
spriteBatch.DrawString(spriteFont, totalTime, new Vector2(33, 17), Color.White);
spriteBatch.DrawString(spriteFont, totalTime, new Vector2(33, 17), Color.White);
spriteBatch.End();
stopWatch.Stop();
totalTime = "Draw Time: " + stopWatch.Elapsed;
stopWatch = new Stopwatch();
base.Draw(gameTime);
}
//resourceMap.Draw(SpriteBatch spriteBatch, SpriteFont font) from Game Draw
public void Draw(SpriteBatch spriteBatch, SpriteFont font)
{
//spriteBatch.End();
//this was originally one for loop to draw all pixels, but was separated to draw each different color pixel after discovering performance issue
for (int c = 0; c < activeCoordinates.Count(); c++)
{
//spriteBatch.Begin();
for (int i = 0; i < activeCoordinates[c].Count; i++)
{
mapArray[(int)activeCoordinates[c][i].X][(int)activeCoordinates[c][i].Y].Draw(spriteBatch);
}
//spriteBatch.End();
}
//spriteBatch.Begin();
spriteBatch.DrawString(font, timeInMilli, new Vector2(33, 0), Color.White);
}
同样,对代码所做的唯一更改是在要绘制的像素列表中添加不同的颜色,因此这里的代码运行良好(以大约 60fps 的速度绘制 100,000 个像素),直到我添加了额外的颜色。
谢谢您的帮助!