cap frame rate to 30 fps?

How do I cap the frame rate of my game to 30 fps? Thank you.

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    graphics.GraphicsProfile = GraphicsProfile.HiDef;
    Content.RootDirectory = "Content";

    // >>>>>>> These two lines below >>>>>>>>>>
    this.IsFixedTimeStep = true;//false;
    this.TargetElapsedTime = TimeSpan.FromSeconds(1d / 30d); //60);

    this.IsMouseVisible = true;
    this.Window.AllowUserResizing = true;
    this.Window.ClientSizeChanged += HeyCallMeWhenTheUserResizesTheWindow;
}
public void HeyCallMeWhenTheUserResizesTheWindow(object sender, EventArgs e)
{
    //graphics.ApplyChanges();
    /* we are here when the windows being resized */
    uiInput.SetWindowSizeRequiredForMouseLook(GraphicsDevice);
}
1 Like

The following code will tell MG to sync on every second V-sync.

public Game1()
{
            graphics = new GraphicsDeviceManager(this);
            
            graphics.PreparingDeviceSettings += (sender, e) => 
            {
                e.GraphicsDeviceInformation.PresentationParameters.PresentationInterval = PresentInterval.Two;
            };
}
2 Likes

Thanks, that works. I’m converting an old xna game to monogame. For some reason my game runs faster in xna then monogame. Is this normal? That’s why I’m trying to lock to 30 fps.

What initialization code are you using? Are you running at a fixed time step? With vsync?

1 Like

TargetElapsedTime = TimeSpan.FromSeconds(1d / 30d);

This is really the only line you need. Fixed time step is the default

2 Likes