glfwGetFramebufferSize
Reads the size of the window's drawing area in PIXELS: the numbers glViewport needs.
Signature
void glfwGetFramebufferSize(GLFWwindow* window, int* width, int* height);
How it works
GLFW measures windows in two units. Screen coordinates are what you pass to glfwCreateWindow. Pixels are the real dots of the framebuffer. On an ordinary screen both are the same; on a Retina Mac or a Windows laptop at 150% scaling, an 800Ă—600 window can have a 1600Ă—1200 framebuffer.
OpenGL works in pixels, so glViewport takes this size. Using glfwGetWindowSize instead draws the scene into one quarter of the window on a Retina screen.
Parameters
The window to measure.
Where to write the width in pixels, or NULL.
Where to write the height in pixels, or NULL.
Tips & pitfalls
A minimized window reports 0Ă—0. Skip rendering then: a projection matrix with aspect = width / 0 is full of infinities.
To react to changes instead of asking every frame, use
glfwSetFramebufferSizeCallback.
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
1int fbW, fbH;
2glfwGetFramebufferSize(window, &fbW, &fbH);
3glViewport(0, 0, fbW, fbH);
4float aspect = fbH > 0 ? (float)fbW / fbH : 1.0f;