GLFW: size, position & monitorsGLFW 3.3

glfwGetWindowContentScale

Reads the display scale the user chose for the window's monitor, e.g. 1.5 for 150%.

Signature

void glfwGetWindowContentScale(GLFWwindow* window, float* xscale, float* yscale);

How it works

On high-DPI screens people enlarge everything (125%, 150%, 200%) so text stays readable. The content scale is that factor. Multiply the size of your text and user interface by it, so a 16-pixel font becomes 24 pixels at 1.5 and looks the same size as everywhere else.

It is not the same as framebuffer size Ă· window size: on Windows and X11 the window size already grows with the scale when GLFW_SCALE_TO_MONITOR is on, on macOS it never does.

Parameters

windowGLFWwindow*

The window to ask about.

xscalefloat*

Where to write the horizontal scale, or NULL.

yscalefloat*

Where to write the vertical scale, or NULL.

Tips & pitfalls

  • It changes when the window moves to another monitor; glfwSetWindowContentScaleCallback tells you when.

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_PLATFORM_ERROR
The operating system or window system reported a failure; the description says which.

Example

example.cpp
1float sx, sy;
2glfwGetWindowContentScale(window, &sx, &sy);
3float fontPx = 16.0f * sy;   // same apparent size at 100%, 150%, 200%
Official GLFW documentation