mirror of
https://github.com/fairyglade/ly.git
synced 2025-12-21 03:34:54 +00:00
Make main code less directly dependent on termbox2
Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
const std = @import("std");
|
||||
const Animation = @import("../tui/Animation.zig");
|
||||
const Cell = @import("../tui/Cell.zig");
|
||||
const TerminalBuffer = @import("../tui/TerminalBuffer.zig");
|
||||
const utils = @import("../tui/utils.zig");
|
||||
|
||||
const ColorMix = @This();
|
||||
|
||||
@@ -19,7 +19,7 @@ terminal_buffer: *TerminalBuffer,
|
||||
frames: u64,
|
||||
pattern_cos_mod: f32,
|
||||
pattern_sin_mod: f32,
|
||||
palette: [palette_len]utils.Cell,
|
||||
palette: [palette_len]Cell,
|
||||
|
||||
pub fn init(terminal_buffer: *TerminalBuffer, col1: u32, col2: u32, col3: u32) ColorMix {
|
||||
return .{
|
||||
@@ -27,19 +27,19 @@ pub fn init(terminal_buffer: *TerminalBuffer, col1: u32, col2: u32, col3: u32) C
|
||||
.frames = 0,
|
||||
.pattern_cos_mod = terminal_buffer.random.float(f32) * math.pi * 2.0,
|
||||
.pattern_sin_mod = terminal_buffer.random.float(f32) * math.pi * 2.0,
|
||||
.palette = [palette_len]utils.Cell{
|
||||
utils.initCell(0x2588, col1, col2),
|
||||
utils.initCell(0x2593, col1, col2),
|
||||
utils.initCell(0x2592, col1, col2),
|
||||
utils.initCell(0x2591, col1, col2),
|
||||
utils.initCell(0x2588, col2, col3),
|
||||
utils.initCell(0x2593, col2, col3),
|
||||
utils.initCell(0x2592, col2, col3),
|
||||
utils.initCell(0x2591, col2, col3),
|
||||
utils.initCell(0x2588, col3, col1),
|
||||
utils.initCell(0x2593, col3, col1),
|
||||
utils.initCell(0x2592, col3, col1),
|
||||
utils.initCell(0x2591, col3, col1),
|
||||
.palette = [palette_len]Cell{
|
||||
Cell.init(0x2588, col1, col2),
|
||||
Cell.init(0x2593, col1, col2),
|
||||
Cell.init(0x2592, col1, col2),
|
||||
Cell.init(0x2591, col1, col2),
|
||||
Cell.init(0x2588, col2, col3),
|
||||
Cell.init(0x2593, col2, col3),
|
||||
Cell.init(0x2592, col2, col3),
|
||||
Cell.init(0x2591, col2, col3),
|
||||
Cell.init(0x2588, col3, col1),
|
||||
Cell.init(0x2593, col3, col1),
|
||||
Cell.init(0x2592, col3, col1),
|
||||
Cell.init(0x2591, col3, col1),
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -80,7 +80,7 @@ fn draw(self: *ColorMix) void {
|
||||
}
|
||||
|
||||
const cell = self.palette[@as(usize, @intFromFloat(math.floor(length(uv) * 5.0))) % palette_len];
|
||||
utils.putCell(x, y, cell);
|
||||
cell.put(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Animation = @import("../tui/Animation.zig");
|
||||
const Cell = @import("../tui/Cell.zig");
|
||||
const TerminalBuffer = @import("../tui/TerminalBuffer.zig");
|
||||
const utils = @import("../tui/utils.zig");
|
||||
|
||||
const Doom = @This();
|
||||
|
||||
@@ -11,7 +11,7 @@ pub const STEPS = 12;
|
||||
allocator: Allocator,
|
||||
terminal_buffer: *TerminalBuffer,
|
||||
buffer: []u8,
|
||||
fire: [STEPS + 1]utils.Cell,
|
||||
fire: [STEPS + 1]Cell,
|
||||
|
||||
pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, top_color: u32, middle_color: u32, bottom_color: u32) !Doom {
|
||||
const buffer = try allocator.alloc(u8, terminal_buffer.width * terminal_buffer.height);
|
||||
@@ -21,20 +21,20 @@ pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, top_color: u
|
||||
.allocator = allocator,
|
||||
.terminal_buffer = terminal_buffer,
|
||||
.buffer = buffer,
|
||||
.fire = [_]utils.Cell{
|
||||
utils.initCell(' ', 0x00000000, 0),
|
||||
utils.initCell(0x2591, top_color, 0),
|
||||
utils.initCell(0x2592, top_color, 0),
|
||||
utils.initCell(0x2593, top_color, 0),
|
||||
utils.initCell(0x2588, top_color, 0),
|
||||
utils.initCell(0x2591, middle_color, 2),
|
||||
utils.initCell(0x2592, middle_color, 2),
|
||||
utils.initCell(0x2593, middle_color, 2),
|
||||
utils.initCell(0x2588, middle_color, 2),
|
||||
utils.initCell(0x2591, bottom_color, 4),
|
||||
utils.initCell(0x2592, bottom_color, 4),
|
||||
utils.initCell(0x2593, bottom_color, 4),
|
||||
utils.initCell(0x2588, bottom_color, 4),
|
||||
.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),
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -73,11 +73,13 @@ fn draw(self: *Doom) void {
|
||||
|
||||
const dest_y = dest / self.terminal_buffer.width;
|
||||
const dest_x = dest % self.terminal_buffer.width;
|
||||
utils.putCell(dest_x, dest_y, self.fire[buffer_dest]);
|
||||
const dest_cell = self.fire[buffer_dest];
|
||||
dest_cell.put(dest_x, dest_y);
|
||||
|
||||
const source_y = source / self.terminal_buffer.width;
|
||||
const source_x = source % self.terminal_buffer.width;
|
||||
utils.putCell(source_x, source_y, self.fire[buffer_source]);
|
||||
const source_cell = self.fire[buffer_source];
|
||||
source_cell.put(source_x, source_y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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 Animation = @import("../tui/Animation.zig");
|
||||
const TerminalBuffer = @import("../tui/TerminalBuffer.zig");
|
||||
const utils = @import("../tui/utils.zig");
|
||||
|
||||
const interop = @import("../interop.zig");
|
||||
const termbox = interop.termbox;
|
||||
|
||||
pub const FRAME_DELAY: usize = 8;
|
||||
@@ -13,6 +13,8 @@ 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 Matrix = @This();
|
||||
|
||||
pub const Dot = struct {
|
||||
@@ -35,6 +37,7 @@ count: usize,
|
||||
fg_ini: 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 {
|
||||
const dots = try allocator.alloc(Dot, terminal_buffer.width * (terminal_buffer.height + 1));
|
||||
@@ -52,6 +55,7 @@ pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, fg_ini: u32,
|
||||
.fg_ini = fg_ini,
|
||||
.min_codepoint = min_codepoint,
|
||||
.max_codepoint = max_codepoint - min_codepoint,
|
||||
.default_cell = .{ .ch = ' ', .fg = fg_ini, .bg = termbox.TB_DEFAULT },
|
||||
};
|
||||
}
|
||||
|
||||
@@ -153,16 +157,13 @@ fn draw(self: *Matrix) void {
|
||||
var y: usize = 1;
|
||||
while (y <= self.terminal_buffer.height) : (y += 1) {
|
||||
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,
|
||||
};
|
||||
|
||||
var fg = self.fg_ini;
|
||||
|
||||
if (dot.value == null or dot.value == ' ') {
|
||||
utils.putCell(x, y - 1, .{ .ch = ' ', .fg = fg, .bg = termbox.TB_DEFAULT });
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dot.is_head) fg = @intCast(0x00FFFFFF | termbox.TB_BOLD); // White and bold
|
||||
utils.putCell(x, y - 1, .{ .ch = @intCast(dot.value.?), .fg = fg, .bg = termbox.TB_DEFAULT });
|
||||
cell.put(x, y - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user