OpenGL程序在Visual Studio 2015中以代码1退出

OpenGL program has exited with code 1 in Visual Studio 2015

本文关键字:代码 退出 2015 程序 Visual Studio OpenGL      更新时间:2023-10-16

我最近把我的电脑升级到Windows 10,并安装了Visual Studio 2015。我尝试在Visual Studio 2015中编写"Hello OpenGL"程序,该项目成功构建,但它已退出代码1。我所得到的只是创建的窗口出现和消失得很快。下面是我的代码:

#include <GLglew.h>
#include <GLfreeglut.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 ProjectsHelloGLDebugHelloGL.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).

首先,感谢给我回复的人。我已经找出了问题所在,我所需要做的就是为窗口注册一个回调函数,所以这里是运行代码:

#include <GLglew.h>
#include <GLfreeglut.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;
}

glutMainLoopEvent改为glutMainLoop

后面的glutMainLoopEvent是一个特定于FreeGLUT的函数,它允许在一个自定义的写循环中放置GLUT事件调度;因此,它必须从循环中调用,并由您决定何时退出程序。

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