Add support for Dear ImGui

Dear ImGui is a bloat-free graphical user interface library for C++ . It outputs optimized vertex buffers that you can render anytime in your 3D-pipeline enabled application. It is fast, portable, renderer agnostic and self-contained (no external dependencies).

Currently Dear ImGui library provides implementations for:

  1. dx9
  2. dx11
  3. dx12
  4. opengl2
  5. opengl3
  6. metal
  7. vulkan

And imgui_impl_sdl.cpp which maps the SDL2 input to imgui.

Could we have a project like SDL_Image (SDL_Imgui) which connects to the SDL_Renderer? There are already examples for SDL2+opengl inside the library. @icculus :blush:

I’m not against the idea, but Dear Imgui supports all the same rendering backends as SDL, so I’m not sure it’s worth the effort.

Im not sure if putting the SDL_renderer underneath imgui is a good idea.
Its basically transforming one render command queue into another, and while both sdl and imgui are working well in that regard i would wonder how well that would work out. And if there are any performance issues.

FWIW, having them work alongside each other is rather safe. Crate an SDL_Renderer with a fixed backend (and enable batching again, its nice), and then you should be able to get the SDL_Renderers GL context with SDL_GL_GetCurrentContext and init imgui with it. Afterwards you just need to flush one and then render the other.

EDIT: of course, now that i read my own writing, providing these exact steps inside an SDL_imgui would be a nice to have.

EDIT2: oh, and imgui is c++, while sdl is strictly c. Id prefer SDL_imgui in c++, however i can see reaons why you would want it not to be. and there are C bindings for imgui. It would be rather annyoing for people already using it in c++ though.

Dear ImGui doesn’t provide or support any backends or APIs. AFAIK it gives you lists of vertices, texture coords, color, etc., and leaves it up to your application to do the drawing. They do provide sample code that uses SDL for input + sample drawing code for DirectX, OpenGL, Metal, Vulkan, etc. that I’m sure a lot of people just copy/paste.

I think what @gavriil08 is asking for is SDL_Renderer’s functionality to be filled out enough that using it as an API-neutral backend for Dear ImGui is possible. That way 2D games that would otherwise do just fine with SDL_Renderer don’t have to write their own DirectX, OpenGL, etc., backends just to use Dear ImGui (or other similar immediate mode GUI libraries like Nuke).

As far as I can tell, the only thing SDL_Renderer would need added is the ability to pass it polygon vertices, color, and texture coords.

And I’m sure @gavriil08 is also asking for a sample Dear ImGui SDL_Renderer backend :wink:

@sjr you are on point!

I want to write 2D with just Sdl2, I don’t want to touch opengl or dx9 or whatever :slight_smile: but debugging is essential for me, so is imgui:)

1 Like

Hi there. I compiled ImGUI DX+SDL sample code provided with ImGUI, but now I’m fighting to introduce some SDL stuff in the main loop. I created a DX SDL_renderer, but I cannot succeed in displaying both SDL_renderer and ImGUI widgets at the same time… @gavriil08 did you advance on this subject ? Any hint ? Thanks.

Kind of, I ditched the SDL2 renderer in favour of sokol gfx(wonderful library btw). But you can also find a port of imgui to sdl2 on github (imgui-sdl or whatever) tho it’s not perfect and depending of number of widgets you use it will hit your FPS hard.

Hi, thx for your answer. I tried imgui-sdl by Tyyppi77 which is nice work, but alas, the rendering quality is not as “smooth” as what you can have directly with ImGUI / DX11, as you can see on the comparison image below (see the radio button, the white triangles, the fonts, the rounded widgets). The fact is that SDL does not provide nice way to render texture triangles as needed by ImGUI, so Tyyppi77 did what he could. And now that a big part of my game is developed in SDL, it will be hard for me to switch on another API…

I don’t think you absolutely need to change APIs, if you don’t need shaders I think staying with SDL2 is a very good option for rendering. Indeed imgui may not look as good but for what it does it’s enough. Good luck!

I’ve no idea if this is helpful, but my extensions to SDL2_gfx include the aaFilledPolygonRGBA() function which will render high quality antialiased triangles. You can find it here.

I so wish drawing a triangle could get added to SDL! I’ve not ported one of my games from native iOS to SDL Android/Windows purely because it can’t do something as simple as draw a triangle. :rage:

Did you try using another font ? I’m using DroidSans at size 19 or 20 + Awesome Fonts and it seems to be correct.

FYI , I put some screen shots there : https://framagit.org/ericb/miniDart

Last, there is another possibility, documented there : https://github.com/ocornut/imgui/blob/master/docs/FONTS.txt

HTH

@gavriil08 Well, I will continue to search a way to get the best rendering quality for imgui :wink:

@rtrussell Thx, I will definitely have a look at your triangles for my game’s need. But concerning imgui, I got in touch with Omar (the developer) who thinks that I do not need to use a SDL backend for graphics, and that SDL2 and imgui/DX should be able to share the same DX context. So I’m looking in this direction now.

@ericb I tried other fonts, with no better results. Tyyppi77 who wrotes this simple imgui-SDL backend admits that he does not introduce any antialiasing or fancy stuff, because his purpose was not quality but a simple debug tool.

@ binbinhfr

Did you try the freetype solution ? I tested it, and is seems to work well, but maybe I don’t care as much as you with that … :slight_smile:

aaFilledPolygonRGBA() is overkill (and consequently relatively slow) if all you need is antialiased triangles, but at least it’s an off-the-shelf solution. As far as fonts are concerned, SDL2_ttf (which uses FreeType) is perfectly capable of achieving the quality you want.

Just in case, and for the record, the topic was already discussed, and seems to be in good shape on Dear ImGui site : https://github.com/ocornut/imgui/tree/master/misc/freetype

Not sure it will fit your needs though …