ona/src/demos/graphics.zig

99 lines
2.6 KiB
Zig
Raw Normal View History

const ona = @import("ona");
const std = @import("std");
const CRT = extern struct {
width: f32,
height: f32,
time: f32,
padding: [4]u8 = undefined,
};
2025-09-18 19:07:29 +01:00
const State = struct {
images: [2]ona.gfx.Images.Handle = [_]ona.gfx.Images.Handle{.default} ** 2,
last_time: f64 = 0,
image_index: usize = 0,
2025-09-18 19:07:29 +01:00
crt_effect: ona.gfx.Effects.Handle = .default,
loaded_image: ona.gfx.Images.Handle = .default,
};
2025-09-18 19:07:29 +01:00
fn load(display: ona.Write(ona.gfx.Display), state: ona.Write(State), images: ona.gfx.Images, effects: ona.gfx.Effects) !void {
display.ptr.size = .{ 1280, 720 };
state.ptr.loaded_image = try images.load(ona.gfx.QoiImage{
.path = try .parse("graphics_image.qoi"),
});
errdefer {
images.unload(state.ptr.images[1]);
}
state.ptr.crt_effect = try effects.load(ona.gfx.GlslEffect{
.fragment = .{
.glsl = @embedFile("crt.frag"),
.name = "crt.frag",
},
});
errdefer {
effects.unload(state.ptr.crt_effect);
}
state.ptr.images[0] = try images.load(ona.gfx.CheckerImage{
.colors = .{ .black, .purple },
.square_size = 4,
.width = 8,
.height = 8,
});
errdefer {
images.unload(state.ptr.images[0]);
}
state.ptr.images[1] = try images.load(ona.gfx.CheckerImage{
.colors = .{ .black, .grey },
.square_size = 4,
.width = 8,
.height = 8,
});
errdefer {
images.unload(state.ptr.images[1]);
}
}
pub fn main() void {
ona.realtime_app
2025-08-05 18:07:09 +01:00
.with(.initModule(ona.hid))
.with(.initModule(ona.gfx))
2025-09-18 19:07:29 +01:00
.with(.initState(State{}))
.with(.initSystem(.load, .of(load)))
.with(.initSystem(.render, .of(render)))
.with(.initSystem(.update, .of(update)))
.run();
}
2025-08-08 13:52:45 +01:00
2025-09-18 19:07:29 +01:00
fn render(scene: ona.gfx.Scene, state: ona.Read(State), display: ona.Write(ona.gfx.Display), time: ona.Read(ona.App.Time)) void {
const width, const height = display.ptr.size;
scene.updateEffect(state.ptr.crt_effect, CRT{
.width = @floatFromInt(width),
.height = @floatFromInt(height),
.time = @floatCast(time.ptr.elapsed),
});
scene.drawSprite(state.ptr.images[state.ptr.image_index], .{
.size = .{ @floatFromInt(width), @floatFromInt(height) },
.effect = state.ptr.crt_effect,
});
}
fn update(state: ona.Write(State), time: ona.Read(ona.App.Time)) void {
const update_seconds = 5;
if ((time.ptr.elapsed - state.ptr.last_time) > update_seconds) {
state.ptr.image_index = (state.ptr.image_index + 1) % state.ptr.images.len;
state.ptr.last_time = time.ptr.elapsed;
}
}