Skip to content

floooh/sokol-zig

Repository files navigation

build

Auto-generated Zig bindings for the sokol headers.

For Zig version 0.13.0 and 0.14.0-dev

In case of breaking changes in Zig, the bindings might fall behind. Please don't hesitate to ping me via a Github issue, or even better, provide a PR :)

Support for stable Zig versions is in branches (e.g. zig-0.12.0), those versions are 'frozen in time' though.

Related projects:

Building the samples

Supported platforms are: Windows, macOS, Linux (with X11) and web

On Linux install the following packages: libglu1-mesa-dev, mesa-common-dev, xorg-dev, libasound-dev (or generally: the dev packages required for X11, GL and ALSA development)

To build the platform-native samples:

# just build:
zig build
# build and run samples:
zig build run-clear
zig build run-triangle
zig build run-quad
zig build run-bufferoffsets
zig build run-cube
zig build run-noninterleaved
zig build run-texcube
zig build run-offscreen
zig build run-instancing
zig build run-mrt
zig build run-saudio
zig build run-sgl
zig build run-sgl-context
zig build run-sgl-points
zig build run-debugtext
zig build run-debugtext-print
zig build run-debugtext-userfont
zig build run-shapes

(also run zig build -l to get a list of build targets)

By default, the backend 3D API will be selected based on the target platform:

  • macOS: Metal
  • Windows: D3D11
  • Linux: GL

To force the GL backend on macOS or Windows, build with -Dgl=true:

> zig build -Dgl=true run-clear

The clear sample prints the selected backend to the terminal:

sokol-zig ➤ zig build -Dgl=true run-clear
Backend: .sokol.gfx.Backend.GLCORE33

For the web-samples, run:

zig build -Dtarget=wasm32-emscripten
# or to build and run one of the samples
zig build run-clear -Dtarget=wasm32-emscripten
...

When building with target wasm32-emscripten for the first time, the build script will install and activate the Emscripten SDK into the Zig package cache for the latest SDK version. There is currently no build system functionality to update or delete the Emscripten SDK after this first install. The current workaround is to delete the global Zig cache (run zig env to see where the Zig cache resides).

Improving the Emscripten SDK integration with the Zig build system is planned for the future.

How to integrate sokol-zig into your project

Add a build.zig.zon file to your project which has at least a .sokol dependency:

.{
    .name = "my_project",
    .version = "0.1.0",
    .paths = .{
        "src",
        "build.zig",
        "build.zig.zon",
    },
    .dependencies = .{
        .sokol = .{
            .url = "git+https://github.com/floooh/sokol-zig.git#[commit-hash]",
            .hash = "[content-hash]",
        },
    },
}

The easiest way to populate or update the sokol dependency is to run this on the cmdline:

zig fetch --save=sokol git+https://github.com/floooh/sokol-zig.git

This will automatically use the latest sokol-zig commit.

For a native-only project, a build.zig file looks entirely vanilla:

const std = @import("std");
const Build = std.Build;
const OptimizeMode = std.builtin.OptimizeMode;

pub fn build(b: *Build) !void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});
    const dep_sokol = b.dependency("sokol", .{
        .target = target,
        .optimize = optimize,
    });
   const hello = b.addExecutable(.{
        .name = "hello",
        .target = target,
        .optimize = optimize,
        .root_source_file = b.path("src/hello.zig"),
    });
    hello.root_module.addImport("sokol", dep_sokol.module("sokol"));
    b.installArtifact(hello);
    const run = b.addRunArtifact(hello);
    b.step("run", "Run hello").dependOn(&run.step);
}

If you also want to run on the web via -Dtarget=wasm32-emscripten, the web platform build must look special, because Emscripten must be used for linking, and to run the build result in a browser, a special run step must be created.

Such a 'hybrid' build script might look like this (copied straight from pacman.zig):

const std = @import("std");
const Build = std.Build;
const OptimizeMode = std.builtin.OptimizeMode;
const sokol = @import("sokol");

pub fn build(b: *Build) !void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});
    const dep_sokol = b.dependency("sokol", .{
        .target = target,
        .optimize = optimize,
    });

    // special case handling for native vs web build
    if (