ona/src/main.zig

57 lines
1.6 KiB
Zig
Raw Normal View History

const coral = @import("coral");
const std = @import("std");
const ona = @import("ona");
const Actors = struct {
instances: coral.stack.Sequential(ona.gfx.Queue.Instance2D) = .{.allocator = coral.heap.allocator},
move_x: ona.act.Axis = .{.keys = .{.a, .d}},
move_y: ona.act.Axis = .{.keys = .{.w, .s}},
};
pub fn main() !void {
try ona.start_app(setup, .{
.tick_rate = 60,
.execution = .{.thread_share = 0.1},
});
}
fn load(display: coral.ReadBlocking(ona.gfx.Display), actors: coral.Write(Actors)) !void {
display.res.resize(1280, 720);
try actors.res.instances.push_many(800, .{
.origin = .{75, 75},
.xbasis = .{100, 0},
.ybasis = .{0, 100},
.color = ona.gfx.color.compress(ona.gfx.color.rgb(1, 0, 0)),
});
}
fn exit(actors: coral.Write(Actors)) void {
actors.res.instances.deinit();
}
fn render(gfx: ona.gfx.Queue, actors: coral.Write(Actors)) !void {
try gfx.buffer.draw_2d(.{
.mesh_2d = gfx.primitives.quad_mesh,
.instances = actors.res.instances.values,
});
}
fn update(actors: coral.Write(Actors), mapping: coral.Read(ona.act.Mapping)) !void {
actors.res.instances.values[0].origin += .{
mapping.res.axis_strength(actors.res.move_x),
mapping.res.axis_strength(actors.res.move_y),
};
}
fn setup(world: *coral.World, events: ona.App.Events) !void {
try world.set_resource(.none, Actors{});
try world.on_event(events.load, coral.system_fn(load), .{.label = "load"});
try world.on_event(events.update, coral.system_fn(update), .{.label = "update"});
try world.on_event(events.exit, coral.system_fn(exit), .{.label = "exit"});
try world.on_event(events.render, coral.system_fn(render), .{.label = "render actors"});
}