From 5cfa3e8b1fab8d7dcc81cb811c5be4071487eff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Gro=C3=9F?= Date: Wed, 30 May 2018 14:20:19 +0200 Subject: [PATCH] Added mutable reference to buffer directly in graphics. Updated example and tests with the new version. --- examples/embedded_linux/src/main.rs | 40 ++-- src/drawing/mod.rs | 280 ++++++++++++++-------------- 2 files changed, 163 insertions(+), 157 deletions(-) diff --git a/examples/embedded_linux/src/main.rs b/examples/embedded_linux/src/main.rs index c790d07..388b535 100644 --- a/examples/embedded_linux/src/main.rs +++ b/examples/embedded_linux/src/main.rs @@ -105,22 +105,22 @@ fn main() { let mut buffer = [0u8; 15000]; // draw something - let graphics = Graphics::new(400, 300); - graphics.clear(&mut buffer, &Color::White); - graphics.draw_line(&mut buffer, 0,0,400,300, &Color::Black); + let mut graphics = Graphics::new(400, 300, &mut buffer); + graphics.clear(&Color::White); + graphics.draw_line(0,0,400,300, &Color::Black); - graphics.draw_filled_rectangle(&mut buffer, 200,200, 230, 230, &Color::Black); - graphics.draw_line(&mut buffer, 202,202,218,228, &Color::White); + graphics.draw_filled_rectangle(200,200, 230, 230, &Color::Black); + graphics.draw_line(202,202,218,228, &Color::White); - graphics.draw_circle(&mut buffer, 200, 150, 130, &Color::Black); + graphics.draw_circle(200, 150, 130, &Color::Black); - graphics.draw_pixel(&mut buffer, 390, 290, &Color::Black); + graphics.draw_pixel(390, 290, &Color::Black); - graphics.draw_horizontal_line(&mut buffer, 0, 150, 400, &Color::Black); + graphics.draw_horizontal_line(0, 150, 400, &Color::Black); - graphics.draw_vertical_line(&mut buffer, 200, 50, 200, &Color::Black); + graphics.draw_vertical_line(200, 50, 200, &Color::Black); - epd4in2.display_and_transfer_frame(&buffer, None).expect("display and transfer error"); + epd4in2.display_and_transfer_frame(graphics.get_buffer(), None).expect("display and transfer error"); epd4in2.delay_ms(3000); @@ -128,19 +128,19 @@ fn main() { //Test fast updating a bit more let mut small_buffer = [0x00; 128]; - let circle_graphics = Graphics::new(32,32); - circle_graphics.draw_circle(&mut small_buffer, 16,16, 10, &Color::Black); + let mut circle_graphics = Graphics::new(32,32, &mut small_buffer); + circle_graphics.draw_circle(16,16, 10, &Color::Black); - epd4in2.set_partial_window(&small_buffer, 16,16, 32, 32, false).expect("Partial Window Error"); + epd4in2.set_partial_window(circle_graphics.get_buffer(), 16,16, 32, 32, false).expect("Partial Window Error"); epd4in2.display_frame().expect("Display Frame Error"); - epd4in2.set_partial_window(&small_buffer, 128,64, 32, 32, false).expect("Partial Window Error"); + epd4in2.set_partial_window(circle_graphics.get_buffer(), 128,64, 32, 32, false).expect("Partial Window Error"); epd4in2.display_frame().expect("Display Frame Error"); - epd4in2.set_partial_window(&small_buffer, 320,24, 32, 32, false).expect("Partial Window Error"); + epd4in2.set_partial_window(circle_graphics.get_buffer(), 320,24, 32, 32, false).expect("Partial Window Error"); epd4in2.display_frame().expect("Display Frame Error"); - epd4in2.set_partial_window(&small_buffer, 160,240, 32, 32, false).expect("Partial Window Error"); + epd4in2.set_partial_window(circle_graphics.get_buffer(), 160,240, 32, 32, false).expect("Partial Window Error"); epd4in2.display_frame().expect("Display Frame Error"); epd4in2.delay_ms(3000); @@ -149,10 +149,10 @@ fn main() { //pub fn draw_string_8x8(&self, buffer: &mut[u8], x0: u16, y0: u16, input: &str, color: &Color) { - graphics.draw_string_8x8(&mut buffer, 16, 16, "hello", &Color::Black); - graphics.draw_char_8x8(&mut buffer, 250, 250, '#', &Color::Black); - graphics.draw_char_8x8(&mut buffer, 300, 16, '7', &Color::Black); - epd4in2.display_and_transfer_frame(&buffer, None).expect("display and transfer error"); + graphics.draw_string_8x8(16, 16, "hello", &Color::Black); + graphics.draw_char_8x8(250, 250, '#', &Color::Black); + graphics.draw_char_8x8(300, 16, '7', &Color::Black); + epd4in2.display_and_transfer_frame(graphics.get_buffer(), None).expect("display and transfer error"); epd4in2.delay_ms(3000); diff --git a/src/drawing/mod.rs b/src/drawing/mod.rs index 24c0ce0..60487be 100644 --- a/src/drawing/mod.rs +++ b/src/drawing/mod.rs @@ -50,30 +50,37 @@ impl Display { #[allow(dead_code)] -pub struct Graphics { +pub struct Graphics<'a> { width: u16, height: u16, rotation: Displayorientation, + buffer: &'a mut [u8] //buffer: Box//[u8; 15000], } -impl Graphics { +impl<'a> Graphics<'a> { /// width needs to be a multiple of 8! - pub fn new(width: u16, height: u16) -> Graphics{ - Graphics {width, height, rotation: Displayorientation::Rotate0} + pub fn new(width: u16, height: u16, buffer: &'a mut [u8]) -> Graphics<'a>{ + let len = buffer.len(); + assert!(width / 8 * height >= len as u16); + Graphics {width, height, rotation: Displayorientation::Rotate0, buffer} } /// Clears/Fills the full buffer with `color` - pub fn clear(&self, buffer: &mut[u8], color: &Color) { - for elem in buffer.iter_mut() { + pub fn clear(&mut self, color: &Color) { + for elem in self.buffer.iter_mut() { *elem = color.get_byte_value(); } } + pub fn get_buffer(&'a self) -> &'a [u8] { + self.buffer + } + /// Draw a single Pixel with `color` /// /// limited to i16::max images (buffer_size) at the moment - pub fn draw_pixel(&self, buffer: &mut[u8], x: u16, y: u16, color: &Color) { + pub fn draw_pixel(&mut self, x: u16, y: u16, color: &Color) { let (idx, bit) = match self.rotation { Displayorientation::Rotate0 | Displayorientation::Rotate180 => ((x as usize / 8 + (self.width as usize / 8) * y as usize) , @@ -83,16 +90,16 @@ impl Graphics { 0x80 >> (y % 8)), }; - if idx >= buffer.len() { + if idx >= self.buffer.len() { return; } match color { Color::Black => { - buffer[idx] &= !bit; + self.buffer[idx] &= !bit; }, Color::White => { - buffer[idx] |= bit; + self.buffer[idx] |= bit; } } } @@ -101,7 +108,7 @@ impl Graphics { /// /// limited to i16::max images (buffer_size) at the moment #[allow(dead_code)] - fn draw_byte(&self, buffer: &mut[u8], x: u16, y: u16, filling: u8, color: &Color) { + fn draw_byte(&mut self, x: u16, y: u16, filling: u8, color: &Color) { let idx = match self.rotation { Displayorientation::Rotate0 | Displayorientation::Rotate180 => x as usize / 8 + (self.width as usize / 8) * y as usize, @@ -109,31 +116,31 @@ impl Graphics { => y as usize / 8 + (self.width as usize / 8) * x as usize, }; - if idx >= buffer.len() { + if idx >= self.buffer.len() { return; } match color { Color::Black => { - buffer[idx] = !filling; + self.buffer[idx] = !filling; }, Color::White => { - buffer[idx] = filling; + self.buffer[idx] = filling; } } } ///TODO: test! - pub fn draw_char(&self, buffer: &mut[u8], x0: u16, y0: u16, input: char, font: &Font, color: &Color) { - self.draw_char_helper(buffer, x0, y0, input, font, color); + pub fn draw_char(&mut self, x0: u16, y0: u16, input: char, font: &Font, color: &Color) { + self.draw_char_helper(x0, y0, input, font, color); } ///TODO: test! /// no autobreak line yet - pub fn draw_string(&self, buffer: &mut[u8], x0: u16, y0: u16, input: &str, font: &Font, color: &Color) { + pub fn draw_string(&mut self, x0: u16, y0: u16, input: &str, font: &Font, color: &Color) { let mut counter = 0; for input_char in input.chars() { - self.draw_char(buffer, x0 + counter, y0, input_char, font, color); + self.draw_char(x0 + counter, y0, input_char, font, color); counter += font.get_char_width(input_char) as u16; } } @@ -141,7 +148,7 @@ impl Graphics { //TODO: add support for font_height = 0 //TODO: add support for char offset in y direction to reduce font file size - fn draw_char_helper(&self, buffer: &mut[u8], x0: u16, y0: u16, input: char, font: &Font, color: &Color) { + fn draw_char_helper(&mut self, x0: u16, y0: u16, input: char, font: &Font, color: &Color) { //width: u8, height: u8, charbuffer: &[u8] //TODO: font.get_char(input) -> FontChar {width, height, [u8]} //TODO: font.get_char_offset(input) -> u16 @@ -155,8 +162,7 @@ impl Graphics { for &elem in buff.iter() { for _ in 0..8 { - self.draw_pixel( - buffer, + self.draw_pixel( x0 + width_counter as u16, y0 + row_counter, &Color::get_color(elem, width_counter % 8, color)); @@ -174,12 +180,12 @@ impl Graphics { } /// Draws a single 8x8 Char somewhere (1 pixel padding included) - pub fn draw_char_8x8(&self, buffer: &mut[u8], x0: u16, y0: u16, input: char, color: &Color) { + pub fn draw_char_8x8(&mut self, x0: u16, y0: u16, input: char, color: &Color) { let mut counter = 0; // includes special draw_char instructions as this one is ordered columnwise and not rowwise (first byte == first 8 pixel columnwise) for &elem in font::bitmap_8x8(input).iter() { for i in 0..8u8 { - self.draw_pixel(buffer, x0 + counter, y0 + 7 - i as u16, &Color::convert_color(elem, i, color)) + self.draw_pixel(x0 + counter, y0 + 7 - i as u16, &Color::convert_color(elem, i, color)) } counter += 1; } @@ -190,10 +196,10 @@ impl Graphics { /// Is quite small for the 400x300 E-Ink /// /// no autobreak line yet - pub fn draw_string_8x8(&self, buffer: &mut[u8], x0: u16, y0: u16, input: &str, color: &Color) { + pub fn draw_string_8x8(&mut self, x0: u16, y0: u16, input: &str, color: &Color) { let mut counter = 0; for input_char in input.chars() { - self.draw_char_8x8(buffer, x0 + counter*8, y0, input_char, color); + self.draw_char_8x8(x0 + counter*8, y0, input_char, color); counter += 1; } } @@ -214,7 +220,7 @@ impl Graphics { // } //bresenham algorithm for lines /// draw line - pub fn draw_line(&self, buffer: &mut[u8], x0: u16, y0: u16, x1: u16, y1: u16, color: &Color) { + pub fn draw_line(&mut self, x0: u16, y0: u16, x1: u16, y1: u16, color: &Color) { let mut x0 = x0 as i16; let x1 = x1 as i16; let mut y0 = y0 as i16; @@ -229,7 +235,7 @@ impl Graphics { let mut err = dx + dy; loop { - self.draw_pixel(buffer, x0 as u16, y0 as u16, color); + self.draw_pixel(x0 as u16, y0 as u16, color); if x0 == x1 && y0 == y1 { break; @@ -251,50 +257,50 @@ impl Graphics { /// Draw a horizontal line /// TODO: maybe optimize by grouping up the bytes? But is it worth the longer and more complicated function? is it even faster? - pub fn draw_horizontal_line(&self, buffer: &mut[u8], x: u16, y: u16, length: u16, color: &Color) { + pub fn draw_horizontal_line(&mut self, x: u16, y: u16, length: u16, color: &Color) { for i in 0..length { - self.draw_pixel(buffer, x + i, y, color); + self.draw_pixel(x + i, y, color); } } /// Draws a vertical line - pub fn draw_vertical_line(&self, buffer: &mut[u8], x: u16, y: u16, length: u16, color: &Color) { + pub fn draw_vertical_line(&mut self, x: u16, y: u16, length: u16, color: &Color) { for i in 0..length { - self.draw_pixel(buffer, x, y + i, color); + self.draw_pixel(x, y + i, color); } } /// Draws a rectangle. (x0,y0) is top left corner, (x1,y1) bottom right - pub fn draw_rectangle(&self, buffer: &mut[u8], x0: u16, y0: u16, x1: u16, y1: u16, color: &Color) { + pub fn draw_rectangle(&mut self, x0: u16, y0: u16, x1: u16, y1: u16, color: &Color) { let (min_x, max_x) = if x0 <= x1 { (x0, x1) } else { (x1, x0) }; let (min_y, max_y) = if y0 <= y1 { (y0, y1) } else { (y1, y0) }; let x_len = max_x - min_x; let y_len = max_y - min_y; - self.draw_horizontal_line(buffer, min_x, min_y, x_len, color); - self.draw_horizontal_line(buffer, min_x, max_y, x_len, color); - self.draw_vertical_line(buffer, min_x, min_y, y_len, color); - self.draw_vertical_line(buffer, max_x, min_y, y_len, color); + self.draw_horizontal_line(min_x, min_y, x_len, color); + self.draw_horizontal_line(min_x, max_y, x_len, color); + self.draw_vertical_line(min_x, min_y, y_len, color); + self.draw_vertical_line(max_x, min_y, y_len, color); } /// Draws a filled rectangle. For more info see draw_rectangle - pub fn draw_filled_rectangle(&self, buffer: &mut[u8], x0: u16, y0: u16, x1: u16, y1: u16, color: &Color) { + pub fn draw_filled_rectangle(&mut self, x0: u16, y0: u16, x1: u16, y1: u16, color: &Color) { let (min_x, max_x) = if x0 <= x1 { (x0, x1) } else { (x1, x0) }; let (min_y, max_y) = if y0 <= y1 { (y0, y1) } else { (y1, y0) }; let x_len = max_x - min_x; let y_len = max_y - min_y; for i in 0..y_len { - self.draw_horizontal_line(buffer, min_x, min_y + i, x_len, color); + self.draw_horizontal_line(min_x, min_y + i, x_len, color); } } - fn draw_pixel_helper(&self, buffer: &mut[u8], x: i16, y: i16, color: &Color) { + fn draw_pixel_helper(&mut self, x: i16, y: i16, color: &Color) { if x >= 0 && y >= 0 { - self.draw_pixel(buffer, x as u16, y as u16, color); + self.draw_pixel(x as u16, y as u16, color); } } -fn draw_circle_helper(&self, buffer: &mut[u8], x0: u16, y0: u16, radius: u16, filled: bool, color: &Color) { +fn draw_circle_helper(&mut self, x0: u16, y0: u16, radius: u16, filled: bool, color: &Color) { let mut x = radius - 1; let mut y = 0; let mut dx = 1; @@ -303,9 +309,9 @@ fn draw_circle_helper(&self, buffer: &mut[u8], x0: u16, y0: u16, radius: u16, fi while x >= y { if filled { - self.circle_helper_filled_putpixel(buffer, x0, y0, x, y, color); + self.circle_helper_filled_putpixel(x0, y0, x, y, color); } else { - self.circle_helper_putpixel(buffer, x0, y0, x, y, color); + self.circle_helper_putpixel(x0, y0, x, y, color); } if err <= 0 { @@ -323,35 +329,35 @@ fn draw_circle_helper(&self, buffer: &mut[u8], x0: u16, y0: u16, radius: u16, fi } -fn circle_helper_putpixel(&self, buffer: &mut[u8], x0: u16, y0: u16, x: u16, y: u16, color: &Color) { - self.draw_horizontal_line(buffer, x0 - x, y0 + y, 2*x, color); +fn circle_helper_putpixel(&mut self, x0: u16, y0: u16, x: u16, y: u16, color: &Color) { + self.draw_horizontal_line(x0 - x, y0 + y, 2*x, color); // self.draw_pixel(buffer, x0 + x, y0 + y, color); // self.draw_pixel(buffer, x0 - x, y0 + y, color); - self.draw_horizontal_line(buffer, x0 - y, y0 + x, 2*y, color); + self.draw_horizontal_line(x0 - y, y0 + x, 2*y, color); // self.draw_pixel(buffer, x0 + y, y0 + x, color); // self.draw_pixel(buffer, x0 - y, y0 + x, color); - self.draw_horizontal_line(buffer, x0 - x, y0 - y, 2*x, color); + self.draw_horizontal_line(x0 - x, y0 - y, 2*x, color); // self.draw_pixel(buffer, x0 - x, y0 - y, color); // self.draw_pixel(buffer, x0 + x, y0 - y, color); - self.draw_horizontal_line(buffer, x0 - y, y0 - y, 2*y, color); + self.draw_horizontal_line(x0 - y, y0 - y, 2*y, color); // self.draw_pixel(buffer, x0 - y, y0 - x, color); // self.draw_pixel(buffer, x0 + y, y0 - x, color); } //TODO: Test -fn circle_helper_filled_putpixel(&self, buffer: &mut[u8], x0: u16, y0: u16, x: u16, y: u16, color: &Color) { - self.draw_pixel(buffer, x0 + x, y0 + y, color); - self.draw_pixel(buffer, x0 + y, y0 + x, color); - self.draw_pixel(buffer, x0 - y, y0 + x, color); - self.draw_pixel(buffer, x0 - x, y0 + y, color); - self.draw_pixel(buffer, x0 - x, y0 - y, color); - self.draw_pixel(buffer, x0 - y, y0 - x, color); - self.draw_pixel(buffer, x0 + y, y0 - x, color); - self.draw_pixel(buffer, x0 + x, y0 - y, color); +fn circle_helper_filled_putpixel(&mut self, x0: u16, y0: u16, x: u16, y: u16, color: &Color) { + self.draw_pixel(x0 + x, y0 + y, color); + self.draw_pixel(x0 + y, y0 + x, color); + self.draw_pixel(x0 - y, y0 + x, color); + self.draw_pixel(x0 - x, y0 + y, color); + self.draw_pixel(x0 - x, y0 - y, color); + self.draw_pixel(x0 - y, y0 - x, color); + self.draw_pixel(x0 + y, y0 - x, color); + self.draw_pixel(x0 + x, y0 - y, color); } @@ -359,13 +365,13 @@ fn circle_helper_filled_putpixel(&self, buffer: &mut[u8], x0: u16, y0: u16, x: ///TODO: test if circle looks good /// Draws a circle - pub fn draw_circle(&self, buffer: &mut[u8], x0: u16, y0: u16, radius: u16, color: &Color) { - self.draw_circle_helper(buffer, x0, y0, radius, false, color); + pub fn draw_circle(&mut self, x0: u16, y0: u16, radius: u16, color: &Color) { + self.draw_circle_helper(x0, y0, radius, false, color); } ///TODO: test if circle looks good /// Draws a circle - pub fn draw_circle2(&self, buffer: &mut[u8], x: u16, y: u16, radius: u16, color: &Color) { + pub fn draw_circle2(&mut self, x: u16, y: u16, radius: u16, color: &Color) { let radius = radius as i16; let x_mid = x as i16; let y_mid = y as i16; @@ -374,10 +380,10 @@ fn circle_helper_filled_putpixel(&self, buffer: &mut[u8], x0: u16, y0: u16, x: let mut err: i16 = 2 - 2 * radius; loop { - self.draw_pixel_helper(buffer, x_mid - x_pos, y_mid + y_pos, color); - self.draw_pixel_helper(buffer, x_mid - y_pos, y_mid - x_pos, color); - self.draw_pixel_helper(buffer, x_mid + x_pos, y_mid - y_pos, color); - self.draw_pixel_helper(buffer, x_mid + y_pos, y_mid + x_pos, color); + self.draw_pixel_helper(x_mid - x_pos, y_mid + y_pos, color); + self.draw_pixel_helper(x_mid - y_pos, y_mid - x_pos, color); + self.draw_pixel_helper(x_mid + x_pos, y_mid - y_pos, color); + self.draw_pixel_helper(x_mid + y_pos, y_mid + x_pos, color); let radius = err; @@ -399,8 +405,8 @@ fn circle_helper_filled_putpixel(&self, buffer: &mut[u8], x0: u16, y0: u16, x: ///TODO: test! - pub fn draw_filled_circle(&self, buffer: &mut[u8], x0: u16, y0: u16, radius: u16, color: &Color) { - self.draw_circle_helper(buffer, x0, y0, radius, true, color); + pub fn draw_filled_circle(&mut self, x0: u16, y0: u16, radius: u16, color: &Color) { + self.draw_circle_helper(x0, y0, radius, true, color); } @@ -429,12 +435,12 @@ mod graphics { #[test] fn test_filled_rectangle() { let mut buffer = [Color::White.get_byte_value(); 150]; - let graphics = Graphics::new(40, 30); - graphics.draw_filled_rectangle(&mut buffer, 0, 0, 40, 30, &Color::Black); + let mut graphics = Graphics::new(40, 30, &mut buffer); + graphics.draw_filled_rectangle(0, 0, 40, 30, &Color::Black); - assert_eq!(buffer[0], Color::Black.get_byte_value()); + assert_eq!(graphics.buffer[0], Color::Black.get_byte_value()); - for &elem in buffer.iter() { + for &elem in graphics.buffer.iter() { assert_eq!(elem, Color::Black.get_byte_value()); } @@ -446,13 +452,13 @@ mod graphics { #[test] fn test_filled_rectangle2() { let mut buffer = [Color::White.get_byte_value(); 8]; - let graphics = Graphics::new(8, 8); - graphics.draw_filled_rectangle(&mut buffer, 0, 0, 4, 4, &Color::Black); + let mut graphics = Graphics::new(8, 8, &mut buffer); + graphics.draw_filled_rectangle(0, 0, 4, 4, &Color::Black); - assert_eq!(buffer[0], 0x0f); + assert_eq!(graphics.buffer[0], 0x0f); let mut counter = 0; - for &elem in buffer.iter() { + for &elem in graphics.buffer.iter() { counter += 1; if counter <= 4 { @@ -468,27 +474,27 @@ mod graphics { #[test] fn test_horizontal_line() { let mut buffer = [Color::White.get_byte_value(); 4]; - let graphics = Graphics::new(16, 2); - graphics.draw_horizontal_line(&mut buffer, 1, 0, 14, &Color::Black); + let mut graphics = Graphics::new(16, 2, &mut buffer); + graphics.draw_horizontal_line(1, 0, 14, &Color::Black); - assert_eq!(buffer[0], 0x80); - assert_eq!(buffer[1], 0x01); - assert_eq!(buffer[2], Color::White.get_byte_value()); - assert_eq!(buffer[3], Color::White.get_byte_value()); + assert_eq!(graphics.buffer[0], 0x80); + assert_eq!(graphics.buffer[1], 0x01); + assert_eq!(graphics.buffer[2], Color::White.get_byte_value()); + assert_eq!(graphics.buffer[3], Color::White.get_byte_value()); } #[test] fn test_vertical_line() { let mut buffer = [Color::White.get_byte_value(); 8]; - let graphics = Graphics::new(8, 8); - graphics.draw_vertical_line(&mut buffer, 0, 0, 8, &Color::Black); + let mut graphics = Graphics::new(8, 8, &mut buffer); + graphics.draw_vertical_line(0, 0, 8, &Color::Black); - graphics.draw_vertical_line(&mut buffer, 5, 0, 8, &Color::Black); + graphics.draw_vertical_line(5, 0, 8, &Color::Black); - assert_eq!(buffer[0], 0x7b); + assert_eq!(graphics.buffer[0], 0x7b); - for &elem in buffer.iter() { + for &elem in graphics.buffer.iter() { assert_eq!(elem, 0x7bu8); } @@ -498,17 +504,17 @@ mod graphics { #[test] fn draw_line_1() { let mut buffer = [Color::White.get_byte_value(); 8]; - let graphics = Graphics::new(8, 8); + let mut graphics = Graphics::new(8, 8, &mut buffer); - graphics.draw_vertical_line(&mut buffer, 5, 0, 8, &Color::Black); + graphics.draw_vertical_line(5, 0, 8, &Color::Black); let mut buffer2 = [Color::White.get_byte_value(); 8]; - let graphics2 = Graphics::new(8, 8); + let mut graphics2 = Graphics::new(8, 8, &mut buffer2); - graphics2.draw_line(&mut buffer2, 5, 0, 5, 8, &Color::Black); + graphics2.draw_line(5, 0, 5, 8, &Color::Black); - for i in 0..buffer.len() { - assert_eq!(buffer[i], buffer2[i]); + for i in 0..graphics.buffer.len() { + assert_eq!(graphics.buffer[i], graphics2.buffer[i]); } } @@ -516,15 +522,15 @@ mod graphics { #[test] fn draw_line_2() { let mut buffer = [Color::White.get_byte_value(); 4]; - let graphics = Graphics::new(16, 2); - graphics.draw_horizontal_line(&mut buffer, 1, 0, 14, &Color::Black); + let mut graphics = Graphics::new(16, 2, &mut buffer); + graphics.draw_horizontal_line(1, 0, 14, &Color::Black); let mut buffer2 = [Color::White.get_byte_value(); 4]; - let graphics2 = Graphics::new(16, 2); - graphics2.draw_line(&mut buffer2, 1, 0, 14, 0, &Color::Black); + let mut graphics2 = Graphics::new(16, 2, &mut buffer2); + graphics2.draw_line(1, 0, 14, 0, &Color::Black); - for i in 0..buffer.len() { - assert_eq!(buffer[i], buffer2[i]); + for i in 0..graphics.buffer.len() { + assert_eq!(graphics.buffer[i], graphics2.buffer[i]); } } @@ -532,12 +538,12 @@ mod graphics { #[test] fn draw_line_3() { let mut buffer = [Color::White.get_byte_value(); 8]; - let graphics = Graphics::new(8, 8); + let mut graphics = Graphics::new(8, 8, &mut buffer); - graphics.draw_line(&mut buffer, 0, 0, 16, 16, &Color::Black); + graphics.draw_line(0, 0, 16, 16, &Color::Black); - for i in 0..buffer.len() { - assert_eq!(buffer[i], !(0x80 >> i % 8)); + for i in 0..graphics.buffer.len() { + assert_eq!(graphics.buffer[i], !(0x80 >> i % 8)); } } @@ -546,33 +552,33 @@ mod graphics { #[test] fn test_pixel() { let mut buffer = [Color::White.get_byte_value(); 8]; - let graphics = Graphics::new(8, 8); - graphics.draw_pixel(&mut buffer, 1, 0, &Color::Black); + let mut graphics = Graphics::new(8, 8, &mut buffer); + graphics.draw_pixel(1, 0, &Color::Black); - assert_eq!(buffer[0], !0x40); + assert_eq!(graphics.buffer[0], !0x40); let mut buffer = [Color::White.get_byte_value(); 16]; - let graphics = Graphics::new(16, 8); - graphics.draw_pixel(&mut buffer, 9, 0, &Color::Black); - assert_eq!(buffer[0], Color::White.get_byte_value()); - assert_eq!(buffer[1], !0x40); + let mut graphics = Graphics::new(16, 8, &mut buffer); + graphics.draw_pixel(9, 0, &Color::Black); + assert_eq!(graphics.buffer[0], Color::White.get_byte_value()); + assert_eq!(graphics.buffer[1], !0x40); } #[test] fn test_byte() { let mut buffer = [Color::White.get_byte_value(); 8]; - let graphics = Graphics::new(8, 8); - graphics.draw_byte(&mut buffer, 0, 0, 0xff, &Color::Black); + let mut graphics = Graphics::new(8, 8, &mut buffer); + graphics.draw_byte(0, 0, 0xff, &Color::Black); - assert_eq!(buffer[0], Color::Black.get_byte_value()); + assert_eq!(graphics.buffer[0], Color::Black.get_byte_value()); - for i in 1..buffer.len() { - assert_eq!(buffer[i], Color::White.get_byte_value()); + for i in 1..graphics.buffer.len() { + assert_eq!(graphics.buffer[i], Color::White.get_byte_value()); } - graphics.draw_byte(&mut buffer, 0, 0, 0x5A, &Color::Black) ; - assert_eq!(buffer[0], !0x5A); + graphics.draw_byte(0, 0, 0x5A, &Color::Black) ; + assert_eq!(graphics.buffer[0], !0x5A); } #[test] @@ -580,30 +586,30 @@ mod graphics { // Test ! let mut buffer = [Color::White.get_byte_value(); 8]; - let graphics = Graphics::new(8, 8); - graphics.draw_char_8x8(&mut buffer, 0, 0, '!', &Color::Black); + let mut graphics = Graphics::new(8, 8, &mut buffer); + graphics.draw_char_8x8(0, 0, '!', &Color::Black); for i in 0..5 { - assert_eq!(buffer[i], !0x20); + assert_eq!(graphics.buffer[i], !0x20); } - assert_eq!(buffer[5], Color::White.get_byte_value()); - assert_eq!(buffer[6], !0x20); - assert_eq!(buffer[7], Color::White.get_byte_value()); + assert_eq!(graphics.buffer[5], Color::White.get_byte_value()); + assert_eq!(graphics.buffer[6], !0x20); + assert_eq!(graphics.buffer[7], Color::White.get_byte_value()); // Test H let mut buffer = [Color::White.get_byte_value(); 8]; - let graphics = Graphics::new(8, 8); - graphics.draw_char_8x8(&mut buffer, 0, 0, 'H', &Color::Black); + let mut graphics = Graphics::new(8, 8, &mut buffer); + graphics.draw_char_8x8(0, 0, 'H', &Color::Black); for i in 0..3 { - assert_eq!(buffer[i], !0x88); + assert_eq!(graphics.buffer[i], !0x88); } - assert_eq!(buffer[3], !0xF8); + assert_eq!(graphics.buffer[3], !0xF8); for i in 4..7 { - assert_eq!(buffer[i], !0x88); + assert_eq!(graphics.buffer[i], !0x88); } - assert_eq!(buffer[7], Color::White.get_byte_value()); + assert_eq!(graphics.buffer[7], Color::White.get_byte_value()); } #[test] @@ -611,24 +617,24 @@ mod graphics { // Test !H let mut buffer = [Color::White.get_byte_value(); 16]; - let graphics = Graphics::new(16, 8); - graphics.draw_string_8x8(&mut buffer, 0, 0, "!H", &Color::Black); + let mut graphics = Graphics::new(16, 8, &mut buffer); + graphics.draw_string_8x8(0, 0, "!H", &Color::Black); for i in 0..5 { - assert_eq!(buffer[i*2], !0x20); + assert_eq!(graphics.buffer[i*2], !0x20); } - assert_eq!(buffer[5*2], Color::White.get_byte_value()); - assert_eq!(buffer[6*2], !0x20); - assert_eq!(buffer[7*2], Color::White.get_byte_value()); + assert_eq!(graphics.buffer[5*2], Color::White.get_byte_value()); + assert_eq!(graphics.buffer[6*2], !0x20); + assert_eq!(graphics.buffer[7*2], Color::White.get_byte_value()); for i in 0..3 { - assert_eq!(buffer[i*2 + 1], !0x88); + assert_eq!(graphics.buffer[i*2 + 1], !0x88); } - assert_eq!(buffer[3*2 + 1], !0xF8); + assert_eq!(graphics.buffer[3*2 + 1], !0xF8); for i in 4..7 { - assert_eq!(buffer[i*2 + 1], !0x88); + assert_eq!(graphics.buffer[i*2 + 1], !0x88); } - assert_eq!(buffer[7*2 + 1], Color::White.get_byte_value()); + assert_eq!(graphics.buffer[7*2 + 1], Color::White.get_byte_value()); } } \ No newline at end of file