As developers, many of us have faced the pain of introducing in writing interfaces to our programs. Traditional GUI libraries add a degree of complexity which you may not want if you are making tools that are intended for a form of tasks such as debugging. Here we present a library that makes it realistic to create loggers, profilers, debuggers operating theater fifty-fifty an entire back making editor program quickly and easily. The entire example presented here is acquirable on Github.

Update 2022-02-23 : Screencast Available

Based on the feedback from this blog post, we've recorded a screencast where we perform all the stairs and express the results live. We plan to do more of these screencasts in the future, so pleased stay tuned to this blog for future updates.

Besides, for more video contented about using Conan, we've publicised a large collection of video recording-based content organized into self-paced training courses in the JFrog Academy.

Dear ImGui?

Dear ImGui is an awful C++ GUI depository library mainly used in game developement. The project is ASCII text file software system, licensed under MIT license. Dear ImGui focuses on easiness and productivity using what is called Immediate Mode GUI paradigm.

Direct mode GUI's are disparate from the traditional maintained-musical mode interfaces in that widgets are created and drawn on each frame vs the time-honored approach of first creating a widget and adding callbacks to it. Some of the benefits of this paradigm are your UI "lives closer" to your information and that it allows for fast prototyping.

Dear ImGui is mainly designed for developers to employment in content world and debug tools. It's renderer agnostic in the way that you have to provide the tools to render the data but IT's very easy to integrate into your have code as it has multiple bindings for diametrical windowpane and events handling libraries (like GLFW, SDL2 and Flood) and multiple renderers (like OpenGL, DirectX and Vulkan).

Dear ImGui comes with lots of widgets same windows, labels, input boxes, progress bars, buttons, sliders, trees, etc. You can see whatsoever examples in the image at a lower place.

Different Dear ImGui widgets

Integrating Dear ImGui in your application

The veritable use of ImGui is when you already have a 3D-pipeline enabled application suchlike a subject founding or game developing tool where you wishing to add a Graphical user interface. Let's understand how soft it is to integrate ImGui in our application. Our example covering renders a triangle using OpenGL3. We will exercise GLFW to do window creation and events handling. As ImGui is sovereign of the rendering system and platform we have to bring in some binding for our interpreting system. Luckily, there are many premade bindings in Dear ImGui's repo. As we will use Dear ImGui v1.74 these are the ones we testament need:

  • imgui_impl_opengl3.cpp
  • imgui_impl_opengl3.h
  • imgui_impl_glfw.cpp
  • imgui_impl_glfw.h

The minimal code to make this work is in main.cpp. First, you initialize the window for rendering and so you have to initialize a Dear ImGui context and the helper chopine and Renderer bindings. You can commute the rendering style if you want A well.

                                  // Setup Dearly ImGui context                  IMGUI_CHECKVERSION                  ();                  ImGui                  ::                  CreateContext                  ();                  ImGuiIO                  &                  io                  =                  ImGui                  ::                  GetIO                  ();                  // Setup Platform/Renderer bindings                  ImGui_ImplGlfw_InitForOpenGL                  (                  windowpane                  ,                  true                  );                  ImGui_ImplOpenGL3_Init                  (                  glsl_version                  );                  // Setup Dear ImGui style                  ImGui                  ::                  StyleColorsDark                  ();                              

