diff --git a/src/main.zig b/src/main.zig index b3ac966..d98388a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,8 +1,10 @@ const std = @import("std"); -const Io = std.Io; - const grome = @import("grome"); +const Io = std.Io; +const Rect = grome.matrix.Rect; +const DoubleBuffer = grome.draw.DoubleBuffer; + pub fn main(init: std.process.Init) !void { // This is appropriate for anything that lives as long as the process. const arena: std.mem.Allocator = init.arena.allocator(); @@ -13,10 +15,8 @@ pub fn main(init: std.process.Init) !void { const stdout_writer = &stdout_file_writer.interface; // alright lets play with the matrix - const width = 50; - const height = 50; - var matrix = try grome.Matrix(u8, width, height).init(arena, ' '); - var iter = matrix.row_iterator(); + const size = Rect{ .height = 20, .width = 50 }; + var matrix = try DoubleBuffer.init(arena, size); const density = (width * height) / 100; for (0..density) |_| { diff --git a/src/root.zig b/src/root.zig index 0661403..1262543 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,6 +1,6 @@ const std = @import("std"); pub const matrix = @import("grome/matrix.zig"); -pub const Matrix = matrix.Matrix; +pub const draw = @import("view/draw.zig"); test { std.testing.refAllDecls(@This()); diff --git a/src/view/draw.zig b/src/view/draw.zig index 5637b62..5769ad3 100644 --- a/src/view/draw.zig +++ b/src/view/draw.zig @@ -1,37 +1,55 @@ const std = @import("std"); const mem = std.mem; -const Allocator = mem.Allocator; -const Matrix = @import("../grome/matrix.zig").Matrix; +const grome = @import("root"); -/// DoubleBufferWriter manages two buffers for efficient rendering: a display buffer +const Allocator = mem.Allocator; +const Matrix = grome.matrix.Matrix; +const SubMatrix = grome.matrix.SubMatrix; +const Point = grome.matrix.Point; +const Rect = grome.matrix.Rect; + +/// DoubleBuffer manages two buffers for efficient rendering: a display buffer /// showing the current screen state, and a write buffer for accumulating changes /// that will be committed to the display on the next render cycle. This ABO /// approach minimizes unnecessary updates by only diffing changed cells. -pub fn DoubleBufferWriter(width: usize, height: usize) type { - return struct { - // display holds the existing data. - display: Matrix(Cell, width, height), - // write is where we are writing the active changes which will be commited - // on the next rendering - write: Matrix(Cell, width, height), +pub const DoubleBuffer = struct { + // display holds the existing data. + display: Matrix(Cell), + // write is where we are writing the active changes which will be commited + // on the next rendering + write: Matrix(Cell), - pub fn init(gpa: Allocator, len: usize) DoubleBufferWriter { - const display = gpa.alloc(Cell, len); - const write = gpa.alloc(Cell, len); + pub fn init(gpa: Allocator, size: Rect) !DoubleBuffer { + const display = try gpa.alloc(Cell, size.area()); + const write = try gpa.alloc(Cell, size.area()); - return .{ - .display = display, - .write = write, - }; - } + return .{ + .display = display, + .write = write, + }; + } - pub fn deinit(self: *@This(), gpa: Allocator) void { - gpa.free(self.display); - gpa.free(self.write); - } - }; -} + pub fn deinit(self: *@This(), gpa: Allocator) void { + gpa.free(self.display); + gpa.free(self.write); + } + + pub fn frame(m: Matrix, loc: Point, size: Rect) Frame { + return .init(m, 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 init(m: Matrix, loc: Point, size: Rect) Frame { + return .{ + .data = SubMatrix(Cell).init(m, loc, size), + }; + } +}; pub const Cell = struct { char: [8]u8,