summaryrefslogtreecommitdiff
path: root/src/sketch/web.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/sketch/web.zig')
-rw-r--r--src/sketch/web.zig46
1 files changed, 37 insertions, 9 deletions
diff --git a/src/sketch/web.zig b/src/sketch/web.zig
index bc7fead..27313a6 100644
--- a/src/sketch/web.zig
+++ b/src/sketch/web.zig
@@ -18,7 +18,18 @@ const js = struct {
extern "host" fn openFile(command: [*]const u8, command_len: usize, pattern: [*]const u8, pattern_len: usize) void;
extern "canvas" fn clear(r: u32, g: u32, b: u32, a: u32) void;
extern "canvas" fn fill(x: f32, y: f32, w: f32, h: f32, r: u32, g: u32, b: u32, a: u32) void;
- extern "canvas" fn image(id: u32, x: f32, y: f32, w: f32, h: f32, sx: f32, sy: f32, sw: f32, sh: f32, alpha: u32) void;
+ extern "canvas" fn image(id: u32, x: f32, y: f32, w: f32, h: f32, sx: f32, sy: f32, sw: f32, sh: f32, r: u32, g: u32, b: u32, a: u32) void;
+ /// The image (or `sw` x `sh` at `sx`, `sy`) with its corners at the
+ /// three points: top left, top right, bottom left.
+ extern "canvas" fn imageCorners(id: u32, x0: f32, y0: f32, x1: f32, y1: f32, x2: f32, y2: f32, sx: f32, sy: f32, sw: f32, sh: f32, r: u32, g: u32, b: u32, a: u32) void;
+ /// Fills the subpaths `points[ends[i - 1] .. ends[i]]` nonzero:
+ /// `points` is `len` pairs of f32, `ends` `ends_len` u32s, `colors`
+ /// `colors_len` RGBA quadruples, one per subpath (else `r, g, b, a`).
+ extern "canvas" fn shape(points: [*]const [2]f32, len: usize, ends: [*]const u32, ends_len: usize, colors: [*]const abi.Color, colors_len: usize, r: u32, g: u32, b: u32, a: u32) void;
+ /// Draws into image `id`, or the canvas for 0.
+ extern "canvas" fn target(id: u32) void;
+ /// `abi.BlendMode` by value.
+ extern "canvas" fn blend(mode: u32) void;
extern "canvas" fn text(x: f32, y: f32, scale: f32, ptr: [*]const u8, len: usize, r: u32, g: u32, b: u32, a: u32) void;
extern "canvas" fn clip(enabled: u32, x: f32, y: f32, w: f32, h: f32) void;
extern "canvas" fn canvas(w: u32, h: u32) void;
@@ -136,8 +147,11 @@ fn Driver(comptime App: type) type {
const now: u64 = @intFromFloat(@max(0, ms) * std.time.ns_per_ms);
const dt = if (last == 0) 0 else now -| last;
last = now;
+ // `in.dt` comes from the tick, as on native, so it is what a
+ // replay of the tick would give.
+ const tick = abi.Event.tick(now, dt);
if (event_count < events.len) {
- events[event_count] = .{ .kind = .tick, .x = @floatCast(@as(f64, @floatFromInt(dt)) / std.time.ns_per_s) };
+ events[event_count] = tick;
event_count += 1;
}
for (events[0..event_count]) |*e| {
@@ -147,7 +161,7 @@ fn Driver(comptime App: type) type {
_ = scratch.reset(.retain_capacity);
commands = .empty;
input.now = now;
- input.dt = dt;
+ input.dt = tick.tickDt();
input.frame += 1;
input.events = events[0..event_count];
input.started = input.frame == 1;
@@ -159,9 +173,15 @@ fn Driver(comptime App: type) type {
G.update(&ctx, std.mem.asBytes(&state));
input.events = &.{};
event_count = 0;
+ // No timeline here: only the first frame comes from nowhere.
+ input.jumped = input.frame == 1;
G.draw(&ctx, std.mem.asBytes(&state));
+ js.target(0);
+ js.blend(0);
js.clip(0, 0, 0, 0, 0);
for (commands.items) |c| submit(c);
+ js.target(0);
+ js.blend(0);
// After the frame, as on native: lines `draw` asked to run.
for (commands.items) |c| if (c == .run) run(c.run);
@@ -176,7 +196,13 @@ fn Driver(comptime App: type) type {
switch (c) {
.clear => |v| js.clear(v.r, v.g, v.b, v.a),
.rect => |v| js.fill(v.dst.x, v.dst.y, v.dst.w, v.dst.h, v.color.r, v.color.g, v.color.b, v.color.a),
- .image => |v| js.image(@backingInt(v.image), v.dst.x, v.dst.y, v.dst.w, v.dst.h, v.src.x, v.src.y, v.src.w, v.src.h, v.tint.a),
+ .image => |v| if (v.corners) |q|
+ js.imageCorners(@backingInt(v.image), q.top_left[0], q.top_left[1], q.top_right[0], q.top_right[1], q.bottom_left[0], q.bottom_left[1], v.src.x, v.src.y, v.src.w, v.src.h, v.tint.r, v.tint.g, v.tint.b, v.tint.a)
+ else
+ js.image(@backingInt(v.image), v.dst.x, v.dst.y, v.dst.w, v.dst.h, v.src.x, v.src.y, v.src.w, v.src.h, v.tint.r, v.tint.g, v.tint.b, v.tint.a),
+ .shape => |v| js.shape(v.points.ptr, v.points.len, v.ends.ptr, v.ends.len, v.colors.ptr, v.colors.len, v.color.r, v.color.g, v.color.b, v.color.a),
+ .target => |v| js.target(@backingInt(v)),
+ .blend => |v| js.blend(@backingInt(v)),
.text => |v| js.text(v.x, v.y, v.scale, v.bytes.ptr, v.bytes.len, v.color.r, v.color.g, v.color.b, v.color.a),
.clip => |v| if (v) |r| js.clip(1, r.x, r.y, r.w, r.h) else js.clip(0, 0, 0, 0, 0),
.canvas => |v| {
@@ -320,8 +346,10 @@ fn Driver(comptime App: type) type {
/// it, and returns its length; -1 when there is no such enum field.
fn tag(len: usize) callconv(.c) i32 {
const name = path[0..@min(len, path.len)];
- for (G.description.state.fields) |f| {
- if (f.kind != .@"enum" or !std.mem.eql(u8, std.mem.span(f.path), name)) continue;
+ // Paths may index arrays and vector lanes (`list[2].mood`).
+ if (abi.layout.resolve(G.description.state.fields, name)) |r| {
+ const f = r.field;
+ if (f.kind != .@"enum") return -1;
const value: u64 = @intFromFloat(field(len));
var pairs = std.mem.tokenizeScalar(u8, std.mem.span(f.tags), ',');
while (pairs.next()) |pair| {
@@ -339,9 +367,9 @@ fn Driver(comptime App: type) type {
fn field(len: usize) callconv(.c) f64 {
const name = path[0..@min(len, path.len)];
const bytes = std.mem.asBytes(&state);
- for (G.description.state.fields) |f| {
- if (!std.mem.eql(u8, std.mem.span(f.path), name)) continue;
- const raw = bytes[f.offset..][0..f.size];
+ if (abi.layout.resolve(G.description.state.fields, name)) |r| {
+ const f = r.field;
+ const raw = bytes[r.offset..][0..f.size];
var value: u64 = 0;
for (raw[0..@min(raw.len, 8)], 0..) |b, i| value |= @as(u64, b) << @intCast(i * 8);
return switch (f.kind) {