绘图变慢,需要大量改进! [英] Drawing goes slow, need a lot of improvements!

查看:118
本文介绍了绘图变慢,需要大量改进!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好!



我正在从我的阵列中抽出一个世界。基本上,世界是由32乘32的块组成,但我需要一个不仅绘制每个块的视图。基本上,我需要绘制每个像素。为此,我做了一个很好的for循环。虽然我使用的是一种非常先进的绘图机制(我认为是DirectX或其他东西),但它仍然落后于地狱。但它确实有效,我想知道这里是否有任何可以改进的东西。我正在考虑将Ints用于之前的LoopBlockX或其他东西。我不确定,欢迎任何帮助。



当地人:

BlockSize = 32

ChunkSize = 20





Hello everybody!

I am currently drawing a world out of my array. And basically, the world is made of blocks who are 32 by 32, but I need a view that does not only draw every block. Basically, I need to draw every pixel. And to do that, I made a nice for loop. Although I am using a very advanced drawing mechanism(I think DirectX or something) this lags like hell. But it does work, and I would like to know if there is anything in here that can be improved. I was thinking about using the Ints for the previous LoopBlockX or something. I am not sure, any help is welcome.

Also the Locals:
BlockSize = 32
ChunkSize = 20


public void DrawWorld()
{
    // -- List of the Blocks.
    List<Block> BlockList = new List<Block>();

    // -- Loop through the chunkX.
    for (int i = 0; i < World.ChunkSize * ID.BlockSize; i++)
    {
        // -- Loop through the chunkY.
        for (int j = 0; j < World.ChunkSize * ID.BlockSize; j++)
        {
            // -- Get the Block.
            Block Block = null ;

            // -- Get the right block x.
            int LoopBlockX = (ThePlayer.BlockX - (World.ChunkSize / 2)) + (i / ID.BlockSize);
            int LoopBlockY = (ThePlayer.BlockY - (World.ChunkSize / 2)) + (j / ID.BlockSize);

            Block = ThePlayer.GetBlock(LoopBlockX, LoopBlockY) ;

            if (!BlockList.Contains(Block))
            {
                BlockList.Add(Block);

                // -- Color.
                Texture2D toDraw = null;

                // -- Get the Right Block.
                switch (Block.BlockID)
                {
                    case ID.BLOCK_AIR:
                        toDraw = BlockAir;
                        break;
                    case ID.BLOCK_DIRT:
                        toDraw = BlockDirt;
                        break;
                    case ID.BLOCK_GRASS:
                        toDraw = BlockGrass;
                        break;
                    case ID.BLOCK_SAND:
                        toDraw = BlockSand;
                        break;
                    case ID.BLOCK_STONE:
                        toDraw = BlockStone;
                        break;
                    case ID.BLOCK_COBBLESTONE:
                        toDraw = BlockCobblestone;
                        break;
                    case ID.BLOCK_BRICK:
                        toDraw = BlockBrick;
                        break;
                    case ID.BLOCK_WOODPLANK:
                        toDraw = BlockPlank;
                        break;

                }

                Rectangle _Rec = new Rectangle(i, j, ID.BlockSize, ID.BlockSize);
                spriteBatch.Draw(toDraw, _Rec, Color.White);

            }
        }
    }

    BlockList.Clear();
}

推荐答案

让我们从一个简单的数学开始...外部循环运行32 * 20 = 640次,并且每次内循环也运行640次...因此导致代码的640 * 640 = 409,600次迭代。这不会很快,除非你的操作非常简单(即便如此,它可能不是我)。



我的建议是这样的:找到一种方法来切割减少迭代次数。我不完全明白你在这里要做什么,但是我看到了一个很大的红旗:BlockList。你最多只能碰到一次。



但是,如果我理解你的代码和描述,修复应该相当简单:用 i ++ 替换 i + = ID.BlockSize (与 j 相同),因为最多只需要检查每第32行和每列中的像素(或任何块大小)。如果一开始可能看起来有点奇怪,但是如果你考虑一下这会打到每个区块一次而且只打一次。然后你应该能够完全删除BlockList(每次调用BlockList.Contains都可以进行与列表中的项目一样多的比较,这会让你的速度更慢)。



如果你觉得你仍然必须使用BlockList(或者我完全误解了发生了什么),至少要使用 HashSet [ ^ ]而不是List,在查找时应该快得多。
Well let''s start with a little simple math...the outer loop runs 32 * 20 = 640 times, and each of those times the inner loop also runs 640 times...so that leads to 640 * 640 = 409,600 iterations of the code. This is not going to be fast, unless your operations are extremely simple (even then, it might not me).

My recommendation is this: find a way to cut down on the number of iterations. I don''t fully understand what you''re trying to do here, but there''s a big red flag I see: the BlockList. You should only hit upon each block at most once.

However, if I am understanding your code and description, the fix should be fairly simple: replace i++ with i += ID.BlockSize (and the same with j), because at most you should only need to check the pixel in every 32nd row and column (or whatever the block size is). If might seem a little weird at first, but if you think about it this would hit every block once and only once. Then you should be able to remove the BlockList completely (every call to BlockList.Contains can take as many comparisons as there are items in the list, this will slow you down even more).

If you feel you must still use the BlockList (or I''m completely misunderstanding what''s going on), at least use a HashSet[^] instead of a List, it should be much faster at look-ups.


这篇关于绘图变慢,需要大量改进!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