Description: Use packaged imgui
 Use the packaged libimgui -- adapt CMakeLists.txt and to obsolete functions removed
 in newer imgui.
Author: Tobias Frost <tobi@debian.org>
Forwarded: https://github.com/RobertBeckebans/RBDOOM-3-BFG/pull/657
Last-Update: 2024-11-06
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
--- a/neo/CMakeLists.txt
+++ b/neo/CMakeLists.txt
@@ -69,6 +69,10 @@
 option(USE_SYSTEM_RAPIDJSON
                 "Use the system rapidjson instead of the bundled one" OFF)
 
+option(USE_SYSTEM_IMGUI
+                "Use the system imgui instead of the bundled one" OFF)
+
+
 set(CPU_TYPE "" CACHE STRING "When set, passes this string as CPU-ID which will be embedded into the binary.")
 
 # SRS - Turn on optimization when cross-compiling from Apple arm64 to x86_64
@@ -355,6 +359,18 @@
 endif (JPEG_FOUND)
 
 
+if (USE_SYSTEM_IMGUI)
+    find_package(PkgConfig REQUIRED)
+    pkg_check_modules(IMGUI REQUIRED imgui)
+endif()
+
+if(IMGUI_FOUND)
+    include_directories(${IMGUI_INCLUDE_DIRS})
+else()
+    include_directories("libs/imgui/../")
+    set(IMGUI_LIBRARIES "")
+endif()
+
 macro(SET_OPTION option value)
   set(${option} ${value} CACHE "" INTERNAL FORCE)
 endmacro()
@@ -481,7 +497,6 @@
 endif (RAPIDJSON_FOUND)
 
 
