pub fn draw(&mut self) {
        // we prepare an "output_screen" String variable to store in one-shot the screen we'll write.
        // This is an optimization because we write all we need once instead of writing small bit of screen by small bit of screen.
        // Actually, this does not change much for Linux terminals (like 5 fps gained from this)
        // But for windows terminal we can see huge improvements (example lines-fps goes from 35-40 fps to 65-70 for a 100x50 term)
        // reset cursor position
        queue!(self.stdout, crossterm::cursor::MoveTo(0, 0)).unwrap();
        let mut first = true;
        let mut current_colors: (Color, Color) = (Color::Reset, Color::Reset);
        let mut moving = false;
        self.screen_last_frame.check_empty(); // refresh internal "empty" value
                                              // iterates through the screen memory and prints it on the output buffer
        for y in 0..self.height as i32 {
            for x in 0..self.width as i32 {
                let pixel = self.screen.get_pxl(x, y).unwrap();
                // we check if the screen has been modified at this coordinate
                // if so, we write like normally, else we set a 'moving' flag
                if self.screen_last_frame.is_empty()
                    || pixel != self.screen_last_frame.get_pxl(x, y).unwrap()
                {
                    if moving {
                        // if the moving flag is set, we need to write a goto instruction first
                        // this optimization minimize useless write on the screen
                        // actually writing to the screen is very slow so it's a good compromise
                        queue!(self.stdout, crossterm::cursor::MoveTo(x as u16, y as u16)).unwrap();
                        moving = false;
                    }
                    // we check if the last color is the same as the current one.
                    // if the color is the same, only print the character
                    // the less we write on the output the faster we'll get
                    // and additional characters for colors we already have set is
                    // time consuming
                    if current_colors != pixel.get_colors() || first {
                        current_colors = pixel.get_colors();
                        queue!(
                            self.stdout,
                            style::SetForegroundColor(pixel.fg),
                            style::SetBackgroundColor(pixel.bg),
                            style::Print(pixel.chr)
                        )
                        .unwrap();
                        first = false;
                    } else {
                        queue!(self.stdout, style::Print(pixel.chr)).unwrap();
                    }
                } else {
                    moving = true
                }
            }
            if y < self.height as i32 - 1 {
                queue!(self.stdout, style::Print("\r\n")).unwrap();
            }
        }
        // flush the buffer into user's terminal
        self.stdout.flush().unwrap();
        self.screen_last_frame = self.screen.clone();
    }
/// Draw the screen in the terminal  
/// For best results, use it once per frame
///
/// usage:
/// ```
/// engine.print(0,0,"Hello, world!"); // <- prints "Hello, world!" in 'screen' memory
/// engine.draw(); // display 'screen' memory to the user's terminal
/// ```
Draw the screen in the terminal
For best results, use it once per frame


