GLFW: window & contextGLFW 3.0

glfwCreateWindow

Opens a window together with its OpenGL context, using the hints set before.

Signature

GLFWwindow* glfwCreateWindow(int width, int height, const char* title, GLFWmonitor* monitor, GLFWwindow* share);

How it works

One call gives you two things: the window on the screen and the OpenGL context (the GPU state every gl* call works on). The context is NOT current yet: call glfwMakeContextCurrent before any gl* call, and load the functions with glad after that.

The returned pointer is the handle every other window function takes. It is NULL when creation failed, most often because the OpenGL version you asked for with glfwWindowHint is not available.

Parameters

widthint

Width of the content area in screen coordinates (not counting the title bar). On high-DPI screens the framebuffer can have more pixels: see glfwGetFramebufferSize.

heightint

Height of the content area in screen coordinates.

titleconst char*

Text of the title bar, in UTF-8. Change it later with glfwSetWindowTitle.

monitorGLFWmonitor*

NULL for a normal window. A monitor (e.g. glfwGetPrimaryMonitor()) for full screen on it.

shareGLFWwindow*

NULL, or another window whose context should share objects (buffers, textures, shaders) with the new one.

Return value

The window handle, or NULL on failure (the error callback says why).

Tips & pitfalls

  • Always check for NULL. Using a NULL window crashes on the next call.

  • Calling gl* before glfwMakeContextCurrent and the glad loader crashes: the function pointers are still empty.

  • Width and height are what you asked for; the framebuffer size can differ. Read it with glfwGetFramebufferSize for glViewport.

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GLFW_NOT_INITIALIZED
Called before glfwInit or after glfwTerminate.
GLFW_INVALID_ENUM
A hint, mode, key or button argument is not a valid GLFW constant.
GLFW_INVALID_VALUE
A value is out of range, such as a size of zero or a context version that does not exist.
GLFW_API_UNAVAILABLE
The driver does not support OpenGL at all, or not the API asked for.
GLFW_VERSION_UNAVAILABLE
The requested context version (for example 4.6) is higher than the driver supports. On macOS the maximum is 4.1.
GLFW_FORMAT_UNAVAILABLE
No pixel format matches the hints, for example too many MSAA samples.
GLFW_PLATFORM_ERROR
The operating system or window system reported a failure; the description says which.

Example

example.cpp
1GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);
2if (!window) {
3    glfwTerminate();
4    return 1;
5}
6glfwMakeContextCurrent(window);
7if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) return 1;
Official GLFW documentation