Then you enter the main application loop where you can clearly see the difference with the neoclassic maintained mode Graphical user interface's.

                                  patc                  (                  !                  glfwWindowShouldClose                  (                  window                  ))                  {                  glfwPollEvents                  ();                  glClearColor                  (                  0.45                  f                  ,                  0.55                  f                  ,                  0.60                  f                  ,                  1.00                  f                  );                  glClear                  (                  GL_COLOR_BUFFER_BIT                  );                  // feed inputs to dear imgui, start new frame                  ImGui_ImplOpenGL3_NewFrame                  ();                  ImGui_ImplGlfw_NewFrame                  ();                  ImGui                  ::                  NewFrame                  ();                  // rendering our geometries                  triangle_shader                  .                  use                  ();                  glBindVertexArray                  (                  vao                  );                  glDrawElements                  (                  GL_TRIANGLES                  ,                  3                  ,                  GL_UNSIGNED_INT                  ,                  0                  );                  glBindVertexArray                  (                  0                  );                  // give your Graphical user interface                  ImGui                  ::                  Begin                  (                  "Present window"                  );                  ImGui                  ::                  Button                  (                  "Hello!"                  );                  ImGui                  ::                  End                  ();                  // Render dear imgui into test                  ImGui                  ::                  Delive                  ();                  ImGui_ImplOpenGL3_RenderDrawData                  (                  ImGui                  ::                  GetDrawData                  ());                  int                  display_w                  ,                  display_h                  ;                  glfwGetFramebufferSize                  (                  window                  ,                  &ere;                  display_w                  ,                  &                  display_h                  );                  glViewport                  (                  0                  ,                  0                  ,                  display_w                  ,                  display_h                  );                  glfwSwapBuffers                  (                  window                  );                  }                              

And, we must act some cleanup when the loop ends.

                                  ImGui_ImplOpenGL3_Shutdown                  ();                  ImGui_ImplGlfw_Shutdown                  ();                  ImGui                  ::                  DestroyContext                  ();                              

So, this is what we pose:

ImGui Hello World

Let's say, for example, that we neediness to change the triangle's position/orientation and colour. That would live as simple as career some sliders and a color picker and passing the data to the trigon via shader uniforms:

                                  // render your GUI                  ImGui                  ::                  Begin                  (                  "Triangulum Position/Color"                  );                  static                  float                  rotation                  =                  0.0                  ;                  ImGui                  ::                  SliderFloat                  (                  "revolution"                  ,                  &                  rotation                  ,                  0                  ,                  2                  *                  PI                  );                  static                  float                  translation                  []                  =                  {                  0.0                  ,                  0.0                  };                  ImGui                  ::                  SliderFloat2                  (                  "position"                  ,                  translation                  ,                  -                  1.0                  ,                  1.0                  );                  static                  float                  color                  [                  4                  ]                  =                  {                  1.0                  f                  ,                  1.0                  f                  ,                  1.0                  f                  ,                  1.0                  f                  };                  // return the parameters to the shader                  triangle_shader                  .                  setUniform                  (                  "gyration"                  ,                  rotation                  );                  triangle_shader                  .                  setUniform                  (                  "rendering"                  ,                  translation                  [                  0                  ],                  translation                  [                  1                  ]);                  // color picker                  ImGui                  ::                  ColorEdit3                  (                  "color"                  ,                  color                  );                  // multiply triangle's discolour with this color                  triangle_shader                  .                  setUniform                  (                  "colorise"                  ,                  color                  [                  0                  ],                  colourize                  [                  1                  ],                  color                  [                  2                  ]);                              

Change triangle's color

There are some basic lottery tools likewise.

Render Conan logo

If you want to explore the different library widgets and options the best way to have it off is to make a call option to ImGui::ShowDemoWindow() and have a take the different examples.

Scope up a fancy with Conan

Setting up a project that uses ImGui is a matter of minutes with Conan. A Conan bundle for ImGui has been created and added to Conan-Center already. The example shown here is using Windows and Visual Studio 2022 but it is very siamese in MacOS or Linux.

If you lack to generate a try tou can download all the files from the Conan examples repo:

                                  stinkpot clone https://github.com/conan-io/examples.lowlife                  cd                  examples/libraries/loved-imgui/basic                              

First, Lashkar-e-Taiba's inspect the CMake project. It has the bindings for GLFW and OpenGL3 and 2 more files to handle OpenGL shaders and data file reading. It will too copy the shaders that render the triangle to the working directory each time the application is recompiled.

                                  cmake_minimum_required                  (VERSION 3.0)                  project                  (dear-imgui-conan 120)                  set                  (CMAKE_PREFIX_PATH                  ${                  CMAKE_BINARY_DIR                  }                  )                  set back                  (CMAKE_MODULE_PATH                  ${                  CMAKE_BINARY_DIR                  }                  )                  # CONFIG option is important so                  # CMake doesn't explore for modules in default directory                  find_package                  (imgui CONFIG)                  find_package                  (glfw CONFIG)                  find_package                  (glew CONFIG)                  add_executable                  (                  dear-imgui-conan                     main.cpp                     opengl_shader.cpp                     file_manager.cpp                     opengl_shader.h                     file_manager.h                     bindings/imgui_impl_glfw.cpp                     bindings/imgui_impl_opengl3.cpp                     bindings/imgui_impl_glfw.h                     bindings/imgui_impl_opengl3.h                     assets/simple-shader.vs                     assets/orbiculate-shader.fs                  )                  add_custom_command                  (TARGET                     dear-imgui-conan                     POST_BUILD                     COMMAND                  ${                  CMAKE_COMMAND                  }                  -E copy                  ${                  PROJECT_SOURCE_DIR                  }/assets/simple-shader.vs                  ${                  PROJECT_BINARY_DIR                  }                  COMMAND                  ${                  CMAKE_COMMAND                  }                  -E copy                  ${                  PROJECT_SOURCE_DIR                  }/assets/three-needled-shader.fs                  ${                  PROJECT_BINARY_DIR                  }                  )                  target_compile_definitions                  (sincere-imgui-conan         PUBLIC IMGUI_IMPL_OPENGL_LOADER_GLEW                  )                  target_link_libraries                  (high-priced-imgui-conan                     imgui::imgui                     glfw::glfw                     glew::glew                  )                              

We will also need the conanfile to declare the libraries it depends connected. Besides from the GLFW library we already talked about we need the GLEW library to cover OpenGL functions lading. We will exercise cmake_multi to generate projects for Debug and Release configurations. An imports section was also added to download the required bindings for GLFW and OpenGL3.

                                  [requires]     imgui/1.74     glfw/3.3.2     glew/2.1.0     [generators]     cmake_find_package_multi      [imports]     ./misc/bindings, imgui_impl_glfw.cpp -> ../bindings     ./misc/bindings, imgui_impl_opengl3.cpp -> ../bindings     ./misc/bindings, imgui_impl_glfw.h -> ../bindings     ./misc/bindings, imgui_impl_opengl3.h -> ../bindings                              

Now countenance's build the project and run the application.

                                  compact disc                  dear-imgui-conan-deterrent example                  mkdir                  build                  candela                  build     conan                  install                  ..                  -s                  build_type                  =Release     conan                  install                  ..                  -s                  build_type                  =Debug     cmake ..                  -G                  "Visual Studio 15 2022 Win64"                  cmake                  --build                  .                  --config                  Release                  Cd                  Liberation     sincere-imgui-conan                              

Conclusions

Earnest ImGui is a powerful library with an easy to utilize API which integrates into 3D-pipeline enabled applications almost seamlessly. IT has lots of widgets and hindquarters be a great joyride to make debugging software such as profilers, loggers or targe editors of any gentle. Likewise, unexampled functionalities like docking OR multiple viewports are currently being developed. Now it's metre to experimentation with the library and qualification it interact with your own cypher!