summaryrefslogtreecommitdiff
path: root/build.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 11:13:20 -0300
committerGabriel Schneider <[email protected]>2026-11-12 13:22:02 -0300
commit86ffca0024326110962f0880727f4eb84e6a48d1 (patch)
tree055f489341053c1db258606bb63b282bc7345a89 /build.zig
parenta62a6aa896adda901d335ef38814a13b8839545c (diff)
downloadsketchbook-86ffca0024326110962f0880727f4eb84e6a48d1.tar.gz
sketchbook-86ffca0024326110962f0880727f4eb84e6a48d1.zip
sketchbook: frames for ffmpeg (--export, on a clock of its own), text sizes, a font from the environment
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'build.zig')
-rw-r--r--build.zig74
1 files changed, 72 insertions, 2 deletions
diff --git a/build.zig b/build.zig
index ef6bc0c..b980c91 100644
--- a/build.zig
+++ b/build.zig
@@ -176,6 +176,9 @@ pub fn build(b: *Build) void {
render_web.has_side_effects = true;
render_web.expectExitCode(0);
test_web.dependOn(&render_web.step);
+ // `Host.textSize` against the text drawn (test/text.zig).
+ const text = sb.app(.{ .name = "text", .root_source_file = b.path("test/text.zig") });
+ test_step.dependOn(&text.addScript(text.addExecutable(), b.path("test/text.script"), .{}).step);
// The examples, so they always compile.
for ([_][]const u8{ "pen", "flock" }) |example| {
test_step.dependOn(&sb.app(.{ .name = example, .root_source_file = b.path(b.fmt("examples/{s}.zig", .{example})) }).addLibrary().step);
@@ -183,6 +186,33 @@ pub fn build(b: *Build) void {
// The README's example, so the documentation always compiles.
test_step.dependOn(&sb.app(.{ .name = "readme", .root_source_file = b.path("test/readme.zig") }).addLibrary().step);
+ // Export: two builds of one sketch export the same frames, on the
+ // export's own clock (test/export.script); the raw stream is as long as
+ // its frames, up to where the sketch quits; and ffmpeg, if there is
+ // one, makes a video of it with as many.
+ const export_a = sb.app(.{ .name = "export-a", .root_source_file = b.path("test/export.zig") });
+ const export_b = sb.app(.{ .name = "export-b", .root_source_file = b.path("test/export.zig") });
+ const export_exe = export_a.addExecutable();
+ const export_options: App.ExportOptions = .{ .fps = 30, .frames = 5, .size = .{ 320, 180 } };
+ const exported = export_a.addScript(export_exe, b.path("test/export.script"), .{});
+ exported.addArg("--define");
+ exported.addPrefixedDirectoryArg("a=", export_a.addExport(export_exe, export_options).frames);
+ exported.addArg("--define");
+ exported.addPrefixedDirectoryArg("b=", export_b.addExport(export_b.addExecutable(), export_options).frames);
+ test_step.dependOn(&exported.step);
+ const raw = b.addSystemCommand(&.{ "sh", "-c",
+ \\set -e
+ \\test "$("$0" --state "$1" --export - --fps 30 --size 320x180 | wc -c)" = $((320 * 180 * 4 * 8))
+ \\command -v ffmpeg >/dev/null && command -v ffprobe >/dev/null || exit 0
+ \\"$0" --state "$1" --export - --fps 30 --size 320x180 | ffmpeg -loglevel error -y -f rawvideo -pix_fmt rgba -s 320x180 -r 30 -i - -pix_fmt yuv420p "$2"
+ \\test "$(ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of csv=p=0 "$2")" = 8
+ });
+ raw.addArtifactArg(export_exe);
+ _ = raw.addOutputFileArg("raw.state");
+ _ = raw.addOutputFileArg("raw.mp4");
+ raw.expectExitCode(0);
+ test_step.dependOn(&raw.step);
+
const reload = b.addRunArtifact(host_exe);
reload.addArgs(&.{ "--headless", "--fresh", "--script" });
reload.addFileArg(b.path("test/reload.script"));
@@ -946,6 +976,38 @@ pub const App = struct {
return run;
}
+ pub const ExportOptions = struct {
+ fps: u32 = 60,
+ /// Null: until the sketch quits (`f.quit()`).
+ frames: ?u64 = null,
+ /// The canvas of a sketch that takes the room it is given.
+ size: ?[2]u32 = null,
+ };
+
+ pub const Export = struct {
+ run: *Build.Step.Run,
+ /// `000000.png`, `000001.png`, ...
+ frames: Build.LazyPath,
+ };
+
+ /// Exports the frames of `exe` (`addExecutable`) as PNGs into a
+ /// directory of the cache: headless, from fresh state, frame k at
+ /// k/fps seconds whatever the wall clock does.
+ pub fn addExport(app: App, exe: *Build.Step.Compile, options: ExportOptions) Export {
+ const b = app.sb.b;
+ const run = b.addRunArtifact(exe);
+ run.addArg("--state");
+ _ = run.addOutputFileArg(b.fmt("{s}-export.state", .{app.name}));
+ run.addArg("--export");
+ const frames = run.addOutputDirectoryArg("frames");
+ run.addArgs(&.{ "--fps", b.fmt("{d}", .{options.fps}) });
+ if (options.frames) |n| run.addArgs(&.{ "--frames", b.fmt("{d}", .{n}) });
+ if (options.size) |size| run.addArgs(&.{ "--size", b.fmt("{d}x{d}", .{ size[0], size[1] }) });
+ run.setCwd(b.path("."));
+ run.expectExitCode(0);
+ return .{ .run = run, .frames = frames };
+ }
+
/// Prints the app's live state; `zig build probe -- --watch` follows it.
pub fn addProbe(app: App, state: ?[]const u8) *Build.Step.Run {
const b = app.sb.b;
@@ -1201,8 +1263,8 @@ pub const App = struct {
/// The conventional wiring. Installs the executable, and adds:
/// `run` (host + hot reload), `lib` (rebuild the library; use `--watch`),
- /// `web` and `serve` (with `web` options), `probe`, `e2e` (scripts) and
- /// `test`.
+ /// `web` and `serve` (with `web` options), `probe`, `export` (frames
+ /// for ffmpeg), `e2e` (scripts) and `test`.
pub fn addSteps(app: App, options: StepsOptions) Steps {
const b = app.sb.b;
const library = app.addLibrary();
@@ -1222,6 +1284,14 @@ pub const App = struct {
break :blk web;
} else null;
+ // `--export` takes the first argument after `--`.
+ const exporting = b.addRunArtifact(exe);
+ exporting.addArgs(&.{ "--state", b.fmt("{s}.state", .{app.name}), "--export" });
+ exporting.addPassthruArgs();
+ exporting.setCwd(b.path("."));
+ exporting.stdio = .inherit;
+ b.step("export", "Export frames: -- DIR for PNGs, or - for raw RGBA on stdout; then --fps N, --frames N, --size WxH").dependOn(&exporting.step);
+
const e2e = b.step("e2e", "Play the end-to-end scripts headless");
const script_exe = if (app.sb.script_optimize == app.sb.optimize) exe else app.addScriptExecutable();
for (options.scripts) |script| e2e.dependOn(&app.addScript(script_exe, script, .{}).step);