Skip to content

SampleImplementation

Nigel Kennington edited this page Oct 25, 2023 · 10 revisions

For this stage, we will duplicate the larger example from the Dear Imgui readme so you can see some of the inherent differences in Monogame from the parent project.

If you have not already configured your game for the MonoGame.ImGuiNet project, you should start with the Project Setup page and then come back here.

Add the Renderer

First, we must add the following 4 sections of code to the basic Game1.cs.

  1. Declare a new variable for the ImgGuiRenderer to the class variables of Game1.cs:
public class Game1 : Game
{
    private GraphicsDeviceManager _graphics;
    private SpriteBatch _spriteBatch;
    public static ImGuiRenderer GuiRenderer;
  1. Instance the GuiRenderer in the Initialise method:
protected override void Initialize()
{
    GuiRenderer = new ImGuiRenderer(this);

    base.Initialize();
}
  1. Rebuild the font atlas in the LoadContent method:
protected override void LoadContent()
{
    _spriteBatch = new SpriteBatch(GraphicsDevice);

    GuiRenderer.RebuildFontAtlas();
}
  1. Call the layout methods after base.Draw in the Draw method:
protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    base.Draw(gameTime);

    GuiRenderer.BeforeLayout(gameTime);

    GuiRenderer.AfterLayout();
}

Think of these last two calls in the same way as MonoGame's _spriteBatch.Begin and _spriteBatch.End All of our actual drawing of Dear ImGui's elements will happen between these two lines.

Add a test tool

Populate the tool

Clone this wiki locally