Sithregal
Sithregal

Reputation: 60

GLFW non-responsive fullscreen window

I am having some issues with GLFW's window creation. 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. I read that version 3.0 has support for multiple windows, but it is still in development.

I have provided my own function to handle keyboard input. Using the initial 400 by 400 window, the program functions as expected; it will enter fullscreen on f or F, will exit when the escape key is pressed, and will complain when anything else is pressed.

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. 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. 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?

EDIT------- 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 ); 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

I converted my code to 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.

Upvotes: 1

Views: 1854

Answers (1)

datenwolf
datenwolf

Reputation: 162164

Right now the event queue of GLFW is not pumped. 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.

Upvotes: 3

Related Questions