fix alignment bugs

This commit is contained in:
hynak 2026-01-22 21:06:18 -05:00
commit 30f73117d7

View file

@ -306,12 +306,51 @@ full_color: bool,
frame_time: u32,
time_previous: i64,
is_color_format_16: bool,
offset_alignment: DurOffsetAlignment,
offset: IVec2,
// if the user has an even number of columns or rows, we will default to the left or higher position (e.g. 4 columns center = .x..)
fn center(v: u32) i64 {
return @intCast((v / 2) + (v % 2));
}
fn calc_start_position(terminal_buffer: *TerminalBuffer, dur_movie: *DurFormat, offset_alignment: DurOffsetAlignment, offset: IVec2) IVec2 {
const buf_width: u32 = @intCast(terminal_buffer.width);
const buf_height: u32 = @intCast(terminal_buffer.height);
const movie_width: u32 = @intCast(dur_movie.columns.?);
const movie_height: u32 = @intCast(dur_movie.lines.?);
const start_pos: IVec2 = switch (offset_alignment) {
DurOffsetAlignment.center => .{ center(buf_width) - center(movie_width), center(buf_height) - center(movie_height) },
DurOffsetAlignment.topleft => .{ 0, 0 },
DurOffsetAlignment.topcenter => .{ center(buf_width) - center(movie_width), 0 },
DurOffsetAlignment.topright => .{ buf_width - movie_width, 0 },
DurOffsetAlignment.centerleft => .{ 0, center(buf_height) - center(movie_height) },
DurOffsetAlignment.centerright => .{ buf_width - movie_width, center(buf_height) - center(movie_height) },
DurOffsetAlignment.bottomleft => .{ 0, buf_height - movie_height },
DurOffsetAlignment.bottomcenter => .{ center(buf_width) - center(movie_width), buf_height - movie_height },
DurOffsetAlignment.bottomright => .{ buf_width - movie_width, buf_height - movie_height },
};
return start_pos + offset;
}
fn calc_frame_size(terminal_buffer: *TerminalBuffer, dur_movie: *DurFormat) UVec2 {
const buf_width: u32 = @intCast(terminal_buffer.width);
const buf_height: u32 = @intCast(terminal_buffer.height);
const movie_width: u32 = @intCast(dur_movie.columns.?);
const movie_height: u32 = @intCast(dur_movie.lines.?);
// Draw only the needed amount if movie smaller than screen. If movie is bigger, we will just draw entire screen
const frame_width = if (movie_width < buf_width) movie_width else buf_width;
const frame_height = if (movie_height < buf_height) movie_height else buf_height;
return .{ frame_width, frame_height };
}
pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, log_writer: *std.io.Writer, file_path: []const u8, offset_alignment: DurOffsetAlignment, x_offset: i32, y_offset: i32, full_color: bool) !DurFile {
var dur_movie: DurFormat = .init(allocator);
@ -334,30 +373,10 @@ pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, log_writer:
return error.InvalidColorFormat;
}
const buf_width: u32 = @intCast(terminal_buffer.width);
const buf_height: u32 = @intCast(terminal_buffer.height);
const offset: IVec2 = .{ x_offset, y_offset };
const movie_width: u32 = @intCast(dur_movie.columns.?);
const movie_height: u32 = @intCast(dur_movie.lines.?);
var start_pos: IVec2 = switch (offset_alignment) {
DurOffsetAlignment.center => .{ center(buf_width) - center(movie_width), center(buf_height) - center(movie_height) },
DurOffsetAlignment.topleft => .{ 0, 0 },
DurOffsetAlignment.topcenter => .{ center(buf_width) - center(movie_width), 0 },
DurOffsetAlignment.topright => .{ buf_width - movie_width, 0 },
DurOffsetAlignment.centerleft => .{ 0, center(buf_width) - center(movie_width) },
DurOffsetAlignment.centerright => .{ buf_width - movie_width, center(buf_width) - center(movie_width) },
DurOffsetAlignment.bottomleft => .{ 0, buf_width - movie_height },
DurOffsetAlignment.bottomcenter => .{ center(buf_width) - center(movie_width), buf_width - movie_height },
DurOffsetAlignment.bottomright => .{ buf_width - movie_width, buf_height - movie_height },
};
start_pos += .{ x_offset, y_offset };
// Draw only the needed amount if movie smaller than screen. If movie is bigger, we will just draw entire screen
const frame_width = if (movie_width < buf_width) movie_width else buf_width;
const frame_height = if (movie_height < buf_height) movie_height else buf_height;
const start_pos = calc_start_position(terminal_buffer, &dur_movie, offset_alignment, offset);
const frame_size = calc_frame_size(terminal_buffer, &dur_movie);
// Convert dur fps to frames per ms
const frame_time: u32 = @intFromFloat(1000 / dur_movie.framerate.?);
@ -367,12 +386,14 @@ pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, log_writer:
.terminal_buffer = terminal_buffer,
.frames = 0,
.time_previous = std.time.milliTimestamp(),
.frame_size = .{ frame_width, frame_height },
.frame_size = frame_size,
.start_pos = start_pos,
.full_color = full_color,
.dur_movie = dur_movie,
.frame_time = frame_time,
.is_color_format_16 = eql(u8, dur_movie.colorFormat.?, "16"),
.offset_alignment = offset_alignment,
.offset = offset,
};
}
@ -384,7 +405,11 @@ fn deinit(self: *DurFile) void {
self.dur_movie.deinit();
}
fn realloc(_: *DurFile) anyerror!void {}
fn realloc(self: *DurFile) anyerror!void {
// when terminal size changes, we need to recalculate the start_pos and frame_size based on the new size
self.start_pos = calc_start_position(self.terminal_buffer, &self.dur_movie, self.offset_alignment, self.offset);
self.frame_size = calc_frame_size(self.terminal_buffer, &self.dur_movie);
}
fn draw(self: *DurFile) void {
const current_frame = self.dur_movie.frames.items[self.frames];