diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 06:03:15 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | c300a059850115a87a34456dfd60ac97c4a09890 (patch) | |
| tree | c058035ba628771aafefdcca9ba96bbe43276fa1 | |
| parent | e7679f416daeee551e81a592c7fe06b8d8f7b705 (diff) | |
| download | sketchbook-c300a059850115a87a34456dfd60ac97c4a09890.tar.gz sketchbook-c300a059850115a87a34456dfd60ac97c4a09890.zip | |
examples/live: the picture, typed over itself
The sketch: State holds the text (a plain 4 KiB buffer, typed characters
appended, backspace deleting the last, return a newline: marked ponytail,
the place pardes's editor takes in part 2), the last good out and color
tapes, the problem and the time, summed from recorded ticks. `update`
compiles the text at every keystroke; a program that parses replaces the
tapes and clears the problem, one that does not sets the problem and
changes nothing else, so the last good picture stays.
`draw` runs picture.frag over the color tape into an image the size of
the canvas, 1280x720, or half of it when the tape is longer than 64
instructions (the probe: 128 took 2.7 ms at full size, 0.85 at half),
and scales it up. The text is drawn over it on a dark band with the
library's text calls (the code face at 1.25, a column a fixed advance),
the caret at the end, the problem as a red mark under its byte and its
words under the text, and live/recording/replay/paused with the time in
a corner.
Test: test/live.script, the start of the scripted proof: a tone typed
(its eight instructions asserted), a gradient (hsv over x and y, four),
an edit (y to x: three, the picture different), a broken line (the
problem at byte 52, the tape and the picture the same) and its mend.
zig build test: 56 of 56 steps, 6 of 6 tests.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
| -rw-r--r-- | examples/live/build.zig | 4 | ||||
| -rw-r--r-- | examples/live/src/main.zig | 163 | ||||
| -rw-r--r-- | examples/live/test/live.script | 37 |
3 files changed, 203 insertions, 1 deletions
diff --git a/examples/live/build.zig b/examples/live/build.zig index 2ad85fb..2f76306 100644 --- a/examples/live/build.zig +++ b/examples/live/build.zig @@ -29,8 +29,10 @@ pub fn build(b: *std.Build) void { .{ .name = "shaders", .module = shaders.module }, }; + const app = sb.app(.{ .name = "live", .root_source_file = b.path("src/main.zig"), .imports = imports }); + const steps = app.addSteps(.{ .scripts = &.{b.path("test/live.script")} }); const lang_test = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/Lang.zig"), .target = target, .optimize = optimize }) }); - b.step("test", "Run the language's tests").dependOn(&b.addRunArtifact(lang_test).step); + steps.test_step.dependOn(&b.addRunArtifact(lang_test).step); // The interpreter's GPU time over made-up tapes (`--profile-gpu`). const probe = sb.app(.{ .name = "live-probe", .root_source_file = b.path("src/probe.zig"), .imports = imports }); diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig new file mode 100644 index 0000000..02a9c6d --- /dev/null +++ b/examples/live/src/main.zig @@ -0,0 +1,163 @@ +//! live: a program typed over its own picture. Each line is `name = +//! expression` over t, x and y (src/Lang.zig); the `color` line is what is +//! seen. At every keystroke that leaves a program that parses, `update` +//! compiles it into a tape kept in State, which one interpreter on the GPU +//! (shaders/vm.glsl) runs per pixel. While a line is broken the last good +//! tape keeps playing and the problem is marked at its byte. +//! +//! State is the text, the tapes, the problem and the time, all changed by +//! `update` from recorded keys and ticks, so a replay or a seek brings back +//! the text, the tape and the picture of that moment. Session holds the +//! GPU objects. +const std = @import("std"); +const sk = @import("sketch"); +const gpu = sk.abi.gpu; +const Lang = @import("Lang"); +const shaders = @import("shaders"); + +pub const name = "live"; +pub const width = 1280; +pub const height = 720; + +pub const State = struct { + // ponytail: a plain buffer, typed characters appended and backspace + // deleting the last; part 2 puts pardes's editor here. + text: [4096]u8 = @splat(0), + len: u32 = 0, + /// The last program that parsed. + out: Lang.Tape = .{}, + color: Lang.Tape = .{}, + /// What is wrong with the text, if it does not parse (`len` 0: nothing). + problem: Lang.Problem = .{}, + /// Seconds, from recorded ticks: a replay draws the same frames. + time: f32 = 0, +}; + +pub const Session = struct { + picture: sk.Image = .none, + size: [2]u32 = .{ 0, 0 }, +}; + +pub fn update(s: *State, in: *const sk.Input) void { + var edited = false; + for (in.events) |e| switch (e.kind) { + .tick => s.time += e.x, + .text => { + var utf8: [4]u8 = undefined; + const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue; + if (s.len + n > s.text.len) continue; + @memcpy(s.text[s.len..][0..n], utf8[0..n]); + s.len += n; + edited = true; + }, + .key_down => switch (e.key()) { + .backspace => if (s.len > 0) { + s.len -= 1; + while (s.len > 0 and s.text[s.len] & 0xc0 == 0x80) s.len -= 1; + edited = true; + }, + .@"return", .keypad_enter => if (s.len < s.text.len) { + s.text[s.len] = '\n'; + s.len += 1; + edited = true; + }, + else => {}, + }, + else => {}, + }; + if (!edited) return; + switch (Lang.compile(s.text[0..s.len])) { + .program => |p| { + s.out = p.out; + s.color = p.color; + s.problem = .{}; + }, + .problem => |p| s.problem = p, + } +} + +const picture_pipeline: gpu.Pipeline = .{ + .vertex = .{ .code = shaders.fullscreen_vert }, + .fragment = .{ .code = shaders.picture_frag, .uniform_buffers = 1 }, + .stride = 0, + .attributes = &.{}, + .blend = .none, +}; + +pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { + f.clear(0x000000); + if (!f.host.gpu()) { + f.text(16, 16, 2, "live needs a GPU: the host has none.", 0xff8080); + return; + } + // A tape past 64 instructions is drawn at half resolution: at 1280x720 + // 128 instructions took 2.7 ms of the GPU, at half 0.85 (the probe). + const half: u32 = if (s.color.len > 64) 2 else 1; + const size: [2]u32 = .{ width / half, height / half }; + if (!std.meta.eql(ss.size, size)) { + if (ss.picture != .none) f.host.freeImage(ss.picture); + ss.picture = f.host.newImage(size[0], size[1]); + ss.size = size; + } + f.gpu(.{ + .label = "picture", + .image = ss.picture, + .width = size[0], + .height = size[1], + .pipelines = &.{picture_pipeline}, + .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, Lang.Picture{ + .tape = std.mem.bytesToValue([Lang.max_tape][4]u32, &s.color.code), + .len = s.color.len, + .time = s.time, + .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) }, + }) }}, + }); + f.image(ss.picture, .{ .x = 0, .y = 0, .w = width, .h = height }, .{}); + code(s, f); +} + +const scale = 1.25; +const margin = 16; +const ink = 0xe8e8e8; +const red = 0xff5a50; + +/// The text over the picture on a dark band, the caret at its end, and the +/// problem: a red mark under its byte and its words under the text. +// ponytail: one face at one size, so a column is a fixed advance; part 2 +// draws the editor's text with sketchbook's new text instead. +fn code(s: *const State, f: *sk.Frame) void { + const text = s.text[0..s.len]; + const cell = f.host.textSizeIn(.code, scale, "M"); + const lines = std.mem.count(u8, text, "\n") + 1; + var widest: f32 = 0; + var it = std.mem.splitScalar(u8, text, '\n'); + while (it.next()) |line| widest = @max(widest, f.host.textSizeIn(.code, scale, line)[0]); + const problem = s.problem.len > 0; + if (problem) widest = @max(widest, f.host.textSizeIn(.code, scale, s.problem.text())[0]); + const band_h = @as(f32, @floatFromInt(lines + @intFromBool(problem))) * cell[1] + 2 * margin; + f.fill(.{ .x = 0, .y = 0, .w = @max(widest, 24 * cell[0]) + 2 * margin + cell[0], .h = band_h }, sk.Color{ .r = 0, .g = 0, .b = 0, .a = 150 }); + + var y: f32 = margin; + var start: usize = 0; + it = std.mem.splitScalar(u8, text, '\n'); + while (it.next()) |line| : (y += cell[1]) { + f.textIn(.code, margin, y, scale, line, ink); + const end = start + line.len; + if (problem and s.problem.at >= start and s.problem.at <= end) { + const x = margin + f.host.textSizeIn(.code, scale, text[start..s.problem.at])[0]; + f.fill(.{ .x = x, .y = y + cell[1] - 2, .w = cell[0], .h = 2 }, red); + } + // The caret, after the last byte. + if (end == text.len) f.fill(.{ .x = margin + f.host.textSizeIn(.code, scale, line)[0], .y = y, .w = 2, .h = cell[1] }, ink); + start = end + 1; + } + if (problem) f.textIn(.code, margin, y, scale, s.problem.text(), red); + + const mode = switch (f.in.mode) { + .live => "live", + .recording => "recording", + .playing => "replay", + .paused => "paused", + }; + f.print(width - 200, height - 24, 1, ink, "{s} {d:.1} s", .{ mode, s.time }); +} diff --git a/examples/live/test/live.script b/examples/live/test/live.script new file mode 100644 index 0000000..386ab33 --- /dev/null +++ b/examples/live/test/live.script @@ -0,0 +1,37 @@ +# A program typed, its tapes after each line, a broken line and its mend. +# Screenshots land in .zig-cache/live/ for review. +frames 2 +type out = sin(tau * 220 * t) * 0.3 +expect problem.len 0 +expect out.len 8 +expect out.code[5].op sin +expect out.code[7].op mul +key return +type color = hsv(x, 0.5, y) +expect color.len 4 +expect color.code[0].op x +expect color.code[3].op hsv +frames 2 +screenshot .zig-cache/live/gradient.png +# An edit: y becomes x, and the picture changes. +key backspace +key backspace +type x) +expect color.len 3 +expect color.code[2].op hsv +expect color.code[2].c 0 +frames 2 +screenshot .zig-cache/live/edited.png +expect-different-screenshot .zig-cache/live/gradient.png .zig-cache/live/edited.png 640 300 600 380 +# Broken: the tape and the picture stay, the problem is set at its byte. +key backspace +expect problem.at 52 +expect color.len 3 +expect color.code[2].c 0 +frames 2 +screenshot .zig-cache/live/broken.png +expect-same-screenshot .zig-cache/live/edited.png .zig-cache/live/broken.png 0 640 300 600 380 +# Mended. +type ) +expect problem.len 0 +expect color.len 3 |
