如何在不杀死FPS的情况下绘制数千个图块

时间:2013-10-10 13:44:46

标签: c# xna tiles-game

我到处寻找解决这个问题的方法(我可能只是盲目地看到解决方案)。我的游戏当前在屏幕上渲染瓷砖地图,并且不会渲染实际上不在屏幕边界内的瓷砖。但是,每个图块为16x16像素,这意味着如果屏幕上的每个像素都包含分辨率为1920x1080的图块,则会绘制8100个图块。

每个周期绘制很多瓷砖真的会杀死我的FPS。如果我运行800x600分辨率,我的FPS会达到~20,而在1920x1080,它会以大约3-5 FPS运行。这真让我疯狂。

我尝试过线程和使用异步任务,但那些只是闪烁屏幕。可能只是我编码不正确。

这是我目前使用的绘图代码。

        // Get top-left tile-X
        Vector topLeft = new Vector(Screen.Camera.X / 16 - 1, 
            Screen.Camera.Y / 16 - 1);
        Vector bottomRight = new Vector(topLeft.X + (Screen.Width / 16) + 2, 
            topLeft.Y + (Screen.Height / 16) + 2);

        // Iterate sections
        foreach (WorldSection section in Sections)
        {
            // Continue if out of bounds
            if (section.X + ((Screen.Width / 16) + 2) < (int)topLeft.X ||
                section.X >= bottomRight.X)
                continue;

            // Draw all tiles within the screen range
            for (int x = topLeft.X; x < bottomRight.X; x++)
                for (int y = topLeft.Y; y < bottomRight.Y; y++)
                    if (section.Blocks[x - section.X, y] != '0')
                        DrawBlock(section.Blocks[x - section.X, y], 
                            x + section.X, y);
        }

有8到12个部分。每个图块由二维数组中的char对象表示。

绘制块方法:

public void DrawBlock(char block, int x int y)
    {
        // Get the source rectangle
        Rectangle source = new Rectangle(Index(block) % Columns * FrameWidth,
            Index(block) / Columns * FrameHeight, FrameWidth, FrameHeight);

        // Get position
        Vector2 position = new Vector2(x, y);

        // Draw the block
        Game.spriteBatch.Draw(Frameset, position * new Vector2(FrameWidth, FrameHeight) - Screen.Camera, source, Color.White);
    }

Index()方法只返回与char对应的tile的帧索引。

我想知道如何能够在不以这种方式杀死帧速率的情况下实时绘制这么多内容。我提供的代码显然没有得到很好的优化,或者我应该做些什么才能在不降低性能的情况下绘制这么多单独的瓷砖?

1 个答案:

答案 0 :(得分:0)

不确定这是否是处理问题的最佳方法,但我已经开始使用RenderTarget2D将世界的块预渲染为纹理。我必须一次在实际屏幕边界周围的给定区域内加载块,因为一次加载所有块会使内存不足。

当您接近当前预渲染区域的边界时,它将根据您在世界中的新位置重新处理块。处理大约需要100毫秒,因此在加载新区域时,玩家将在此期间感觉轻微减速。我真的不喜欢这样,但至少FPS现在是60岁。

这是我的块处理器:

    public bool ProcessChunk(int x, int y)
    {
        // Create render target
        using (RenderTarget2D target = new RenderTarget2D(Game.CurrentDevice, 16 * 48, 16 * 48,
            false, SurfaceFormat.Color, DepthFormat.Depth24))
        {

            // Set render target
            Game.CurrentDevice.SetRenderTarget(target);

            // Clear back buffer
            Game.CurrentDevice.Clear(Color.Black * 0f);

            // Begin drawing
            Game.spriteBatch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend);

            // Get block coordinates
            int bx = x * 48,
                by = y * 48;

            // Draw blocks
            int count = 0;
            foreach (WorldSection section in Sections)
            {
                // Continue if section is out of chunk bounds
                if (section.X >= bx + 48) continue;

                // Draw all tiles within the screen range
                for (int ax = 0; ax < 48; ax++)
                    for (int ay = 0; ay < 48; ay++)
                    {
                        // Get the block character
                        char b = section.Blocks[ax + bx - section.X, ay + by];

                        // Draw the block unless it's an empty block
                        if (b != '0')
                        {
                            Processor.Blocks[b.ToString()].DrawBlock(new Vector2(ax, ay), true);
                            count++;
                        }
                    }
            }

            // End drawing
            Game.spriteBatch.End();

            // Clear target
            target.GraphicsDevice.SetRenderTarget(null);

            // Set texture
            if (count > 0)
            {
                // Create texture
                Chunks[x, y] = new Texture2D(Game.CurrentDevice, target.Width, target.Height, true, target.Format);

                // Set data
                Color[] data = new Color[target.Width * target.Height];
                target.GetData<Color>(data);
                Chunks[x, y].SetData<Color>(data);

                // Return true
                return true;
            }
        }

        // Return false
        return false;
    }

如果有这种方法可以如何改进任何建议,我不会伤心地听到他们!

感谢您提供的帮助!

相关问题