我正在製作SDL程序,有時需要獲取矩形的寬度。當我做這樣的事情:爲什麼System.Drawing.Rectangle.getWidth()如此昂貴?
int VideoWidth = Video.Screen.Width; //Expensive operation
int VideoHeight = Video.Screen.Height;
int ChunksRendered = 0;
foreach (Chunk c in Chunks)
{
if (c.X + 256 + DeferredX > 0 && c.X + DeferredX < VideoWidth)
{
if (c.Y + 256 + DeferredY > 0 && c.Y + DeferredY < VideoHeight)
{
BlitSurface.Blit(c.Render(ref Textures, ref BlitSurface), new Point(c.X + DeferredX, c.Y + DeferredY));
ChunksRendered++;
}
}
}
我得到大約550 FPS我的機器上,但是當我這樣做:
int ChunksRendered = 0;
foreach (Chunk c in Chunks)
{
if (c.X + 256 + DeferredX > 0 && c.X + DeferredX < Video.Screen.Width)
{
if (c.Y + 256 + DeferredY > 0 && c.Y + DeferredY < Video.Screen.Height)
{
BlitSurface.Blit(c.Render(ref Textures, ref BlitSurface), new Point(c.X + DeferredX, c.Y + DeferredY));
ChunksRendered++;
}
}
}
我只得到大約200 FPS。分析顯然,這會導致速度放慢,但爲什麼要讓這個變量如此昂貴?
確實每幀上的屏幕寬度的變化? –
不,但你可以在遊戲中更改分辨率 –