diff --git a/src/animations/DurFile.zig b/src/animations/DurFile.zig index c0048ee..2de518e 100644 --- a/src/animations/DurFile.zig +++ b/src/animations/DurFile.zig @@ -355,11 +355,11 @@ fn center(v: i64) i64 { } 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 buf_width: i64 = @intCast(terminal_buffer.width); + const buf_height: i64 = @intCast(terminal_buffer.height); - const movie_width: u32 = dur_movie.columns; - const movie_height: u32 = dur_movie.lines; + const movie_width: i64 = @intCast(dur_movie.columns); + const movie_height: i64 = @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) }, @@ -542,21 +542,21 @@ fn draw(self: *DurFile) void { const buf_height: u32 = @intCast(self.terminal_buffer.height); // y is used as an iterator in the durformat, while cell_y gives us the correct placement for the cell (same for x) - for (@max(0, -self.start_pos[VEC_Y])..@intCast(self.frame_size[VEC_Y])) |y| { + const start_pos_y: u64 = @max(0, -self.start_pos[VEC_Y]); + const lines: u64 = @intCast(self.dur_movie.lines.?); + const end_pos_y: u64 = @intCast(@min(lines, buf_height - self.start_pos[VEC_Y])); + for (start_pos_y..end_pos_y) |y| { const y_offset_i = @as(i32, @intCast(y)) + self.start_pos[VEC_Y]; - // we skip the pass if it falls outside of the draw window (ensure no int underflow) - const cell_y: u32 = if (y_offset_i >= 0 and y_offset_i < buf_height) @intCast(y_offset_i) else continue; + const cell_y: u32 = @intCast(y_offset_i); var iter = std.unicode.Utf8View.initUnchecked(current_frame.contents[y]).iterator(); - for (@max(0, -self.start_pos[VEC_X])..@intCast(self.frame_size[VEC_X])) |x| { + const start_pos_x: u64 = @max(0, -self.start_pos[VEC_X]); + const columns: u64 = @intCast(self.dur_movie.columns.?); + const end_pos_x: u64 = @intCast(@min(columns, buf_width - self.start_pos[VEC_X])); + for (start_pos_x..end_pos_x) |x| { const x_offset_i = @as(i32, @intCast(x)) + self.start_pos[VEC_X]; - // skip pass, same as y but also increment the codepoint iter to fetch correct values in later passes - const cell_x: u32 = if (x_offset_i >= 0 and x_offset_i < buf_width) @intCast(x_offset_i) else { - _ = iter.nextCodepoint().?; - continue; - }; - + const cell_x: u32 = @intCast(x_offset_i); const codepoint: u21 = iter.nextCodepoint().?; const color_map = current_frame.colorMap[x][y];