win11: cmake+glfw+imgui

  1. 下载源码:imgui github地址

  2. 将需要的文件拖拽入项目外部库的imgui文件夹
    在这里插入图片描述
    backends文件夹里选择与环境适配的文件,我这里用了glfw和opengl3
    在这里插入图片描述
    目录结构:
    在这里插入图片描述

  3. 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)
    
  4. 测试代码:

    #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;
    }
    
  5. 运行结果:glfw黑框中成功出现imgui文本界面!
    在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值