Draw the screen in the terminal For best results use it once per frame
pub fn draw(&mut self) {
        // we prepare an "output_screen" String variable to store in one-shot the screen we'll write.
        // This is an optimization because we write all we need once instead of writing small bit of screen by small bit of screen.
        // Actually, this does not change much for Linux terminals (like 5 fps gained from this)
        // But for windows terminal we can see huge improvements (example lines-fps goes from 35-40 fps to 65-70 for a 100x50 term)
        // reset cursor position
        queue!(self.stdout, crossterm::cursor::MoveTo(0, 0)).unwrap();
        let mut first = true;
        let mut current_colors: (Color, Color) = (Color::Reset, Color::Reset);
        let mut moving = false;
        self.screen_last_frame.check_empty(); // refresh internal "empty" value
                                              // iterates through the screen memory and prints it on the output buffer
        for y in 0..self.height as i32 {
            for x in 0..self.width as i32 {
                let pixel = self.screen.get_pxl(x, y).unwrap();
                // we check if the screen has been modified at this coordinate
                // if so, we write like normally, else we set a 'moving' flag
                if self.screen_last_frame.is_empty()
                    || pixel != self.screen_last_frame.get_pxl(x, y).unwrap()
                {
                    if moving {
                        // if the moving flag is set, we need to write a goto instruction first
                        // this optimization minimize useless write on the screen
                        // actually writing to the screen is very slow so it's a good compromise
                        queue!(self.stdout, crossterm::cursor::MoveTo(x as u16, y as u16)).unwrap();
                        moving = false;
                    }
                    // we check if the last color is the same as the current one.
                    // if the color is the same, only print the character
                    // the less we write on the output the faster we'll get
                    // and additional characters for colors we already have set is
                    // time consuming
                    if current_colors != pixel.get_colors() || first {
                        current_colors = pixel.get_colors();
                        queue!(
                            self.stdout,
                            style::SetForegroundColor(pixel.fg),
                            style::SetBackgroundColor(pixel.bg),
                            style::Print(pixel.chr)
                        )
                        .unwrap();
                        first = false;
                    } else {
                        queue!(self.stdout, style::Print(pixel.chr)).unwrap();
                    }
                } else {
                    moving = true
                }
            }
            if y < self.height as i32 - 1 {
                queue!(self.stdout, style::Print("\r\n")).unwrap();
            }
        }
        // flush the buffer into user's terminal
        self.stdout.flush().unwrap();
        self.screen_last_frame = self.screen.clone();
    }
pub fn draw ( & mut self ) { // we prepare an "output_screen" String variable to store in one-shot the screen we'll write. // This is an optimization because we write all we need once instead of writing small bit of screen by small bit of screen. // Actually, this does not change much for Linux terminals (like 5 fps gained from this) // But for windows terminal we can see huge improvements (example lines-fps goes from 35-40 fps to 65-70 for a 100x50 term) // reset cursor position queue ! ( self stdout crossterm cursor MoveTo ( 0 0 ) ) . unwrap ( ) ; let mut first = true ; let mut current_colors : ( Color , Color ) = ( Color :: Reset , Color :: Reset ) ; let mut moving = false ; self . screen_last_frame . check_empty ( ) ; // refresh internal "empty" value // iterates through the screen memory and prints it on the output buffer for y in 0 .. self . height as i32 { for x in 0 .. self . width as i32 { let pixel = self . screen . get_pxl ( x , y ) . unwrap ( ) ; // we check if the screen has been modified at this coordinate // if so, we write like normally, else we set a 'moving' flag if self . screen_last_frame . is_empty ( ) || pixel != self . screen_last_frame . get_pxl ( x , y ) . unwrap ( ) { if moving { // if the moving flag is set, we need to write a goto instruction first // this optimization minimize useless write on the screen // actually writing to the screen is very slow so it's a good compromise queue ! ( self stdout crossterm cursor MoveTo ( x as u16 y as u16 ) ) . unwrap ( ) ; moving = false ; } // we check if the last color is the same as the current one. // if the color is the same, only print the character // the less we write on the output the faster we'll get // and additional characters for colors we already have set is // time consuming if current_colors != pixel . get_colors ( ) || first { current_colors = pixel . get_colors ( ) ; queue ! ( self stdout style SetForegroundColor ( pixel fg ) style SetBackgroundColor ( pixel bg ) style Print ( pixel chr ) ) . unwrap ( ) ; first = false ; } else { queue ! ( self stdout style Print ( pixel chr ) ) . unwrap ( ) ; } } else { moving = true } } if y < self . height as i32 - 1 { queue ! ( self stdout style Print ( " \r \n " ) ) . unwrap ( ) ; } } // flush the buffer into user's terminal self . stdout . flush ( ) . unwrap ( ) ; self . screen_last_frame = self . screen . clone ( ) ; }
Draw the screen in the terminal
For best results, use it once per frame
Draw the screen in the terminal For best results use it once per frame
&mut self
&mut self