summaryrefslogtreecommitdiff
path: root/test/replay.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/replay.zig')
-rw-r--r--test/replay.zig48
1 files changed, 48 insertions, 0 deletions
diff --git a/test/replay.zig b/test/replay.zig
new file mode 100644
index 0000000..096acab
--- /dev/null
+++ b/test/replay.zig
@@ -0,0 +1,48 @@
+//! Replay fixture: state driven by the pointer, buttons, keys and time,
+//! frame by frame, so any difference between a live run and its replay
+//! (batched frames, input state, dt) shows in the bytes.
+const sk = @import("sketch");
+
+pub const name = "replay";
+pub const width = 640;
+pub const height = 360;
+
+const Dot = struct {
+ pos: @Vector(2, f32) = @splat(0),
+ kind: enum(u8) { small = 1, big = 2 } = .small,
+};
+
+pub const State = struct {
+ /// Chases the pointer a little every frame: where it ends depends on
+ /// where the pointer was at each frame, not only at the end.
+ position: @Vector(2, f32) = .{ 320, 180 },
+ velocity: @Vector(2, f32) = @splat(0),
+ /// Sum of `in.dt` over updates, and how many there were.
+ elapsed: u64 = 0,
+ updates: u32 = 0,
+ /// Frames with the left button, and with shift, held.
+ pressing: u32 = 0,
+ shifting: u32 = 0,
+ /// One per click, shaped like `sk.List`.
+ dots: struct { buffer: [8]Dot = @splat(.{}), len: u32 = 0 } = .{},
+};
+
+pub fn update(s: *State, in: *const sk.Input) void {
+ const mouse: @Vector(2, f32) = .{ in.mouse_x, in.mouse_y };
+ s.velocity = (s.velocity + (mouse - s.position) * @as(@Vector(2, f32), @splat(0.01))) * @as(@Vector(2, f32), @splat(0.9));
+ s.position += s.velocity;
+ s.elapsed += in.dt;
+ s.updates += 1;
+ if (in.buttons & 1 != 0) s.pressing += 1;
+ if (in.held(sk.Key.left_shift)) s.shifting += 1;
+ for (in.events) |e| if (e.kind == .mouse_up and s.dots.len < s.dots.buffer.len) {
+ s.dots.buffer[s.dots.len] = .{ .pos = .{ e.x, e.y }, .kind = if (in.held(sk.Key.left_shift)) .big else .small };
+ s.dots.len += 1;
+ };
+}
+
+pub fn draw(s: *const State, f: *sk.Frame) void {
+ f.clear(0x202020);
+ for (s.dots.buffer[0..s.dots.len]) |d| f.fill(.{ .x = d.pos[0] - 4, .y = d.pos[1] - 4, .w = 8, .h = 8 }, 0x80c0ff);
+ f.fill(.{ .x = s.position[0] - 10, .y = s.position[1] - 10, .w = 20, .h = 20 }, 0xff8040);
+}