简体   繁体   English

GLFW无响应全屏窗口

[英]GLFW non-responsive fullscreen window

I am having some issues with GLFW's window creation. 我在GLFW的窗口创建方面遇到了一些问题。 I am wanting to have a program capable of toggling between windowed and fullscreen mode. 我想要一个能够在窗口模式和全屏模式之间切换的程序。 To do this in GLFW 2.7.8 one must first destroy the active window, then create a new one. 要在GLFW 2.7.8中执行此操作,必须首先销毁活动窗口,然后创建一个新窗口。 I read that version 3.0 has support for multiple windows, but it is still in development. 我读到版本3.0支持多个窗口,但它仍处于开发阶段。

I have provided my own function to handle keyboard input. 我提供了自己的功能来处理键盘输入。 Using the initial 400 by 400 window, the program functions as expected; 使用最初的400乘400窗口,程序按预期运行; it will enter fullscreen on f or F, will exit when the escape key is pressed, and will complain when anything else is pressed. 它将在f或F上进入全屏,在按下退出键时将退出,并在按下任何其他内容时进行投诉。

However, when fullscreen mode is entered, the window becomes unresponsive with regards to my provided keyboard function. 但是,当输入全屏模式时,窗口对我提供的键盘功能无响应。 It will continue to run through the loop in main() and will respond to something like the glfwGetKey(GLFW_KEY_ESC) test. 它将继续在main()中循环运行,并将响应类似glfwGetKey(GLFW_KEY_ESC)测试的内容。 Regardless of if I have the mouse cursor enabled or not, the cursor does not appear. 无论是否启用了鼠标光标,光标都不会出现。

Again, the fullscreen window is created and the KeyboardCallback function returns back into the main loop. 同样,创建全屏窗口, KeyboardCallback函数返回主循环。 I wish to understand why the fullscreen window is not working with my keyboard function, and why it is not displaying properly. 我想了解为什么全屏窗口无法使用我的键盘功能,以及为什么它无法正常显示。

I am not drawing anything to the window, as I am trying to get some experience with various window abstraction libraries specifically. 我没有在窗口上绘制任何东西,因为我试图获得一些专门用于各种窗口抽象库的经验。 Drawing a simple triangle did nothing to solve he problem, and the fullscreen window remains black. 绘制一个简单的三角形没有解决他的问题,全屏窗口仍然是黑色的。

My code is as follows: 我的代码如下:

#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");
}

I tried James' approach from here but to no effect. 我从这里尝试了詹姆斯的方法但没有效果。 Are there GLFW flags I am forgetting to set? 我忘了设置GLFW标志吗?

EDIT------- 5/20 编辑------- 5/20

I had a thought. 我有一个想法。 Perhaps my callback was being unregistered when the window is destroyed. 当窗口被破坏时,我的回调可能是未注册的。 Turns out that re-registering my function when the new window is created made the window responsive. 事实证明,在创建新窗口时重新注册我的功能使窗口响应。

While this solves my original problem, I now face a new one. 虽然这解决了我原来的问题,但我现在面临一个新的问题。 I cannot render to the new window properly. 我无法正确渲染到新窗口。 I can insert glClear( GL_COLOR_BUFFER_BIT ); 我可以插入glClear( GL_COLOR_BUFFER_BIT ); into my main loop, which will successfully set a background colour for the new window. 进入我的主循环,它将成功为新窗口设置背景颜色。 For some reason it does not interact with my arrays and buffers to draw the image. 由于某种原因,它不与我的数组和缓冲区交互来绘制图像。
Code with attempted drawing 尝试绘图的代码

EDIT------- 5/21 编辑------- 5/21

I converted my code to GLFW 3.0.0. 我将我的代码转换为GLFW 3.0.0。 The window management is much cleaner (albeit more complex) and I do not have the rendering issue. 窗口管理更清晰(虽然更复杂),我没有渲染问题。 Probably because I explicitly have to state the current context to use. 可能是因为我明确要说明要使用的当前上下文。

I would still like to solve the rendering issue, both out of curiosity and for if/when I return to 2.7. 我仍然想解决渲染问题,既出于好奇,也出现了if / when返回2.7。

Right now the event queue of GLFW is not pumped . 现在GLFW的事件队列没有被抽取 You must either set the GLFW_AUTO_POLL_EVENTS option so that glfwSwapBuffers pumps the event queue, or you call glfwPollEvents at the start of a main event loop iteration. 您必须设置GLFW_AUTO_POLL_EVENTS选项以便glfwSwapBuffers泵送事件队列,或者在主事件循环迭代开始时调用glfwPollEvents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM