【问题标题】:glfw closes as soon as it opens on visual studio 2017glfw 在 Visual Studio 2017 上打开后立即关闭
【发布时间】:2017-11-18 02:14:35
【问题描述】:

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

我的代码有什么问题?

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

【问题讨论】:

  • 你没有事件循环。
  • 本应如此。您创建了一个窗口,但在您的程序终止后不久,您的窗口再次死亡。你需要某种循环来让你的程序保持活力。

标签: c++ visual-studio opengl glfw


【解决方案1】:

You need an event loop to pump the message queue and swap buffers:

while( !glfwWindowShouldClose(window) )
{
    glfwPollEvents();

    // draw stuff

    glfwSwapBuffers(window);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2014-06-14
    • 1970-01-01
    • 2017-08-20
    • 2017-11-21
    • 2014-11-23
    • 2018-05-02
    相关资源
    最近更新 更多