【发布时间】:2012-11-11 12:40:00
【问题描述】:
我正在使用 OpenGL/GLUT 开发游戏,我需要打开一个新窗口来显示游戏获胜时的分数。
为此,我将调用glutCreateWindow() 并在调用mainEventLoop() 后注册回调。
这有问题吗?我应该怎么做呢?
【问题讨论】:
标签: c++ opengl glut freeglut glutcreatewindow
我正在使用 OpenGL/GLUT 开发游戏,我需要打开一个新窗口来显示游戏获胜时的分数。
为此,我将调用glutCreateWindow() 并在调用mainEventLoop() 后注册回调。
这有问题吗?我应该怎么做呢?
【问题讨论】:
标签: c++ opengl glut freeglut glutcreatewindow
这有问题吗?
是的。
为什么不直接在游戏的同一个窗口中绘制结果呢?
您为什么首先使用 GLUT?这不是一个很好的游戏框架。最好使用 GLFW 或 SDL。
我应该如何正确地做?
通过向您的引擎添加一个小型 GUI 系统,您可以在屏幕上覆盖统计数据(如 HUD)和得分屏幕。
【讨论】:
您将需要两个显示回调函数,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;
}
【讨论】: