2023-04-19 01:25:35 +02:00
|
|
|
const coral = @import("coral");
|
|
|
|
|
|
|
|
const kym = @import("kym");
|
|
|
|
|
|
|
|
const ona = @import("ona");
|
|
|
|
|
2023-05-06 03:47:52 +02:00
|
|
|
const transform2d_typename = @typeName(ona.gfx.Transform2D);
|
2023-04-19 01:25:35 +02:00
|
|
|
|
2023-05-06 03:47:52 +02:00
|
|
|
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);
|
2023-04-19 01:25:35 +02:00
|
|
|
|
2023-05-06 03:47:52 +02:00
|
|
|
try environment.raw_set(object, "create_sprite", sprite_creator.as_value());
|
|
|
|
try environment.global_set("canvas", object.as_value());
|
2023-04-19 01:25:35 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 03:47:52 +02:00
|
|
|
// 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);
|
|
|
|
// }
|
|
|
|
|
2023-04-19 01:25:35 +02:00
|
|
|
pub fn main() anyerror!void {
|
|
|
|
return ona.App.run(anyerror, start);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start(app: *ona.App) anyerror!void {
|
2023-05-06 03:47:52 +02:00
|
|
|
var kym_env = try kym.vm.Environment.init(ona.allocator, .{
|
2023-04-19 01:25:35 +02:00
|
|
|
.stack_max = 256,
|
2023-05-06 03:47:52 +02:00
|
|
|
.calls_max = 256,
|
2023-04-19 01:25:35 +02:00
|
|
|
});
|
|
|
|
|
2023-05-06 03:47:52 +02:00
|
|
|
defer kym_env.deinit();
|
2023-04-19 01:25:35 +02:00
|
|
|
|
2023-05-06 03:47:52 +02:00
|
|
|
try bind_canvas(&kym_env, app.canvas());
|
2023-04-19 01:25:35 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
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;
|
2023-05-06 03:47:52 +02:00
|
|
|
const index_allocation = try coral.io.allocate_many(u8, index_size, ona.allocator);
|
2023-04-19 01:25:35 +02:00
|
|
|
|
2023-05-06 03:47:52 +02:00
|
|
|
defer coral.io.deallocate(ona.allocator, index_allocation);
|
2023-04-19 01:25:35 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-05-06 03:47:52 +02:00
|
|
|
{
|
|
|
|
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, &.{});
|
|
|
|
}
|
2023-04-19 01:25:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (app.poll()) {
|
2023-05-06 03:47:52 +02:00
|
|
|
app.present();
|
2023-04-19 01:25:35 +02:00
|
|
|
}
|
|
|
|
}
|