ggerganov / imtui

ImTui: Immediate Mode Text-based User Interface C++ Library
https://imtui.ggerganov.com
MIT License
2.94k stars 123 forks source link

TUI not displayed properly #18

Closed rxdu closed 3 years ago

rxdu commented 3 years ago

I tried to integrate imtui to an existing project which is already using imgui. You can find the project setup here: https://github.com/rxdu/imtoolkit/tree/imtui

But it seems the UI is not displayed properly. I think it might be the same issue as the one mentioned in #8 . A screenshot is given below:

image

What I have tried:

  1. Keep everything untouched in this imtui repo under master branch: works fine
  2. Keep everything untouched in this imtui repo under v1.79 branch: works fine
  3. In my imtoolkit repo, use imgui@v1.79 (official repo), imtui@v1.79: not working
  4. In my imtoolkit repo, use imgui@9874077 (official repo master at about a month ago), imtui@master: not working

I'm not sure if it's caused by any compile flags in cmake or if you've made any modifications to the imgui library? Any suggestions on how to fix this? Thank you!

ggerganov commented 3 years ago

The problem is that ImTui uses a modified version of ImGui, while in your project you link against the original ImGui which is not compatible with ImTui. I don't think there is a way currently to have both ImTui and the original ImGui in a single library - you need to have ImTui in a separate lib, linked against the modified ImGui.

Here is one solution that I tried and it works. It adds a second library imcore-tui that is linked with TUI samples. Not sure if this works for you, but here it is anyway:

diff --git a/src/imcore/CMakeLists.txt b/src/imcore/CMakeLists.txt
index 0e06141..31ad787 100644
--- a/src/imcore/CMakeLists.txt
+++ b/src/imcore/CMakeLists.txt
@@ -25,6 +25,12 @@ set(IMGUI_CORE_SRC
     imgui/imgui_widgets.cpp
     imgui/imgui_demo.cpp
 )
+set(IMGUI_FOR_IMTUI_CORE_SRC
+    imtui/third-party/imgui/imgui/imgui.cpp
+    imtui/third-party/imgui/imgui/imgui_draw.cpp
+    imtui/third-party/imgui/imgui/imgui_widgets.cpp
+    imtui/third-party/imgui/imgui/imgui_demo.cpp
+)
 set(IMGUI_BACKEND_SRC
     imgui/examples/imgui_impl_glfw.cpp
     imgui/examples/imgui_impl_opengl3.cpp
@@ -42,8 +48,9 @@ set(IMTUI_SRC
     imtui/src/imtui-impl-ncurses.cpp)
 set(IMTUI_DEP_LIBS ${CURSES_LIBRARIES})
 endif()
-add_library(imcore ${IMGUI_CORE_SRC} ${IMGUI_BACKEND_SRC} ${GL_LOADER_SRC} ${IMPLOT_SRC} ${IMTUI_SRC})
-target_link_libraries(imcore PUBLIC glfw OpenGL::GL ${GLFW3_LIBRARY} ${CMAKE_DL_LIBS} ${IMTUI_DEP_LIBS})
+
+add_library(imcore ${IMGUI_CORE_SRC} ${IMGUI_BACKEND_SRC} ${GL_LOADER_SRC} ${IMPLOT_SRC})
+target_link_libraries(imcore PUBLIC glfw OpenGL::GL ${GLFW3_LIBRARY} ${CMAKE_DL_LIBS})
 target_compile_definitions(imcore PUBLIC "-DIMGUI_IMPL_OPENGL_LOADER_GL3W")
 target_include_directories(imcore PUBLIC
     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
@@ -53,4 +60,12 @@ target_include_directories(imcore PUBLIC
     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/imtui/include>
     $<INSTALL_INTERFACE:include>)

+add_library(imcore-tui ${IMGUI_FOR_IMTUI_CORE_SRC} ${IMPLOT_SRC} ${IMTUI_SRC})
+target_link_libraries(imcore-tui PUBLIC ${CMAKE_DL_LIBS} ${IMTUI_DEP_LIBS})
+target_include_directories(imcore-tui PUBLIC
+    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
+    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/imtui/third-party/imgui/imgui>
+    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/imtui/include>
+    $<INSTALL_INTERFACE:include>)
+
 add_subdirectory(sample)
diff --git a/src/imcore/sample/CMakeLists.txt b/src/imcore/sample/CMakeLists.txt
index b762a05..279dc42 100644
--- a/src/imcore/sample/CMakeLists.txt
+++ b/src/imcore/sample/CMakeLists.txt
@@ -5,4 +5,4 @@ add_executable(implot_sample implot_sample.cpp)
 target_link_libraries(implot_sample PRIVATE imcore)

 add_executable(imtui_sample imtui_sample.cpp)
-target_link_libraries(imtui_sample PRIVATE imcore)
+target_link_libraries(imtui_sample PRIVATE imcore-tui)
rxdu commented 3 years ago

I see. That answered my question. Thank you very much for your help!