To use ImGui.NET in a graphics application, you must set up a windowing system (like SDL2 via Veldrid), a graphics device, and an ImGuiController. The ImGuiController acts as the bridge between your engine's input/rendering and the ImGui state.
Key lifecycle steps:
- Setup: Create the window and graphics device using
VeldridStartup.CreateWindowAndGraphicsDevice. - Controller Initialization: Instantiate
ImGuiController with the graphics device, framebuffer description, and window dimensions. - Input Handling: In your main loop, call
_controller.Update(deltaTime, snapshot) where snapshot is the input state from your window. - Rendering: Call
_controller.Render(gd, commandList) within your command list recording to draw the UI. - Cleanup: Dispose of the controller and graphics resources when the application exits.
// 1. Setup
VeldridStartup.CreateWindowAndGraphicsDevice(
new WindowCreateInfo(50, 50, 1280, 720, WindowState.Normal, "ImGui.NET Sample Program"),
new GraphicsDeviceOptions(true, null, true, ResourceBindingModel.Improved, true, true),
out _window,
out _gd);
// 2. Controller Initialization
_controller = new ImGuiController(_gd, _gd.MainSwapchain.Framebuffer.OutputDescription, _window.Width, _window.Height);
// 3. Main Loop
while (_window.Exists)
{
InputSnapshot snapshot = _window.PumpEvents();
_controller.Update(deltaTime, snapshot);
_cl.Begin();
_cl.SetFramebuffer(_gd.MainSwapchain.Framebuffer);
_controller.Render(_gd, _cl);
_cl.End();
_gd.SubmitCommands(_cl);
_gd.SwapBuffers(_gd.MainSwapchain);
}
// 4. Cleanup
_controller.Dispose();
_gd.Dispose();