working on things

This commit is contained in:
2026-04-16 23:20:22 -05:00
parent e2e4ed864e
commit 175f1e6443
3 changed files with 64 additions and 50 deletions
+33 -18
View File
@@ -1,7 +1,7 @@
const std = @import("std");
const mem = std.mem;
const grome = @import("root");
const grome = @import("../root.zig");
const Allocator = mem.Allocator;
const Matrix = grome.matrix.Matrix;
@@ -15,20 +15,27 @@ const Rect = grome.matrix.Rect;
/// approach minimizes unnecessary updates by only diffing changed cells.
pub const DoubleBuffer = struct {
// display holds the existing data.
display: Matrix(Cell),
a: Matrix(Cell),
// write is where we are writing the active changes which will be commited
// on the next rendering
write: Matrix(Cell),
b: Matrix(Cell),
// Front and back hold pointers to a and b, they will switch back and forth
// based on what is needed.
front: *Matrix(Cell),
back: *Matrix(Cell),
pub fn init(gpa: Allocator, size: Rect) !DoubleBuffer {
const display = try gpa.alloc(Cell, size.area());
errdefer gpa.free(display);
var a = try Matrix(Cell).init(gpa, .{ .char = [_]u8{' '} ** 8, .width = 1 }, size);
errdefer a.deinit(gpa);
const write = try gpa.alloc(Cell, size.area());
var b = try Matrix(Cell).init(gpa, .{ .char = [_]u8{' '} ** 8, .width = 1 }, size);
return .{
.display = display,
.write = write,
.a = a,
.b = b,
.front = &a,
.back = &b,
};
}
@@ -37,19 +44,23 @@ pub const DoubleBuffer = struct {
gpa.free(self.write);
}
pub fn frame(m: Matrix, loc: Point, size: Rect) Frame {
return .init(m, loc, size);
pub fn frame(self: *@This(), loc: Point, size: Rect) SubMatrix(Cell) {
return .init(self.back, loc, size);
}
};
/// Frame is a location in the matrix so you can change things if you want
pub const Frame = struct {
data: SubMatrix(Cell),
pub fn render(self: *@This(), w: *std.Io.Writer) !usize {
std.mem.swap(*Matrix(Cell), &self.front, &self.back);
pub fn init(m: Matrix, loc: Point, size: Rect) Frame {
return .{
.data = SubMatrix(Cell).init(m, loc, size),
};
var i = try w.write("\\033[H\\033[2J");
var iter = self.front.row_iterator();
while (iter.next()) |row| {
for (row) |cell| {
i += try cell.render(w);
}
i += try w.write("\n");
}
return i;
}
};
@@ -61,4 +72,8 @@ pub const Cell = struct {
pub fn eql(self: *const Cell, b: *const Cell) bool {
return mem.eql(u8, self.char, b.char) and self.width == b.width;
}
pub fn render(self: *const @This(), w: *std.Io.Writer) !usize {
return w.write(self.char[0..self.width]);
}
};