【问题标题】:OpenGL /GLUT Creating window after mainEventLoop()OpenGL /GLUT 在 mainEventLoop() 之后创建窗口
【发布时间】:2012-11-11 12:40:00
【问题描述】:

我正在使用 OpenGL/GLUT 开发游戏,我需要打开一个新窗口来显示游戏获胜时的分数。

为此,我将调用glutCreateWindow() 并在调用mainEventLoop() 后注册回调。

这有问题吗?我应该怎么做呢?

【问题讨论】:

    标签: c++ opengl glut freeglut glutcreatewindow


    【解决方案1】:

    这有问题吗?

    是的。

    为什么不直接在游戏的同一个窗口中绘制结果呢?

    您为什么首先使用 GLUT?这不是一个很好的游戏框架。最好使用 GLFW 或 SDL。

    我应该如何正确地做?

    通过向您的引擎添加一个小型 GUI 系统,您可以在屏幕上覆盖统计数据(如 HUD)和得分屏幕。

    【讨论】:

    • 谢谢。我刚开始学习计算机图形学,所以这就是我使用 glut,freeglut 更准确的原因。所以我想知道是否可以按照我的要求做?
    • @coredump:您可以从用户输入事件和空闲处理程序创建额外的窗口。但是创建额外的窗口只会分散用户的注意力。对于游戏来说,这显然不是正确的方法。
    【解决方案2】:

    您将需要两个显示回调函数,display( )display2( ) 用于每个窗口加上 window = glutCreateWindow("Window 1");window2 = glutCreateWindow("Window 2");

    代码示例:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <GL/glut.h>
    
    int window2 = 0, window = 0, width = 400, height = 400;
    
    void display(void)
    {
        glClearColor(0.0, 1.0, 1.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
        glLoadIdentity();
        printf("display1\n");
        glFlush();
    }
    
    void display2(void)
    {
        glClearColor(1.0, 0.0, 0.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
        glLoadIdentity();
        printf("display2\n");
        glFlush();
    }
    
    void reshape (int w, int h)
    {
        glViewport(0,0,(GLsizei)w,(GLsizei)h);
        glutPostRedisplay();
    }
    
    int main(int argc, char **argv)
    {
        // Initialization stuff
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB);
        glutInitWindowSize(width, height);
    
        // Create  window main
        window = glutCreateWindow("Window 1");
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
        glutInitWindowPosition(100,100);
    
        // Create second window
        window2 = glutCreateWindow("Window 2");
        glutDisplayFunc(display2);
        glutReshapeFunc(reshape);
    
        // Enter Glut Main Loop and wait for events
        glutMainLoop();
    return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-11
      • 1970-01-01
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多