TransWikia.com

Frame-rate drops when drawing 2D map tiles

Game Development Asked by TheCo on December 6, 2021

Update: I figured out it has nothing to do with the camera but instead framerate drops when drawing my map tiles onto the screen.

Framerate drops

Here’s a link to a video showcasing the bug in action (For further reference): https://streamable.com/m4zuf3

Any and all guidance is appreciated, thanks.

Here’s the draw functions:

    protected override void Draw(GameTime gameTime)
    {
        m_mainLevel.Draw(spriteBatch);
    }
        public void Draw(SpriteBatch sb)
        {
            //Draw Background
            for (var i = 0; i < BackgroundTiles.Length; i++)
            {
                int id = BackgroundTiles[i].m_id;

                int column = id % TilesetTilesWide;
                int row = (int)Math.Floor((double)id / (double)TilesetTilesWide);

                int width = Map.TileWidth * 2;
                int height = Map.TileWidth * 2;

                float x = (i % Map.Width) * width;
                float y = (float)Math.Floor(i / (double)Map.Width) * height;

                Rectangle tilesetRec = new Rectangle(TileWidth * column, TileHeight * row, TileWidth, TileHeight);
                Rectangle dest = new Rectangle((int)x, (int)y, width, height);
                sb.Draw(Tileset, dest, tilesetRec, Color.White);
            }
        }

Is the problem that I’m drawing too many tiles per frame? the map size is 250 x 250 tiles, with each tile being 16x16px.

One Answer

I solved it.

if(bounds.Intersects(dest)) sb.Draw(Tileset, dest, tilesetRec, Color.White);

Answered by TheCo on December 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP