From 86ffca0024326110962f0880727f4eb84e6a48d1 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 11:13:20 -0300 Subject: 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 --- src/host/root.zig | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'src/host/root.zig') diff --git a/src/host/root.zig b/src/host/root.zig index 8cc15e1..d01a63f 100644 --- a/src/host/root.zig +++ b/src/host/root.zig @@ -9,6 +9,11 @@ //! --profile-gpu time each GPU command alone, print the times at exit //! --console LINE run a console line once the sketch is loaded (repeatable) //! --define NAME=VALUE substitute {NAME} in the script +//! --export DIR|- headless, from fresh state: every frame as DIR/000000.png, ... +//! or for - as raw RGBA on stdout, until the sketch quits +//! --fps N export: frames a second (60); each tick is exactly 1/N s +//! --frames N export: stop after N frames +//! --size WxH the first canvas and, headless, the room for it const std = @import("std"); const c = @import("c"); const abi = @import("abi"); @@ -48,6 +53,22 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { const pair = args.next() orelse fatal("--define needs NAME=VALUE"); const eq = std.mem.indexOfScalar(u8, pair, '=') orelse fatal("--define needs NAME=VALUE"); try defines.append(gpa, .{ pair[0..eq], pair[eq + 1 ..] }); + } else if (std.mem.eql(u8, arg, "--export")) { + options.export_to = args.next() orelse fatal("--export needs a directory, or - for stdout"); + options.headless = true; + options.fresh = true; + } else if (std.mem.eql(u8, arg, "--fps")) { + options.fps = std.fmt.parseInt(u32, args.next() orelse "", 10) catch 0; + if (options.fps == 0) fatal("--fps needs a whole number of frames a second"); + } else if (std.mem.eql(u8, arg, "--frames")) { + options.frames = std.fmt.parseInt(u64, args.next() orelse "", 10) catch fatal("--frames needs a number"); + } else if (std.mem.eql(u8, arg, "--size")) { + const size = args.next() orelse ""; + const x = std.mem.indexOfScalar(u8, size, 'x') orelse fatal("--size needs WxH"); + const w = std.fmt.parseInt(u32, size[0..x], 10) catch 0; + const h = std.fmt.parseInt(u32, size[x + 1 ..], 10) catch 0; + if (w == 0 or h == 0) fatal("--size needs WxH"); + options.size = .{ w, h }; } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) { std.debug.print("{s}", .{usage}); return; @@ -58,21 +79,29 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { } } if (linked == null and options.library == null) fatal("no sketch library given (see --help)"); + if (options.export_to) |to| { + if (script_path != null) fatal("--export plays no script: it runs the sketch on its own"); + if (!std.mem.eql(u8, to, "-")) { + try std.Io.Dir.cwd().createDirPath(init.io, to); + } else if (try std.Io.File.stdout().isTty(init.io)) fatal("--export -: stdout is a terminal; pipe it to ffmpeg"); + } // A scripted run from fresh state works on files of its own, moved to // the given path at exit: two runs of one build step (two `zig build`s // at once share its output path) never share a state, store or - // recordings file, nor fail on the store's lock. + // recordings file, nor fail on the store's lock. An export's are + // removed instead: nothing reads them, and a player may have the given + // ones open. var private_buf: [std.fs.max_path_bytes]u8 = undefined; const given = options.state; - const private = script_path != null and options.fresh; + const private = (script_path != null or options.export_to != null) and options.fresh; if (private) options.state = std.mem.print(&private_buf, "{s}.{d}", .{ given, std.os.linux.getpid() }) catch fatal("--state path too long"); defer if (private) for ([_][]const u8{ "", ".inputs", ".store" }) |suffix| { var from: [std.fs.max_path_bytes + 16]u8 = undefined; var to: [std.fs.max_path_bytes + 16]u8 = undefined; const a = std.mem.printSentinel(&from, "{s}{s}", .{ options.state, suffix }, 0) catch continue; const b = std.mem.printSentinel(&to, "{s}{s}", .{ given, suffix }, 0) catch continue; - _ = std.os.linux.rename(a.ptr, b.ptr); + _ = if (options.export_to != null) std.os.linux.unlink(a.ptr) else std.os.linux.rename(a.ptr, b.ptr); }; sdl_heap.install(); @@ -99,6 +128,11 @@ const usage = \\ --profile-gpu time each GPU command alone, print the times at exit \\ --console LINE run a console line once the sketch is loaded (repeatable) \\ --define NAME=VALUE substitute {NAME} in the script + \\ --export DIR|- headless, from fresh state: every frame as DIR/000000.png, ... + \\ or for - as raw RGBA on stdout, until the sketch quits + \\ --fps N export: frames a second (60); each tick is exactly 1/N s + \\ --frames N export: stop after N frames + \\ --size WxH the first canvas and, headless, the room for it \\ ; -- cgit v1.3