Clean termbox2 usage + fix animation bug

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion 2025-03-16 11:40:27 +01:00
parent 1672d4a9ec
commit 13ba52319c
No known key found for this signature in database
6 changed files with 77 additions and 40 deletions

View File

@ -22,19 +22,19 @@ pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, top_color: u
.terminal_buffer = terminal_buffer,
.buffer = buffer,
.fire = [_]Cell{
Cell.init(' ', 0x00000000, 0),
Cell.init(0x2591, top_color, 0),
Cell.init(0x2592, top_color, 0),
Cell.init(0x2593, top_color, 0),
Cell.init(0x2588, top_color, 0),
Cell.init(0x2591, middle_color, 2),
Cell.init(0x2592, middle_color, 2),
Cell.init(0x2593, middle_color, 2),
Cell.init(0x2588, middle_color, 2),
Cell.init(0x2591, bottom_color, 4),
Cell.init(0x2592, bottom_color, 4),
Cell.init(0x2593, bottom_color, 4),
Cell.init(0x2588, bottom_color, 4),
Cell.init(' ', TerminalBuffer.Color.DEFAULT, TerminalBuffer.Color.DEFAULT),
Cell.init(0x2591, top_color, TerminalBuffer.Color.DEFAULT),
Cell.init(0x2592, top_color, TerminalBuffer.Color.DEFAULT),
Cell.init(0x2593, top_color, TerminalBuffer.Color.DEFAULT),
Cell.init(0x2588, top_color, TerminalBuffer.Color.DEFAULT),
Cell.init(0x2591, middle_color, top_color),
Cell.init(0x2592, middle_color, top_color),
Cell.init(0x2593, middle_color, top_color),
Cell.init(0x2588, middle_color, top_color),
Cell.init(0x2591, bottom_color, middle_color),
Cell.init(0x2592, bottom_color, middle_color),
Cell.init(0x2593, bottom_color, middle_color),
Cell.init(0x2588, bottom_color, middle_color),
},
};
}

14
src/animations/Dummy.zig Normal file
View File

@ -0,0 +1,14 @@
const std = @import("std");
const Animation = @import("../tui/Animation.zig");
const Dummy = @This();
pub fn animation(self: *Dummy) Animation {
return Animation.init(self, deinit, realloc, draw);
}
fn deinit(_: *Dummy) void {}
fn realloc(_: *Dummy) anyerror!void {}
fn draw(_: *Dummy) void {}

View File