-	
 add_subdirectory(idlib)
 
 file(GLOB AAS_INCLUDES aas/*.h)
@@ -534,8 +549,13 @@
 file(GLOB IRRXML_INCLUDES libs/irrxml/src/*.h)
 file(GLOB IRRXML_SOURCES libs/irrxml/src/*.cpp)
 
+if(NOT USE_SYSTEM_IMGUI)
 file(GLOB IMGUI_INCLUDES libs/imgui/*.h)
 file(GLOB IMGUI_SOURCES libs/imgui/*.cpp)
+else()
+set(IMGUI_INCLUDES "")
+set(IMGUI_SOURCES "")
+endif()
 
 file(GLOB STB_INCLUDES libs/stb/*.h)
 
@@ -1867,6 +1887,7 @@
             ${PNG_LIBRARY}
             ${JPEG_LIBRARY}
             ${GLEW_LIBRARY}
+            ${IMGUI_LIBRARIES}
 	    	${CMAKE_DL_LIBS}
 			)
 	endif()
--- a/neo/imgui/BFGimgui.h
+++ b/neo/imgui/BFGimgui.h
@@ -2,7 +2,7 @@
 #ifndef NEO_IMGUI_BFGIMGUI_H_
 #define NEO_IMGUI_BFGIMGUI_H_
 
-#include "libs/imgui/imgui.h"
+#include "imgui/imgui.h"
 
 #include "../idlib/math/Vector.h"
 
--- a/neo/renderer/GuiModel.cpp
+++ b/neo/renderer/GuiModel.cpp
@@ -31,7 +31,7 @@
 #pragma hdrstop
 
 #include "RenderCommon.h"
-#include "libs/imgui/imgui.h"
+#include "imgui/imgui.h"
 
 const float idGuiModel::STEREO_DEPTH_NEAR = 0.0f;
 const float idGuiModel::STEREO_DEPTH_MID  = 0.5f;
--- a/neo/renderer/Image_intrinsic.cpp
+++ b/neo/renderer/Image_intrinsic.cpp
@@ -30,7 +30,7 @@
 #include "precompiled.h"
 #pragma hdrstop
 
-#include "libs/imgui/imgui.h"
+#include "imgui/imgui.h"
 
 #include "RenderCommon.h"
 #include "SMAA/AreaTex.h"
@@ -942,7 +942,11 @@
 
 	// Store our identifier
 	//io.Fonts->TexID = ( void* )( intptr_t )image->GetImGuiTextureID();
+#if (IMGUI_VERSION_NUM < 19140)
 	io.Fonts->TexID = ( void* )( intptr_t )declManager->FindMaterial( "_imguiFont" );
+#else
+	io.Fonts->TexID = ( ImTextureID )( intptr_t )declManager->FindMaterial( "_imguiFont" );
+#endif
 
 	// Cleanup (don't clear the input data if you want to append new fonts later)
 	//io.Fonts->ClearInputData();
--- a/neo/renderer/RenderSystem_init.cpp
+++ b/neo/renderer/RenderSystem_init.cpp
@@ -31,7 +31,7 @@
 #include "precompiled.h"
 #pragma hdrstop
 
-#include "libs/imgui/imgui.h"
+#include "imgui/imgui.h"
 
 #include "RenderCommon.h"
 
@@ -1620,7 +1620,11 @@
 
 #if IMGUI_BFGUI
 	ImGuiIO& io = ImGui::GetIO();
+#if (IMGUI_VERSION_NUM < 19140)
 	io.Fonts->TexID = ( void* )( intptr_t )tr.imgGuiMaterial;
+#else
+	io.Fonts->TexID = ( ImTextureID )( intptr_t )tr.imgGuiMaterial;
+#endif
 #endif
 }
 
--- a/neo/ui/DeviceContext.cpp
+++ b/neo/ui/DeviceContext.cpp
@@ -31,7 +31,7 @@
 
 #include "DeviceContext.h"
 
-#include "libs/imgui/imgui.h"
+#include "imgui/imgui.h"
 #include "../renderer/GuiModel.h"
 
 extern idCVar in_useJoystick;
--- a/neo/imgui/BFGimguiImpl.cpp
+++ b/neo/imgui/BFGimguiImpl.cpp
@@ -249,7 +249,9 @@
 	g_DisplaySize.y = windowHeight;
 	io.DisplaySize = g_DisplaySize;
 
+#if (IMGUI_VERSION_NUM <= 16000)
 	io.RenderDrawListsFn = idRenderBackend::ImGui_RenderDrawLists;
+#endif
 
 	// RB: FIXME double check
 	io.SetClipboardTextFn = SetClipboardText;
@@ -291,7 +293,11 @@
 			byte* pixels = NULL;
 			io.Fonts->GetTexDataAsRGBA32( &pixels, &width, &height );

+#if (IMGUI_VERSION_NUM < 19140)
 			io.Fonts->TexID = ( void* )image;
+#else
+			io.Fonts->TexID = ( ImTextureID )image;
+#endif
 		}
 	}
 }
@@ -437,6 +443,10 @@
 		//ImGui::End();
 
 		ImGui::Render();
+#if (IMGUI_VERSION_NUM > 16000)
+		idRenderBackend::ImGui_RenderDrawLists(ImGui::GetDrawData());
+#endif
+
 		g_haveNewFrame = false;
 	}
 }
--- a/neo/renderer/OpenGL/RenderBackend_GL.cpp
+++ b/neo/renderer/OpenGL/RenderBackend_GL.cpp
@@ -48,7 +48,7 @@
 #include "../RenderBackend.h"
 #include "../../framework/Common_local.h"
 
-#include "../../imgui/imgui.h"
+#include "imgui/imgui.h"
 
 idCVar r_drawFlickerBox( "r_drawFlickerBox", "0", CVAR_RENDERER | CVAR_BOOL, "visual test for dropping frames" );
 idCVar stereoRender_warp( "stereoRender_warp", "0", CVAR_RENDERER | CVAR_ARCHIVE | CVAR_BOOL, "use the optical warping renderprog instead of stereoDeGhost" );
--- a/neo/tools/imgui/lighteditor/LightEditor.cpp
+++ b/neo/tools/imgui/lighteditor/LightEditor.cpp
@@ -714,7 +714,11 @@
 #if !IMGUI_BFGUI
 			ImGui::Image( ( void* )currentTexture->GetImGuiTextureID(), size, ImVec2( 0, 0 ), ImVec2( 1, 1 ),
 #else
+#if (IMGUI_VERSION_NUM < 19140)
 			ImGui::Image( ( void* )currentTextureMaterial, size, ImVec2( 0, 0 ), ImVec2( 1, 1 ),
+#else
+			ImGui::Image( (ImTextureID)(intptr_t)currentTextureMaterial, size, ImVec2( 0, 0 ), ImVec2( 1, 1 ),
+#endif
 #endif
 						  ImColor( 255, 255, 255, 255 ), ImColor( 255, 255, 255, 128 ) );
 		}
