Search code examples
c#xna

Foreach loop causing lag?


In my previous question here. I successfully found out how to read and draw a level each frame from an image.

Now that I have inputted a Player class, the player moves really choppy despite the code not doing so. I know this because if I comment the level reading code out, the player moves fine.

I think it's the foreach loop doing this. I have no idea and help would be appreciated!

Just to save you time from clicking, here's my successful level-reading code.

public void readLevel(string path, GraphicsDevice graphics)
{

    //GET AN ARRAY OF COLORS
    Texture2D level = Content.Load<Texture2D>(path);
    Color[] colors = new Color[level.Width * level.Height];
    level.GetData(colors);

    //READ EACH PIXEL AND DRAW LEVEL
    Color brickRGB = new Color(128, 128, 128);
    Color blankRGB = new Color(87, 0, 127);

    int placeX = 0;
    int placeY = 0;

    foreach (Color pixel in colors)
    {
        SpriteBatch spriteBatch = new SpriteBatch(graphics);
        spriteBatch.Begin();

        if (pixel == brickRGB)
        {
            Texture2D brick = Content.Load<Texture2D>("blocks/brick");
            spriteBatch.Draw(brick, new Rectangle(placeX, placeY, 40, 40), Color.White);

            Rectangle rect = new Rectangle(placeX, placeY, 40, 40);
            blocks.Add(rect);
        }
        else if (pixel == blankRGB)
        {
            Texture2D back = Content.Load<Texture2D>("titlescreen/back");
            spriteBatch.Draw(back, new Rectangle(placeX, placeY, 40, 40), Color.White);
        }

        if (placeX == 840)
        {
            placeX = 0;
            placeY += 40;
        }
        else placeX += 40;
        spriteBatch.End();
    }
}

Game.cs:

class Game
{
    Player player;
    LevelReader reader;
    int level = 1;

    public Game(ContentManager content)
    {
        reader = new LevelReader(content);
        player = new Player(content);
    }

    public void Update()
    {
        player.Update();
    }
    public void Draw(GraphicsDevice graphics)
    {
        reader.readLevel("levels/l" + level, graphics);
        player.Draw(graphics);

    }
}

Solution

  • You might want to move the calls to spritebatch.Begin() and spritebatch.End() outside of your loop.

    SpriteBatch spriteBatch = new SpriteBatch(graphics);
            spriteBatch.Begin();
    foreach (Color pixel in colors)
        {
    
    
            if (pixel == brickRGB)
            {
                Texture2D brick = Content.Load<Texture2D>("blocks/brick");
                spriteBatch.Draw(brick, new Rectangle(placeX, placeY, 40, 40), Color.White);
    
                Rectangle rect = new Rectangle(placeX, placeY, 40, 40);
                blocks.Add(rect);
            }
            else if (pixel == blankRGB)
            {
                Texture2D back = Content.Load<Texture2D>("titlescreen/back");
                spriteBatch.Draw(back, new Rectangle(placeX, placeY, 40, 40), Color.White);
            }
    
            if (placeX == 840)
            {
                placeX = 0;
                placeY += 40;
            }
            else placeX += 40;
    
        }
    spriteBatch.End();