Home > Software engineering >  Why does the GLFW window open for a second and then close?
Why does the GLFW window open for a second and then close?

Time:08-30

I tried compiling and running this code for a few hours now and couldn't find out whats wrong, I tried taking out certain bits and pieces and adding more and linking libraries etc. but the window opens for a second then closes and doesn't leave any error messages. If it even matters or your curious, the IDE I'm using is Code blocks.

#include <GL/glew.h>
#define GLFW_DLL
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdbool.h>

// function declarations
void framebuffer_callback(GLFWwindow* window, int width, int height);
void process_input(GLFWwindow* window);

int main(void)
{
    // initialize GLFW
    if (!glfwInit())
    {
        fprintf(stderr, "Failed to initialize GLFW.\n");
        glfwTerminate();
        return -1;
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    #ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    #endif // __APPLE__

    // create the window
    GLFWwindow* window = glfwCreateWindow(800, 600, "Revised", NULL, NULL);

    if (window == NULL)
    {
        printf("Window failed.\n");
        glfwTerminate();
        return -1;
    }

    // put GLFW on the main thread and bind a frame buffer function
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_callback);

    // load all OpenGL function pointers
    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        fprintf(stderr, "Failed to load GLEW: %s\n", glewGetErrorString(err));
        return -1;
    }
    printf("GLEW version: %s\n", glewGetString(GLEW_VERSION));

    // mainloop
    if (!glfwWindowShouldClose(window))
    {
        // bind custom functions
        process_input(window);

        // double buffer and check for I/O events
        glfwPollEvents();
        glfwSwapBuffers(window);
    }

    glfwTerminate();
    return 0;
}

// function definitions
void framebuffer_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}

void process_input(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
    {
        glfwSetWindowShouldClose(window, GL_TRUE);
    }
}

CodePudding user response:

Your main loop is only an if condition, and not a while loop. So it makes sense that it only runs 1 time.

if (!glfwWindowShouldClose(window))

should be

while (!glfwWindowShouldClose(window))
  • Related