phil-opp / blog_os

Writing an OS in Rust

Home Page:http://os.phil-opp.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I do the scroll?

xuanplus opened this issue · comments

When the screen is filled with characters, I hope the line will scroll to up, and write to the last line, but to clear.

so, I get this:

fn scroll(&mut self) {
        let info = self.info.unwrap();
        
        // for i in 0..(info.height - LINE_HEIGHT * 2) {
        //     let old = (i + LINE_HEIGHT) * info.stride;
        //     let new = i * info.stride;
        //     self.buffer.as_mut().unwrap()[old..(old + info.stride)].copy_from_slice(
        //         &self.buffer.as_ref().clone().unwrap().as_ref()[new..(new + info.stride)],
        //     )
        // }

        for i in 0..(info.height - (LINE_HEIGHT * 2)) {
            for j in 0..info.width {
                let index = ((i + LINE_HEIGHT) * info.stride + j) * info.bytes_per_pixel;
                let color = [
                    self.buffer.as_ref().unwrap().as_ref()[index..(index + info.bytes_per_pixel)]
                        [0],
                    self.buffer.as_ref().unwrap().as_ref()[index..(index + info.bytes_per_pixel)]
                        [1],
                    self.buffer.as_ref().unwrap().as_ref()[index..(index + info.bytes_per_pixel)]
                        [2],
                ];
                let index = (i * info.stride + j) * info.bytes_per_pixel;
                self.buffer.as_mut().unwrap()[index..(index + info.bytes_per_pixel)]
                    .copy_from_slice(&color);
            }
        }

        for i in (info.height - (LINE_HEIGHT * 2))..(info.height - LINE_HEIGHT) {
            for j in 0..info.width {
                let index = (i * info.stride + j) * info.bytes_per_pixel;
                self.buffer.as_mut().unwrap()[index..(index + info.bytes_per_pixel)]
                    .copy_from_slice(&[0, 0, 0])
            }
        }

        self.y_pos -= LINE_HEIGHT;
    }

It works, but too slow. It scrolls per pixel line. :(

So I want to copy a pixel line at once to up. This is what the comment do.

But I can't get the ref after mut ref. What should I do?

Oh, I get it.

for i in 0..(info.height - LINE_HEIGHT * 2) {
    let old = (i + LINE_HEIGHT) * info.stride;
    let new = i * info.stride;
    self.buffer.as_mut().unwrap().copy_within(
        (old * info.bytes_per_pixel)..((old + info.stride) * info.bytes_per_pixel),
        new * info.bytes_per_pixel
    );
}