diff options
| author | Gabriel Schneider <[email protected]> | 2026-09-25 21:53:00 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-09-25 21:54:39 -0300 |
| commit | 2dfddee8c6fab6eb76cbfe3d95f7c30bfb870702 (patch) | |
| tree | 32a8bbb55226721834fd5675e7bafc163508dd1e /src/host | |
| parent | 50f4ee716aeafa13118cbcfa0f5cb37f7d3940a4 (diff) | |
| download | sketchbook-2dfddee8c6fab6eb76cbfe3d95f7c30bfb870702.tar.gz sketchbook-2dfddee8c6fab6eb76cbfe3d95f7c30bfb870702.zip | |
sketchbook: what the game's review needed
- input.started: true on the first update of a run, never after a reload,
for a sketch to stop what should not resume from saved state.
- A console line that errors fails its script. Sketch commands answer
unknown, done or failed (abi.Outcome) instead of a bool that hid errors.
- addScript also takes the hot-reloadable library, run through the generic
host, so scripts can reload a real build.
- Sounds that do not load are .none, not an empty clip.
- sk_tag reads an enum field's tag name from the web build, for tests.
Diffstat (limited to 'src/host')
| -rw-r--r-- | src/host/Console.zig | 8 | ||||
| -rw-r--r-- | src/host/Host.zig | 19 | ||||
| -rw-r--r-- | src/host/audio.zig | 11 | ||||
| -rw-r--r-- | src/host/script.zig | 6 |
4 files changed, 30 insertions, 14 deletions
diff --git a/src/host/Console.zig b/src/host/Console.zig index d9aa469..e6be2f0 100644 --- a/src/host/Console.zig +++ b/src/host/Console.zig @@ -135,7 +135,7 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const if (eq(command, "help")) { self.log("save|load [1..7] snapshot N | record|play [0..15] recording N | stop | pause | step N | seek start|end|<ms>"); self.log("diff [N] | reset | get <field> | set <field> <value> | overlay memory|sound | quit"); - if (!host.forward(self, line, now)) self.log("Backtick hides the console. Tab completes."); + if (host.forward(self, line, now) == .unknown) self.log("Backtick hides the console. Tab completes."); return &.{}; } if (eq(command, "overlay")) { @@ -210,7 +210,11 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const } else { // Sketch commands edit state outside the recorded input. h.stop(now); - if (!host.forward(self, line, now)) return error.UnknownCommand; + switch (host.forward(self, line, now)) { + .unknown => return error.UnknownCommand, + .failed => return error.CommandFailed, + .done => {}, + } } return &.{}; } diff --git a/src/host/Host.zig b/src/host/Host.zig index 81a5dc8..92d1b55 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -67,6 +67,7 @@ canvas: [2]u32 = .{ 0, 0 }, cursor: abi.Cursor = .arrow, quit: bool = false, reloaded: bool = true, +started: bool = true, last_now: u64 = 0, /// Saved after the next frame is drawn (scripts). screenshot: ?[:0]const u8 = null, @@ -223,6 +224,8 @@ pub fn update(self: *Host, events: []const abi.Event) void { if (events.len > 0 and self.timeline.mode != .live and self.timeline.mode != .recording) self.input.now = events[events.len - 1].time; self.input.events = events; self.input.mode = self.timeline.mode; + self.input.started = self.started; + self.started = false; const ctx = self.context(); self.sketch.update(&ctx, self.live().ptr); self.input.events = &.{}; @@ -307,7 +310,7 @@ pub fn route(self: *Host, e: abi.Event) !void { }, .key_down => { const key = consoleKey(e) orelse return; - if (self.console.edit(key, self.completions)) |line| self.runLine(line, e.time); + if (self.console.edit(key, self.completions)) |line| self.runLine(line, e.time) catch {}; return; }, .key_up => return, @@ -319,17 +322,19 @@ pub fn route(self: *Host, e: abi.Event) !void { try self.pending.append(self.gpa, e); } -/// Runs a console line now, replaying whatever a seek asks for. -pub fn runLine(self: *Host, line: []const u8, now: u64) void { +/// Runs a console line now, replaying whatever a seek asks for. An error is +/// printed to the console, and returned for scripts to fail on. +pub fn runLine(self: *Host, line: []const u8, now: u64) !void { const replay = self.console.execute(self, line, now) catch |err| { - self.console.print("{t}. Type help for usage.", .{err}); - return; + // The sketch has printed its own error already. + if (err != error.CommandFailed) self.console.print("{t}. Type help for usage.", .{err}); + return err; }; if (self.timeline.mode == .paused) self.update(replay); } -/// Offers a line to the sketch; false if it did not take it. -pub fn forward(self: *Host, _: *Console, line: []const u8, now: u64) bool { +/// Offers a line to the sketch. +pub fn forward(self: *Host, _: *Console, line: []const u8, now: u64) abi.Outcome { self.input.now = now; const ctx = self.context(); return self.sketch.command(&ctx, self.live().ptr, self.session.bytes.ptr, &line); diff --git a/src/host/audio.zig b/src/host/audio.zig index fc8dcfb..87332b1 100644 --- a/src/host/audio.zig +++ b/src/host/audio.zig @@ -55,19 +55,22 @@ pub const Audio = struct { return out; } + /// `.none` when the file is missing or unreadable, remembered so the + /// warning prints once. pub fn load(self: *Audio, path: []const u8) abi.Audio { if (self.paths.get(path)) |audio| return audio; - const audio = self.add(decodeFile(self.gpa, path) catch |err| blk: { + const audio: abi.Audio = if (decodeFile(self.gpa, path)) |samples| self.add(samples) else |err| blk: { std.log.warn("sound {s}: {t}", .{ path, err }); - break :blk &.{}; - }); + break :blk .none; + }; const key = self.gpa.dupe(u8, path) catch return audio; self.paths.put(self.gpa, key, audio) catch self.gpa.free(key); return audio; } + /// `.none` when `wav` does not decode. pub fn decode(self: *Audio, wav: []const u8) abi.Audio { - return self.add(decodeWav(self.gpa, wav) catch &.{}); + return self.add(decodeWav(self.gpa, wav) catch return .none); } fn add(self: *Audio, samples: []f32) abi.Audio { diff --git a/src/host/script.zig b/src/host/script.zig index 4563a47..ec716e9 100644 --- a/src/host/script.zig +++ b/src/host/script.zig @@ -73,7 +73,11 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons } else if (eq(u8, command, "type")) { for (rest) |byte| try host.route(.{ .kind = .text, .time = now.*, .value = byte }); } else if (eq(u8, command, "console")) { - host.runLine(rest, now.*); + // A failing console line fails the script, like a failed expect. + host.runLine(rest, now.*) catch |err| { + std.debug.print(" console {s}: {t}\n", .{ rest, err }); + return err; + }; } else if (eq(u8, command, "frames")) { const count = try std.fmt.parseInt(usize, rest, 10); for (0..count) |_| try advance(host, now); |
