ImGui入门教程03.01:Hello World

系列索引:ImGui入门教程教程

目的

测试ImGui库是否可以正常调用。

为了配合OpenGL学习教程,本系列界面库使用glfw3。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <GLFW/glfw3.h>
#include "imgui.h"

static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}

int main(int, char**)
{
// Setup window
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
return 1;

GLFWwindow* window = glfwCreateWindow(1280, 720, "GLFW Window", NULL, NULL);
if (window == NULL)
return 1;

glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync

// Main loop
while (!glfwWindowShouldClose(window))
{
// Poll and handle events (inputs, window resize, etc.)
glfwPollEvents();

int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

//display func

glfwMakeContextCurrent(window);
glfwSwapBuffers(window);
}

glfwDestroyWindow(window);
glfwTerminate();

return 0;
}

编译命令

1
2
all:
g++ main.cpp -o helloworld -lglfw -lGL -limgui -I/usr/include/imgui

运行输出为glfw窗口,效果参考GLFW入门教程02.02:空白界面,那么安装的库/头文件可以正常链接。


ImGui入门教程03.01:Hello World
https://blog.jackeylea.com/imgui/hello-world-of-imgui/
作者
JackeyLea
发布于
2024年8月6日
许可协议