libvaxis/examples/image.zig

77 lines
2.1 KiB
Zig
Raw Normal View History

const std = @import("std");
const vaxis = @import("vaxis");
const log = std.log.scoped(.main);
const Event = union(enum) {
key_press: vaxis.Key,
winsize: vaxis.Winsize,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const deinit_status = gpa.deinit();
if (deinit_status == .leak) {
log.err("memory leak", .{});
}
}
const alloc = gpa.allocator();
var tty = try vaxis.Tty.init();
defer tty.deinit();
2024-04-30 16:02:16 +02:00
var vx = try vaxis.init(alloc, .{});
defer vx.deinit(alloc, tty.anyWriter());
var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx };
try loop.init();
2024-04-30 16:02:16 +02:00
try loop.start();
2024-04-30 16:02:16 +02:00
defer loop.stop();
try vx.enterAltScreen(tty.anyWriter());
try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s);
const imgs = [_]vaxis.Image{
try vx.loadImage(alloc, tty.anyWriter(), .{ .path = "examples/zig.png" }),
try vx.loadImage(alloc, tty.anyWriter(), .{ .path = "examples/vaxis.png" }),
};
defer vx.freeImage(tty.anyWriter(), imgs[0].id);
defer vx.freeImage(tty.anyWriter(), imgs[1].id);
var n: usize = 0;
var clip_y: usize = 0;
while (true) {
2024-04-30 16:02:16 +02:00
const event = loop.nextEvent();
switch (event) {
.key_press => |key| {
if (key.matches('c', .{ .ctrl = true })) {
return;
} else if (key.matches('l', .{ .ctrl = true })) {
vx.queueRefresh();
} else if (key.matches('j', .{}))
clip_y += 1
else if (key.matches('k', .{}))
clip_y -|= 1;
},
.winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws),
}
n = (n + 1) % imgs.len;
const win = vx.window();
win.clear();
const img = imgs[n];
const dims = try img.cellSize(win);
const center = vaxis.widgets.alignment.center(win, dims.cols, dims.rows);
try img.draw(center, .{ .scale = .contain, .clip_region = .{
.y = clip_y,
} });
try vx.render(tty.anyWriter());
}
}