ona/demos/effects.zig

93 lines
2.3 KiB
Zig
Raw Normal View History

const coral = @import("coral");
const ona = @import("ona");
const std = @import("std");
const CRT = extern struct {
width: f32,
height: f32,
time: f32,
padding: [4]u8 = undefined,
};
const Effects = struct {
render_texture: coral.Texture = .default,
crt_effect: coral.Effect = .default,
};
pub const main = ona.start(setup, .{
.tick_rate = 60,
.execution = .{.thread_share = 0.1},
.middleware = &.{coral.setup},
});
fn load(display: ona.Write(coral.Display), actors: ona.Write(Effects), assets: ona.Write(coral.Assets)) !void {
display.state.width, display.state.height = .{1280, 720};
actors.state.render_texture = try assets.state.load_texture(.{
.format = .rgba8,
.access = .{
.render = .{
.width = display.state.width,
.height = display.state.height,
},
},
});
actors.state.crt_effect = try assets.state.load_effect_file(ona.files.bundle, "./crt.frag.spv");
}
fn render(commands: coral.Commands, effects: ona.Write(Effects), app: ona.Read(ona.App), display: ona.Write(coral.Display)) !void {
try commands.set_target(.{
.texture = effects.state.render_texture,
.clear_color = coral.colors.black,
.clear_depth = 0,
.clear_stencil = 0,
});
const display_width: f32 = @floatFromInt(display.state.width);
const display_height: f32 = @floatFromInt(display.state.height);
const display_transform = coral.Transform2D{
.origin = .{display_width / 2, display_height / 2},
.xbasis = .{display_width, 0},
.ybasis = .{0, display_height},
};
try commands.draw_texture(.{
.texture = .default,
.transform = display_transform,
});
try commands.set_effect(.{
.effect = effects.state.crt_effect,
.properties = std.mem.asBytes(&CRT{
.width = display_width,
.height = display_height,
.time = @floatCast(app.state.elapsed_time),
}),
});
try commands.set_target(.{
.texture = .backbuffer,
.clear_color = null,
.clear_depth = null,
.clear_stencil = null,
});
try commands.draw_texture(.{
.texture = effects.state.render_texture,
.transform = display_transform,
});
}
fn setup(world: *ona.World, events: ona.App.Events) !void {
try world.set_state(Effects{});
try world.on_event(events.load, ona.system_fn(load), .{.label = "load"});
try world.on_event(events.render, ona.system_fn(render), .{.label = "render actors"});
}