Forked from imgui_sdl. Full credits go to Tyyppi77 for his initial implementation, I simply styled it to be similar to the other ImGUI bindings for my own projects at the time.
ImGuiSDLRenderer is a lightweight SDL2 based renderer for Dear ImGui. Dear ImGui is designed to be easily rendered using modern 3D renderers like OpenGL or DirectX, but while SDL2's renderer does use hardware acceleration (the beforementioned APIs) behind the scenes, it does not provide an interface to pass generic vertex data to OpenGL. ImGuiSDLRenderer implements the rendering using a combination of a software based triangle rasterizer and a simple textured/filled rectangle drawer from SDL2. To improve the performance, the slower triangle blits are cached into render textures.
ImGuiSDLRenderer consists of two files that you can simply add to your project to use ImGuiSDL:
- imgui_sdl_renderer.h
- imgui_sdl_renderer.cpp
There is a full usage example provided in example.cpp, and the public API is exposed and documented in imgui_sdl.h.
You need to use ImGui_ImplSDL2_InitForOpengl(window, NULL);
or any of the other existing SDL2 implementation initializers in order to have ImGUI work correctly on standalone, before you initialize the renderer. see the example in "examples" for more, which does indeed work for most/any platform with SDL2 supported.
Here's how you initialize ImGuiSDLRenderer:
// Create your SDL renderer or use an existing one.
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// Initialize ImGuiSDLRenderer by calling Initialize with your SDL_Renderer.
ImGui_ImplSDLRenderer_Init(renderer);
Then to render ImGui, call ImGui_ImplSDLRenderer_RenderDrawData
after calling ImGui::Render
. You probably want to do this after all other rendering that happens in your project:
ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
To cleanup at exit, you can call ImGui_ImplSDLRenderer_Shutdown()
, but that doesn't do anything critical, so if you don't care about cleaning up memory at application exit, you don't need to call this.
As you can see the results are not perfect, but this display is definitely good enough to be used as a debug UI which is the main use case for Dear ImGui anyways. The rendering is also done in a little bit simpler style than what Dear ImGui uses by default, mainly to increase the amount of rendering that can be done using rectangles.
The implementation doesn't rely on any non-standard SDL functionality, imgui_sdl_renderer.h simply includes SDL.h and imgui.h. You can easily change these two includes to point to the correct locations if you use some sort of other include file scheme.
Do note that this is just a renderer for SDL2. For input handling, you shoud use the great SDL2 implementation provided in the Dear ImGui repository, or you could of course roll your own event provider.