summaryrefslogtreecommitdiff
path: root/src/host/Console.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/Console.zig')
-rw-r--r--src/host/Console.zig55
1 files changed, 52 insertions, 3 deletions
diff --git a/src/host/Console.zig b/src/host/Console.zig
index 88e4864..9f14830 100644
--- a/src/host/Console.zig
+++ b/src/host/Console.zig
@@ -14,7 +14,7 @@ pub const Key = enum { left, right, home, end, backspace, delete, clear, tab, su
const Line = struct { bytes: [118]u8 = @splat(0), len: usize = 0 };
const host_commands = [_][]const u8{
- "help", "overlay memory", "overlay sound", "get", "set", "reset", "save", "load", "snapshot", "recording", "marks", "clips",
+ "help", "overlay memory", "overlay sound", "get", "set", "reset", "save", "load", "snapshot", "recording", "marks", "clips", "play session",
"record", "stop", "play", "pause", "step", "seek start", "seek end", "diff", "quit", "exit",
"overlay timing", "sessions", "timing on", "timing off", "timing frame", "timing work", "timing reset", "timing stats",
};
@@ -238,6 +238,17 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const
h.startRecording();
self.recording = .of(n);
self.print("Started recording {s} from snapshot {s}.", .{ n, from });
+ } else if (eq(command, "play") and arg != null and eq(arg.?, "session")) {
+ var more = std.mem.tokenizeAny(u8, rest, " \t");
+ const index = std.fmt.parseInt(u64, more.next() orelse return error.ExpectedSession, 10) catch return error.InvalidSession;
+ var at: ?u64 = null;
+ if (more.next()) |word| {
+ if (!eq(word, "at")) return error.UnexpectedArgument;
+ const seconds = std.fmt.parseFloat(f64, more.next() orelse return error.ExpectedPosition) catch return error.InvalidPosition;
+ at = @intFromFloat(@max(0, seconds) * std.time.ns_per_s);
+ }
+ try stop(host);
+ return self.playSession(host, index, at, now);
} else if (eq(command, "play")) {
const n = try nameOr(arg, &self.recording);
try stop(host);
@@ -301,8 +312,12 @@ pub fn draw(self: *const Console, host: *Host, out: *std.ArrayList(abi.Command),
const width: f32 = @floatFromInt(host.canvas[0]);
const h = &host.timeline;
try out.append(arena, .{ .rect = .{ .dst = .{ .x = 0, .y = top, .w = width, .h = panel_height }, .color = .{ .r = 24, .g = 28, .b = 28 } } });
- const status = try std.fmt.allocPrint(arena, "{s} | {t} | snapshot {s} | recording {s} | event {d} | {d}ms | rec {t}", .{
- host.name, h.mode, self.snapshot.get(), self.recording.get(), h.cursor, h.position / std.time.ns_per_ms, host.sessions.mode(),
+ const s = &host.sessions;
+ const playing = h.mode == .playing or h.mode == .paused;
+ const frame = if (playing) (if (h.key) |k| k.frame else 0) + h.fed else s.frames;
+ const at = (if (playing) h.position else s.last_time -| s.first_time) / std.time.ns_per_s;
+ const status = try std.fmt.allocPrint(arena, "{s} | {t} | session {d} frame {d} ({d}:{d:0>2}) | mark {s} | clip {s} | rec {t}", .{
+ host.name, h.mode, if (playing) h.session orelse s.session else s.session, frame, at / 60, at % 60, self.snapshot.get(), self.recording.get(), s.mode(),
});
try out.append(arena, .{ .text = .{ .x = 12, .y = top + 8, .scale = 1, .color = hint, .bytes = status } });
for (&self.lines, 0..) |*line, i| {
@@ -378,6 +393,40 @@ fn loadClip(host: *Host, n: []const u8) !void {
host.timeline.load(key, events);
}
+/// Plays session `index` from its start, or paused `at` nanoseconds into
+/// it: from the newest keyframe before then, replayed up to it.
+fn playSession(self: *Console, host: *Host, index: u64, at: ?u64, now: u64) ![]const abi.Event {
+ const s = &host.sessions;
+ const h = &host.timeline;
+ if (index == s.session) try s.commit(.none);
+ const sum = try s.summary(index);
+ if (sum.keys == 0) return error.EmptyRecording;
+ const t0 = (try s.keyAt(index, 0)).time;
+ const k = if (at) |t| try s.keyBefore(index, .time, t0 + t) orelse 0 else 0;
+ const key = try s.load(index, k);
+ errdefer key.free(host.gpa);
+ var events: std.ArrayList(abi.Event) = .empty;
+ errdefer events.deinit(host.gpa);
+ try s.events(index, key.batch, key.frame, sum.frames, &events);
+ var checks: @TypeOf(h.checks) = .empty;
+ errdefer checks.deinit(host.gpa);
+ for (k + 1..sum.keys) |j| {
+ const later = try s.keyAt(index, j);
+ try checks.append(host.gpa, .{ .frame = later.frame, .key = j, .observed = switch (later.why) {
+ .periodic, .mark, .before_reload, .exit => true,
+ else => false,
+ } });
+ }
+ h.load(key, events);
+ h.session = index;
+ h.checks = checks;
+ try h.play(now);
+ self.print("Playing session {d} from frame {d}.", .{ index, key.frame });
+ if (key.build != host.build or !eq(key.source, host.source)) self.print("session {d} frame {d} was built from {s}; replaying with {s}", .{ index, key.frame, key.source, host.source });
+ if (at) |t| return h.seekTime((t0 + t) -| h.base);
+ return &.{};
+}
+
/// A replay of a clip made by other code may diverge: say so.
fn warnBuild(self: *Console, host: *const Host) void {
const key = host.timeline.key orelse return;