【发布时间】:2013-05-20 05:25:25
【问题描述】:
我在 GLFW 的窗口创建方面遇到了一些问题。我想要一个能够在窗口模式和全屏模式之间切换的程序。要在 GLFW 2.7.8 中执行此操作,必须首先销毁活动窗口,然后创建一个新窗口。我读到 3.0 版支持多窗口,但仍在开发中。
我提供了自己的函数来处理键盘输入。使用最初的 400 x 400 窗口,程序按预期运行;它会在 f 或 F 时进入全屏,当按下退出键时会退出,当按下其他任何键时会报错。
但是,当进入全屏模式时,窗口对我提供的键盘功能无响应。它将继续运行 main() 中的循环,并响应glfwGetKey(GLFW_KEY_ESC) 测试之类的内容。不管我是否启用了鼠标光标,光标都不会出现。
再次创建全屏窗口,KeyboardCallback 函数返回主循环。我想了解为什么全屏窗口不能与我的键盘功能一起使用,以及为什么它不能正常显示。
我没有在窗口上绘制任何东西,因为我正在尝试专门获得各种窗口抽象库的经验。绘制一个简单的三角形并不能解决问题,全屏窗口仍然是黑色的。
我的代码如下:
#include <stdio.h>
#include <stdlib.h>
// glfw32 - 2.7.8
#include <GL\glfw.h>
#pragma comment (lib, "GLFW.lib")
#pragma comment (lib, "opengl32.lib")
using namespace std;
// constants and globals
const int WINDOW_WIDTH=400, WINDOW_HEIGHT=400;
static bool fullscreen=false;
// function declarations
void GLFWCALL KeyboardCallback(int key, int action);
void FatalError(const char* msg) {
fprintf(stderr, "ERROR: Failure in \"%s\"\n", msg);
exit(1);
}
int main() {
// ititialize GLFW
glfwInit();
glfwEnable(GLFW_MOUSE_CURSOR);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
// initial window, 400x400 with 32-bit depth buffer in windowed mode
glfwOpenWindow(WINDOW_WIDTH, WINDOW_HEIGHT, 0,0,0,0, 32, 0, GLFW_WINDOW);
glfwSetWindowTitle("Simple Title");
glfwSetWindowPos(0, 0);
// set custom keyboard callback
glfwSetKeyCallback(KeyboardCallback);
while (true) { // loop until exit
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers();
// debug
//printf("Looping...\n");
if ( glfwGetKey(GLFW_KEY_ESC) ) {break;}
}
glfwTerminate();
printf("\nHave a nice day\n");
return 0;
}
void GLFWCALL KeyboardCallback(int key, int action) {
//printf("In keyboard function\n");
if (action) { // if key DOWN,
switch(key) { // do something...
case 'f':
case 'F': {
fullscreen = !fullscreen;
printf("Toggle Fullscreen: %s\n", (fullscreen ? "On" : "Off"));
glfwCloseWindow();
if (! glfwOpenWindow(WINDOW_WIDTH, WINDOW_HEIGHT, 0,0,0,0, 32, 0,
fullscreen ? GLFW_FULLSCREEN : GLFW_WINDOW)) {
FatalError("toggle fullscreen");
}
glfwSetWindowTitle("New Title");
break;
}
case GLFW_KEY_ESC: {
printf("\nGoodbye cruel world...\n");
glfwTerminate();
exit(0);
}
default: {
printf("Key not implemented: %c\n", key);
break;
}
}
}
printf("Exiting keyboard function\n");
}
我从here 尝试了 James 的方法,但没有效果。是否有我忘记设置的 GLFW 标志?
编辑------- 5/20
我有一个想法。当窗口被销毁时,也许我的回调正在取消注册。事实证明,在创建新窗口时重新注册我的函数会使窗口响应。
虽然这解决了我原来的问题,但我现在面临一个新问题。我无法正确渲染到新窗口。我可以将glClear( GL_COLOR_BUFFER_BIT ); 插入到我的主循环中,这将成功地为新窗口设置背景颜色。由于某种原因,它不会与我的数组和缓冲区交互来绘制图像。
Code with attempted drawing
编辑------- 5/21
我将代码转换为 GLFW 3.0.0。窗口管理更干净(尽管更复杂),而且我没有渲染问题。可能是因为我必须明确说明要使用的当前上下文。
出于好奇和如果/当我返回 2.7 时,我仍然想解决渲染问题。
【问题讨论】:
-
在再次调用 glfwOpenWindow(..) 之前,您是否尝试过重置 glfw 窗口提示?
-
是的。我将提示中断到
WindowPrep函数中,并尝试在旧窗口被销毁和新窗口创建之间调用它。它对图像的渲染没有影响。 -
你不应该通过 GLFW 回调关闭 GLFW 窗口。