mirror of https://github.com/fairyglade/ly.git
				
				
				
			Clean termbox2 usage + fix animation bug
Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
		
							parent
							
								
									1672d4a9ec
								
							
						
					
					
						commit
						13ba52319c
					
				|  | @ -22,19 +22,19 @@ pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, top_color: u | ||||||
|         .terminal_buffer = terminal_buffer, |         .terminal_buffer = terminal_buffer, | ||||||
|         .buffer = buffer, |         .buffer = buffer, | ||||||
|         .fire = [_]Cell{ |         .fire = [_]Cell{ | ||||||
|             Cell.init(' ', 0x00000000, 0), |             Cell.init(' ', TerminalBuffer.Color.DEFAULT, TerminalBuffer.Color.DEFAULT), | ||||||
|             Cell.init(0x2591, top_color, 0), |             Cell.init(0x2591, top_color, TerminalBuffer.Color.DEFAULT), | ||||||
|             Cell.init(0x2592, top_color, 0), |             Cell.init(0x2592, top_color, TerminalBuffer.Color.DEFAULT), | ||||||
|             Cell.init(0x2593, top_color, 0), |             Cell.init(0x2593, top_color, TerminalBuffer.Color.DEFAULT), | ||||||
|             Cell.init(0x2588, top_color, 0), |             Cell.init(0x2588, top_color, TerminalBuffer.Color.DEFAULT), | ||||||
|             Cell.init(0x2591, middle_color, 2), |             Cell.init(0x2591, middle_color, top_color), | ||||||
|             Cell.init(0x2592, middle_color, 2), |             Cell.init(0x2592, middle_color, top_color), | ||||||
|             Cell.init(0x2593, middle_color, 2), |             Cell.init(0x2593, middle_color, top_color), | ||||||
|             Cell.init(0x2588, middle_color, 2), |             Cell.init(0x2588, middle_color, top_color), | ||||||
|             Cell.init(0x2591, bottom_color, 4), |             Cell.init(0x2591, bottom_color, middle_color), | ||||||
|             Cell.init(0x2592, bottom_color, 4), |             Cell.init(0x2592, bottom_color, middle_color), | ||||||
|             Cell.init(0x2593, bottom_color, 4), |             Cell.init(0x2593, bottom_color, middle_color), | ||||||
|             Cell.init(0x2588, bottom_color, 4), |             Cell.init(0x2588, bottom_color, middle_color), | ||||||
|         }, |         }, | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -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 {} | ||||||
|  | @ -1,19 +1,17 @@ | ||||||
| const std = @import("std"); | const std = @import("std"); | ||||||
| const interop = @import("../interop.zig"); |  | ||||||
| const Animation = @import("../tui/Animation.zig"); | const Animation = @import("../tui/Animation.zig"); | ||||||
| const Cell = @import("../tui/Cell.zig"); | const Cell = @import("../tui/Cell.zig"); | ||||||
| const TerminalBuffer = @import("../tui/TerminalBuffer.zig"); | const TerminalBuffer = @import("../tui/TerminalBuffer.zig"); | ||||||
| 
 | 
 | ||||||
| const Allocator = std.mem.Allocator; | const Allocator = std.mem.Allocator; | ||||||
| const Random = std.Random; | const Random = std.Random; | ||||||
| const termbox = interop.termbox; |  | ||||||
| 
 | 
 | ||||||
| pub const FRAME_DELAY: usize = 8; | pub const FRAME_DELAY: usize = 8; | ||||||
| 
 | 
 | ||||||
| // Characters change mid-scroll | // Characters change mid-scroll | ||||||
| pub const MID_SCROLL_CHANGE = true; | 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(); | const Matrix = @This(); | ||||||
| 
 | 
 | ||||||
|  | @ -34,12 +32,12 @@ dots: []Dot, | ||||||
| lines: []Line, | lines: []Line, | ||||||
| frame: usize, | frame: usize, | ||||||
| count: usize, | count: usize, | ||||||
| fg_ini: u32, | fg: u32, | ||||||
| min_codepoint: u16, | min_codepoint: u16, | ||||||
| max_codepoint: u16, | max_codepoint: u16, | ||||||
| default_cell: Cell, | 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 dots = try allocator.alloc(Dot, terminal_buffer.width * (terminal_buffer.height + 1)); | ||||||
|     const lines = try allocator.alloc(Line, terminal_buffer.width); |     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, |         .lines = lines, | ||||||
|         .frame = 3, |         .frame = 3, | ||||||
|         .count = 0, |         .count = 0, | ||||||
|         .fg_ini = fg_ini, |         .fg = fg, | ||||||
|         .min_codepoint = min_codepoint, |         .min_codepoint = min_codepoint, | ||||||
|         .max_codepoint = max_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 dot = self.dots[buf_width * y + x]; | ||||||
|             const cell = if (dot.value == null or dot.value == ' ') self.default_cell else Cell{ |             const cell = if (dot.value == null or dot.value == ' ') self.default_cell else Cell{ | ||||||
|                 .ch = @intCast(dot.value.?), |                 .ch = @intCast(dot.value.?), | ||||||
|                 .fg = if (dot.is_head) DOT_HEAD_COLOR else self.fg_ini, |                 .fg = if (dot.is_head) DOT_HEAD_COLOR else self.fg, | ||||||
|                 .bg = termbox.TB_DEFAULT, |                 .bg = self.terminal_buffer.bg, | ||||||
|             }; |             }; | ||||||
| 
 | 
 | ||||||
