From e77b01d4b5504cea08a3f88cf8502897d868a1d3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 16 Nov 2026 18:04:21 -0300 Subject: Add glTF scene rendering, resource setup and 9P controls --- src/sketch/root.zig | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) (limited to 'src/sketch/root.zig') diff --git a/src/sketch/root.zig b/src/sketch/root.zig index 52f05b2..434478c 100644 --- a/src/sketch/root.zig +++ b/src/sketch/root.zig @@ -10,6 +10,7 @@ //! pub const Store = struct { ... }; // on disk: survives restarts too //! pub const commands = .{"hello"}; // console completions //! pub fn init(state: *State, host: sk.Host) void +//! pub fn setup(session: *Session, init: *sk.Init) !void //! pub fn update(state: *State, in: *const sk.Input) void //! pub fn draw(state: *const State, session: *Session, store: *Store, f: *sk.Frame) void //! pub fn command(state: *State, store: *Store, c: *sk.Console, line: []const u8) !bool @@ -59,6 +60,14 @@ pub const Voice = abi.Voice; pub const BlendMode = abi.BlendMode; pub const Face = abi.Face; +/// Resource initialization, once per fresh Session (also when State is restored). +/// No State, Input, scratch allocator, or drawing commands are available here. +pub const Init = struct { + host: Host, + session: std.mem.Allocator, + store: std.mem.Allocator, +}; + /// What `draw` receives: input, host services, memory by lifetime and the /// command list. Commands may be built from anything: the methods copy what /// they keep (text, GPU descriptions) into `scratch`, except bulk data @@ -288,7 +297,7 @@ pub const Console = struct { /// The entry points of `App`, for a host that links it statically. pub fn sketch(comptime App: type) abi.Sketch { const G = Glue(App); - return .{ .info = G.info, .defaults = G.defaults, .init = G.init, .update = G.update, .draw = G.draw, .command = G.command }; + return .{ .info = G.info, .defaults = G.defaults, .init = G.init, .setup = G.setup, .update = G.update, .draw = G.draw, .command = G.command }; } /// Exports `App` under `abi.Sketch.symbols`, for a hot-reloaded library. @@ -298,6 +307,7 @@ pub fn exportSketch(comptime App: type) void { @export(&G.info, .{ .name = names.info }); @export(&G.defaults, .{ .name = names.defaults }); @export(&G.init, .{ .name = names.init }); + @export(&G.setup, .{ .name = names.setup }); @export(&G.update, .{ .name = names.update }); @export(&G.draw, .{ .name = names.draw }); @export(&G.command, .{ .name = names.command }); @@ -325,6 +335,7 @@ pub fn Glue(comptime App: type) type { ctx: *const abi.Context, state: [*]u8, frame: ?*Frame = null, + resource_init: ?*Init = null, console: ?*Console = null, line: []const u8 = "", }; @@ -342,6 +353,9 @@ pub fn Glue(comptime App: type) type { }; if (comptime is.named("update") and (T == *Session or T == *Store)) @compileError("update must be deterministic: it gets State and Input only, so a replay reproduces it"); + if (comptime is.named("setup") and (T == *State or T == *const State or T == *const Input)) + @compileError("setup initializes resources: it gets Session, Store, Init and Host only"); + if (comptime (T == *Init or T == *const Init) and !is.named("setup")) @compileError(name ++ " gets no Init: only setup does"); if (comptime is.named("draw") and T == *State) @compileError("draw only reads State: take *const State"); if (comptime T == *Frame and !is.named("draw")) @compileError(name ++ " gets no Frame: only draw does"); if (comptime T == *Console and !is.named("command")) @compileError(name ++ " gets no Console: only command does"); @@ -355,6 +369,8 @@ pub fn Glue(comptime App: type) type { offer.ctx.input else if (T == Host) offer.ctx.host + else if (T == *Init or T == *const Init) + offer.resource_init.? else if (T == *Frame) offer.frame.? else if (T == *Console) @@ -380,6 +396,16 @@ pub fn Glue(comptime App: type) type { pub fn init(ctx: *const abi.Context, s: [*]u8) callconv(.c) void { if (@hasDecl(App, "init")) call(App.init, "init", .{ .ctx = ctx, .state = s }); } + pub fn setup(ctx: *const abi.Context) callconv(.c) bool { + if (!@hasDecl(App, "setup")) return true; + var resource_init: Init = .{ .host = ctx.host, .session = ctx.session, .store = ctx.store }; + const result = call(App.setup, "setup", .{ .ctx = ctx, .state = undefined, .resource_init = &resource_init }); + if (@typeInfo(@TypeOf(result)) == .error_union) result catch |err| { + ctx.host.print("session setup failed: {t}", .{err}); + return false; + }; + return true; + } pub fn update(ctx: *const abi.Context, s: [*]u8) callconv(.c) void { if (@hasDecl(App, "update")) call(App.update, "update", .{ .ctx = ctx, .state = s }); } @@ -411,3 +437,35 @@ test { _ = @import("list.zig"); _ = @import("derived.zig"); } + +test "setup receives session resources without a Frame and reports errors" { + const App = struct { + pub const Session = struct { bytes: []u8 = &.{}, calls: u32 = 0 }; + pub fn setup(session: *Session, resources: *Init) !void { + session.calls += 1; + session.bytes = try resources.session.dupe(u8, "ready"); + return error.TestSetupFailure; + } + }; + const Log = struct { + fn log(ctx: *anyopaque, message: []const u8) void { + const seen: *bool = @ptrCast(@alignCast(ctx)); + seen.* = std.mem.eql(u8, message, "session setup failed: TestSetupFailure"); + } + }; + var seen = false; + var vtable: abi.Host.VTable = undefined; + vtable.log = Log.log; + var session: App.Session = .{}; + var ctx: abi.Context = undefined; + ctx.host = .{ .ctx = &seen, .vtable = &vtable }; + ctx.session_root = std.mem.asBytes(&session); + ctx.session = std.testing.allocator; + ctx.store = std.testing.allocator; + const app = sketch(App); + try std.testing.expect(!app.setup(&ctx)); + defer std.testing.allocator.free(session.bytes); + try std.testing.expectEqualStrings("ready", session.bytes); + try std.testing.expectEqual(@as(u32, 1), session.calls); + try std.testing.expect(seen); +} -- cgit v1.3