2024-01-19 06:17:57 +01:00
|
|
|
const std = @import("std");
|
2024-01-19 17:21:49 +01:00
|
|
|
const assert = std.debug.assert;
|
2024-01-19 06:17:57 +01:00
|
|
|
|
|
|
|
const Cell = @import("cell.zig").Cell;
|
|
|
|
|
2024-01-19 19:21:14 +01:00
|
|
|
const log = std.log.scoped(.screen);
|
|
|
|
|
2024-01-19 06:17:57 +01:00
|
|
|
const Screen = @This();
|
|
|
|
|
2024-01-19 20:13:20 +01:00
|
|
|
width: usize = 0,
|
|
|
|
height: usize = 0,
|
2024-01-19 06:17:57 +01:00
|
|
|
|
|
|
|
buf: []Cell = undefined,
|
|
|
|
|
2024-01-19 20:13:20 +01:00
|
|
|
/// sets each cell to the default cell
|
|
|
|
pub fn init(self: *Screen) void {
|
|
|
|
for (self.buf, 0..) |_, i| {
|
|
|
|
self.buf[i] = .{};
|
|
|
|
}
|
2024-01-19 06:17:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deinit(self: *Screen, alloc: std.mem.Allocator) void {
|
|
|
|
alloc.free(self.buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resize(self: *Screen, alloc: std.mem.Allocator, w: usize, h: usize) !void {
|
|
|
|
alloc.free(self.buf);
|
|
|
|
self.buf = try alloc.alloc(Cell, w * h);
|
|
|
|
self.width = w;
|
|
|
|
self.height = h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// writes a cell to a location. 0 indexed
|
2024-01-19 19:21:14 +01:00
|
|
|
pub fn writeCell(self: *Screen, col: usize, row: usize, cell: Cell) void {
|
2024-01-19 06:17:57 +01:00
|
|
|
if (self.width < col) {
|
|
|
|
// column out of bounds
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (self.height < row) {
|
|
|
|
// height out of bounds
|
|
|
|
return;
|
|
|
|
}
|
2024-01-19 19:21:14 +01:00
|
|
|
const i = (row * self.width) + col;
|
2024-01-19 17:21:49 +01:00
|
|
|
assert(i < self.buf.len);
|
2024-01-19 06:17:57 +01:00
|
|
|
self.buf[i] = cell;
|
|
|
|
}
|