Skip to content

Commit 8396660

Browse files
committed
fix(renderer): 修复动画帧延迟计算
- 使用 saturating_sub避免负数结果 - 确保计算结果不会溢出 - 提高代码的健壮性和可靠性
1 parent 90c8c82 commit 8396660

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/display/renderer.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,23 @@ pub fn render_video(results: Vec<(ImageProcessorResult, usize)>, config: Config)
104104
let time = std::time::Instant::now();
105105
print!("\x1b[0;0H");
106106
println!("{}", frame);
107-
std::thread::sleep(Duration::from_micros(delay - (time.elapsed().as_micros() as u64) - config.fps.pow(2)));
107+
std::thread::sleep(Duration::from_micros(
108+
delay
109+
.saturating_sub(time.elapsed().as_micros() as u64)
110+
.saturating_sub(config.fps.pow(2)),
111+
));
108112
}
109113
} else {
110114
for frame in frames {
111115
// As same as above
112116
let time = std::time::Instant::now();
113117
print!("\x1b[0;0H");
114118
println!("{}", frame);
115-
std::thread::sleep(Duration::from_micros(delay - (time.elapsed().as_micros() as u64) - config.fps.pow(2)));
119+
std::thread::sleep(Duration::from_micros(
120+
delay
121+
.saturating_sub(time.elapsed().as_micros() as u64)
122+
.saturating_sub(config.fps.pow(2)),
123+
));
116124
}
117125
}
118126
}

0 commit comments

Comments
 (0)