The MonoGame Draw() Cycle
By Stephen Armstrong // November 3, 2019
An introduction to MonoGame’s Draw cycle.
The Draw() cycle is where your game comes to life. During this cycle, you select the graphics and text to be rendered to the screen.
By default, your game will try to run the Draw() in Game1.cs sixty (60) times a second. It may not be a perfect sixty, and will be impacted if you try to draw too much.
To avoid impacting the Draw() performance, do not run any game logic here – it should ideally solely be for outputting graphics and other on-screen elements.
Starting code
The following is the default starting Draw() code in a new MonoGame project:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
GraphicsDevice.Clear() must always be called first. This tells the game to clear the screen. If not cleared, then previously-drawn graphics will still be displayed and it’ll be a mess.
The color (by default: Color.CornflowerBlue) will tell the game what default background to use. Try changing this to Color.Black or Color.Red and then running your game.
SpriteBatches
SpriteBatch allows 2D sprites and text to be rendered in groups – which is more efficient than rendering them one by one.
A MonoGame project creates and initializes a SpriteBatch (called spriteBatch) by default. But you must tell this spriteBatch to Begin() and End() before you can render text and sprites.
In the Draw() cycle, add the following code:
spriteBatch.Begin();
// Sprites and text to render goes here
spriteBatch.End();
Your SpriteBatch is now set up.
Further reading
Future tutorials will include examples of how to use the Draw() cycle and SpriteBatches for displaying text and sprites within your game.