Search code examples
javaopengllwjglglfw

Purpose of False GLFW_VISIBILE Hint While Binding OpenGL Context


I'm currently following a tutorial that has the initialization code to create the window

private void init() {
    // Setup an error callback. The default implementation
    // will print the error message in System.err.
    errorCallback = GLFWErrorCallback.createPrint(System.err);
    glfwSetErrorCallback(errorCallback);

    // Initialize GLFW. Most GLFW functions will not work before doing this.
    if (!glfwInit()) {
        throw new IllegalStateException("Unable to initialize GLFW");
    }

    // Configure our window
    glfwDefaultWindowHints(); // optional, the current window hints are already the default
    glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable

    int WIDTH = 300;
    int HEIGHT = 300;

    // Create the window
    window = glfwCreateWindow(WIDTH, HEIGHT, "Hello World!", NULL, NULL);
    if (window == NULL) {
        throw new RuntimeException("Failed to create the GLFW window");
    }

    // Setup a key callback. It will be called every time a key is pressed, repeated or released.
    glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
        @Override
        public void invoke(long window, int key, int scancode, int action, int mods) {
            if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
                glfwSetWindowShouldClose(window, true); // We will detect this in our rendering loop
            }
        }
    });

    // Get the resolution of the primary monitor
    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    // Center our window
    glfwSetWindowPos(
            window,
            (vidmode.width() - WIDTH) / 2,
            (vidmode.height() - HEIGHT) / 2
    );

    // Make the OpenGL context current
    glfwMakeContextCurrent(window);
    // Enable v-sync
    glfwSwapInterval(1);

    // Make the window visible
    glfwShowWindow(window);
}

Notice the glfwWindowHint(GLFW_VISIBLE, GL_FALSE); hint is disabled, then after creating the window, setting the key callbacks, binding the opengl context, it's enabled again glfwShowWindow(window);

The documentation doesn't suggest doing anything like this, and removing both lines doesn't seem to change anything. Why disable the hint in the first place?

Tutorial: https://lwjglgamedev.gitbooks.io/3d-game-development-with-lwjgl/content/chapter1/chapter1.html


Solution

  • It disables visablity so it can set up the size, change position, and initialize the context without the client having to witness it. From the Window Visablity section in the GLFW docs:

    Windowed mode windows can be created initially hidden with the GLFW_VISIBLE window hint. Windows created hidden are completely invisible to the user until shown. This can be useful if you need to set up your window further before showing it, for example moving it to a specific location.

    Youre program follows that exact example by calling

    glfwSetWindowPos(
                window,
                (vidmode.width() - WIDTH) / 2,
                (vidmode.height() - HEIGHT) / 2
        );