在Visual Studio 2015中,OpenGL程序退出并显示代码1

7

我最近将我的电脑升级到了Windows 10,并安装了Visual Studio 2015。我试图在Visual Studio 2015中编写一个“Hello OpenGL”程序,项目已经成功构建,但是它以代码1退出。我得到的只是创建的窗口很快就出现并消失了。以下是我的代码:

#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(800, 600);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Hello OpenGL");

    glutMainLoopEvent();

    return 0;
}

如我之前所提到的,该项目已成功构建,以下是构建结果:

1>------ Build started: Project: HelloGL, Configuration: Debug Win32 ------
1>  main.cpp
1>  HelloGL.vcxproj -> D:\OpenGL Projects\HelloGL\Debug\HelloGL.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

但是当我按下F5调试程序时,结果却让我感到沮丧:

The thread 0x23d4 has exited with code 1 (0x1).
The thread 0x20b8 has exited with code 1 (0x1).
The thread 0x10d0 has exited with code 1 (0x1).
The program '[7040] HelloGL.exe' has exited with code 1 (0x1).

设置断点,然后调试。 - Ali Kazmi
4
我认为这很合理。如果没有窗口事件的回调,那么这个程序除了立即退出之外就无事可做了。 - Andon M. Coleman
@AliKazmi 感谢您的回复。实际上,我已经尝试在调用glutMainLoopEvent()处设置断点,这会产生一个控制台窗口和一个普通窗口,所以我猜问题出在'gutMainLoopEvent()'上。 - dulq
2个回答

8

首先,感谢回复我的人。 我已经弄清楚了问题,需要为窗口注册回调函数,因此下面是运行代码:

#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>

// myDisplay
void myDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
    glFlush();
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(800, 600);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Hello OpenGL");

    // Register a callback function for the window's repainting event
    glutDisplayFunc(myDisplay);

    glutMainLoop();

    return 0;
}

哎呀,这篇文章两个小时前在哪里啊?! - svarog

3

请使用 glutMainLoop 替代 glutMainLoopEvent

glutMainLoopEvent 是 FreeGLUT 特有的函数,允许将 GLUT 事件分派放入自定义编写的循环中;因此必须在循环内部调用它,并且决定何时退出程序由您决定。

glutMainLoop 实现了自己的主循环,并在最后一个窗口关闭时退出程序。


谢谢你的回复,@datenwolf。事实上我首先使用了 glutMainLoop,然后我改用了 glutMainLoopEvent,但都没有起作用。 - dulq

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接