Dear Bindings is tool to generate a C API for Dear ImGui, and metadata so other languages can easily generate their own bindings on top (see our Software using Dear Bindings list).
At present, it can convert imgui.h
(i.e. the main Dear ImGui API), and has (semi-experimental, but not totally untested) support for imgui_internal.h
and most of the backend headers.
It should be compatible with Dear ImGui v1.84 onwards (some earlier versions also work but compatibility isn't guaranteed).
The intention with Dear Bindings is to try and produce a C header file which is as close as reasonably possible to what a human would generate, and thus special attention has been given to preserving formatting, comments and the like such that (maybe!) a user won't even necessarily realise that they are working with a wrapper.
You can find prebuilt versions (consisting of cimgui.h
, cimgui.cpp
, cimgui.json
and their equivalents for imgui_internal.h
)
for both master
and docking
branch in our
Continuous Integration (Actions) page.
For a given build, click "Artifacts" to find them.
- Python 3.10+
- ply (Python Lex-Yacc, v3.11 tested)
(ply can be automatically installed via requirements.txt
- see "Usage" below for more details)
- v0.10 adds (somewhat experimental) support for comverting
imgui_internal.h
. - v0.08 adds structure default values to metadata, and fixes a few bugs.
- v0.07 adds some new metadata elements, new examples and fixes a number of bugs (especially around metadata and backends).
- v0.06 fixes a small issue with ImGui v1.90.0 WIP where
ListBox()
andComboBox()
have deprecated variants that cause name clashes. Those variants are now renamed toImGui_ListBoxObsolete()
andImGui_ComboBoxObsolete()
respectively. - v0.05 introduced significantly enhanced type information in the JSON output, and experimental support for generating bindings for ImGui backends
- Note that there are a number of small changes in the JSON format related to this that will require modification to code that consumes the JSON files - search Changelog.txt for
BREAKING CHANGE
for full details
- Note that there are a number of small changes in the JSON format related to this that will require modification to code that consumes the JSON files - search Changelog.txt for
- v0.04 introduced a number of bugfixes and other tweaks
You can see a full list of recent changes here.
Dear Bindings was designed as a potential replacement to the cimgui project.
dear_bindings | cimgui |
---|---|
Written in Python | Written in Lua |
Preserve comments and alignment | -- |
Use more polished rules to name functions, resolve overloads and offer simplified and *Ex alternatives. | -- |
Experimental bindings for imgui_internal.h (as a separate file). | Can generate bindings for imgui_internal.h (but output is in the same header, making it difficult to tell if you are using a public or internal function). |
Currently not mature, more likely to have issues | Has been used for years. |
If you don't have a Python environment, then install Python (at least version 3.10), and then in the project directory run:
pip install -r requirements.txt
...which should install the prerequisite Python libraries automatically.
Then you can do:
python dear_bindings.py -o cimgui ../imgui/imgui.h
...and if you want imgui_internal.h available as well:
python dear_bindings.py -o cimgui_internal --include ../imgui/imgui.h ../imgui/imgui_internal.h
For an all-in-one build (Windows-only right now), you can do:
BuildAllBindings.bat
With a target imgui.h
, Dear Bindings generates cimgui.h
(defines the C
API), cimgui.cpp
(implements the C binding to the underlying C++ code), and
cimgui.json
(a metadata file, see below).
Correspondingly, cimgui_internal.h
, cimgui_internal.cpp
and
cimgui_internal.json
contain the bindings for imgui_internal.h
, which has
a lot of useful functions for more advanced use-cases that are not exposed in the
main public API for one reason or another.
Using a C++ compiler, you can compile cimgui.cpp
along with imgui/*.cpp
into a static library. This can be used to integrate with a C program, for
example, by including the generated C header cimgui.h
and linking against
this library.
Other command line arguments:
usage: dear_bindings.py [-h] -o OUTPUT [-t TEMPLATEDIR]
[--nopassingstructsbyvalue] [--backend]
src
positional arguments:
src Path to source header file to process (generally
imgui.h)
options:
-h, --help show this help message and exit
-o OUTPUT, --output OUTPUT
Path to output files (generally cimgui). This should
have no extension, as <output>.h, <output>.cpp and
<output>.json will be written.
-t TEMPLATEDIR, --templatedir TEMPLATEDIR
Path to the implementation template directory
(default: ./src/templates)
--nopassingstructsbyvalue
Convert any by-value struct arguments to pointers (for
other language bindings)
--generateunformattedfunctions
Generate unformatted variants of format string
supporting functions
--backend Indicates that the header being processed is a backend
header (experimental)
--imgui-include-dir IMGUI_INCLUDE_DIR
Path to ImGui headers to use in emitted include files.
Should include a trailing slash (eg "Imgui/").
(default: blank)
--include INCLUDED_FILE
Path to additional .h files to include (e.g. imgui.h
if converting imgui_internal.h, and/or the file you
set IMGUI_USER_CONFIG to, if any)
--emit-combined-json-metadata
Emit a single combined metadata JSON file instead of
emitting separate metadata JSON files for each header
Result code 0 is returned on success, 1 on conversion failure and 2 on
parameter errors
You can find details of the cimgui.json
file format here.
The generated header should hopefully be relatively self-explanatory, but here are some of the key differences between it and the original ImGui C++ API:
C++ | C | Notes |
---|---|---|
ImGui::Text() |
ImGui_Text() |
The ImGui namespace is removed and everything inside it prefixed with ImGui_ . |
void ImGuiStyle::ScaleAllSizes(float scale_factor) |
void ImGuiStyle_ScaleAllSizes(ImGuiStyle* self, float scale_factor) |
Structures are also flattened, with member functions renamed to prefix the structure name and (if not static) take a pointer to the structure as the first argument. |
void ColorConvertRGBtoHSV(float r, float g, float b, float& out_h, float& out_s, float& out_v) |
void ImGui_ColorConvertRGBtoHSV(float r, float g, float b, float* out_h, float* out_s, float* out_v) |
Non-const references are converted to pointers. |
ImDrawList::PathLineTo(const ImVec2& pos) |
ImDrawList_PathLineTo(ImDrawList* self, ImVec2 pos) |
Const references are simply passed by-value. |
Since default parameters aren't available in C, multiple versions of functions are generated as a way of emulating them. Specifically, the "vanilla" version of a function will have all of the default arguments elided and set to their default, which a new version with Ex
appended to the name will be generated that allows all arguments to be set. For example, this function:
bool IsMouseDragging(ImGuiMouseButton button, float lock_threshold = -1.0f);
...becomes:
bool ImGui_IsMouseDraggingEx(ImGuiMouseButton button, float lock_threshold /* = -1.0f */);
bool ImGui_IsMouseDragging(ImGuiMouseButton button); // Implied lock_threshold = -1.0f
As can be seen, ImGui_IsMouseDragging()
takes just the (non-defaulted) button
argument and internally defaults lock_threshold
to -1.0f
, whilst the ImGui_IsMouseDraggingEx()
version allows both parameters to be set.
In cases of overloaded functions in the original API, since C does not support this the function names are changed to disambiguate them. The code attempts to generate a reasonably sensible disambiguation by using a minimal set of the argument types necessary to uniquely identify each version of the function. This behaviour combines with the expansion for default parameters, so for example:
bool ListBoxHeader(const char* label, int items_count, int height_in_items = -1);
bool ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0, 0));
Becomes these four functions in the C header:
bool ImGui_ListBoxHeaderExInt(const char* label, int items_count, int height_in_items /* = -1 */);
bool ImGui_ListBoxHeaderInt(const char* label, int items_count); // Implied height_in_items = -1
bool ImGui_ListBoxHeaderEx(const char* label, ImVec2 size /* = ImVec2(0, 0) */);
bool ImGui_ListBoxHeader(const char* label);
The generated code and metadata preserves #define
settings for various options (such as IMGUI_USE_BGRA_PACKED_COLOR
), so those can be utilised as normal. If the original ImGui code and user code is being compiled separately then care must be taken that the #define
settings are the same. For programmatic binding generation, the exported metadata contains information on which elements are affected by #ifdef
checks so appropriate action to match behaviour in the target language can be taken.
Constructors/destructors for heap objects are removed from the API, as ensuring the correct allocation/deallocation behaviour across library boundaries can be awkward and so it seemed safer to avoid them. ImGui support zero-clear construction, so data structures can almost always safely be constructed with a simple memset()
.
The one exception to this is ImVector
. There is one specific case where the ImGui API - ImFontGlyphRangesBuilder::BuildRanges()
- requires the user to construct a vector that the library will then write into (potentially performing allocations library-side). To facilitate this, two helper functions called ImVector_Construct()
and ImVector_Destruct()
are provided which can be used to construct and subsequently destroy an ImVector (of any type).
Utilising these, the safe pattern for using ImFontGlyphRangesBuilder::BuildRanges()
looks like this:
ImFontGlyphRangesBuilder builder;
memset(&builder, 0, sizeof(builder));
ImFontGlyphRangesBuilder_Clear(&builder);
ImFontGlyphRangesBuilder_AddChar(&builder, L'!');
ImFontGlyphRangesBuilder_AddChar(&builder, L'?');
ImVector_ImWchar ranges;
ImVector_Construct(&ranges); // Construct new vector using ImGui's heap functions
ImFontGlyphRangesBuilder_BuildRanges(&builder, &ranges);
for (int i = 0; i < ranges.Size; i++)
{
// Do something with ranges.Data[i]
}
ImVector_Destruct(&ranges); // Free the vector using ImGui's heap functions
Templates are expanded into their concrete instantiations, so for example ImVector<char>
gets expanded to ImVector_char
. Functions are removed from templates because at present in the cases where they presently exist they are generally hard to use correctly from C (see the above notes about constructors) and thus it seemed simpler/safer to have users interact directly with the structure contents if they need to.
See the note above about
ImVector_Construct
for an exception to this rule.
These minor features are removed, mostly because they either rely on C++ language features to function correctly or are helpers that don't make sense as part of the bindings.
ImGuiOnceUponAFrame, ImNewDummy, ImNewWrapper, ImGui::Value
A semi-experimental feature has been added to generate binding for the various backends.
To convert a backend header, use --backend
on the command line - for example:
python dear_bindings.py --backend -o cimgui_impl_opengl3 imgui\backends\imgui_impl_opengl3.h
Tested Backends:
- Win32
- DirectX 9
- DirectX 11
- DirectX 12
- OpenGL 2
- OpenGL 3
- Vulkan
- SDL 2
All other backends (except Metal/OSX) at least appear to convert cleanly with reasonable looking results. Further
testing (adding to the list above) would be most appreciated.
The Metal/OSX backends have been excluded for now as the Objective-C code in them looks like it would probably make life
painful. Please provide feedback if there is a use-case for these.
The BuildAllBindings.bat
file can be used to convert imgui.h
, imgui_internal.h
and all of the convertable backends.
Some simple example/test programs can be found in the examples/
folder.
They assume that C bindings files (for both imgui.h
and imgui_internal.h
) have been generated into the generated/
folder (BuildAllBindings.bat
can be used to do this automatically on Windows, I'm afraid other OSes will have to do it by hand for now).
example_null
is a very basic app that simply runs a few cycles of the ImGui update/draw loop. It has no rendering engine so nothing actually gets drawn.
example_win32_directx9
and example_sdl2_opengl2
are the ImGui samples of the same names with minimal changes to port it into C.
The Examples.sln solution file can be used to build all three examples on Windows using Visual Studio 2022 (older versions may work too).
On Windows ImGuiLib
is used as an ancillary project to provide ImGui wrapped up as a static library with C function exports.
To build example_sdl2_opengl
, you will need to have SDL2 installed and the SDL2_DIR environment variable set to point to your SDL2 installation.
example_null
and example_sdl2_opengl2
both contain makefiles that should build correctly on OSX and Linux (tested on Mac OS Sonoma and Ubuntu 22.04.3 LTS).
You'll need SDL2 installed via brew install SDL2
on OSX or apt install libsdl2-dev
or similar for that sample to work.
These samples do not use ImGuiLib but just link the required object files directly.
See our Software using Dear Bindings wiki page.
Developed by Ben Carter and other contributers.
Much of the imgui_internal.h
support was added by @ZimM-LostPolygon.
Dear Bindings is copyright (c) 2021-2024 and licensed under the MIT license. See LICENSE.txt for full details.
You can get in touch with me via e-mail at "contact at-sign shironekolabs dot com".