Drawing grid from lines

I made simple tetris game, and it works good on PC, but i want to port it for android, and it lags too much. I found that problem is because of way how I draw Grid, is there a way to optimize it?

Here is function to draw grid:

        private void DrawGrid(int rows, int cols, int addedX, int addedY)
        {
            for (int i = 1; i <= rows; i++)
            {
                for (int j = 1; j <= cols; j++)
                    DrawRectangle(_spriteBatch, new Rectangle(j * cellSize + addedX,
                        i * cellSize + addedY, cellSize, cellSize), GameShared.Settings.BlackTheme ? Color.Gray : Color.Black, 2);
            }
        }

Draw Rectangle:

        private void DrawRectangle(SpriteBatch spriteBatch, Rectangle rectangle, Color color, int lineWidth)
        {
            Texture2D _pointTexture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1);
            _pointTexture.SetData<Color>(new Color[] { Color.White });

            spriteBatch.Draw(_pointTexture, new Rectangle(rectangle.X, rectangle.Y, lineWidth, rectangle.Height + lineWidth), color);
            spriteBatch.Draw(_pointTexture, new Rectangle(rectangle.X, rectangle.Y, rectangle.Width + lineWidth, lineWidth), color);
            spriteBatch.Draw(_pointTexture, new Rectangle(rectangle.X + rectangle.Width, rectangle.Y, lineWidth, rectangle.Height + lineWidth), color);
            spriteBatch.Draw(_pointTexture, new Rectangle(rectangle.X, rectangle.Y + rectangle.Height, rectangle.Width + lineWidth, lineWidth), color);
        }

You should save the _pointTexture so that you don’t have to recreate it every frame. Then you’ll only need the 4 Draw calls inside DrawRectangle.

I’d create _pointTexture inside my LoadContent method when the game starts.

1 Like

It looks like you’re creating a new Texture2D every time you call DrawRectangle. Texture2D is an IDisposable so you should always be sure to dispose of them at some point or else your program will use more and more memory with each passing frame that you create more and more of them without disposing.

Apos solution is correct, but if you want to test what kind of awful impact the memory leaks are causing, try wrapping your _pointTexture in a using statement to see the difference (and pay attention to your IDE’s diagnostic tools which show how much process memory your game is using while it’s running)

private void DrawRectangle(SpriteBatch spriteBatch, Rectangle rectangle, Color color, int lineWidth)
{
    using (Texture2D _pointTexture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1))
    {
        _pointTexture.SetData<Color>(new Color[] { Color.White });

        spriteBatch.Draw(_pointTexture, new Rectangle(rectangle.X, rectangle.Y, lineWidth, rectangle.Height + lineWidth), color);
        spriteBatch.Draw(_pointTexture, new Rectangle(rectangle.X, rectangle.Y, rectangle.Width + lineWidth, lineWidth), color);
        spriteBatch.Draw(_pointTexture, new Rectangle(rectangle.X + rectangle.Width, rectangle.Y, lineWidth, rectangle.Height + lineWidth), color);
        spriteBatch.Draw(_pointTexture, new Rectangle(rectangle.X, rectangle.Y + rectangle.Height, rectangle.Width + lineWidth, lineWidth), color);
    }
}
1 Like

Thank you, it helped