GLFW: window & contextGLFW 3.0

glfwWindowHint

Sets one option for the NEXT window created with glfwCreateWindow: OpenGL version, profile, MSAA, visibility…

Signature

void glfwWindowHint(int hint, int value);

How it works

glfwCreateWindow has only a few parameters. Everything else is set beforehand with hints: you call glfwWindowHint once per option, then create the window.

Hints stay set for every later window until glfwDefaultWindowHints or glfwTerminate. A hint is a request: if the driver cannot give an OpenGL version you asked for, glfwCreateWindow fails with GLFW_VERSION_UNAVAILABLE instead of silently giving you another one.

Parameters

hintint

Which option to set.

Common values

GLFW_CONTEXT_VERSION_MAJOR / _MINOR
The OpenGL version to create, e.g. 4 and 6 for OpenGL 4.6. macOS stops at 4.1.
GLFW_OPENGL_PROFILE
GLFW_OPENGL_CORE_PROFILE (modern functions only), GLFW_OPENGL_COMPAT_PROFILE (old ones too) or GLFW_OPENGL_ANY_PROFILE.
GLFW_OPENGL_FORWARD_COMPAT
GLFW_TRUE removes everything deprecated. Required on macOS for any core context 3.2 or newer.
GLFW_CONTEXT_DEBUG
GLFW_TRUE asks for a debug context, so glDebugMessageCallback gets detailed messages. Old name: GLFW_OPENGL_DEBUG_CONTEXT.
GLFW_SAMPLES
Samples per pixel for MSAA on the default framebuffer, e.g. 4. Default 0 (off).
GLFW_DEPTH_BITS / GLFW_STENCIL_BITS
Bits of the depth and stencil buffers. Defaults 24 and 8, which is what you want.
GLFW_SRGB_CAPABLE
GLFW_TRUE asks for a framebuffer that can do sRGB conversion (with glEnable(GL_FRAMEBUFFER_SRGB)).
GLFW_RESIZABLE
GLFW_FALSE gives a window the user cannot resize. Default GLFW_TRUE.
GLFW_VISIBLE
GLFW_FALSE creates the window hidden; show it later with glfwShowWindow (e.g. after moving it).
GLFW_POSITION_X / _Y
GLFW 3.4: where the window opens, in screen coordinates. Default GLFW_ANY_POSITION (the system decides).
GLFW_SCALE_TO_MONITOR
GLFW_TRUE grows the window on high-DPI monitors (Windows, X11) so it keeps its apparent size.
valueint

The new value: a number, GLFW_TRUE / GLFW_FALSE, or a constant such as GLFW_OPENGL_CORE_PROFILE.

Tips & pitfalls

  • Hints must come AFTER glfwInit (which resets them) and BEFORE glfwCreateWindow.

  • Asking for 4.6 on a machine with only 4.1 (macOS, old GPUs) fails. Ask for the oldest version your code really needs.

  • String hints (such as the X11 class name) use glfwWindowHintString instead.

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.

Example

example.cpp
1glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
2glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
3glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
4#ifdef __APPLE__
5glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);           // macOS: 4.1 at most
6glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
7#endif
8glfwWindowHint(GLFW_SAMPLES, 4);                         // 4x MSAA
9GLFWwindow* window = glfwCreateWindow(1280, 720, "Hello", nullptr, nullptr);
Official GLFW documentation