Search code examples
cuser-interfacewindowglfw

GLFW Window does not close


The window never closes when clicking the close-button and closeWindowCallback() is never called. Why is that?

Running under Ubuntu 18.04, compiled with gcc.

#include <stdlib.h>
#include <stdio.h>
#include <GLFW/glfw3.h>

void error_callback(int error, const char *description)
{
  fprintf(stdout, "Error: %s\n", description);
}

void closeWindowCallback(GLFWwindow *window)
{
  printf("close\n");

  glfwSetWindowShouldClose(window, GL_TRUE);
}

int main(void)
{
  glfwSetErrorCallback(error_callback);
  if (!glfwInit())
  {

  }
  GLFWwindow *window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
  if (!window)
  {

  }
  glfwMakeContextCurrent(window);
  glfwSetWindowCloseCallback(window, closeWindowCallback);

  while (!glfwWindowShouldClose(window))
  {
  }
  glfwDestroyWindow(window);

  glfwTerminate();
  return 0;
}

Solution

  • I took it from the official documentation:

    while (!glfwWindowShouldClose(window))
    {
        render(window);
    
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    

    You need to poll events to close the X11 window.