|             cell.put(x, y - 1); |             cell.put(x, y - 1); | ||||||
|  |  | ||||||
|  | @ -2,11 +2,9 @@ | ||||||
| 
 | 
 | ||||||
| const std = @import("std"); | const std = @import("std"); | ||||||
| const ini = @import("zigini"); | const ini = @import("zigini"); | ||||||
| const interop = @import("../interop.zig"); |  | ||||||
| const Save = @import("Save.zig"); | const Save = @import("Save.zig"); | ||||||
| const enums = @import("../enums.zig"); | const enums = @import("../enums.zig"); | ||||||
| 
 | 
 | ||||||
| const termbox = interop.termbox; |  | ||||||
| const color_properties = [_][]const u8{ | const color_properties = [_][]const u8{ | ||||||
|     "bg", |     "bg", | ||||||
|     "border_fg", |     "border_fg", | ||||||
|  | @ -178,25 +176,25 @@ fn mapColor(color: u16) ![]const u8 { | ||||||
|     const color_no_styling = color & 0x00FF; |     const color_no_styling = color & 0x00FF; | ||||||
|     const styling_only = color & 0xFF00; |     const styling_only = color & 0xFF00; | ||||||
| 
 | 
 | ||||||
|     if (color_no_styling > termbox.TB_WHITE or styling_only > 0x8000) { // TB_DIM in 16-bit mode |     // If color is "greater" than TB_WHITE, or the styling is "greater" than TB_DIM, | ||||||
|         return error.InvalidColor; |     // 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) { |     var new_color: u32 = switch (color_no_styling) { | ||||||
|         termbox.TB_DEFAULT => termbox.TB_DEFAULT, |         0x0000 => 0x00000000, // Default | ||||||
|         termbox.TB_BLACK => termbox.TB_HI_BLACK, |         0x0001 => 0x20000000, // "Hi-black" styling | ||||||
|         termbox.TB_RED => 0x00FF0000, |         0x0002 => 0x00FF0000, // Red | ||||||
|         termbox.TB_GREEN => 0x0000FF00, |         0x0003 => 0x0000FF00, // Green | ||||||
|         termbox.TB_YELLOW => 0x00FFFF00, |         0x0004 => 0x00FFFF00, // Yellow | ||||||
|         termbox.TB_BLUE => 0x000000FF, |         0x0005 => 0x000000FF, // Blue | ||||||
|         termbox.TB_MAGENTA => 0x00FF00FF, |         0x0006 => 0x00FF00FF, // Magenta | ||||||
|         termbox.TB_CYAN => 0x0000FFFF, |         0x0007 => 0x0000FFFF, // Cyan | ||||||
|         termbox.TB_WHITE => 0x00FFFFFF, |         0x0008 => 0x00FFFFFF, // White | ||||||
|         else => unreachable, |         else => unreachable, | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     // Only applying styling if color isn't black and styling isn't also black |     // 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 |         // Shift styling by 16 to the left to apply it to the new 32-bit color | ||||||
|         new_color |= @as(u32, @intCast(styling_only)) << 16; |         new_color |= @as(u32, @intCast(styling_only)) << 16; | ||||||
|     } |     } | ||||||
|  |  | ||||||
							
								
								
									
										10
									
								
								src/main.zig
								
								
								
								
							
							
						
						
									
										10
									
								
								src/main.zig
								
								
								
								
							|  | @ -8,9 +8,10 @@ const bigclock = @import("bigclock.zig"); | ||||||
| const enums = @import("enums.zig"); | const enums = @import("enums.zig"); | ||||||
| const Environment = @import("Environment.zig"); | const Environment = @import("Environment.zig"); | ||||||
| const interop = @import("interop.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 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 Animation = @import("tui/Animation.zig"); | ||||||
| const TerminalBuffer = @import("tui/TerminalBuffer.zig"); | const TerminalBuffer = @import("tui/TerminalBuffer.zig"); | ||||||
| const Session = @import("tui/components/Session.zig"); | const Session = @import("tui/components/Session.zig"); | ||||||
|  | @ -339,7 +340,10 @@ pub fn main() !void { | ||||||
|     var animation: Animation = undefined; |     var animation: Animation = undefined; | ||||||
| 
 | 
 | ||||||
|     switch (config.animation) { |     switch (config.animation) { | ||||||
|         .none => {}, |         .none => { | ||||||
|  |             var dummy = Dummy{}; | ||||||
|  |             animation = dummy.animation(); | ||||||
|  |         }, | ||||||
|         .doom => { |         .doom => { | ||||||
|             var doom = try Doom.init(allocator, &buffer, config.doom_top_color, config.doom_middle_color, config.doom_bottom_color); |             var doom = try Doom.init(allocator, &buffer, config.doom_top_color, config.doom_middle_color, config.doom_bottom_color); | ||||||
|             animation = doom.animation(); |             animation = doom.animation(); | ||||||
|  |  | ||||||
|  | @ -18,6 +18,29 @@ pub const InitOptions = struct { | ||||||
|     input_len: u8, |     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, | random: Random, | ||||||
| width: usize, | width: usize, | ||||||
| height: usize, | height: usize, | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue