···3344const log = std.log.scoped(.main);
5566-// Our EventType. This can contain internal events as well as Vaxis events.
77-// Internal events can be posted into the same queue as vaxis events to allow
88-// for a single event loop with exhaustive switching. Booya
96const Event = union(enum) {
107 key_press: vaxis.Key,
118 winsize: vaxis.Winsize,
1212- focus_in,
1313- focus_out,
1414- foo: u8,
159};
16101711pub fn main() !void {
1812 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
1913 defer {
2014 const deinit_status = gpa.deinit();
2121- //fail test; can't try in defer as defer is executed after we return
2215 if (deinit_status == .leak) {
2316 log.err("memory leak", .{});
2417 }
2518 }
2619 const alloc = gpa.allocator();
27202828- // Initialize Vaxis with our event type
2921 var vx = try vaxis.init(Event, .{});
3030- // deinit takes an optional allocator. If your program is exiting, you can
3131- // choose to pass a null allocator to save some exit time.
3222 defer vx.deinit(alloc);
33233434- // Start the read loop. This puts the terminal in raw mode and begins
3535- // reading user input
3624 try vx.startReadThread();
3725 defer vx.stopReadThread();
38263939- // Optionally enter the alternate screen
4027 try vx.enterAltScreen();
41284242- // Sends queries to terminal to detect certain features. This should
4343- // _always_ be called, but is left to the application to decide when
4429 try vx.queryTerminal();
45304646- const img = try vx.loadImage(alloc, .{ .path = "vaxis.png" });
3131+ const imgs = [_]vaxis.Image{
3232+ try vx.loadImage(alloc, .{ .path = "examples/zig.png" }),
3333+ try vx.loadImage(alloc, .{ .path = "examples/vaxis.png" }),
3434+ };
47354836 var n: usize = 0;
49375050- // The main event loop. Vaxis provides a thread safe, blocking, buffered
5151- // queue which can serve as the primary event queue for an application
5252- outer: while (true) {
5353- // nextEvent blocks until an event is in the queue
3838+ while (true) {
5439 const event = vx.nextEvent();
5555- log.debug("event: {}\r\n", .{event});
5656- // exhaustive switching ftw. Vaxis will send events if your EventType
5757- // enum has the fields for those events (ie "key_press", "winsize")
5840 switch (event) {
5941 .key_press => |key| {
6060- n += 1;
6142 if (key.matches('c', .{ .ctrl = true })) {
6262- break :outer;
4343+ return;
6344 } else if (key.matches('l', .{ .ctrl = true })) {
6445 vx.queueRefresh();
6565- } else if (key.matches('n', .{ .ctrl = true })) {
6666- try vx.notify("vaxis", "hello from vaxis");
6767- } else {}
4646+ }
6847 },
6969-7048 .winsize => |ws| try vx.resize(alloc, ws),
7171- else => {},
7249 }
73507474- // vx.window() returns the root window. This window is the size of the
7575- // terminal and can spawn child windows as logical areas. Child windows
7676- // cannot draw outside of their bounds
5151+ n = (n + 1) % imgs.len;
7752 const win = vx.window();
7878-7979- // Clear the entire space because we are drawing in immediate mode.
8080- // vaxis double buffers the screen. This new frame will be compared to
8181- // the old and only updated cells will be drawn
8253 win.clear();
83548484- const child = win.initChild(n, n, .expand, .expand);
8585-8686- img.draw(child, false, 0);
5555+ const img = imgs[n];
5656+ const dims = try img.cellSize(win);
5757+ const center = vaxis.alignment.center(win, dims.cols, dims.rows);
5858+ const scale = false;
5959+ const z_index = 0;
6060+ img.draw(center, scale, z_index);
87618888- // Render the screen
8962 try vx.render();
9063 }
9164}