glfw3 为基础开发 GUI 彷佛是一个不错选项,有不少人尝试这么作了。今天也小试一把。git
工具: mingw(不是 mingw-w64),头文件 GLFW/ ,库文件 glfw3.dllgithub
须要注意,mingw-w64 的话,glfw3.dll 版本要匹配。须要用 mingw-w64 从新编译?windows
openGL 基本上 windows 都已经有安装了,不须要额外安装。(确认 C:\Windows\System32\opengl32.dll 存在)工具
代码:oop
#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); /* Loop until the user closes the window */ while (!glfwWindowShouldClose(window)) { /* Render here */ glClear(GL_COLOR_BUFFER_BIT); /* Swap front and back buffers */ glfwSwapBuffers(window); /* Poll for and process events */ glfwPollEvents(); } glfwTerminate(); return 0; }
编译指令:ui
gcc test.c glfw3.dll -lopengl32
第一次编译的时候,由于没有添加 -lopengl32,一直报错,找了很久:spa
undefined reference to `__imp_glClear'
添加 -lopengl32 以后就没问题了。code
=========================================blog
另外还有两个使用 glad 来载入 openGL 的例子。能够看下面:开发
=========================================
关于 windows 下 openGL/GLFS/glu 会默认打开一个 cmd console,能够参考这个这里:
https://stackoverflow.com/questions/5995433/removing-console-window-for-glut-freeglut-glfw
我选取的是 FreeConsole() 这个方案,不过,须要在全部 include 的最前面添加 #include <Winsock2.h>。这个方案实际上是在程序开始之初,直接把 console 释放掉,因此启动 GUI 程序时,会有黑影一闪而过。
=========================================
GLFS+IMGUI 的一个 demo :https://github.com/urddru/imgui-glfw
项目使用 cmake 来编译,可能须要小改点编译规则,须要 cmake 基础。