const coral = @import("coral"); const kym = @import("kym"); const ona = @import("ona"); const transform2d_typename = @typeName(ona.gfx.Transform2D); fn bind_canvas(environment: *kym.vm.Environment, canvas: *ona.gfx.Canvas) !void { const object = try environment.new(.{}); defer environment.release(object); const sprite_creator = try environment.new(.{ .userinfo = @ptrToInt(canvas), .behavior = &.{ .caller = struct { fn call(context: kym.vm.Object.CallContext) kym.vm.RuntimeError!kym.vm.Value { try context.check(context.arguments.len == 2, "2 arguments expected"); const transform = context.arguments[0].object; try context.check(transform.userinfo == @ptrToInt(transform2d_typename), "`transform2d` expected"); _ = try @intToPtr(*ona.gfx.Canvas, context.object.userinfo).create_sprite( coral.io.bytes_to(ona.gfx.Transform2D, transform.userdata).?); return .nil; } }.call, }, }); defer environment.release(sprite_creator); try environment.raw_set(object, "create_sprite", sprite_creator.as_value()); try environment.global_set("canvas", object.as_value()); } // fn bind_transform_2d(environment: *kym.Environment) !*kym.Object { // const allocator = environment.allocator(); // const transform_2d = try allocator.allocate_one(ona.gfx.Transform2D); // errdefer allocator.deallocate(transform_2d); // const object = try environment.new(@ptrToInt(transform_2d), .{ // .destructor = struct { // fn destruct() void { // allocator.deallocate(transform_2d); // } // }.destruct, // }); // defer environment.release(object); // try environment.set_field(environment.globals(), object); // } pub fn main() anyerror!void { return ona.App.run(anyerror, start); } fn start(app: *ona.App) anyerror!void { var kym_env = try kym.vm.Environment.init(ona.allocator, .{ .stack_max = 256, .calls_max = 256, }); defer kym_env.deinit(); try bind_canvas(&kym_env, app.canvas()); { const index_path = "index.kym"; const index_file = try app.data_fs().open_readable(index_path); defer if (!index_file.close()) ona.log_error(&.{.{.string = "failed to close "}, .{.string = index_path}}); const index_size = (try app.data_fs().query(index_path)).size; const index_allocation = try coral.io.allocate_many(u8, index_size, ona.allocator); defer coral.io.deallocate(ona.allocator, index_allocation); var index_buffer = coral.buffer.Fixed{.data = index_allocation[0 .. index_size]}; { var stream_buffer = [_]u8{0} ** 1024; if ((try coral.io.stream(index_buffer.as_writer(), index_file.as_reader(), &stream_buffer)) != index_size) return error.IoUnavailable; } { const index_script = try kym_env.new_script(.{ .name = index_path, .data = index_buffer.data }); defer kym_env.release(index_script); _ = try kym_env.call(index_script, &.{}); } } while (app.poll()) { app.present(); } }