0

我正在 XNA 中创建一个需要绘制数千个小矩形/正方形的游戏。随着数量的增加,性能会变得更差。这是我当前使用的代码:

protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();

            foreach (SandBit sandBit in sandBitManager.grid)
            {
                Point p = sandBit.Position;
                spriteBatch.Draw(square, new Rectangle(p.X, p.Y, sandBit.SIZE, sandBit.SIZE), Color.White);
            }

            spriteBatch.End();
            base.Draw(gameTime);
        }

我要求spriteBatch.Draw()每一个方格,我正在重绘整个屏幕只是为了添加一个方格。我已经进行了大量搜索,我相信解决方案是绘制一个纹理,然后调用Draw()该纹理,但我找不到相关示例。

4

1 回答 1

0

试着不要在你的绘图函数中调用 Rectangle 构造函数,你可能会从中获得很大的性能提升(只需一遍又一遍地使用相同的矩形对象,并为属性设置不同的值)。

鉴于这是一个 2D 游戏(没有粒子或任何东西),您可能需要考虑为沙子使用更大的预生成纹理,而不是大量的小矩形。无论你做什么,在你的绘图循环中都有一个巨大的枚举最终会赶上你。

        Rectangle sandBitRect = new Rectangle()
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();

            foreach (SandBit sandBit in sandBitManager.grid)
            {
                Point p = sandBit.Position;
                sandBitRect.Left = p.X;
                sandBitRect.Top = p.Y;
                sandBitRect.Width = sandBit.SIZE;
                sandBitRect.Height = sandBit.SIZE;
                spriteBatch.Draw(square, sandBitRect, Color.White);
            }

            spriteBatch.End();
            base.Draw(gameTime);
        }
于 2013-12-27T01:23:37.750 回答