GLFW is the windowing and input layer that most LWJGL 3 applications use. It handles OS window creation, OpenGL context setup, keyboard and mouse events, and the swap-buffer loop across Windows, macOS, and Linux. This guide walks through every step fromDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/LWJGL/lwjgl3/llms.txt
Use this file to discover all available pages before exploring further.
glfwInit to glfwTerminate, using patterns drawn directly from the LWJGL sample suite.
Initialize GLFW and install an error callback
Always install an error callback before calling any other GLFW function. If GLFW encounters a problem it will invoke the callback rather than silently failing.
GLFWErrorCallback.createPrint(System.err) allocates a native callback that forwards every error to System.err. You must free it on shutdown (see the cleanup step).glfwInit() returns false if the platform or environment doesn’t support GLFW. Always check the return value.Configure window hints
Window hints tell GLFW what kind of window and context to create. Set them after Common hints for OpenGL version targeting:
glfwInit but before glfwCreateWindow.| Hint | Purpose |
|---|---|
GLFW_CONTEXT_VERSION_MAJOR / MINOR | Minimum OpenGL version (e.g., 3, 3) |
GLFW_OPENGL_PROFILE | GLFW_OPENGL_CORE_PROFILE for core profile |
GLFW_OPENGL_FORWARD_COMPAT | Required on macOS for core profile |
GLFW_OPENGL_DEBUG_CONTEXT | Enables the OpenGL debug message extension |
Create a window
glfwCreateWindow returns a long handle — LWJGL represents all native pointers as long. A return value of NULL (0L) means creation failed.NULL for windowed mode. The fifth argument is for context sharing between windows.Register callbacks
GLFW delivers input and window events through callbacks you register once. LWJGL represents each callback as a functional interface backed by a native function pointer.Available callback types include key, character, mouse button, cursor position, cursor enter/leave, scroll, drop, window position, size, close, focus, iconify, maximize, content scale, and framebuffer size. See
Events.java in the LWJGL samples for a demonstration of all of them.Make the OpenGL context current
Before you can call any OpenGL function you must bind the GLFW window’s context to the calling thread, then create LWJGL’s capabilities wrapper.
GL.createCapabilities() queries the driver for all supported extensions and OpenGL versions. The returned GLCapabilities object is stored per-thread and consulted whenever you call an OpenGL function.Run the main loop
The main loop polls events, renders a frame, and swaps buffers until the user signals the window should close.
glfwSwapInterval(1) enables vsync by synchronising buffer swaps to the monitor refresh rate, which prevents screen tearing. Use 0 to disable it.Clean up
Release all resources in reverse order of acquisition. LWJGL callbacks are native allocations — failing to free them is a memory leak.
glfwFreeCallbacks (from org.lwjgl.glfw.Callbacks) iterates every window-level callback slot, resets each one to NULL, and calls Callback.free() on the previous value. Non-window callbacks such as the error callback and monitor callback must be freed separately.Putting it together
The following is the minimal skeleton of a working GLFW application:Next steps
- Add OpenGL rendering — see Rendering 2D and 3D Graphics with OpenGL
- Add Vulkan rendering — see Getting Started with Vulkan in LWJGL 3
- Add 3D audio — see 3D Audio Playback with OpenAL in LWJGL 3