The MonoGame Draw() Cycle
By Stephen Armstrong // November 3rd, 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.
The following is the default starting Draw() code in a new MonoGame project:
- protected override void Draw(GameTime gameTime)
- // TODO: Add your drawing code here
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.
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:
- // Sprites and text to render goes here
Your SpriteBatch is now set up.
Future tutorials will include examples of how to use the Draw() cycle and SpriteBatches for displaying text and sprites within your game.