diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-30 21:34:41 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-12 13:22:02 -0300 |
| commit | 2348776791e9dd17afeb12a06ba42a4a71917bcf (patch) | |
| tree | 780c4b12b76c8b97a28408dd40944d65f159fdc7 /examples | |
| parent | 590ba7b9b579b97e34b51ab7e3460ebc85713343 (diff) | |
| download | sketchbook-2348776791e9dd17afeb12a06ba42a4a71917bcf.tar.gz sketchbook-2348776791e9dd17afeb12a06ba42a4a71917bcf.zip | |
sketchbook: a drawing layer on top, host fixes for replay and migration
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/flock.zig | 56 | ||||
| -rw-r--r-- | examples/pen.zig | 43 |
2 files changed, 99 insertions, 0 deletions
diff --git a/examples/flock.zig b/examples/flock.zig new file mode 100644 index 0000000..78df621 --- /dev/null +++ b/examples/flock.zig @@ -0,0 +1,56 @@ +//! The drawing layer's low levels, mixed with today's Frame methods: many +//! triangles tessellated into two `sk.draw.Tessellation`s (L1) and emitted +//! as two `Command.shape`s (L0), between an `f.fill` and an `f.print`. +//! Batching like this is the sketch's own choice, and changes nothing in +//! the picture: both hosts draw the shapes of a batch one after another. +const std = @import("std"); +const sk = @import("sketch"); +const vec = sk.math; + +pub const name = "flock"; +pub const width = 640; +pub const height = 360; + +const Boid = struct { + position: vec.Vec = .{ 0, 0 }, + velocity: vec.Vec = .{ 1, 0 }, +}; + +pub const State = struct { + rng: std.Random.Xoshiro256 = .init(7), + boids: sk.List(Boid, 512) = .{}, + clock: sk.Clock = .{}, +}; + +pub fn update(s: *State, in: *const sk.Input) void { + // Dragging adds boids, where the pointer was at each move. + if (in.buttons & 1 != 0) for (in.events) |e| if (e.kind == .mouse_move) { + const r = s.rng.random(); + s.boids.appendBounded(.{ .position = .{ e.x, e.y }, .velocity = vec.unit(r.float(f32) * 2 * std.math.pi) }) catch {}; + }; + for (0..s.clock.steps(in)) |_| for (s.boids.slice()) |*b| { + // Steer toward the centre, gently; wrap at the edges. + const centre: vec.Vec = .{ width / 2, height / 2 }; + b.velocity = vec.withLength(b.velocity + vec.withLength(centre - b.position, 0.03), 2); + b.position += b.velocity; + b.position = @mod(b.position + vec.Vec{ width, height }, vec.Vec{ width, height }); + }; +} + +const shape = [_]vec.Vec{ .{ 8, 0 }, .{ -8, -4 }, .{ -8, 4 } }; + +pub fn draw(s: *const State, f: *sk.Frame) void { + f.clear(0xffffff); + f.fill(.{ .x = 0, .y = height - 40, .w = width, .h = 40 }, 0xdcdcdc); // L0: a Frame method + var bodies: sk.draw.Tessellation = .empty; // L1: our own triangles + var edges: sk.draw.Tessellation = .empty; + for (s.boids.constSlice()) |b| { + const xf: vec.Affine = .place(b.position, vec.angle(b.velocity)); + bodies.fillPolygon(f.scratch, &shape, xf) catch return; + edges.strokePolygon(f.scratch, &shape, 1, xf) catch return; + } + f.shape(&bodies, .gray(127)); + // L0 itself: the command, built by hand from the same data. + f.emit(.{ .shape = .{ .color = .black, .points = edges.points.items, .ends = edges.ends.items, .indices = edges.indices.items } }); + f.print(8, height - 30, 2, 0x000000, "{d} boids, drag to add", .{s.boids.len}); +} diff --git a/examples/pen.zig b/examples/pen.zig new file mode 100644 index 0000000..acbd236 --- /dev/null +++ b/examples/pen.zig @@ -0,0 +1,43 @@ +//! The drawing layer's convenience level: a ball that chases the mouse, +//! and three spinners around it, drawn with `sk.draw` and plain-data Pens. +//! Stepped at a fixed 60 Hz by `sk.Clock`, so a replay matches. +const std = @import("std"); +const sk = @import("sketch"); +const vec = sk.math; + +pub const name = "pen"; +pub const width = 640; +pub const height = 360; + +pub const State = struct { + position: vec.Vec = .{ width / 2, height / 2 }, + velocity: vec.Vec = .{ 0, 0 }, + spin: f32 = 0, + clock: sk.Clock = .{ .hz = 60 }, +}; + +pub fn update(s: *State, in: *const sk.Input) void { + for (0..s.clock.steps(in)) |_| { + const mouse: vec.Vec = .{ in.mouse_x, in.mouse_y }; + s.velocity = vec.clampLength(s.velocity + vec.withLength(mouse - s.position, 0.2), 5); + s.position += s.velocity; + s.spin += 0.03; + } +} + +const ball: sk.draw.Pen = .{ .fill = .gray(127), .stroke = .black, .width = 2 }; +const spinner: sk.draw.Pen = .{ .fill = sk.Color.gray(200).alpha(160), .stroke = .black, .width = 2 }; + +pub fn draw(s: *const State, f: *sk.Frame) void { + f.clear(sk.Color.white); + sk.draw.circle(f, s.position, 24, ball); + for (0..3) |i| { + const turn = s.spin + @as(f32, @floatFromInt(i)) * 2 * std.math.pi / 3.0; + // Push, translate, rotate: a copy of the pen, placed. + const pen = spinner.transformed(.place(s.position + vec.scale(vec.unit(turn), 60), turn)); + sk.draw.circle(f, .{ 0, 0 }, 12, pen); + sk.draw.line(f, .{ 0, 0 }, .{ 12, 0 }, pen); + sk.draw.polygon(f, &.{ .{ 18, 0 }, .{ 14, -4 }, .{ 14, 4 } }, pen); + } + f.print(8, 8, 1, 0x000000, "steps {d}", .{s.clock.count}); +} |
