liveBook · Manning

GLFW

As explained in Systems Programming with Zig

GLFW is a cross-platform library for creating windows and OpenGL contexts and handling input and related application events.

GLFW handles the platform-facing work of an OpenGL application: creating windows and contexts, processing input, and dealing with operating-system events. It leaves the actual graphics work to OpenGL. section 7.1.3 section 7.6

For a Zig application, install GLFW and translate its C header so Zig can call its functions. GLFW itself needs to dynamically link to system libraries on its supported platforms, even if you prefer to statically link other dependencies. section 7.2.1

Listing 7.2 Translating our header
const std = @import("std");
const Translator = @import("translate_c").Translator; #1

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const translate_c = b.dependency("translate_c", .{});

    const translator: Translator = .init(translate_c, .{ #2
        .c_source_file = b.path("src/c.h"),
        .target = target,
        .optimize = optimize,
        .link_system_libs = &.{
            .{ .name = "glfw3" }, #3
        },
    });

    const c = translator.mod; #4

    const mod = b.createModule(.{
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
        .imports = &.{
            .{ .name = "c", .module = c }, #5
        },
    });

    // ... other build stuff ...
}

The build script translates a root C header, links the glfw3 system library, and exposes the translated module to Zig code. section 7.2.1

A minimal window has a straightforward lifecycle: initialize GLFW, create a window, make its context current, and run a frame loop until the window should close. Each iteration swaps buffers and polls events. Destroy the window and terminate GLFW on exit. section 7.3.1

Listing 7.3 Ready, set, loop!
pub fn main() !void {
    const init_success = c.glfwInit(); #1
    if (init_success == c.GLFW_FALSE) return error.GlfwInitFailed; #1
    defer c.glfwTerminate(); #2

    const window = c.glfwCreateWindow(1280, 720,
    "learning opengl", null, null); #3
    if (window == null) return error.GlfwCreateWindowFailed; #3
    defer c.glfwDestroyWindow(window);

    c.glfwMakeContextCurrent(window); #4

    while (c.glfwWindowShouldClose(window) == c.GLFW_FALSE) { #5
        // ... handle input and draw stuff ...

        c.glfwSwapBuffers(window); #6
        c.glfwPollEvents(); #7
    }
}

This Zig listing checks initialization and window creation, uses defer for cleanup, and runs the window loop. section 7.3.1

Check errors explicitly: GLFW functions may return GLFW_FALSE for failure, while functions returning pointers typically use NULL. GLFW windows are double-buffered by default. The front framebuffer holds the visible pixels; drawing to a back framebuffer and swapping helps avoid displaying an incomplete frame. section 7.3.1

GLFW can request OpenGL features and acquire a context, but its default system headers may be missing or out of sync with the supported OpenGL version. A loader such as glad supplies a consistent OpenGL 3.3 Core header across platforms. section 7.1.3

How Systems Programming with Zig covers it

How deep it goes
Walks through C header translation, system linking, initialization, error checks, cleanup, and a frame loop, while leaving some GLFW features out.
Examples
Builds a blank 1280-by-720 window with a Zig build script and a GLFW frame loop.
What you can do after
Apply GLFW from Zig to create a window, manage its context, poll events, and clean up resources.
How it connects
Connects GLFW to OpenGL contexts, C interoperability, event loops, double buffering, and the glad loader.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.