summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 09:56:27 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitb52da82a9405ac4c9f1c18e2ef53594d5c1f6721 (patch)
tree3db7876a8ac8c06dcda45db88a5750fd6bb49678
parenta380608d79ec755e857a5b5e7bacc96e6720efd5 (diff)
downloadsketchbook-b52da82a9405ac4c9f1c18e2ef53594d5c1f6721.tar.gz
sketchbook-b52da82a9405ac4c9f1c18e2ef53594d5c1f6721.zip
examples/live: each byte's place is laid out when the text moves, not every frame: the places and their layout are kept in the Session beside the revision they were made at, and made again at a jump (a revision counts keys, it does not name a text); with the text library optimised, drawing the code in a Debug host goes from 2.85 ms to 0.38 ms a frame at the storyboard's 320 bytes and from 28 ms to 5.3 ms at 3.5 KiB; zig build test 92/92 steps
Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
-rw-r--r--examples/live/src/main.zig23
1 files changed, 18 insertions, 5 deletions
diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig
index 65bf6cd..5e7fe64 100644
--- a/examples/live/src/main.zig
+++ b/examples/live/src/main.zig
@@ -87,6 +87,12 @@ pub const Session = struct {
scope: scope.Scope = .{},
/// `capture` appends what is heard to `<stem>.capture.f32`.
capturing: bool = false,
+ /// Each byte's place in the code as drawn, and the text's revision they
+ /// were laid out at: a layout shapes every piece of the text, and the
+ /// text moves at a key, not at a frame.
+ places: [Editor.capacity.text + 1]sk.text.Caret = undefined,
+ laid: sk.text.Layout = .{},
+ laid_at: ?u32 = null,
};
/// `capture` starts or stops appending what is heard to the sketch's file
@@ -314,7 +320,7 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void {
});
f.image(ss.picture, .{ .x = 0, .y = 0, .w = room[0], .h = room[1] }, .{});
sound(s, ss, f, room);
- code(s, f, room);
+ code(s, ss, f, room);
}
/// The out tape over the sample clock, as much as keeps the host's lead
@@ -424,14 +430,21 @@ fn box(at: []const sk.text.Caret, i: usize, space: f32) sk.Rect {
/// caret at each range's head that says the mode (a block in normal and
/// select, a bar in insert), the mode's name in the corner; the problem as
/// a red mark under its byte and its words under the text. Every place is
-/// measured: one layout gives each byte's, another draws.
-fn code(s: *const State, f: *sk.Frame, room: [2]f32) void {
+/// measured: one layout gives each byte's (kept until the text moves),
+/// another draws.
+fn code(s: *const State, ss: *Session, f: *sk.Frame, room: [2]f32) void {
const text = s.ed.bytes(f.scratch) catch return;
const ranges = f.scratch.create([pe.max_selections]pe.Selection) catch return;
const got = s.ed.ranges(f.scratch, ranges) catch return;
const p = paragraph(f.scratch, &s.ed, text);
- const at = f.scratch.alloc(sk.text.Caret, text.len + 1) catch return;
- const laid = sk.text.places(text, p, at);
+ const at = ss.places[0 .. text.len + 1];
+ // a revision counts keys, it does not name a text: a jump can land on
+ // another text with the same count
+ if (ss.laid_at != s.ed.revision or f.in.jumped) {
+ ss.laid = sk.text.places(text, p, at);
+ ss.laid_at = s.ed.revision;
+ }
+ const laid = ss.laid;
const space = sk.text.size(.code, scale, " ")[0];
const problem = s.problem.len > 0;
const words: [2]f32 = if (problem) sk.text.size(.code, scale, s.problem.text()) else .{ 0, 0 };