extrawurst's blog

The cimgui project

• dlang and cpp

So I needed a flexible and easy to use intermediate GUI for one of my projects in D. There was a pretty descent library natively written in D called dimgui that I started to use. It made me fall in love with the simple concept of an IMGUI but I was soon wanting more features than this library provided. I started looking around for something similar but more mature. I found IMGUI written in C++ but with a very simple C-like interface. I wrote a c-wrapper cimgui for it and afterwards added bindings for the D programming lanugage in a derelict-like style.

See the original imgui example below in a D application: cimgui example

This is a screenshot of my user interface written in it: cimgui usage in my D project

This (pseudo) code shows the cimgui usage in the D programming language:

import derelict.imgui.imgui;

void main()
{
    ... load opengl and initialize window ...

    DerelictImgui.load(); // initialize dynamic bindings

    ImGuiIO* io = ig_GetIO();
    // callback that imgui calls to let us render everything for it 
    // (that makes imgui very platform independent, you can render using opengl, directX ... whatever)
    io.RenderDrawListsFn = &renderImgui;

    while(true)
    {
        io.MousePos = ImVec2(123,456);
        ... fill mouse and keyboard input info into the io variable ...

        ig_NewFrame(); // tell imgui that a new frame begins

        static float f = 0.0f;
        float[3] clear_color = [0.3f, 0.4f, 0.8f];

        ig_Text("Hello, world!");
        ig_SliderFloat("float", &f, 0.0f, 1.0f);
        ig_ColorEdit3("clear color", clear_color);

        if (ig_Button("Test Button")) { ... }

        // close this frames UI and makes imgui call our render callback
        ig_Render();
    }


    ig_Shutdown(); // shutdown
}

You can find the full example D project on github: imgui_d_test

comments powered by Disqus