Click here to Skip to main content
12,895,911 members (46,750 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# XNA3.0
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))

                // -- Color.
                Texture2D toDraw = null;

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


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


Posted 7-Jan-13 9:42am
Yvar Birx2.4K
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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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 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.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100