Skip to content

Latest commit

 

History

History
87 lines (62 loc) · 2.29 KB

File metadata and controls

87 lines (62 loc) · 2.29 KB

Zig build package and bindings for GLFW 3.4

Getting started

Example build.zig:

const std = @import("std");

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

    const exe = b.addExecutable(.{ ... });

    const zglfw = b.dependency("zglfw", .{
        .target = target,
        .optimize = optimize,
    });
    exe.root_module.addImport("zglfw", zglfw.module("root"));

    if (target.result.os.tag != .emscripten) {
        exe.linkLibrary(zglfw.artifact("glfw"));
    }

    b.installArtifact(exe);
}

Now in your code you may import and use zglfw:

const glfw = @import("zglfw");

pub fn main() !void {
    try glfw.init();
    defer glfw.terminate();

    const window = try glfw.createWindow(600, 600, "zig-gamedev: minimal_glfw_gl", null, null);
    defer glfw.destroyWindow(window);

    // or, using the equivalent, encapsulated, "objecty" API:
    const window = try glfw.Window.create(600, 600, "zig-gamedev: minimal_glfw_gl", null, null);
    defer window.destroy();

    // setup your graphics context here

    while (!window.shouldClose()) {
        glfw.pollEvents();

        // render your things here

        window.swapBuffers();
    }
}

See zig-gamedev samples for more complete usage examples.

Usage with Vulkan

To match types from zglfw functions and Vulkan library import_vulkan option may be used. When using this option vulkan import must be provided to the root module.

Example build.zig with vulkan-zig:

const vulkan_headers = b.dependency("vulkan_headers", .{});
const vulkan = b.dependency("vulkan", .{
    .registry = vulkan_headers.path("registry/vk.xml"),
}).module("vulkan-zig");
exe.root_module.addImport("vulkan", vulkan);

const zglfw = b.dependency("zglfw", .{
    .target = target,
    .optimize = optimize,
    .import_vulkan = true,
});

const zglfw_mod = zglfw.module("root");
zglfw_mod.addImport("vulkan", vulkan);
exe.root_module.addImport("zglfw", zglfw_mod);

if (target.result.os.tag != .emscripten) {
    exe.linkLibrary(zglfw.artifact("glfw"));
}