GLFW入门教程02.02:空白界面

目的

显示一个最基本的窗口界面。

源码

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
#include <stdio.h>
#include <GLFW/glfw3.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;
}

此代码将作为后续开发的模板。

主要流程为

flowchart TB

A(Start) --> B[初始化]
B --> C[创建窗口]
C --> D[上下文设置]
D --> E[显示循环]
E --> F[释放资源]
F --> G(End)

函数调用图为

flowchart TB

A(Start) --> B[glfwInit]
B --> C[glfwCreateWindow]
C --> D[glfwMakeContextCurrent]
D --> E{glfwWindowShouldClose}
E --No--> F[glfwPollEvents]
F --> G[glfwGetFramebufferSize]
G --> H[glViewport]
H --> I[glClearColor]
I --> J[glClear]
J --> L[glfwMakeContextCurrent]
L --> M[glfwSwapBuffers]
M --> E
E --Yes--> O[glfwDestroyWindow]
O --> P[glfwTerminate]
P --> Q(End)

编译

1
2
all:
gcc main.c -o window -lglfw -lGL

结果

窗口


GLFW入门教程02.02:空白界面
https://blog.jackeylea.com/glfw/simple-window-of-glfw/
作者
JackeyLea
发布于
2024年8月4日
许可协议