summaryrefslogtreecommitdiff
path: root/test
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 /test
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 'test')
-rw-r--r--test/export.script11
-rw-r--r--test/export.zig23
-rw-r--r--test/text.script7
-rw-r--r--test/text.zig26
4 files changed, 67 insertions, 0 deletions
diff --git a/test/export.script b/test/export.script
new file mode 100644
index 0000000..bd238e7
--- /dev/null
+++ b/test/export.script
@@ -0,0 +1,11 @@
+# Two exports of the same sketch ({a}, {b}: directories of PNGs, 30 fps,
+# 320x180, five frames) are the same pictures, and frame k is k/30 s in.
+expect-same-screenshot {a}/000000.png {b}/000000.png
+expect-same-screenshot {a}/000004.png {b}/000004.png
+expect-different-screenshot {a}/000000.png {a}/000004.png
+# Frame 0 is before any time passed; frame 4 has the bar 40 px wide.
+expect-pixel {a}/000000.png 0 0 32 32 32 255
+expect-pixel {a}/000004.png 39 0 255 128 64 255
+expect-pixel {a}/000004.png 41 0 32 32 32 255
+# The canvas is the size asked for.
+expect-pixel {a}/000004.png 319 179 32 32 32 255
diff --git a/test/export.zig b/test/export.zig
new file mode 100644
index 0000000..d8b35ad
--- /dev/null
+++ b/test/export.zig
@@ -0,0 +1,23 @@
+//! Export's test: time comes from the ticks alone and moves a bar, the
+//! canvas is the room the host gives (`--size`), and it quits by itself.
+const sk = @import("sketch");
+
+pub const State = struct { t: f32 = 0, frames: u32 = 0 };
+
+pub fn update(s: *State, in: *const sk.Input) void {
+ for (in.events) |e| if (e.kind == .tick) {
+ s.t += e.x;
+ s.frames += 1;
+ };
+}
+
+pub fn draw(s: *const State, f: *sk.Frame) void {
+ const w = f.in.window_width;
+ const h = f.in.window_height;
+ f.canvas(@intFromFloat(w), @intFromFloat(h));
+ f.clear(0x202020);
+ // 300 px a second: 10 px a frame at 30 fps, whatever the wall clock did.
+ f.fill(sk.Rect{ .w = s.t * 300, .h = h / 2 }, 0xff8040);
+ f.print(8, h - 24, 1, 0xffffff, "frame {d} of {d:.0}x{d:.0}", .{ s.frames, w, h });
+ if (s.frames == 8) f.quit();
+}
diff --git a/test/text.script b/test/text.script
new file mode 100644
index 0000000..5bea6f5
--- /dev/null
+++ b/test/text.script
@@ -0,0 +1,7 @@
+# `textSize` is what `text` draws: at scale 1 ten digits of the built-in
+# font advance about 80 px on a 16 px line, and the box under them is that.
+screenshot .zig-cache/text/native.png
+expect-pixel .zig-cache/text/native.png 84 11 32 64 96 255
+expect-pixel .zig-cache/text/native.png 84 24 32 64 96 255
+expect-pixel .zig-cache/text/native.png 97 11 16 16 16 255
+expect-pixel .zig-cache/text/native.png 84 27 16 16 16 255
diff --git a/test/text.zig b/test/text.zig
new file mode 100644
index 0000000..b570435
--- /dev/null
+++ b/test/text.zig
@@ -0,0 +1,26 @@
+//! Text for layout: each line sits on a box of the size `textSize` gives
+//! it, at whole and fractional scales, the same picture at any canvas size.
+const sk = @import("sketch");
+
+pub const width = 640;
+pub const height = 360;
+
+const lines = [_]struct { scale: f32, text: []const u8 }{
+ .{ .scale = 1, .text = "0123456789" },
+ .{ .scale = 1.5, .text = "mov rax, [rbp-0x18] ; 1.5" },
+ .{ .scale = 2.25, .text = "0x7ffed849acf0 i -> W" },
+ .{ .scale = 4, .text = "fn main() !void {}" },
+};
+
+pub fn draw(f: *sk.Frame) void {
+ f.canvas(@intFromFloat(f.in.window_width), @intFromFloat(f.in.window_height));
+ f.clear(0x101010);
+ const k = f.in.window_height / height;
+ var y: f32 = 10 * k;
+ for (lines) |line| {
+ const size = f.host.textSize(line.scale * k, line.text);
+ f.fill(sk.Rect{ .x = 10 * k, .y = y, .w = size[0], .h = size[1] }, 0x204060);
+ f.text(10 * k, y, line.scale * k, line.text, 0xffffff);
+ y += size[1] + 6 * k;
+ }
+}