2024-05-29 19:27:02 +01:00
|
|
|
const coral = @import("coral");
|
|
|
|
|
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
const ona = @import("ona");
|
|
|
|
|
|
|
|
const Actors = struct {
|
2024-06-04 23:05:57 +01:00
|
|
|
instances: coral.stack.Sequential(ona.gfx.Point2D) = .{.allocator = coral.heap.allocator},
|
2024-06-27 20:01:06 +01:00
|
|
|
quad_mesh_2d: ?*ona.gfx.Handle = null,
|
|
|
|
body_texture: ?*ona.gfx.Handle = null,
|
|
|
|
render_texture: ?*ona.gfx.Handle = null,
|
2024-05-30 22:43:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const Player = struct {
|
2024-05-29 19:27:02 +01:00
|
|
|
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},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-02 23:08:25 +01:00
|
|
|
fn load(display: ona.Write(ona.gfx.Display), actors: ona.Write(Actors), assets: ona.Write(ona.gfx.Assets)) !void {
|
2024-06-20 21:54:00 +01:00
|
|
|
display.res.width, display.res.height = .{1280, 720};
|
2024-06-27 20:01:06 +01:00
|
|
|
actors.res.body_texture = try assets.res.create_from_file(coral.files.bundle, "actor.bmp");
|
|
|
|
actors.res.quad_mesh_2d = try assets.res.create_quad_mesh_2d(@splat(1));
|
|
|
|
|
|
|
|
actors.res.render_texture = try assets.res.context.create(.{
|
|
|
|
.texture = .{
|
|
|
|
.format = .rgba8,
|
|
|
|
|
|
|
|
.access = .{
|
|
|
|
.render = .{
|
|
|
|
.width = display.res.width,
|
|
|
|
.height = display.res.height,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2024-05-29 19:27:02 +01:00
|
|
|
|
2024-06-20 21:54:00 +01:00
|
|
|
try actors.res.instances.push_grow(.{0, 0});
|
2024-05-29 19:27:02 +01:00
|
|
|
}
|
|
|
|
|
2024-07-02 23:08:25 +01:00
|
|
|
fn exit(actors: ona.Write(Actors)) void {
|
2024-05-29 19:27:02 +01:00
|
|
|
actors.res.instances.deinit();
|
|
|
|
}
|
|
|
|
|
2024-07-02 23:08:25 +01:00
|
|
|
fn render(queue: ona.gfx.Queue, actors: ona.Write(Actors), display: ona.Read(ona.gfx.Display)) !void {
|
2024-06-27 20:01:06 +01:00
|
|
|
try queue.commands.append(.{.target = .{
|
|
|
|
.texture = actors.res.render_texture.?,
|
|
|
|
.clear_color = .{0, 0, 0, 0},
|
|
|
|
.clear_depth = 0,
|
|
|
|
}});
|
|
|
|
|
2024-06-04 23:05:57 +01:00
|
|
|
for (actors.res.instances.values) |instance| {
|
2024-06-27 20:01:06 +01:00
|
|
|
try queue.commands.append(.{.instance_2d = .{
|
|
|
|
.mesh_2d = actors.res.quad_mesh_2d.?,
|
|
|
|
.texture = actors.res.body_texture.?,
|
|
|
|
|
|
|
|
.transform = .{
|
|
|
|
.origin = instance,
|
|
|
|
.xbasis = .{64, 0},
|
|
|
|
.ybasis = .{0, 64},
|
2024-06-04 23:05:57 +01:00
|
|
|
},
|
2024-06-27 20:01:06 +01:00
|
|
|
}});
|
2024-06-04 23:05:57 +01:00
|
|
|
}
|
2024-06-27 20:01:06 +01:00
|
|
|
|
|
|
|
try queue.commands.append(.{.target = .{
|
|
|
|
.clear_color = null,
|
|
|
|
.clear_depth = null,
|
|
|
|
}});
|
|
|
|
|
|
|
|
try queue.commands.append(.{.instance_2d = .{
|
|
|
|
.mesh_2d = actors.res.quad_mesh_2d.?,
|
|
|
|
.texture = actors.res.render_texture.?,
|
|
|
|
|
|
|
|
.transform = .{
|
|
|
|
.origin = .{@floatFromInt(display.res.width / 2), @floatFromInt(display.res.height / 2)},
|
|
|
|
.xbasis = .{@floatFromInt(display.res.width), 0},
|
|
|
|
.ybasis = .{0, @floatFromInt(display.res.height)},
|
|
|
|
},
|
|
|
|
}});
|
2024-05-29 19:27:02 +01:00
|
|
|
}
|
|
|
|
|
2024-07-02 23:08:25 +01:00
|
|
|
fn update(player: ona.Read(Player), actors: ona.Write(Actors), mapping: ona.Read(ona.act.Mapping)) !void {
|
2024-06-04 23:05:57 +01:00
|
|
|
actors.res.instances.values[0] += .{
|
2024-06-27 20:01:06 +01:00
|
|
|
mapping.res.axis_strength(player.res.move_x) * 10,
|
|
|
|
mapping.res.axis_strength(player.res.move_y) * 10,
|
2024-05-29 19:27:02 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-07-02 23:08:25 +01:00
|
|
|
fn setup(world: *ona.World, events: ona.App.Events) !void {
|
|
|
|
try world.set_resource(Actors{});
|
|
|
|
try world.set_resource(Player{});
|
2024-05-29 19:27:02 +01:00
|
|
|
|
2024-07-02 23:08:25 +01:00
|
|
|
try world.on_event(events.load, ona.system_fn(load), .{.label = "load"});
|
|
|
|
try world.on_event(events.update, ona.system_fn(update), .{.label = "update"});
|
|
|
|
try world.on_event(events.exit, ona.system_fn(exit), .{.label = "exit"});
|
|
|
|
try world.on_event(events.render, ona.system_fn(render), .{.label = "render actors"});
|
2024-05-29 19:27:02 +01:00
|
|
|
}
|