Only draw differences from previous frame

This commit is contained in:
Joscha 2022-05-21 23:38:37 +02:00
parent 9512ddaa3b
commit 67f8919630
2 changed files with 12 additions and 11 deletions

View file

@ -30,7 +30,7 @@ impl Pos {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cell { pub struct Cell {
pub content: Box<str>, pub content: Box<str>,
pub style: ContentStyle, pub style: ContentStyle,
@ -67,6 +67,12 @@ impl Buffer {
y * width + x y * width + x
} }
pub(crate) fn at(&self, x: u16, y: u16) -> &Cell {
assert!(x < self.size.width);
assert!(y < self.size.height);
&self.data[self.index(x, y)]
}
pub fn size(&self) -> Size { pub fn size(&self) -> Size {
self.size self.size
} }
@ -143,14 +149,6 @@ pub struct Cells<'a> {
y: u16, y: u16,
} }
impl<'a> Cells<'a> {
fn at(&self, x: u16, y: u16) -> &'a Cell {
assert!(x < self.buffer.size.width);
assert!(y < self.buffer.size.height);
&self.buffer.data[self.buffer.index(x, y)]
}
}
impl<'a> Iterator for Cells<'a> { impl<'a> Iterator for Cells<'a> {
type Item = (u16, u16, &'a Cell); type Item = (u16, u16, &'a Cell);
@ -160,7 +158,7 @@ impl<'a> Iterator for Cells<'a> {
} }
let (x, y) = (self.x, self.y); let (x, y) = (self.x, self.y);
let cell = self.at(self.x, self.y); let cell = self.buffer.at(self.x, self.y);
assert!(cell.offset == 0); assert!(cell.offset == 0);
self.x += cell.width as u16; self.x += cell.width as u16;

View file

@ -98,8 +98,11 @@ impl Terminal {
} }
fn draw_differences(&mut self) -> io::Result<()> { fn draw_differences(&mut self) -> io::Result<()> {
// TODO Only draw the differences
for (x, y, cell) in self.frame.buffer.cells() { for (x, y, cell) in self.frame.buffer.cells() {
if self.prev_frame_buffer.at(x, y) == cell {
continue;
}
let content = StyledContent::new(cell.style, &cell.content as &str); let content = StyledContent::new(cell.style, &cell.content as &str);
self.out self.out
.queue(MoveTo(x, y))? .queue(MoveTo(x, y))?