Created a block type

This commit is contained in:
2026-04-17 22:45:17 -05:00
parent 6589ac9c58
commit 1de39a5824
7 changed files with 31 additions and 18 deletions
+11
View File
@@ -0,0 +1,11 @@
const container = @import("root.zig");
const Point = container.Point;
const Rect = container.Rect;
/// Block is a location and size, or a definition of the bounds of a
/// submatrix.
pub const Block = struct {
loc: Point,
size: Rect,
};
+3 -2
View File
@@ -4,6 +4,7 @@ const container = @import("root.zig");
const Allocator = mem.Allocator;
const Rect = container.Rect;
const Block = container.Block;
const Rowable = container.Rowable;
const Point = container.Point;
const SubMatrix = container.SubMatrix;
@@ -68,8 +69,8 @@ pub fn Matrix(comptime T: type) type {
}
/// submatrix returns a [SubMatrix]
pub fn submatrix(self: *@This(), loc: Point, area: Rect) SubMatrix(T) {
return SubMatrix(T).init(self, loc, area);
pub fn submatrix(self: *@This(), mask: Block) SubMatrix(T) {
return SubMatrix(T).init(self, mask);
}
// TODO: you were in the middle of debugging this
+1
View File
@@ -1,5 +1,6 @@
pub const Point = @import("point.zig").Point;
pub const Rect = @import("rect.zig").Rect;
pub const Block = @import("block.zig").Block;
pub const Rowable = @import("rowable.zig").Rowable;
pub const Matrix = @import("matrix.zig").Matrix;
pub const SubMatrix = @import("sub_matrix.zig").SubMatrix;
+1 -1
View File
@@ -44,7 +44,7 @@ test "TestRowIterator" {
}
try testing.expectEqual(10, x);
var sub = m.submatrix(.{ .x = 5, .y = 5 }, .{ .width = 5, .height = 5 });
var sub = m.submatrix(.{ .loc = .{ .x = 5, .y = 5 }, .size = .{ .width = 5, .height = 5 } });
var sub_iter = sub.row_iterator();
x = 0;
while (sub_iter.next()) |row| {
+11 -12
View File
@@ -2,9 +2,9 @@ const std = @import("std");
const mem = std.mem;
const container = @import("root.zig");
const Rect = container.Rect;
const Rowable = container.Rowable;
const Block = container.Block;
const Point = container.Point;
const Rowable = container.Rowable;
const Matrix = container.Matrix;
const Error = container.Error;
const RowIterator = container.RowIterator;
@@ -14,15 +14,14 @@ pub fn SubMatrix(comptime T: type) type {
return struct {
// Data is made up of different sections of the
_m: *Matrix(T),
_offset: Point,
_mask: Rect,
_mask: Block,
//_width: usize,
rowable: Rowable(T),
/// init takes the underlying data from the upstream matrix and creates
/// a submatrix of it
pub fn init(m: *Matrix(T), loc: Point, size: Rect) @This() {
return .{ ._m = m, ._offset = loc, ._mask = size, .rowable = Rowable(T){ .rowFn = SubMatrix(T).row } };
pub fn init(m: *Matrix(T), mask: Block) @This() {
return .{ ._m = m, ._mask = mask, .rowable = Rowable(T){ .rowFn = SubMatrix(T).row } };
}
pub fn get(self: *const @This(), loc: Point) !T {
@@ -30,11 +29,11 @@ pub fn SubMatrix(comptime T: type) type {
}
pub fn getPtr(self: *const @This(), loc: Point) !*T {
if (loc.x >= self._mask.width or loc.y >= self._mask.height) {
if (loc.x >= self._mask.size.width or loc.y >= self._mask.size.height) {
return Error.OutOfBounds;
}
return self._m.getPtr(self._offset.add(loc));
return self._m.getPtr(self._mask.loc.add(loc));
}
pub fn set(self: *@This(), loc: Point, val: T) !void {
@@ -43,14 +42,14 @@ pub fn SubMatrix(comptime T: type) type {
pub fn row(interface: *Rowable(T), line: usize) ![]T {
var self: *@This() = @fieldParentPtr("rowable", interface);
if (line >= self._mask.height) {
if (line >= self._mask.size.height) {
return Error.OutOfBounds;
}
// TODO: This can index out of bound
// we need to checkt to make sure everything is copesedic
var r = try self._m.rowable.row(line + self._offset.y);
return r[self._offset.x .. self._offset.x + self._mask.width];
var r = try self._m.rowable.row(line + self._mask.loc.y);
return r[self._mask.loc.x .. self._mask.loc.x + self._mask.size.width];
}
pub fn row_iterator(self: *@This()) RowIterator(T) {
@@ -65,7 +64,7 @@ test "SubMatrix" {
// create our matrix and submatrix
var m = try Matrix(u8).initZero(testing.allocator, .{ .height = 10, .width = 10 });
defer m.deinit(testing.allocator);
var sub = m.submatrix(.{ .x = 4, .y = 4 }, .{ .height = 5, .width = 5 });
var sub = m.submatrix(.{ .loc = .{ .x = 4, .y = 4 }, .size = .{ .height = 5, .width = 5 } });
try sub.set(.{ .x = 0, .y = 0 }, 'a');
// we should see the value updated in the Matrix
+1 -1
View File
@@ -23,7 +23,7 @@ pub fn main(init: std.process.Init) !void {
try double_buf.hard_refresh(w);
while (true) {
var frame = double_buf.frame(.{ .x = 0, .y = 10 }, .{ .height = 1, .width = 120 });
var frame = double_buf.frame(.{ .loc = .{ .x = 0, .y = 10 }, .size = .{ .height = 1, .width = 120 } });
const row = try frame.rowable.row(0);
var fill_next: bool = false;
var unfilled: bool = true;
+3 -2
View File
@@ -7,6 +7,7 @@ const Allocator = mem.Allocator;
const Matrix = grome.container.Matrix;
const SubMatrix = grome.container.SubMatrix;
const Point = grome.container.Point;
const Block = grome.container.Block;
const Rect = grome.container.Rect;
const Cell = grome.view.Cell;
@@ -40,8 +41,8 @@ pub const DoubleBuffer = struct {
// frame returns a subset of the backbuffer where `loc` is the top left point of the
// subsection, and `size` is the area or size of the subset.
pub fn frame(self: *@This(), loc: Point, size: Rect) SubMatrix(Cell) {
return .init(&self.back, loc, size);
pub fn frame(self: *@This(), mask: Block) SubMatrix(Cell) {
return .init(&self.back, mask);
}
// swap the front and back. This should be called right before rendering