From 2348776791e9dd17afeb12a06ba42a4a71917bcf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 21:34:41 -0300 Subject: sketchbook: a drawing layer on top, host fixes for replay and migration --- src/host/script.zig | 200 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 191 insertions(+), 9 deletions(-) (limited to 'src/host/script.zig') diff --git a/src/host/script.zig b/src/host/script.zig index cf551cd..7818c6b 100644 --- a/src/host/script.zig +++ b/src/host/script.zig @@ -8,21 +8,46 @@ //! cancel what losing focus sends //! key NAME press and release a key (`sketch.Key` names) //! type TEXT text input, as typed into a focused field -//! console LINE run a console command +//! console LINE run a console command (`console get` +//! takes no frame: it only reads) //! frames N advance N frames without input -//! expect FIELD VALUE compare a state field (enums by name or number) -//! await FIELD VALUE run frames until it holds (a minute at most) +//! expect FIELD VALUE compare a state field (enums by name or number); +//! FIELD may index arrays and vector lanes, +//! `boids.buffer[3].pos[0]` +//! expect FIELD VALUE ~T numbers within T of VALUE +//! await FIELD VALUE [~T] run frames until it holds (a minute at most) +//! print FIELD write a field's value to stderr (a vector, +//! array or struct: every value in it), no frame +//! expect-snapshot N live state equals snapshot N, leaf by leaf +//! (padding ignored), exactly +//! expect-log TEXT a recent console line contains TEXT +//! expect-no-log TEXT no recent console line does //! expect-audio KEY stream KEY has received samples //! screenshot FILE save the next frame as PNG +//! expect-same-screenshot A B [MAX [X Y W H]] +//! two saved PNGs match, every channel within MAX +//! (default 0), in the rect if given (pixels) +//! expect-different-screenshot A B [X Y W H] +//! two saved PNGs differ somewhere (in the rect) +//! expect-pixel FILE X Y R G B A [TOL] +//! a saved PNG's pixel, every channel within TOL //! load LIBRARY swap in another build of the sketch //! reload load the watched library again //! await-reload SECONDS run frames until a build replaces the library //! //! `{name}` is replaced by `--define name=value` from the command line. +//! +//! Each line but `frames`, `print`, `console get`, the expectations and +//! `await-reload` is followed by one frame; `update` gets one call per +//! frame, live or replayed, so +//! `console play` then `frames N` replays N recorded frames at the +//! script's 60 Hz. const std = @import("std"); +const c = @import("c"); const abi = @import("abi"); const Host = @import("Host.zig"); const fields = @import("fields.zig"); +const memory = @import("memory.zig"); pub const frame_ns: u64 = std.time.ns_per_s / 60; /// The most frames `await` plays: a minute. @@ -84,28 +109,102 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons std.debug.print(" console {s}: {t}\n", .{ rest, err }); return err; }; + if (std.mem.startsWith(u8, rest, "get ")) return; + } else if (eq(u8, command, "print")) { + const path = words.next() orelse return error.ExpectedField; + var buf: [4096]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + fields.formatPath(host.state.layout, host.live(), path, &w) catch |err| switch (err) { + error.UnknownField => return error.UnknownField, + error.WriteFailed => {}, + }; + std.debug.print("{s} = {s}\n", .{ path, w.buffered() }); + return; } else if (eq(u8, command, "frames")) { const count = try std.fmt.parseInt(usize, rest, 10); for (0..count) |_| try advance(host, now); return; } else if (eq(u8, command, "expect") or eq(u8, command, "await")) { const path = words.next() orelse return error.ExpectedField; - const want = std.mem.trim(u8, words.rest(), " \t"); - const leaf = host.state.layout.find(path) orelse return error.UnknownField; + const want, const tolerance = try expected(std.mem.trim(u8, words.rest(), " \t")); + // Looked up again after each frame: a reload may move the field. + _ = host.state.layout.find(path) orelse return error.UnknownField; // `await` plays frames until the field holds the value, for up to a // minute: for waits whose length is the sketch's business. if (eq(u8, command, "await")) { var waited: usize = 0; - while (!fields.matches(leaf, host.live(), want) and waited < await_limit) : (waited += 1) try advance(host, now); + while (!holds(host, path, want, tolerance) and waited < await_limit) : (waited += 1) try advance(host, now); } - if (!fields.matches(leaf, host.live(), want)) { + if (!holds(host, path, want, tolerance)) { var buf: [128]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); - fields.format(leaf, host.live(), &w) catch {}; - std.debug.print(" {s}: expected {s}, found {s}\n", .{ path, want, w.buffered() }); + fields.formatPath(host.state.layout, host.live(), path, &w) catch |err| switch (err) { + error.UnknownField => w.writeAll("no such field") catch {}, + error.WriteFailed => {}, + }; + if (tolerance) |t| { + std.debug.print(" {s}: expected {s} ~{d}, found {s}\n", .{ path, want, t, w.buffered() }); + } else std.debug.print(" {s}: expected {s}, found {s}\n", .{ path, want, w.buffered() }); + return error.ExpectationFailed; + } + return; + } else if (eq(u8, command, "expect-snapshot")) { + const index = std.fmt.parseInt(usize, rest, 10) catch return error.InvalidSlot; + if (!host.timeline.bank.hasSnapshot(index)) return error.EmptySnapshot; + if (!same(host, index)) return error.ExpectationFailed; + return; + } else if (eq(u8, command, "expect-log") or eq(u8, command, "expect-no-log")) { + var found = false; + for (&host.console.lines) |*l| { + if (std.mem.indexOf(u8, l.bytes[0..l.len], rest) != null) found = true; + } + if (found != eq(u8, command, "expect-log")) { + std.debug.print(" console log:\n", .{}); + for (&host.console.lines) |*l| std.debug.print(" {s}\n", .{l.bytes[0..l.len]}); return error.ExpectationFailed; } return; + } else if (eq(u8, command, "expect-same-screenshot") or eq(u8, command, "expect-different-screenshot")) { + const a = words.next() orelse return error.ExpectedFile; + const b = words.next() orelse return error.ExpectedFile; + const want_same = eq(u8, command, "expect-same-screenshot"); + const max: u8 = if (!want_same) 0 else if (words.next()) |m| std.fmt.parseInt(u8, m, 10) catch return error.InvalidValue else 0; + var rect: ?[4]usize = null; + if (words.next()) |x| { + var r: [4]usize = undefined; + r[0] = std.fmt.parseInt(usize, x, 10) catch return error.InvalidRect; + for (r[1..]) |*v| v.* = std.fmt.parseInt(usize, words.next() orelse return error.InvalidRect, 10) catch return error.InvalidRect; + rect = r; + } + const diff = try comparePng(arena, a, b, max, rect); + if ((diff.count == 0) != want_same) { + if (want_same) { + std.debug.print(" {s} vs {s}: {d} pixels differ by more than {d}, first at {d},{d}\n", .{ a, b, diff.count, max, diff.first[0], diff.first[1] }); + } else std.debug.print(" {s} and {s} are the same\n", .{ a, b }); + return error.ExpectationFailed; + } + return; + } else if (eq(u8, command, "expect-pixel")) { + const file = words.next() orelse return error.ExpectedFile; + var n: [7]u8 = .{ 0, 0, 0, 0, 0, 0, 0 }; + var x: usize = 0; + var y: usize = 0; + for (0..7) |i| { + const word = words.next() orelse if (i == 6) break else return error.ExpectedPixel; + if (i < 2) { + (if (i == 0) &x else &y).* = std.fmt.parseInt(usize, word, 10) catch return error.ExpectedPixel; + } else n[i - 2] = std.fmt.parseInt(u8, word, 0) catch return error.ExpectedPixel; + } + const tol = n[4]; + const surface = try loadPng(arena, file); + defer c.SDL_DestroySurface(surface); + if (x >= surface.*.w or y >= surface.*.h) return error.OutsideScreenshot; + const p = (@as([*]const u8, @ptrCast(surface.*.pixels.?)) + y * @as(usize, @intCast(surface.*.pitch)) + x * 4)[0..4]; + for (p, n[0..4]) |got, want| if (@max(got, want) - @min(got, want) > tol) { + std.debug.print(" {s} at {d},{d}: expected {d} {d} {d} {d}, found {d} {d} {d} {d}\n", .{ file, x, y, n[0], n[1], n[2], n[3], p[0], p[1], p[2], p[3] }); + return error.ExpectationFailed; + }; + return; } else if (eq(u8, command, "expect-audio")) { const key = try std.fmt.parseInt(u32, rest, 10); if (host.input.stream(key).pushed == 0) return error.ExpectationFailed; @@ -144,6 +243,89 @@ fn held(host: *const Host) u8 { return abi.Event.withModifiers(mods); } +/// "VALUE" or "VALUE ~TOLERANCE". +fn expected(text: []const u8) !struct { []const u8, ?f64 } { + const tilde = std.mem.lastIndexOfScalar(u8, text, '~') orelse return .{ text, null }; + const tolerance = std.fmt.parseFloat(f64, std.mem.trim(u8, text[tilde + 1 ..], " \t")) catch return error.InvalidTolerance; + return .{ std.mem.trim(u8, text[0..tilde], " \t"), tolerance }; +} + +fn holds(host: *Host, path: []const u8, want: []const u8, tolerance: ?f64) bool { + const leaf = host.state.layout.find(path) orelse return false; + const t = tolerance orelse return fields.matches(leaf, host.live(), want); + const value = fields.number(leaf, host.live()) orelse return false; + const target = std.fmt.parseFloat(f64, want) catch return false; + return @abs(value - target) <= t; +} + +/// Live state against snapshot `index`, leaf by leaf; prints the first few +/// differences. +fn same(host: *Host, index: usize) bool { + const layout = host.state.layout; + const live = host.live(); + const saved = host.timeline.bank.slot(index); + var count: usize = 0; + for (layout.fields) |leaf| { + if (leaf.whole()) continue; + var it = leaf.elements(); + while (it.next()) |offset| { + if (std.mem.eql(u8, live[offset..][0..leaf.size], saved[offset..][0..leaf.size])) continue; + count += 1; + if (count > 5) continue; + var buf: [256]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + leaf.name(offset, &w) catch {}; + w.writeAll(": live ") catch {}; + fields.format(leaf.at(offset), live, &w) catch {}; + w.print(", snapshot {d} ", .{index}) catch {}; + fields.format(leaf.at(offset), saved, &w) catch {}; + std.debug.print(" {s}\n", .{w.buffered()}); + } + } + if (count > 5) std.debug.print(" ... {d} differing values in all\n", .{count}); + return count == 0; +} + +const PngDiff = struct { count: usize = 0, first: [2]usize = .{ 0, 0 } }; + +/// Pixels whose channels differ by more than `max`; a size mismatch +/// counts every pixel. +fn comparePng(arena: std.mem.Allocator, a: []const u8, b: []const u8, max: u8, rect: ?[4]usize) !PngDiff { + const sa = try loadPng(arena, a); + defer c.SDL_DestroySurface(sa); + const sb = try loadPng(arena, b); + defer c.SDL_DestroySurface(sb); + if (sa.*.w != sb.*.w or sa.*.h != sb.*.h) return .{ .count = @intCast(@max(sa.*.w * sa.*.h, sb.*.w * sb.*.h, 1)) }; + var diff: PngDiff = .{}; + const w: usize = @intCast(sa.*.w); + const h: usize = @intCast(sa.*.h); + const r = rect orelse [4]usize{ 0, 0, w, h }; + for (@min(r[1], h)..@min(r[1] + r[3], h)) |y| { + const ra: [*]const u8 = @as([*]const u8, @ptrCast(sa.*.pixels.?)) + y * @as(usize, @intCast(sa.*.pitch)); + const rb: [*]const u8 = @as([*]const u8, @ptrCast(sb.*.pixels.?)) + y * @as(usize, @intCast(sb.*.pitch)); + for (@min(r[0], w)..@min(r[0] + r[2], w)) |x| { + const differs = for (0..4) |i| { + const p = ra[x * 4 + i]; + const q = rb[x * 4 + i]; + if (@max(p, q) - @min(p, q) > max) break true; + } else false; + if (!differs) continue; + if (diff.count == 0) diff.first = .{ x, y }; + diff.count += 1; + } + } + return diff; +} + +fn loadPng(arena: std.mem.Allocator, path: []const u8) !*c.SDL_Surface { + const loaded = c.SDL_LoadPNG((try arena.dupeSentinel(u8, path, 0)).ptr) orelse { + std.debug.print(" {s}: {s}\n", .{ path, c.SDL_GetError() }); + return error.ScreenshotUnreadable; + }; + defer c.SDL_DestroySurface(loaded); + return c.SDL_ConvertSurface(loaded, c.SDL_PIXELFORMAT_RGBA32) orelse error.ScreenshotUnreadable; +} + fn advance(host: *Host, now: *u64) !void { try host.frame(now.*); now.* += frame_ns; -- cgit v1.3