Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


C#
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();
}
Posted
Comments
Yvar Birx 7-Jan-13 15:54pm    
I also understand it's 75% slow because of the huge for loop, but I have no idea on how to fix that unless I want to go back to my griddy block view. :(

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900