Question On GLFWwindow

Hello, So spending a couple of days trying to find the answer to my problem of the GLFWwindow* window not being initialized in another function. My solution I found was making the GLFWwindow* window = glfwGetCurrentContext; This has solved my issue, but now I am unsure if this was the right way to solve the problem, help will be greatly appreciated!

You should either pass the window variable in to the function, or have it accessible via global data. For example the following code alters the example from the GLFW documentation to move the loop into a function called myWindowFunction:

#include <GLFW/glfw3.h>

void myWindowFunction( GLFWwindow* window )
{
    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }
}

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    / * call function which uses window */
	myWindowFunction( window );

    glfwTerminate();
    return 0;
}
1 Like

Hello, so I approach this a little differently, so I have different functions for different parts of the code, I have it made a certain way to allow for different renderers in the future. So to my understanding this too works?

int Engine::EngineInitialize(int Renderer)
{
    if (Renderer == 1)
    {
        glfwInit();
        if (!glfwInit())
            return -1;

        glfwWindowHint(GLFW_DECORATED, GLFW_TRUE);


        GLFWwindow* window = glfwCreateWindow(2560, 1440, "Engine", NULL, NULL);
        if (!window)
        {
            glfwTerminate();
            return -1;
        }
        
        glfwMakeContextCurrent(window);*/

        const char* glsl_version = "#version 150";
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
       

        GLenum err = glGetError();
        GLenum glewinit = glewInit();
        if (glewinit != GLEW_OK)
        {
            std::cout << "Glew Error!" << glewinit;
            exit(EXIT_FAILURE);
        }
}

int Engine::EngineLoop(int Renderer)
{
    
    if (Renderer == 1)
    {
        GLFWwindow* window = glfwGetCurrentContext();

        while (!glfwWindowShouldClose(window))
        {
            glfwSwapInterval(1);
            processInput(window, 65);
            processInput(window, 83);
            glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

            /* Render here */

            glClear(GL_COLOR_BUFFER_BIT);

            /* Swap front and back buffers */
            glfwSwapBuffers(window);

            /* Poll for and process events */
            glfwPollEvents();

        }
        glfwDestroyWindow(window);
        glfwTerminate();
        return 0;
        }
}

You can do it this way, but I would not recommend it.

Unless you are experienced I would also not try to allow for different renderers in the future but instead get this working with GFLW and OpenGL. Once you have experience with this combination you can design a way to support multiple renderer backends.

1 Like

Hello (Thank you for fixing my code post, I am not experienced with posting code :grinning_face_with_smiling_eyes:)

Thank you, if I understand this correctly, the way I choose can work, but by recommended you mean use the code you stated above, and I completely agree with the different renderers, as I still am a beginner, so adding Metal Support, and DirectX shouldn’t be my priority.

Yes, and note that GLFW is a cross platform windowing library, and though it has APIs for easy use with OpenGL and Vulkan (which are cross platform APIs), it is possible to use it with DirectX using the native access APIs - I am less familiar with familiar with Metal but it should also be possible with that API.

1 Like

Sounds good, thank you :slightly_smiling_face: