2024-07-24 23:09:27 +02:00
|
|
|
const gfx = @import("gfx");
|
2024-07-24 01:25:18 +02:00
|
|
|
|
|
|
|
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 {
|
2024-07-24 23:09:27 +02:00
|
|
|
render_texture: gfx.Texture = .default,
|
2024-07-27 02:44:06 +02:00
|
|
|
image_textures: [2]gfx.Texture = [_]gfx.Texture{.default} ** 2,
|
|
|
|
last_time: f64 = 0,
|
|
|
|
image_index: usize = 0,
|
2024-07-24 23:09:27 +02:00
|
|
|
crt_effect: gfx.Effect = .default,
|
2024-07-24 01:25:18 +02:00
|
|
|
};
|
|
|
|
|
2024-07-25 02:11:58 +02:00
|
|
|
fn load(display: ona.Write(gfx.Display), effects: ona.Write(Effects), assets: ona.Write(gfx.Assets)) !void {
|
2024-07-24 01:25:18 +02:00
|
|
|
display.state.width, display.state.height = .{1280, 720};
|
|
|
|
|
2024-07-25 02:11:58 +02:00
|
|
|
effects.state.render_texture = try assets.state.load_texture(.{
|
2024-07-24 01:25:18 +02:00
|
|
|
.format = .rgba8,
|
|
|
|
|
|
|
|
.access = .{
|
|
|
|
.render = .{
|
|
|
|
.width = display.state.width,
|
|
|
|
.height = display.state.height,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-07-25 02:11:58 +02:00
|
|
|
effects.state.crt_effect = try assets.state.load_effect_file(ona.files.bundle, "./crt.frag.spv");
|
2024-07-27 02:44:06 +02:00
|
|
|
|
|
|
|
var descs = gfx.Descs.init(ona.heap.allocator);
|
|
|
|
|
|
|
|
defer {
|
|
|
|
descs.deinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
effects.state.image_textures = .{
|
|
|
|
try assets.state.load_texture(try descs.checker_texture(.{
|
|
|
|
.colors = .{gfx.colors.black, gfx.colors.purple},
|
|
|
|
.width = 8,
|
|
|
|
.height = 8,
|
|
|
|
})),
|
|
|
|
|
|
|
|
try assets.state.load_texture(try descs.checker_texture(.{
|
|
|
|
.colors = .{gfx.colors.black, gfx.colors.grey},
|
|
|
|
.width = 8,
|
|
|
|
.height = 8,
|
|
|
|
}))
|
|
|
|
};
|
2024-07-24 01:25:18 +02:00
|
|
|
}
|
|
|
|
|
2024-07-30 00:50:40 +02:00
|
|
|
pub const main = ona.App.game
|
|
|
|
.with_module(gfx)
|
|
|
|
.with_state(Effects{})
|
|
|
|
.with_system(.load, ona.system_fn(load), .{.label = "load effects"})
|
|
|
|
.with_system(.update, ona.system_fn(update), .{.label = "update effects"})
|
|
|
|
.with_system(.render, ona.system_fn(render), .{.label = "render effects"}).build();
|
|
|
|
|
|
|
|
fn update(effects: ona.Write(Effects), loop: ona.Read(ona.Loop)) void {
|
2024-07-27 02:44:06 +02:00
|
|
|
const update_seconds = 5;
|
|
|
|
|
2024-07-30 00:50:40 +02:00
|
|
|
if ((loop.state.elapsed_time - effects.state.last_time) > update_seconds) {
|
2024-07-27 02:44:06 +02:00
|
|
|
effects.state.image_index = (effects.state.image_index + 1) % effects.state.image_textures.len;
|
2024-07-30 00:50:40 +02:00
|
|
|
effects.state.last_time = loop.state.elapsed_time;
|
2024-07-27 02:44:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-15 00:52:27 +02:00
|
|
|
fn render(commands: gfx.Commands, effects: ona.Write(Effects), loop: ona.Read(ona.Loop), display: ona.Write(gfx.Display)) !void {
|
2024-07-24 01:25:18 +02:00
|
|
|
try commands.set_target(.{
|
|
|
|
.texture = effects.state.render_texture,
|
2024-07-24 23:09:27 +02:00
|
|
|
.clear_color = gfx.colors.black,
|
2024-07-24 01:25:18 +02:00
|
|
|
.clear_depth = 0,
|
|
|
|
.clear_stencil = 0,
|
|
|
|
});
|
|
|
|
|
2024-07-30 00:50:40 +02:00
|
|
|
const width: f32 = @floatFromInt(display.state.width);
|
|
|
|
const height: f32 = @floatFromInt(display.state.height);
|
2024-07-24 01:25:18 +02:00
|
|
|
|
|
|
|
try commands.draw_texture(.{
|
2024-07-27 02:44:06 +02:00
|
|
|
.texture = effects.state.image_textures[effects.state.image_index],
|
2024-07-30 00:50:40 +02:00
|
|
|
.size = .{width, height},
|
2024-07-24 01:25:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
try commands.set_effect(.{
|
|
|
|
.effect = effects.state.crt_effect,
|
|
|
|
|
|
|
|
.properties = std.mem.asBytes(&CRT{
|
2024-07-30 00:50:40 +02:00
|
|
|
.width = width,
|
|
|
|
.height = height,
|
|
|
|
.time = @floatCast(loop.state.elapsed_time),
|
2024-07-24 01:25:18 +02:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
try commands.set_target(.{
|
|
|
|
.clear_color = null,
|
|
|
|
.clear_depth = null,
|
|
|
|
.clear_stencil = null,
|
|
|
|
});
|
|
|
|
|
|
|
|
try commands.draw_texture(.{
|
|
|
|
.texture = effects.state.render_texture,
|
2024-07-30 00:50:40 +02:00
|
|
|
.size = .{width, height},
|
2024-07-24 01:25:18 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|