0
\$\begingroup\$

I am working on creating a game engine using GLFW. To begin, I set up a window class in C++ to test how making a window works. For some reason, when I call the glfwCreateWindow function, it returns NULL instead of GLFWwindow*. Here is my code (I have a header file that defines the class and then the c++ file that defines the class):

Here is the window.h header file:

#pragma once

#include <iostream>
#include <GLFW/glfw3.h>

namespace harmony { namespace graphics {

    class Window
    {
    private:
        const char* m_Title;
        int m_Width, m_Height;
        GLFWwindow* m_Window;
        bool m_Closed;
    public:
        Window(const char* name, int width, int height);
        ~Window();
        bool closed() const;
        void update() const;
    private:
        bool init();
    };

} }

And here is the c++ window.cpp file:

#include "window.h"

namespace harmony { namespace graphics {

    Window::Window(const char* title, int width, int height)
    {
        m_Title = title;
        m_Width = width;
        m_Height = height;
        if (!init())
            glfwTerminate();
    }

    Window::~Window()
    {
        glfwTerminate();
    }

    bool Window::init()
    {
        if (!glfwInit)
        {
            std::cout << "Failed to initialized" << std::endl;
            return false;
        }

        m_Window = glfwCreateWindow(m_Width, m_Height, m_Title, NULL, NULL);
        if (!m_Window)
        {
            std::cout << "Failed to create window!" << std::endl;
            return false;
        }
        glfwMakeContextCurrent(m_Window);

        return true;
    }

    bool Window::closed() const
    {
        return glfwWindowShouldClose(m_Window);
    }

    void Window::update() const
    {
        glfwPollEvents();
        glfwSwapBuffers(m_Window);
    }

} }

And lastly here's my main.cpp:

#include "window.h"

int main()
{
    using namespace harmony;
    using namespace graphics;

    Window window("Yay it works!", 800, 600);

    while (!window.closed()) {
        window.update();
    }
    return 0; 
}

I looked it up and there are only a couple cases (in the source code of glfwCreateWindow) where it returns NULL, but I haven't found a good way to troubleshoot which one.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ glfwInit is a function. You’re missing parentheses. \$\endgroup\$
    – Rish
    Apr 13, 2020 at 18:02

1 Answer 1

0
\$\begingroup\$

To troubleshoot glfw, define the glfw error callback:

void error_callback( int error, const char *msg ) {
    std::string s;
    s = " [" + std::to_string(error) + "] " + msg + '\n';
    std::cerr << s << std::endl;
}

Before initnializing glfw, set the callback:

glfwSetErrorCallback( error_callback );

Give it a few hints:

if( GL_TRUE != glfwInit() )
    std::cerr << "Error initialising glfw" << std::endl;
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 4 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 5 );
// If you want the opengl debug context from 4.3 onwards, which you do:
glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE );
glfwWindowHint( GLFW_CLIENT_API, GLFW_OPENGL_API );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );

And the window should open effortlessly, assuming the OS base with driver works edit: and the function glfwInit() is called instead of its pointer questioned.

\$\endgroup\$
3
  • \$\begingroup\$ Thank you so much! It turned out to be the hints that fixed it... \$\endgroup\$ Apr 13, 2020 at 18:20
  • \$\begingroup\$ Not solely. You must also call glfwInit() as @Rish has correctly observed. The function pointer will always be non-NULL but glfw will not beinitialized if you omit the (). \$\endgroup\$
    – user136710
    Apr 13, 2020 at 18:33
  • \$\begingroup\$ Right, I forgot that I changed that as well. \$\endgroup\$ Apr 13, 2020 at 19:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .