Stop drawing the screen to debug

Hello Monogame community,

I wanted to add a way to completely freeze my game to make debugging easier, but when I stop drawing the screen, the display keeps alternating between two frames.

bug

Is there some constraints to what we can do with the Draw function?
Also, I think I should still call the base.Update and base.Draw even if the game is freezed, what do you think?

Update

//Freezing the screen on F12 input
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.F12))
{
    if (!freezeToggled)
    {
        freezed = !freezed;
        freezeToggled = true;
    }
}
else
   freezeToggled = false;

if (!freezed)
{
    //Just making the texture move
    X += Vx;
    
    if (X > 800 - 256) { Vx = -8; }
    if (X < 0) { Vx = 8; }
}

base.Update(gameTime);

Draw

if (!freezed)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    
    spriteBatch.Begin(samplerState: SamplerState.PointClamp);
    spriteBatch.Draw(Grass, new Rectangle(X, 0, 256, 256), Color.White);
    spriteBatch.End();
}
base.Draw(gameTime);

There are two buffers that it switches between, the previous frame and the current one. That’s simply how modern graphics works. Your game gets to fill one of the two buffers in your draw routine while the other one is being displayed to the screen, and then they swap places.

The best way to deal with this is to just always draw, and not have the draw function affect the state of the game.

2 Likes