From 9c79137c9fe64330b6b9d74af293c48cdbf293b0 Mon Sep 17 00:00:00 2001 From: AnErrupTion Date: Thu, 6 Mar 2025 15:42:33 +0100 Subject: [PATCH] Remove all deprecated calls to tb_cell_buffer() Signed-off-by: AnErrupTion --- include/termbox2.h | 32 ++++++++++++++++++++++++-------- src/main.zig | 3 +-- src/tui/TerminalBuffer.zig | 9 +++++---- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/include/termbox2.h b/include/termbox2.h index 406fbac..297d0be 100644 --- a/include/termbox2.h +++ b/include/termbox2.h @@ -506,6 +506,21 @@ int tb_set_cell_ex(int x, int y, uint32_t *ch, size_t nch, uintattr_t fg, uintattr_t bg); int tb_extend_cell(int x, int y, uint32_t ch); +/* Get cell at specified position. + * + * If position is valid, function returns TB_OK and cell contents are copied to + * `cell`. Note if `nech>0`, then `ech` will be a pointer to memory which may + * be invalid or freed after subsequent library calls. Callers must copy this + * memory if they need to persist it for some reason. Modifying memory at `ech` + * results in undefined behavior. + * + * If `back` is non-zero, return cells from the internal back buffer. Otherwise, + * return cells from the front buffer. Note the front buffer is updated on each + * call to tb_present(), whereas the back buffer is updated immediately by + * tb_set_cell() and other functions that mutate cell contents. + */ +int tb_get_cell(int x, int y, int back, struct tb_cell *cell); + /* Set the input mode. Termbox has two input modes: * * 1. `TB_INPUT_ESC` @@ -1771,6 +1786,15 @@ int tb_set_cell_ex(int x, int y, uint32_t *ch, size_t nch, uintattr_t fg, return TB_OK; } +int tb_get_cell(int x, int y, int back, struct tb_cell *cell) { + if_not_init_return(); + int rv; + struct tb_cell *cellp = NULL; + rv = cellbuf_get(back ? &global.back : &global.front, x, y, &cellp); + if (cellp) memcpy(cell, cellp, sizeof(*cell)); + return rv; +} + int tb_extend_cell(int x, int y, uint32_t ch) { if_not_init_return(); #ifdef TB_OPT_EGC @@ -3271,14 +3295,6 @@ static int send_cluster(int x, int y, uint32_t *ch, size_t nch) { ch32 = 0xfffd; // replace non-printable codepoints with U+FFFD } int chu8_len = tb_utf8_unicode_to_char(chu8, ch32); - /* - if (ch32 == 0) { // replace null with space (from termbox 19dbee5) - chu8_len = 1; - chu8[0] = ' '; - } else { - chu8_len = tb_utf8_unicode_to_char(chu8, ch32); - } - */ if_err_return(rv, bytebuf_nputs(&global.out, chu8, (size_t)chu8_len)); } diff --git a/src/main.zig b/src/main.zig index 0185e62..39f1bfd 100644 --- a/src/main.zig +++ b/src/main.zig @@ -378,7 +378,7 @@ pub fn main() !void { if (!update or config.animation != .none) { if (!update) std.time.sleep(std.time.ns_per_ms * 100); - _ = termbox.tb_present(); // Required to update tb_width(), tb_height() and tb_cell_buffer() + _ = termbox.tb_present(); // Required to update tb_width() and tb_height() const width: usize = @intCast(termbox.tb_width()); const height: usize = @intCast(termbox.tb_height()); @@ -388,7 +388,6 @@ pub fn main() !void { buffer.width = width; buffer.height = height; - buffer.buffer = termbox.tb_cell_buffer(); switch (config.animation) { .none => {}, diff --git a/src/tui/TerminalBuffer.zig b/src/tui/TerminalBuffer.zig index 1fc5d5b..8281814 100644 --- a/src/tui/TerminalBuffer.zig +++ b/src/tui/TerminalBuffer.zig @@ -21,7 +21,6 @@ pub const InitOptions = struct { random: Random, width: usize, height: usize, -buffer: [*]termbox.tb_cell, fg: u32, bg: u32, border_fg: u32, @@ -48,7 +47,6 @@ pub fn init(options: InitOptions, labels_max_length: usize, random: Random) Term .random = random, .width = @intCast(termbox.tb_width()), .height = @intCast(termbox.tb_height()), - .buffer = termbox.tb_cell_buffer(), .fg = options.fg, .bg = options.bg, .border_fg = options.border_fg, @@ -87,8 +85,11 @@ pub fn cascade(self: TerminalBuffer) bool { while (y > 0) : (y -= 1) { for (0..self.width) |x| { - const cell = self.buffer[(y - 1) * self.width + x]; - const cell_under = self.buffer[y * self.width + x]; + var cell: termbox.tb_cell = undefined; + var cell_under: termbox.tb_cell = undefined; + + _ = termbox.tb_get_cell(@intCast(x), @intCast(y - 1), 1, &cell); + _ = termbox.tb_get_cell(@intCast(x), @intCast(y), 1, &cell_under); const char: u8 = @truncate(cell.ch); if (std.ascii.isWhitespace(char)) continue;