Click here to Skip to main content
Rate this: bad
good
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))
                    {
                        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 7-Jan-13 9:42am
Yvar Birx2.4K
Comments
Yvar Birx at 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
good
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 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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 254
1 Nirav Prabtani 223
2 Sergey Alexandrovich Kryukov 209
3 _Amy 165
4 Richard MacCutchan 135
0 OriginalGriff 7,799
1 Sergey Alexandrovich Kryukov 6,966
2 Maciej Los 3,994
3 Peter Leow 3,708
4 CHill60 2,742


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2014
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