2024-01-30 20:30:36 +01:00
|
|
|
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();
|
|
|
|
|
2024-04-30 16:02:16 +02:00
|
|
|
var vx = try vaxis.init(alloc, .{});
|
2024-01-30 20:30:36 +01:00
|
|
|
defer vx.deinit(alloc);
|
|
|
|
|
2024-04-30 16:02:16 +02:00
|
|
|
var loop: vaxis.Loop(Event) = .{ .vaxis = &vx };
|
|
|
|
|
2024-05-22 21:06:26 +02:00
|
|
|
try loop.run();
|
2024-04-30 16:02:16 +02:00
|
|
|
defer loop.stop();
|
2024-01-30 20:30:36 +01:00
|
|
|
|
|
|
|
try vx.enterAltScreen();
|
|
|
|
|
|
|
|
try vx.queryTerminal();
|
|
|
|
|
2024-01-31 03:51:54 +01:00
|
|
|
const imgs = [_]vaxis.Image{
|
|
|
|
try vx.loadImage(alloc, .{ .path = "examples/zig.png" }),
|
|
|
|
try vx.loadImage(alloc, .{ .path = "examples/vaxis.png" }),
|
|
|
|
};
|
2024-01-31 14:18:16 +01:00
|
|
|
defer vx.freeImage(imgs[0].id);
|
|
|
|
defer vx.freeImage(imgs[1].id);
|
2024-01-30 23:26:35 +01:00
|
|
|
|
|
|
|
var n: usize = 0;
|
2024-01-30 20:30:36 +01:00
|
|
|
|
2024-05-21 18:08:23 +02:00
|
|
|
var clip_y: usize = 0;
|
|
|
|
|
2024-01-31 03:51:54 +01:00
|
|
|
while (true) {
|
2024-04-30 16:02:16 +02:00
|
|
|
const event = loop.nextEvent();
|
2024-01-30 20:30:36 +01:00
|
|
|
switch (event) {
|
|
|
|
.key_press => |key| {
|
|
|
|
if (key.matches('c', .{ .ctrl = true })) {
|
2024-01-31 03:51:54 +01:00
|
|
|
return;
|
2024-01-30 20:30:36 +01:00
|
|
|
} else if (key.matches('l', .{ .ctrl = true })) {
|
|
|
|
vx.queueRefresh();
|
2024-05-21 18:08:23 +02:00
|
|
|
} else if (key.matches('j', .{}))
|
|
|
|
clip_y += 1
|
|
|
|
else if (key.matches('k', .{}))
|
|
|
|
clip_y -|= 1;
|
2024-01-30 20:30:36 +01:00
|
|
|
},
|
|
|
|
.winsize => |ws| try vx.resize(alloc, ws),
|
|
|
|
}
|
|
|
|
|
2024-01-31 03:51:54 +01:00
|
|
|
n = (n + 1) % imgs.len;
|
2024-01-30 20:30:36 +01:00
|
|
|
const win = vx.window();
|
|
|
|
win.clear();
|
|
|
|
|
2024-01-31 03:51:54 +01:00
|
|
|
const img = imgs[n];
|
|
|
|
const dims = try img.cellSize(win);
|
2024-02-11 19:59:33 +01:00
|
|
|
const center = vaxis.widgets.alignment.center(win, dims.cols, dims.rows);
|
2024-05-21 18:08:23 +02:00
|
|
|
try img.draw(center, .{ .scale = .contain, .clip_region = .{
|
|
|
|
.y = clip_y,
|
|
|
|
} });
|
2024-01-30 20:30:36 +01:00
|
|
|
|
|
|
|
try vx.render();
|
|
|
|
}
|
|
|
|
}
|