-
下载源码:imgui github地址
-
将需要的文件拖拽入项目外部库的
imgui
文件夹
backends文件夹里选择与环境适配的文件,我这里用了glfw和opengl3
目录结构:
-
CMakeLists.txt
cmake_minimum_required(VERSION 3.24) project(proforlearn) set(CMAKE_CXX_STANDARD 17) file(GLOB SRC ${PROJECT_SOURCE_DIR}/external/imgui/*.h ${PROJECT_SOURCE_DIR}/external/imgui/*.cpp ) add_executable(proforlearn ${SRC} main.cpp) find_package(OpenGL REQUIRED) include_directories(${PROJECT_SOURCE_DIR}/external/glfw/include) link_directories(${PROJECT_SOURCE_DIR}/external/glfw/lib-mingw-w64) target_link_libraries(proforlearn opengl32 glfw3.dll) include_directories(${PROJECT_SOURCE_DIR}/external/imgui)
-
测试代码:
#include "imgui.h" #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" #include <GLFW/glfw3.h> int main(void) { GLFWwindow* window; /* Initialize the library */ if (!glfwInit()) return -1; /* Create a windowed mode window and its OpenGL context */ window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); if (!window) { glfwTerminate(); return -1; } /* Make the window's context current */ glfwMakeContextCurrent(window); IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplOpenGL3_Init("#version 330"); /* Loop until the user closes the window */ while (!glfwWindowShouldClose(window)) { /* Render here */ glClear(GL_COLOR_BUFFER_BIT); ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); ImGui::Begin("My name is window, ImGUI window"); ImGui::Text("Hello there new born!"); ImGui::End(); ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); /* Swap front and back buffers */ glfwSwapBuffers(window); /* Poll for and process events */ glfwPollEvents(); } ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); glfwTerminate(); return 0; }
-
运行结果:glfw黑框中成功出现imgui文本界面!