首页
/ imgui-plot 项目使用教程

imgui-plot 项目使用教程

2024-08-16 03:39:01作者:廉皓灿Ida

1. 项目的目录结构及介绍

imgui-plot 项目的目录结构如下:

imgui-plot/
├── examples/
│   ├── example_glfw_opengl3/
│   └── example_sdl_opengl3/
├── implot/
│   ├── implot.cpp
│   ├── implot.h
│   └── implot_internal.h
├── LICENSE
├── README.md
└── CMakeLists.txt

目录介绍

  • examples/: 包含示例代码,展示了如何使用 imgui-plot 库。
    • example_glfw_opengl3/: 使用 GLFW 和 OpenGL3 的示例。
    • example_sdl_opengl3/: 使用 SDL 和 OpenGL3 的示例。
  • implot/: 包含 imgui-plot 的核心文件。
    • implot.cpp: imgui-plot 的实现文件。
    • implot.h: imgui-plot 的头文件。
    • implot_internal.h: imgui-plot 的内部头文件。
  • LICENSE: 项目的许可证文件。
  • README.md: 项目的说明文档。
  • CMakeLists.txt: CMake 配置文件,用于构建项目。

2. 项目的启动文件介绍

项目的启动文件位于 examples/ 目录下,具体为 example_glfw_opengl3/example_sdl_opengl3/ 目录中的 main.cpp 文件。

example_glfw_opengl3/main.cpp

#include "implot.h"
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <GLFW/glfw3.h>

int main() {
    // 初始化 GLFW
    if (!glfwInit()) {
        return -1;
    }

    // 创建窗口
    GLFWwindow* window = glfwCreateWindow(1280, 720, "imgui-plot Example", nullptr, nullptr);
    if (window == nullptr) {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);
    glfwSwapInterval(1); // 启用垂直同步

    // 初始化 ImGui
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImPlot::CreateContext();
    ImGuiIO& io = ImGui::GetIO();
    (void)io;
    ImGui::StyleColorsDark();

    // 设置 ImGui 后端
    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL3_Init("#version 130");

    // 主循环
    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();

        // 开始新帧
        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();

        // 绘制图表
        if (ImPlot::BeginPlot("Example Plot")) {
            ImPlot::PlotLine("Line", x_data, y_data, 1000);
            ImPlot::EndPlot();
        }

        // 渲染
        ImGui::Render();
        int display_w, display_w;
        glfwGetFramebufferSize(window, &display_w, &display_w);
        glViewport(0, 0, display_w, display_w);
        glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
        glClear(GL_COLOR_BUFFER_BIT);
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

        glfwSwapBuffers(window);
    }

    // 清理
    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplGlfw_Shutdown();
    ImPlot::DestroyContext();
    ImGui::DestroyContext();

    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}

example_sdl_opengl3/main.cpp

#include "implot.h
登录后查看全文
热门项目推荐