From 34cb9b2c27b6c0bfbbcde1bbe947341d0888f906 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 18 Apr 2022 16:21:22 -0700 Subject: [PATCH] setup our GPU cells based on terminal state --- src/Grid.zig | 25 +++++++++++++++++++++++-- src/Window.zig | 6 +++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Grid.zig b/src/Grid.zig index 519ba7460..726ae6868 100644 --- a/src/Grid.zig +++ b/src/Grid.zig @@ -168,8 +168,29 @@ pub fn demoCells(self: *Grid) !void { /// updateCells updates our GPU cells from the current terminal view. /// The updated cells will take effect on the next render. pub fn updateCells(self: *Grid, term: Terminal) !void { - _ = self; - _ = term; + // For now, we just ensure that we have enough cells for all the lines + // we have plus a full width. This is very likely too much but its + // the probably close enough while guaranteeing no more allocations. + self.cells.clearRetainingCapacity(); + try self.cells.ensureTotalCapacity( + self.alloc, + term.screen.items.len * term.cols, + ); + + for (term.screen.items) |line, y| { + for (line.items) |cell, x| { + _ = cell; + + self.cells.appendAssumeCapacity(.{ + .grid_col = @intCast(u16, x), + .grid_row = @intCast(u16, y), + .bg_r = @intCast(u8, @mod(x * y, 255)), + .bg_g = @intCast(u8, @mod(x, 255)), + .bg_b = @intCast(u8, 255 - @mod(x, 255)), + .bg_a = 255, + }); + } + } } /// Set the screen size for rendering. This will update the projection diff --git a/src/Window.zig b/src/Window.zig index 4947982a7..66a328cdd 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -141,12 +141,12 @@ fn sizeCallback(window: glfw.Window, width: i32, height: i32) void { .height = @intCast(u32, height), }) catch |err| log.err("error updating grid screen size err={}", .{err}); - // TODO: temp - win.grid.demoCells() catch unreachable; - // Update the size of our terminal state win.terminal.resize(win.grid.size.columns, win.grid.size.rows); + // TODO: this is not the right place for this + win.grid.updateCells(win.terminal) catch unreachable; + // Update the size of our pty win.pty.setSize(.{ .ws_row = @intCast(u16, win.grid.size.rows),