1

This method that draws my tiles seems to be quite slow, Im not sure exactly whats wrong, it belive my culling method isnt working and is drawing stuff offscreen, but im not completeley sure. Here it is:

        // Calculate the visible range of tiles.
        int left = (int)Math.Floor(cameraPosition.X / 16);
        int right = left + spriteBatch.GraphicsDevice.Viewport.Width / 16;
        right = Math.Min(right, Width) + 1; // Width -1 originally - didn't look good as tiles drawn on screen
        if (right > tiles.GetUpperBound(0))
            right = tiles.GetUpperBound(0) + 1; // adding 1 to get the last right tile drawn

        int top = (int)Math.Floor(cameraPosition.Y / 16);
        int bottom = left + spriteBatch.GraphicsDevice.Viewport.Height/ 16;
        bottom = Math.Min(bottom, Height) + 1; // Height -1 originally - didn't look good as tiles drawn on screen
        if (bottom > tiles.GetUpperBound(1))
            bottom = tiles.GetUpperBound(1) + 1; // adding 1 to get the last bottom tile drawn

        // For each tile position
        for (int y = top; y < bottom; ++y)
        {
            for (int x = left; x < right; ++x)
            {
                // If there is a visible tile in that position, draw it
                if (tiles[x, y].BlockType.Name != "Blank")
                {
                    Texture2D texture = tileContent["DirtBlock_" + getTileSetType(tiles,x,y)];
                    spriteBatch.Draw(texture, new Vector2(x * 16, y * 16), Color.White);
                    if (isMinimap)
                    spriteBatch.Draw(pixel, new Vector2(30+x, 30+y), Color.White);
                }

            }
        }

GetTileSetTypes is a function to get what tiles are around it, for different textures, like DirtBlock_North, DirtBlock_Center, etc.

Tile content is just a class with my block textures.

Cyral
  • 13,999
  • 6
  • 50
  • 90
  • how have you determined that this is the code that is slow? how many tiles are generally drawn per frame? where do you call SpriteBatch.Begin and .End? – Dave Cousineau Mar 26 '12 at 19:27
  • Its defidently a SpriteBatch Problem, When I skip the drawing of the tiles only, but load collision and such, I gain 30 fps. This leads me to think the culling method at the top is still drawing stuff off screen and switching graphics is a problem. – Cyral Mar 26 '12 at 20:18
  • you should probably use a profiler so that you can pinpoint exactly what is causing it to be slow. Ex: SlimTune; http://code.google.com/p/slimtune/ – Dave Cousineau Mar 26 '12 at 21:50

1 Answers1

1

Try changing SpriteBatch.Begin to defered and combining all of the tiles onto one texture.

See this GameDev question for info about why deferred is most likely the fastest option for you.

Also realize that every time you draw a new texture you have to take the old one out of the GPU and put the new one in. This process is called texture swapping and usually isn't an issue but you are swapping textures twice per tile which is likely to impact performance noticeably.

This can be fixed by combining multiple sprites onto one texture and using the source rectangle argument. This allows you to draw multiple sprites without a texture swap. There are a few OSS libraries for this. Sprite Sheet Packer is my personal favorite.

Unfortunantly without the project and a profiler I'm just guessing; however, these are the two biggest gotchas for rendering tilemaps I know of. I can't really see anything wrong from here. Below is the code I use to draw my tile maps and as you see its very similar to yours.

If all else fails I would suggest using a profiler to figure out which bits are running slowly.

        //Init the holder
        _holder = new Rectangle(0, 0, TileWidth, TileHeight);

        //Figure out the min and max tile indices to draw
        var minX = Math.Max((int)Math.Floor((float)worldArea.Left / TileWidth), 0);
        var maxX = Math.Min((int)Math.Ceiling((float)worldArea.Right / TileWidth), Width);

        var minY = Math.Max((int)Math.Floor((float)worldArea.Top / TileHeight), 0);
        var maxY = Math.Min((int)Math.Ceiling((float)worldArea.Bottom / TileHeight), Height);

        for (var y = minY; y < maxY; y++) {
            for (var x = minX; x < maxX; x++) {

                _holder.X = x * TileWidth;
                _holder.Y = y * TileHeight;

                var t = tileLayer[y * Width + x];
                spriteBatch.Draw(
                    t.Texture,
                     _holder, 
                    t.SourceRectangle,
                    Color.White, 
                    0,
                    Vector2.Zero,
                    t.SpriteEffects, 
                    0);
            }
        }
Community
  • 1
  • 1
ClassicThunder
  • 1,896
  • 16
  • 25
  • While sprite sheets are most likely the way to go, the OP could try `SpriteSortMode.Texture` rather than just Deferred. This will sort everything by texture which will minimize the texture switching. http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.spritesortmode.aspx – Dave Carlile Mar 26 '12 at 18:19
  • Im going to implement SpriteSheets soon, But I tried with only one texture instead of different ones, and it was only a few fps, so I know thats not the main reason. – Cyral Mar 26 '12 at 18:21
  • 1
    Thank you that's a good point, I refrained from suggesting SpriteSortMode.Texture because if he has objects or scenery the "This can improve performance when drawing non-overlapping sprites of uniform depth" doesn't hold true. It forces him to use a separate spritebatch for just rendering the tiles which does help the rendering time of the tilemap but would hurt overall fps after taking into account NPC's and scenery. – ClassicThunder Mar 26 '12 at 18:24
  • I have a separate spritbatch for drawing backgrounds. Also, Am I doing GetUpperBound right? For a array[,] – Cyral Mar 26 '12 at 18:26