@ -1,19 +1,17 @@
const std = @import("std");
const interop = @import("../interop.zig");
const Animation = @import("../tui/Animation.zig");
const Cell = @import("../tui/Cell.zig");
const TerminalBuffer = @import("../tui/TerminalBuffer.zig");
const Allocator = std.mem.Allocator;
const Random = std.Random;
const termbox = interop.termbox;
pub const FRAME_DELAY: usize = 8;
// Characters change mid-scroll
pub const MID_SCROLL_CHANGE = true;
const DOT_HEAD_COLOR: u32 = @intCast(0x00FFFFFF | termbox.TB_BOLD); // White and bold
const DOT_HEAD_COLOR: u32 = @intCast(TerminalBuffer.Color.WHITE | TerminalBuffer.Styling.BOLD);
const Matrix = @This();
@ -34,12 +32,12 @@ dots: []Dot,
lines: []Line,
frame: usize,
count: usize,
fg_ini: u32,
fg: u32,
min_codepoint: u16,
max_codepoint: u16,
default_cell: Cell,
pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, fg_ini: u32, min_codepoint: u16, max_codepoint: u16) !Matrix {
pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, fg: u32, min_codepoint: u16, max_codepoint: u16) !Matrix {
const dots = try allocator.alloc(Dot, terminal_buffer.width * (terminal_buffer.height + 1));
const lines = try allocator.alloc(Line, terminal_buffer.width);
@ -52,10 +50,10 @@ pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, fg_ini: u32,
.lines = lines,
.frame = 3,
.count = 0,
.fg_ini = fg_ini,
.fg = fg,
.min_codepoint = min_codepoint,
.max_codepoint = max_codepoint - min_codepoint,
.default_cell = .{ .ch = ' ', .fg = fg_ini, .bg = termbox.TB_DEFAULT },
.default_cell = .{ .ch = ' ', .fg = fg, .bg = terminal_buffer.bg },
};
}
@ -159,8 +157,8 @@ fn draw(self: *Matrix) void {
const dot = self.dots[buf_width * y + x];
const cell = if (dot.value == null or dot.value == ' ') self.default_cell else Cell{
.ch = @intCast(dot.value.?),
.fg = if (dot.is_head) DOT_HEAD_COLOR else self.fg_ini,
.bg = termbox.TB_DEFAULT,
.fg = if (dot.is_head) DOT_HEAD_COLOR else self.fg,
.bg = self.terminal_buffer.bg,
};
cell.put(x, y - 1);

View File

@ -2,11 +2,9 @@
const std = @import("std");
const ini = @import("zigini");
const interop = @import("../interop.zig");
const Save = @import("Save.zig");
const enums = @import("../enums.zig");
const termbox = interop.termbox;
const color_properties = [_][]const u8{
"bg",
"border_fg",
@ -178,25 +176,25 @@ fn mapColor(color: u16) ![]const u8 {
const color_no_styling = color & 0x00FF;
const styling_only = color & 0xFF00;
if (color_no_styling > termbox.TB_WHITE or styling_only > 0x8000) { // TB_DIM in 16-bit mode
return error.InvalidColor;
}
// If color is "greater" than TB_WHITE, or the styling is "greater" than TB_DIM,
// we have an invalid color, so return an error
if (color_no_styling > 0x0008 or styling_only > 0x8000) return error.InvalidColor;
var new_color: u32 = switch (color_no_styling) {
termbox.TB_DEFAULT => termbox.TB_DEFAULT,
termbox.TB_BLACK => termbox.TB_HI_BLACK,
termbox.TB_RED => 0x00FF0000,
termbox.TB_GREEN => 0x0000FF00,
termbox.TB_YELLOW => 0x00FFFF00,
termbox.TB_BLUE => 0x000000FF,
termbox.TB_MAGENTA => 0x00FF00FF,
termbox.TB_CYAN => 0x0000FFFF,
termbox.TB_WHITE => 0x00FFFFFF,
0x0000 => 0x00000000, // Default
0x0001 => 0x20000000, // "Hi-black" styling
0x0002 => 0x00FF0000, // Red
0x0003 => 0x0000FF00, // Green
0x0004 => 0x00FFFF00, // Yellow
0x0005 => 0x000000FF, // Blue
0x0006 => 0x00FF00FF, // Magenta
0x0007 => 0x0000FFFF, // Cyan
0x0008 => 0x00FFFFFF, // White
else => unreachable,
};
// Only applying styling if color isn't black and styling isn't also black
if (!(new_color == termbox.TB_HI_BLACK and styling_only == termbox.TB_HI_BLACK)) {
if (!(new_color == 0x20000000 and styling_only == 0x20000000)) {
// Shift styling by 16 to the left to apply it to the new 32-bit color
new_color |= @as(u32, @intCast(styling_only)) << 16;
}

View File

@ -8,9 +8,10 @@ const bigclock = @import("bigclock.zig");
const enums = @import("enums.zig");
const Environment = @import("Environment.zig");
const interop = @import("interop.zig");
const Doom = @import("animations/Doom.zig");
const Matrix = @import("animations/Matrix.zig");
const ColorMix = @import("animations/ColorMix.zig");
const Doom = @import("animations/Doom.zig");
const Dummy = @import("animations/Dummy.zig");
const Matrix = @import("animations/Matrix.zig");
const Animation = @import("tui/Animation.zig");
const TerminalBuffer = @import("tui/TerminalBuffer.zig");
const Session = @import("tui/components/Session.zig");
@ -339,7 +340,10 @@ pub fn main() !void {
var animation: Animation = undefined;
switch (config.animation) {
.none => {},
.none => {
var dummy = Dummy{};
animation = dummy.animation();
},
.doom => {
var doom = try Doom.init(allocator, &buffer, config.doom_top_color, config.doom_middle_color, config.doom_bottom_color);
animation = doom.animation();

View File

@ -18,6 +18,29 @@ pub const InitOptions = struct {
input_len: u8,
};
pub const Styling = struct {
pub const BOLD = termbox.TB_BOLD;
pub const UNDERLINE = termbox.TB_UNDERLINE;
pub const REVERSE = termbox.TB_REVERSE;
pub const ITALIC = termbox.TB_ITALIC;
pub const BLINK = termbox.TB_BLINK;
pub const HI_BLACK = termbox.TB_HI_BLACK;
pub const BRIGHT = termbox.TB_BRIGHT;
pub const DIM = termbox.TB_DIM;
};
pub const Color = struct {
pub const DEFAULT = 0x00000000;
pub const BLACK = Styling.HI_BLACK;
pub const RED = 0x00FF0000;
pub const GREEN = 0x0000FF00;
pub const YELLOW = 0x00FFFF00;
pub const BLUE = 0x000000FF;
pub const MAGENTA = 0x00FF00FF;
pub const CYAN = 0x0000FFFF;
pub const WHITE = 0x00FFFFFF;
};
random: Random,
width: usize,
height: usize,