Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to check error in GLFW

📅 2015-Sep-14 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ error, glfw ⬩ 📚 Archive

To check for errors in GLFW, we register an error callback function with GLFW and it is called automatically when there is an error. This is a cleaner method than having to check explicitly for errors, like in OpenGL or GLEW.

This callback can be registered with GLFW even before GLFW has been initialized. This is useful because errors in initialization can also be caught.

The error values I found in GLFW are:

GLFW_NOT_INITIALIZED
GLFW_NO_CURRENT_CONTEXT
GLFW_INVALID_ENUM
GLFW_INVALID_VALUE
GLFW_OUT_OF_MEMORY
GLFW_API_UNAVAILABLE
GLFW_VERSION_UNAVAILABLE
GLFW_PLATFORM_ERROR
GLFW_FORMAT_UNAVAILABLE

A typical usage of GLFW with error callback:

void ErrorCallback(int, const char* err_str)
{
    std::cout << "GLFW Error: " << err_str << std::endl;
}

void SomeInitFunction()
{
    // Register error callback first
    glfwSetErrorCallback(ErrorCallback);

    // Start GLFW next
    const int ret = glfwInit();
    if (GL_FALSE == ret)
    {
        std::cout << "GLFW Error!n";
    }

    // Other GLFW functions go here ...
}

Tried with: GLFW 3.1.1 and Ubuntu 14.04


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