GLFW在Visual Studio 2017上打开后立即关闭

glfw closes as soon as it opens on visual studio 2017

本文关键字:Visual Studio 2017 GLFW      更新时间:2023-10-16

此测试代码会导致 opengl 窗口一出现就打开并消失。是的,我正在从源代码编译库。

我的代码有什么问题?

#include<gladglad.h>
#include<GLFWglfw3.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
if (glfwInit() == false)
{
    fprintf(stderr, "GLFW Failed to initialise");
    return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow * window = glfwCreateWindow(640, 480, "Test OPENGL", NULL, NULL);
if (!window)
{
    fprintf(stderr, "window failed to open");
    glfwTerminate();
    return -1;
}
glfwMakeContextCurrent(window);
//system("pause");
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
    fprintf(stderr, "Failed to initialize GLAD");
    return -1;
}
return 0;
}

您需要一个事件循环来泵送消息队列和交换缓冲区:

while( !glfwWindowShouldClose(window) )
{
    glfwPollEvents();
    // draw stuff
    glfwSwapBuffers(window);
}