mirror of
https://github.com/fairyglade/ly.git
synced 2025-12-20 19:24:53 +00:00
Reduce dependence on tb_cell and tb_cell_buffer()
Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
@@ -9,7 +9,7 @@ const termbox = interop.termbox;
|
||||
const Doom = @This();
|
||||
|
||||
pub const STEPS = 13;
|
||||
pub const FIRE = [_]termbox.tb_cell{
|
||||
pub const FIRE = [_]utils.Cell{
|
||||
utils.initCell(' ', 9, 0),
|
||||
utils.initCell(0x2591, 2, 0), // Red
|
||||
utils.initCell(0x2592, 2, 0), // Red
|
||||
@@ -68,8 +68,8 @@ pub fn draw(self: Doom) void {
|
||||
if (buffer_dest > 12) buffer_dest = 0;
|
||||
self.buffer[dest] = @intCast(buffer_dest);
|
||||
|
||||
self.terminal_buffer.buffer[dest] = FIRE[buffer_dest];
|
||||
self.terminal_buffer.buffer[source] = FIRE[buffer_source];
|
||||
self.terminal_buffer.buffer[dest] = toTermboxCell(FIRE[buffer_dest]);
|
||||
self.terminal_buffer.buffer[source] = toTermboxCell(FIRE[buffer_source]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,3 +82,11 @@ fn initBuffer(buffer: []u8, width: usize) void {
|
||||
@memset(slice_start, 0);
|
||||
@memset(slice_end, STEPS - 1);
|
||||
}
|
||||
|
||||
fn toTermboxCell(cell: utils.Cell) termbox.tb_cell {
|
||||
return .{
|
||||
.ch = cell.ch,
|
||||
.fg = cell.fg,
|
||||
.bg = cell.bg,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -99,8 +99,8 @@ const E = [_]u21{
|
||||
};
|
||||
// zig fmt: on
|
||||
|
||||
pub fn clockCell(animate: bool, char: u8, fg: u16, bg: u16) [SIZE]termbox.tb_cell {
|
||||
var cells: [SIZE]termbox.tb_cell = undefined;
|
||||
pub fn clockCell(animate: bool, char: u8, fg: u16, bg: u16) [SIZE]utils.Cell {
|
||||
var cells: [SIZE]utils.Cell = undefined;
|
||||
|
||||
var tv: interop.system_time.timeval = undefined;
|
||||
_ = interop.system_time.gettimeofday(&tv, null);
|
||||
@@ -111,13 +111,13 @@ pub fn clockCell(animate: bool, char: u8, fg: u16, bg: u16) [SIZE]termbox.tb_cel
|
||||
return cells;
|
||||
}
|
||||
|
||||
pub fn alphaBlit(buffer: [*]termbox.tb_cell, x: usize, y: usize, tb_width: usize, tb_height: usize, cells: [SIZE]termbox.tb_cell) void {
|
||||
pub fn alphaBlit(x: usize, y: usize, tb_width: usize, tb_height: usize, cells: [SIZE]utils.Cell) void {
|
||||
if (x + WIDTH >= tb_width or y + HEIGHT >= tb_height) return;
|
||||
|
||||
for (0..HEIGHT) |yy| {
|
||||
for (0..WIDTH) |xx| {
|
||||
const cell = cells[yy * WIDTH + xx];
|
||||
if (cell.ch != 0) buffer[(y + yy) * tb_width + (x + xx)] = cell;
|
||||
if (cell.ch != 0) utils.putCell(x + xx, y + yy, cell);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -400,7 +400,7 @@ pub fn main() !void {
|
||||
|
||||
for (clock_str, 0..) |c, i| {
|
||||
const clock_cell = bigclock.clockCell(animate, c, buffer.fg, buffer.bg);
|
||||
bigclock.alphaBlit(buffer.buffer, xo + i * (bigclock.WIDTH + 1), yo, buffer.width, buffer.height, clock_cell);
|
||||
bigclock.alphaBlit(xo + i * (bigclock.WIDTH + 1), yo, buffer.width, buffer.height, clock_cell);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -74,27 +74,30 @@ pub fn init(config: Config, labels_max_length: usize, random: Random) TerminalBu
|
||||
}
|
||||
|
||||
pub fn cascade(self: TerminalBuffer) bool {
|
||||
var changes = false;
|
||||
|
||||
var changed = false;
|
||||
var y = self.height - 2;
|
||||
|
||||
while (y > 0) : (y -= 1) {
|
||||
for (0..self.width) |x| {
|
||||
const c: u8 = @truncate(self.buffer[(y - 1) * self.width + x].ch);
|
||||
if (std.ascii.isWhitespace(c)) continue;
|
||||
const cell = self.buffer[(y - 1) * self.width + x];
|
||||
const cell_under = self.buffer[y * self.width + x];
|
||||
|
||||
const c_under: u8 = @truncate(self.buffer[y * self.width + x].ch);
|
||||
if (!std.ascii.isWhitespace(c_under)) continue;
|
||||
const char: u8 = @truncate(cell.ch);
|
||||
if (std.ascii.isWhitespace(char)) continue;
|
||||
|
||||
changes = true;
|
||||
const char_under: u8 = @truncate(cell_under.ch);
|
||||
if (!std.ascii.isWhitespace(char_under)) continue;
|
||||
|
||||
changed = true;
|
||||
|
||||
if ((self.random.int(u16) % 10) > 7) continue;
|
||||
|
||||
self.buffer[y * self.width + x] = self.buffer[(y - 1) * self.width + x];
|
||||
self.buffer[(y - 1) * self.width + x].ch = ' ';
|
||||
_ = termbox.tb_set_cell(@intCast(x), @intCast(y), cell.ch, cell.fg, cell.bg);
|
||||
_ = termbox.tb_set_cell(@intCast(x), @intCast(y - 1), ' ', cell_under.fg, cell_under.bg);
|
||||
}
|
||||
}
|
||||
|
||||
return changes;
|
||||
return changed;
|
||||
}
|
||||
|
||||
pub fn drawBoxCenter(self: *TerminalBuffer, show_borders: bool, blank_box: bool) void {
|
||||
@@ -117,16 +120,16 @@ pub fn drawBoxCenter(self: *TerminalBuffer, show_borders: bool, blank_box: bool)
|
||||
var c2 = utils.initCell(self.box_chars.bottom, self.border_fg, self.bg);
|
||||
|
||||
for (0..self.box_width) |i| {
|
||||
_ = utils.putCell(@intCast(x1 + i), @intCast(y1 - 1), &c1);
|
||||
_ = utils.putCell(@intCast(x1 + i), @intCast(y2), &c2);
|
||||
utils.putCell(x1 + i, y1 - 1, c1);
|
||||
utils.putCell(x1 + i, y2, c2);
|
||||
}
|
||||
|
||||
c1.ch = self.box_chars.left;
|
||||
c2.ch = self.box_chars.right;
|
||||
|
||||
for (0..self.box_height) |i| {
|
||||
_ = utils.putCell(@intCast(x1 - 1), @intCast(y1 + i), &c1);
|
||||
_ = utils.putCell(@intCast(x2), @intCast(y1 + i), &c2);
|
||||
utils.putCell(x1 - 1, y1 + i, c1);
|
||||
utils.putCell(x2, y1 + i, c2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +138,7 @@ pub fn drawBoxCenter(self: *TerminalBuffer, show_borders: bool, blank_box: bool)
|
||||
|
||||
for (0..self.box_height) |y| {
|
||||
for (0..self.box_width) |x| {
|
||||
_ = utils.putCell(@intCast(x1 + x), @intCast(y1 + y), &blank);
|
||||
utils.putCell(x1 + x, y1 + y, blank);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -191,8 +194,6 @@ pub fn drawConfinedLabel(self: TerminalBuffer, text: []const u8, x: usize, y: us
|
||||
}
|
||||
|
||||
pub fn drawCharMultiple(self: TerminalBuffer, char: u8, x: usize, y: usize, length: usize) void {
|
||||
const yc: c_int = @intCast(y);
|
||||
const cell = utils.initCell(char, self.fg, self.bg);
|
||||
|
||||
for (0..length) |xx| _ = utils.putCell(@intCast(x + xx), yc, &cell);
|
||||
for (0..length) |xx| utils.putCell(x + xx, y, cell);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,13 @@ const interop = @import("../interop.zig");
|
||||
|
||||
const termbox = interop.termbox;
|
||||
|
||||
pub fn initCell(ch: u32, fg: u16, bg: u16) termbox.tb_cell {
|
||||
pub const Cell = struct {
|
||||
ch: u32,
|
||||
fg: u16,
|
||||
bg: u16,
|
||||
};
|
||||
|
||||
pub fn initCell(ch: u32, fg: u16, bg: u16) Cell {
|
||||
return .{
|
||||
.ch = ch,
|
||||
.fg = fg,
|
||||
@@ -11,8 +17,8 @@ pub fn initCell(ch: u32, fg: u16, bg: u16) termbox.tb_cell {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn putCell(x: i32, y: i32, cell: *const termbox.tb_cell) c_int {
|
||||
return termbox.tb_set_cell(x, y, cell.ch, cell.fg, cell.bg);
|
||||
pub fn putCell(x: usize, y: usize, cell: Cell) void {
|
||||
_ = termbox.tb_set_cell(@intCast(x), @intCast(y), cell.ch, cell.fg, cell.bg);
|
||||
}
|
||||
|
||||
// Every codepoint is assumed to have a width of 1.
|
||||
|
||||
Reference in New Issue
Block a user