summaryrefslogtreecommitdiff
path: root/src/host/root.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-09-25 21:47:58 -0300
committerGabriel Schneider <[email protected]>2026-09-25 21:48:16 -0300
commit9c33714591a2147f7e5879e77212ca9886d0c967 (patch)
treed250be9d9bc365d7894d49455c0f6376966b58be /src/host/root.zig
downloadsketchbook-9c33714591a2147f7e5879e77212ca9886d0c967.tar.gz
sketchbook-9c33714591a2147f7e5879e77212ca9886d0c967.zip
Put the game first: lib/sketchbook, sketches/vecanim
The root is the game. The platform library moves to lib/sketchbook (with serve_web.py, which serves any sketch's web build, as a tool of it), and vecanim to sketches/vecanim, still its own package on the same library.
Diffstat (limited to 'src/host/root.zig')
-rw-r--r--src/host/root.zig138
1 files changed, 138 insertions, 0 deletions
diff --git a/src/host/root.zig b/src/host/root.zig
new file mode 100644
index 0000000..637a325
--- /dev/null
+++ b/src/host/root.zig
@@ -0,0 +1,138 @@
+//! The sketchbook host: runs a sketch linked in, or loaded from a library
+//! that it hot-reloads whenever a build replaces it.
+//!
+//! sketchbook [options] [LIBRARY]
+//! --state FILE persistent state (default: <name>.state)
+//! --headless offscreen video, silent audio
+//! --script FILE play a script instead of reading devices
+//! --fresh start from default state: no snapshots or recordings
+//! --define NAME=VALUE substitute {NAME} in the script
+const std = @import("std");
+const c = @import("c");
+const abi = @import("abi");
+pub const Host = @import("Host.zig");
+pub const memory = @import("memory.zig");
+pub const fields = @import("fields.zig");
+pub const script = @import("script.zig");
+pub const timeline = @import("timeline.zig");
+
+/// Entry point for both the generic host (`linked == null`) and apps that
+/// link their sketch in.
+pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void {
+ const gpa = init.gpa;
+ var options: Host.Options = .{};
+ var script_path: ?[]const u8 = null;
+ var defines: std.ArrayList([2][]const u8) = .empty;
+ defer defines.deinit(gpa);
+
+ var args = init.minimal.args.iterate();
+ _ = args.skip();
+ while (args.next()) |arg| {
+ if (std.mem.eql(u8, arg, "--state")) {
+ options.state = args.next() orelse fatal("--state needs a file");
+ } else if (std.mem.eql(u8, arg, "--fresh")) {
+ options.fresh = true;
+ } else if (std.mem.eql(u8, arg, "--headless")) {
+ options.headless = true;
+ } else if (std.mem.eql(u8, arg, "--script")) {
+ script_path = args.next() orelse fatal("--script needs a file");
+ } else if (std.mem.eql(u8, arg, "--define")) {
+ 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, "-h") or std.mem.eql(u8, arg, "--help")) {
+ std.debug.print("{s}", .{usage});
+ return;
+ } else if (std.mem.startsWith(u8, arg, "-")) {
+ fatal("unknown option (see --help)");
+ } else {
+ options.library = arg;
+ }
+ }
+ if (linked == null and options.library == null) fatal("no sketch library given (see --help)");
+
+ sdl_heap.install();
+ {
+ var host: Host = undefined;
+ try host.init(gpa, init.io, init.environ_map, options, linked);
+ defer host.deinit();
+ if (script_path) |path| {
+ const source = try std.Io.Dir.cwd().readFileAlloc(init.io, path, gpa, .limited(1 << 20));
+ defer gpa.free(source);
+ try script.run(&host, source, defines.items);
+ } else try host.run();
+ }
+ if (sdl_heap.leaked()) return error.LeakedSdlMemory;
+}
+
+const usage =
+ \\usage: sketchbook [options] [LIBRARY]
+ \\ --state FILE persistent state (default: sketch.state)
+ \\ --headless offscreen video, silent audio, no vsync
+ \\ --script FILE play a script instead of reading devices
+ \\ --fresh start from default state: no snapshots or recordings
+ \\ --define NAME=VALUE substitute {NAME} in the script
+ \\
+;
+
+fn fatal(message: []const u8) noreturn {
+ std.debug.print("sketchbook: {s}\n", .{message});
+ std.process.exit(2);
+}
+
+/// SDL's allocations go through a leak-checking allocator of their own, so a
+/// session that leaks SDL memory fails loudly at exit.
+const sdl_heap = struct {
+ var debug: std.heap.DebugAllocator(.{ .backing_allocator_zeroes = false }) = .{};
+ const alignment: std.mem.Alignment = .of(std.c.max_align_t);
+ const header = alignment.forward(@sizeOf(usize));
+
+ fn install() void {
+ _ = c.SDL_SetMemoryFunctions(malloc, calloc, realloc, free);
+ }
+ fn leaked() bool {
+ return debug.deinit() == .leak;
+ }
+ fn block(size: usize) ?[*]u8 {
+ const total = std.math.add(usize, header, size) catch return null;
+ const base = debug.allocator().rawAlloc(total, alignment, @returnAddress()) orelse return null;
+ @as(*usize, @ptrCast(@alignCast(base))).* = total;
+ return base + header;
+ }
+ fn whole(mem: *anyopaque) []u8 {
+ const base: [*]u8 = @as([*]u8, @ptrCast(mem)) - header;
+ return base[0..@as(*const usize, @ptrCast(@alignCast(base))).*];
+ }
+ fn malloc(size: usize) callconv(.c) ?*anyopaque {
+ return block(size);
+ }
+ fn calloc(n: usize, size: usize) callconv(.c) ?*anyopaque {
+ const bytes = std.math.mul(usize, n, size) catch return null;
+ const mem = block(bytes) orelse return null;
+ @memset(mem[0..bytes], 0);
+ return mem;
+ }
+ fn realloc(maybe: ?*anyopaque, size: usize) callconv(.c) ?*anyopaque {
+ const old = whole(maybe orelse return malloc(size));
+ const total = std.math.add(usize, header, size) catch return null;
+ if (debug.allocator().rawRemap(old, alignment, total, @returnAddress())) |base| {
+ @as(*usize, @ptrCast(@alignCast(base))).* = total;
+ return base + header;
+ }
+ const new = block(size) orelse return null;
+ @memcpy(new[0..@min(size, old.len - header)], old[header..][0..@min(size, old.len - header)]);
+ debug.allocator().rawFree(old, alignment, @returnAddress());
+ return new;
+ }
+ fn free(maybe: ?*anyopaque) callconv(.c) void {
+ debug.allocator().rawFree(whole(maybe orelse return), alignment, @returnAddress());
+ }
+};
+
+test {
+ _ = memory;
+ _ = fields;
+ _ = timeline;
+ _ = @import("audio.zig");
+ _ = @import("Console.zig");
+}