Question

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.

Was it helpful?

Solution

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);
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top