summaryrefslogtreecommitdiff
path: root/test/shapes.zig
blob: 5145143e152e5bc37fbe37af92316e13c91256d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! The cases `Command.shape` must draw alike on both hosts, for the native
//! screenshot test (test/shapes.script) and the browser one
//! (tools/shapes_web.mjs): overlapping translucent circles, a concave
//! polygon, an acute translucent zig-zag, a ring, a 1 px line. The console
//! line `circles N` switches to N circles batched or not (`batched N`),
//! for timing.
const std = @import("std");
const sk = @import("sketch");

pub const name = "shapes";
pub const width = 400;
pub const height = 400;

pub const State = struct {
    circles: u32 = 0,
    batched: bool = false,
};

pub const commands = .{ "circles", "batched" };

pub fn command(s: *State, line: []const u8) !bool {
    var words = std.mem.tokenizeScalar(u8, line, ' ');
    const verb = words.next() orelse return false;
    if (!std.mem.eql(u8, verb, "circles") and !std.mem.eql(u8, verb, "batched")) return false;
    s.circles = try std.fmt.parseInt(u32, words.next() orelse "0", 10);
    s.batched = std.mem.eql(u8, verb, "batched");
    return true;
}

const red: sk.Color = .{ .r = 255, .g = 0, .b = 0, .a = 128 };
const green: sk.Color = .{ .r = 0, .g = 128, .b = 0, .a = 128 };
const blue: sk.Color = .{ .r = 0, .g = 0, .b = 255, .a = 128 };
const purple: sk.Color = .{ .r = 128, .g = 0, .b = 128, .a = 160 };

const arrow = [_]sk.math.Vec{ .{ 220, 20 }, .{ 380, 20 }, .{ 380, 180 }, .{ 300, 70 }, .{ 220, 180 } };
const zigzag = [_]sk.math.Vec{ .{ 20, 380 }, .{ 60, 220 }, .{ 100, 380 }, .{ 140, 220 }, .{ 180, 380 } };

pub fn draw(s: *const State, f: *sk.Frame) void {
    f.clear(sk.Color.white);
    if (s.circles > 0) return many(s, f);
    // Two commands: the overlap blends twice, on both hosts.
    sk.draw.circle(f, .{ 80, 80 }, 50, .{ .fill = red });
    sk.draw.circle(f, .{ 140, 80 }, 50, .{ .fill = red });
    sk.draw.polygon(f, &arrow, .{ .fill = green });
    var t: sk.draw.Tessellation = .empty;
    t.strokePolyline(f.scratch, &zigzag, 14, .identity) catch return;
    f.shape(&t, blue);
    sk.draw.circle(f, .{ 300, 240 }, 30, .{ .stroke = purple, .width = 8 });
    sk.draw.line(f, .{ 220, 300.5 }, .{ 380, 300.5 }, .{ .stroke = .black });
}

/// Circles spread by a hash, in one shape each or all in one.
fn many(s: *const State, f: *sk.Frame) void {
    var t: sk.draw.Tessellation = .empty;
    for (0..s.circles) |i| {
        const h = std.hash.Wyhash.hash(0, std.mem.asBytes(&i));
        const at: sk.math.Vec = .{ @floatFromInt(h % 400), @floatFromInt((h >> 16) % 400) };
        if (s.batched) {
            t.fillCircle(f.scratch, at, 4, .identity) catch return;
        } else sk.draw.circle(f, at, 4, .{ .fill = .{ .r = @truncate(h >> 32), .g = 40, .b = 90, .a = 160 } });
    }
    if (s.batched) f.shape(&t, .{ .r = 200, .g = 40, .b = 90 });
}