|
|
|
@ -1,11 +1,9 @@
|
|
|
|
|
|
|
|
|
|
pub mod font; |
|
|
|
|
use self::font::Font; |
|
|
|
|
|
|
|
|
|
pub mod color; |
|
|
|
|
use self::color::Color; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)] |
|
|
|
|
pub enum Displayorientation { |
|
|
|
|
/// No rotation
|
|
|
|
@ -42,28 +40,30 @@ impl Display {
|
|
|
|
|
/// - Neccessary Buffersize
|
|
|
|
|
pub fn get_dimensions(&self) -> (u16, u16, u16) { |
|
|
|
|
match self { |
|
|
|
|
Display::Eink42BlackWhite => (400, 300, 15000) |
|
|
|
|
Display::Eink42BlackWhite => (400, 300, 15000), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[allow(dead_code)] |
|
|
|
|
pub struct Graphics<'a> { |
|
|
|
|
width: u16, |
|
|
|
|
height: u16, |
|
|
|
|
rotation: Displayorientation, |
|
|
|
|
buffer: &'a mut [u8] |
|
|
|
|
//buffer: Box<u8>//[u8; 15000],
|
|
|
|
|
buffer: &'a mut [u8], //buffer: Box<u8>//[u8; 15000]
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a> Graphics<'a> { |
|
|
|
|
/// width needs to be a multiple of 8!
|
|
|
|
|
pub fn new(width: u16, height: u16, buffer: &'a mut [u8]) -> Graphics<'a>{ |
|
|
|
|
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} |
|
|
|
|
Graphics { |
|
|
|
|
width, |
|
|
|
|
height, |
|
|
|
|
rotation: Displayorientation::Rotate0, |
|
|
|
|
buffer, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Clears/Fills the full buffer with `color`
|
|
|
|
@ -78,16 +78,18 @@ impl<'a> Graphics<'a> {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Draw a single Pixel with `color`
|
|
|
|
|
///
|
|
|
|
|
///
|
|
|
|
|
/// limited to i16::max images (buffer_size) at the moment
|
|
|
|
|
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) , |
|
|
|
|
0x80 >> (x % 8)), |
|
|
|
|
Displayorientation::Rotate90 | Displayorientation::Rotate270 |
|
|
|
|
=> (y as usize / 8 * self.width as usize + x as usize, |
|
|
|
|
0x80 >> (y % 8)), |
|
|
|
|
Displayorientation::Rotate0 | Displayorientation::Rotate180 => ( |
|
|
|
|
(x as usize / 8 + (self.width as usize / 8) * y as usize), |
|
|
|
|
0x80 >> (x % 8), |
|
|
|
|
), |
|
|
|
|
Displayorientation::Rotate90 | Displayorientation::Rotate270 => ( |
|
|
|
|
y as usize / 8 * self.width as usize + x as usize, |
|
|
|
|
0x80 >> (y % 8), |
|
|
|
|
), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if idx >= self.buffer.len() { |
|
|
|
@ -96,8 +98,8 @@ impl<'a> Graphics<'a> {
|
|
|
|
|
|
|
|
|
|
match color { |
|
|
|
|
Color::Black => { |
|
|
|
|
self.buffer[idx] &= !bit;
|
|
|
|
|
}, |
|
|
|
|
self.buffer[idx] &= !bit; |
|
|
|
|
} |
|
|
|
|
Color::White => { |
|
|
|
|
self.buffer[idx] |= bit; |
|
|
|
|
} |
|
|
|
@ -105,15 +107,17 @@ impl<'a> Graphics<'a> {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Draw a single Pixel with `color`
|
|
|
|
|
///
|
|
|
|
|
///
|
|
|
|
|
/// limited to i16::max images (buffer_size) at the moment
|
|
|
|
|
#[allow(dead_code)] |
|
|
|
|
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, |
|
|
|
|
Displayorientation::Rotate90 | Displayorientation::Rotate270 |
|
|
|
|
=> y as usize / 8 + (self.width as usize / 8) * x as usize, |
|
|
|
|
Displayorientation::Rotate0 | Displayorientation::Rotate180 => { |
|
|
|
|
x as usize / 8 + (self.width as usize / 8) * y as usize |
|
|
|
|
}, |
|
|
|
|
Displayorientation::Rotate90 | Displayorientation::Rotate270 => { |
|
|
|
|
y as usize / 8 + (self.width as usize / 8) * x as usize |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if idx >= self.buffer.len() { |
|
|
|
@ -122,7 +126,7 @@ impl<'a> Graphics<'a> {
|
|
|
|
|
|
|
|
|
|
match color { |
|
|
|
|
Color::Black => { |
|
|
|
|
self.buffer[idx] = !filling;
|
|
|
|
|
self.buffer[idx] = !filling; |
|
|
|
|
}, |
|
|
|
|
Color::White => { |
|
|
|
|
self.buffer[idx] = filling; |
|
|
|
@ -145,7 +149,6 @@ impl<'a> Graphics<'a> {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//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(&mut self, x0: u16, y0: u16, input: char, font: &Font, color: &Color) { |
|
|
|
@ -156,18 +159,17 @@ impl<'a> Graphics<'a> {
|
|
|
|
|
let buff = font.get_char(input); |
|
|
|
|
let char_width = font.get_char_width(input); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut row_counter = 0; |
|
|
|
|
let mut width_counter = 0u8; |
|
|
|
|
for &elem in buff.iter() { |
|
|
|
|
for _ in 0..8 { |
|
|
|
|
self.draw_pixel( |
|
|
|
|
x0 + u16::from(width_counter), |
|
|
|
|
y0 + row_counter, |
|
|
|
|
&Color::get_color(elem, width_counter % 8, color), |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
self.draw_pixel(
|
|
|
|
|
x0 + u16::from(width_counter),
|
|
|
|
|
y0 + row_counter,
|
|
|
|
|
&Color::get_color(elem, width_counter % 8, color)); |
|
|
|
|
|
|
|
|
|
//Widthcounter shows how far we are in x direction
|
|
|
|
|
//Widthcounter shows how far we are in x direction
|
|
|
|
|
width_counter += 1; |
|
|
|
|
// if we have reached
|
|
|
|
|
if width_counter >= char_width { |
|
|
|
@ -185,39 +187,48 @@ impl<'a> Graphics<'a> {
|
|
|
|
|
// 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(x0 + counter, y0 + 7 - u16::from(i), &Color::convert_color(elem, i, color)) |
|
|
|
|
self.draw_pixel( |
|
|
|
|
x0 + counter, |
|
|
|
|
y0 + 7 - u16::from(i), |
|
|
|
|
&Color::convert_color(elem, i, color), |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
counter += 1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Draws Strings with 8x8 Chars (1 pixel padding included)
|
|
|
|
|
///
|
|
|
|
|
///
|
|
|
|
|
/// Is quite small for the 400x300 E-Ink
|
|
|
|
|
///
|
|
|
|
|
///
|
|
|
|
|
/// no autobreak line yet
|
|
|
|
|
pub fn draw_string_8x8(&mut self, x0: u16, y0: u16, input: &str, color: &Color) { |
|
|
|
|
for (counter, input_char) in input.chars().enumerate() { |
|
|
|
|
self.draw_char_8x8(x0 + counter as u16*8, y0, input_char, color); |
|
|
|
|
self.draw_char_8x8( |
|
|
|
|
x0 + counter as u16 * 8,
|
|
|
|
|
y0,
|
|
|
|
|
input_char,
|
|
|
|
|
color,
|
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// void plotLine(int x0, int y0, int x1, int y1)
|
|
|
|
|
// {
|
|
|
|
|
// int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
|
|
|
|
|
// int dy = -abs(y1-y0), sy = y0<y1 ? 1 : -1;
|
|
|
|
|
// int err = dx+dy, e2; /* error value e_xy */
|
|
|
|
|
|
|
|
|
|
// for(;;){ /* loop */
|
|
|
|
|
// setPixel(x0,y0);
|
|
|
|
|
// if (x0==x1 && y0==y1) break;
|
|
|
|
|
// e2 = 2*err;
|
|
|
|
|
// if (e2 >= dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */
|
|
|
|
|
// if (e2 <= dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// void plotLine(int x0, int y0, int x1, int y1)
|
|
|
|
|
// {
|
|
|
|
|
// int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
|
|
|
|
|
// int dy = -abs(y1-y0), sy = y0<y1 ? 1 : -1;
|
|
|
|
|
// int err = dx+dy, e2; /* error value e_xy */
|
|
|
|
|
|
|
|
|
|
// for(;;){ /* loop */
|
|
|
|
|
// setPixel(x0,y0);
|
|
|
|
|
// if (x0==x1 && y0==y1) break;
|
|
|
|
|
// e2 = 2*err;
|
|
|
|
|
// if (e2 >= dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */
|
|
|
|
|
// if (e2 <= dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//bresenham algorithm for lines
|
|
|
|
|
/// draw line
|
|
|
|
|
/// draw line
|
|
|
|
|
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; |
|
|
|
@ -227,9 +238,9 @@ impl<'a> Graphics<'a> {
|
|
|
|
|
let dx = i16::abs(x1 - x0); |
|
|
|
|
let sx = if x0 < x1 { 1 } else { -1 }; |
|
|
|
|
|
|
|
|
|
let dy = - i16::abs(y1 - y0); |
|
|
|
|
let dy = -i16::abs(y1 - y0); |
|
|
|
|
let sy = if y0 < y1 { 1 } else { -1 }; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut err = dx + dy; |
|
|
|
|
|
|
|
|
|
loop { |
|
|
|
@ -239,7 +250,7 @@ impl<'a> Graphics<'a> {
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let e2 = 2*err; |
|
|
|
|
let e2 = 2 * err; |
|
|
|
|
|
|
|
|
|
if e2 >= dy { |
|
|
|
|
err += dy; |
|
|
|
@ -253,7 +264,7 @@ impl<'a> Graphics<'a> {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Draw a horizontal line
|
|
|
|
|
/// 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(&mut self, x: u16, y: u16, length: u16, color: &Color) { |
|
|
|
|
for i in 0..length { |
|
|
|
@ -297,69 +308,63 @@ impl<'a> Graphics<'a> {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
let mut dy = 1; |
|
|
|
|
let mut err: i16 = dx - 2 * radius as i16; |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
let mut dy = 1; |
|
|
|
|
let mut err: i16 = dx - 2 * radius as i16; |
|
|
|
|
|
|
|
|
|
while x >= y { |
|
|
|
|
if filled { |
|
|
|
|
self.circle_helper_filled_putpixel(x0, y0, x, y, color); |
|
|
|
|
} else { |
|
|
|
|
self.circle_helper_putpixel(x0, y0, x, y, color); |
|
|
|
|
} |
|
|
|
|
while x >= y { |
|
|
|
|
if filled { |
|
|
|
|
self.circle_helper_filled_putpixel(x0, y0, x, y, color); |
|
|
|
|
} else { |
|
|
|
|
self.circle_helper_putpixel(x0, y0, x, y, color); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err <= 0 { |
|
|
|
|
y += 1; |
|
|
|
|
err += dy; |
|
|
|
|
dy += 2; |
|
|
|
|
} |
|
|
|
|
if err <= 0 { |
|
|
|
|
y += 1; |
|
|
|
|
err += dy; |
|
|
|
|
dy += 2; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err > 0 { |
|
|
|
|
x -= 1; |
|
|
|
|
dx += 2; |
|
|
|
|
err += dx - 2 * radius as i16; |
|
|
|
|
if err > 0 { |
|
|
|
|
x -= 1; |
|
|
|
|
dx += 2; |
|
|
|
|
err += dx - 2 * radius as i16; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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(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(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(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);
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
//TODO: Test
|
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
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(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(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(&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); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
///TODO: test if circle looks good
|
|
|
|
|
/// Draws a circle
|
|
|
|
@ -373,7 +378,7 @@ fn circle_helper_filled_putpixel(&mut self, x0: u16, y0: u16, x: u16, y: u16, c
|
|
|
|
|
let radius = radius as i16; |
|
|
|
|
let x_mid = x as i16; |
|
|
|
|
let y_mid = y as i16; |
|
|
|
|
let mut x_pos: i16 = 0 - radius;
|
|
|
|
|
let mut x_pos: i16 = 0 - radius; |
|
|
|
|
let mut y_pos = 0; |
|
|
|
|
let mut err: i16 = 2 - 2 * radius; |
|
|
|
|
|
|
|
|
@ -387,12 +392,12 @@ fn circle_helper_filled_putpixel(&mut self, x0: u16, y0: u16, x: u16, y: u16, c
|
|
|
|
|
|
|
|
|
|
if radius <= y_pos { |
|
|
|
|
y_pos += 1; |
|
|
|
|
err += y_pos*2 + 1; |
|
|
|
|
err += y_pos * 2 + 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if radius > x_pos || err > y_pos { |
|
|
|
|
x_pos += 1; |
|
|
|
|
err += x_pos*2 + 1; |
|
|
|
|
err += x_pos * 2 + 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if x_pos >= 0 { |
|
|
|
@ -401,16 +406,12 @@ fn circle_helper_filled_putpixel(&mut self, x0: u16, y0: u16, x: u16, y: u16, c
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///TODO: test!
|
|
|
|
|
pub fn draw_filled_circle(&mut self, x0: u16, y0: u16, radius: u16, color: &Color) { |
|
|
|
|
self.draw_circle_helper(x0, y0, radius, true, color); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
|
|
|
|
############ ############ ############ ############ |
|
|
|
@ -424,8 +425,6 @@ fn circle_helper_filled_putpixel(&mut self, x0: u16, y0: u16, x: u16, y: u16, c
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)] |
|
|
|
|
mod graphics { |
|
|
|
|
use super::*; |
|
|
|
@ -435,15 +434,12 @@ mod graphics {
|
|
|
|
|
let mut buffer = [Color::White.get_byte_value(); 150]; |
|
|
|
|
let mut graphics = Graphics::new(40, 30, &mut buffer); |
|
|
|
|
graphics.draw_filled_rectangle(0, 0, 40, 30, &Color::Black); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(graphics.buffer[0], Color::Black.get_byte_value()); |
|
|
|
|
|
|
|
|
|
for &elem in graphics.buffer.iter() { |
|
|
|
|
|
|
|
|
|
assert_eq!(elem, Color::Black.get_byte_value()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// draw a 4x4 in the top left corner
|
|
|
|
@ -452,12 +448,12 @@ mod graphics {
|
|
|
|
|
let mut buffer = [Color::White.get_byte_value(); 8]; |
|
|
|
|
let mut graphics = Graphics::new(8, 8, &mut buffer); |
|
|
|
|
graphics.draw_filled_rectangle(0, 0, 4, 4, &Color::Black); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(graphics.buffer[0], 0x0f); |
|
|
|
|
|
|
|
|
|
let mut counter = 0; |
|
|
|
|
for &elem in graphics.buffer.iter() { |
|
|
|
|
counter += 1;
|
|
|
|
|
counter += 1; |
|
|
|
|
|
|
|
|
|
if counter <= 4 { |
|
|
|
|
assert_eq!(elem, 0x0f); |
|
|
|
@ -465,8 +461,6 @@ mod graphics {
|
|
|
|
|
assert_eq!(elem, Color::White.get_byte_value()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
@ -474,7 +468,7 @@ mod graphics {
|
|
|
|
|
let mut buffer = [Color::White.get_byte_value(); 4]; |
|
|
|
|
let mut graphics = Graphics::new(16, 2, &mut buffer); |
|
|
|
|
graphics.draw_horizontal_line(1, 0, 14, &Color::Black); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(graphics.buffer[0], 0x80); |
|
|
|
|
assert_eq!(graphics.buffer[1], 0x01); |
|
|
|
|
assert_eq!(graphics.buffer[2], Color::White.get_byte_value()); |
|
|
|
@ -488,12 +482,10 @@ mod graphics {
|
|
|
|
|
graphics.draw_vertical_line(0, 0, 8, &Color::Black); |
|
|
|
|
|
|
|
|
|
graphics.draw_vertical_line(5, 0, 8, &Color::Black); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(graphics.buffer[0], 0x7b); |
|
|
|
|
|
|
|
|
|
for &elem in graphics.buffer.iter() { |
|
|
|
|
|
|
|
|
|
assert_eq!(elem, 0x7bu8); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -509,9 +501,9 @@ mod graphics {
|
|
|
|
|
let mut buffer2 = [Color::White.get_byte_value(); 8]; |
|
|
|
|
let mut graphics2 = Graphics::new(8, 8, &mut buffer2); |
|
|
|
|
|
|
|
|
|
graphics2.draw_line(5, 0, 5, 8, &Color::Black);
|
|
|
|
|
graphics2.draw_line(5, 0, 5, 8, &Color::Black); |
|
|
|
|
|
|
|
|
|
for i in 0..graphics.buffer.len() {
|
|
|
|
|
for i in 0..graphics.buffer.len() { |
|
|
|
|
assert_eq!(graphics.buffer[i], graphics2.buffer[i]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -525,9 +517,9 @@ mod graphics {
|
|
|
|
|
|
|
|
|
|
let mut buffer2 = [Color::White.get_byte_value(); 4]; |
|
|
|
|
let mut graphics2 = Graphics::new(16, 2, &mut buffer2); |
|
|
|
|
graphics2.draw_line(1, 0, 14, 0, &Color::Black);
|
|
|
|
|
graphics2.draw_line(1, 0, 14, 0, &Color::Black); |
|
|
|
|
|
|
|
|
|
for i in 0..graphics.buffer.len() {
|
|
|
|
|
for i in 0..graphics.buffer.len() { |
|
|
|
|
assert_eq!(graphics.buffer[i], graphics2.buffer[i]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -538,15 +530,13 @@ mod graphics {
|
|
|
|
|
let mut buffer = [Color::White.get_byte_value(); 8]; |
|
|
|
|
let mut graphics = Graphics::new(8, 8, &mut buffer); |
|
|
|
|
|
|
|
|
|
graphics.draw_line(0, 0, 16, 16, &Color::Black);
|
|
|
|
|
graphics.draw_line(0, 0, 16, 16, &Color::Black); |
|
|
|
|
|
|
|
|
|
for i in 0..graphics.buffer.len() {
|
|
|
|
|
for i in 0..graphics.buffer.len() { |
|
|
|
|
assert_eq!(graphics.buffer[i], !(0x80 >> i % 8)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn test_pixel() { |
|
|
|
|
let mut buffer = [Color::White.get_byte_value(); 8]; |
|
|
|
@ -555,7 +545,6 @@ mod graphics {
|
|
|
|
|
|
|
|
|
|
assert_eq!(graphics.buffer[0], !0x40); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut buffer = [Color::White.get_byte_value(); 16]; |
|
|
|
|
let mut graphics = Graphics::new(16, 8, &mut buffer); |
|
|
|
|
graphics.draw_pixel(9, 0, &Color::Black); |
|
|
|
@ -573,15 +562,14 @@ mod graphics {
|
|
|
|
|
|
|
|
|
|
for i in 1..graphics.buffer.len() { |
|
|
|
|
assert_eq!(graphics.buffer[i], Color::White.get_byte_value()); |
|
|
|
|
}
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
graphics.draw_byte(0, 0, 0x5A, &Color::Black) ; |
|
|
|
|
graphics.draw_byte(0, 0, 0x5A, &Color::Black); |
|
|
|
|
assert_eq!(graphics.buffer[0], !0x5A); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn test_char_with_8x8_font() { |
|
|
|
|
|
|
|
|
|
// Test !
|
|
|
|
|
let mut buffer = [Color::White.get_byte_value(); 8]; |
|
|
|
|
let mut graphics = Graphics::new(8, 8, &mut buffer); |
|
|
|
@ -592,8 +580,7 @@ mod graphics {
|
|
|
|
|
} |
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
assert_eq!(graphics.buffer[7], Color::White.get_byte_value()); |
|
|
|
|
|
|
|
|
|
// Test H
|
|
|
|
|
let mut buffer = [Color::White.get_byte_value(); 8]; |
|
|
|
@ -603,36 +590,34 @@ mod graphics {
|
|
|
|
|
for i in 0..3 { |
|
|
|
|
assert_eq!(graphics.buffer[i], !0x88); |
|
|
|
|
} |
|
|
|
|
assert_eq!(graphics.buffer[3], !0xF8);
|
|
|
|
|
assert_eq!(graphics.buffer[3], !0xF8); |
|
|
|
|
for i in 4..7 { |
|
|
|
|
assert_eq!(graphics.buffer[i], !0x88); |
|
|
|
|
} |
|
|
|
|
assert_eq!(graphics.buffer[7], Color::White.get_byte_value());
|
|
|
|
|
assert_eq!(graphics.buffer[7], Color::White.get_byte_value()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn test_string_with_8x8_font() { |
|
|
|
|
|
|
|
|
|
// Test !H
|
|
|
|
|
let mut buffer = [Color::White.get_byte_value(); 16]; |
|
|
|
|
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!(graphics.buffer[i*2], !0x20); |
|
|
|
|
assert_eq!(graphics.buffer[i * 2], !0x20); |
|
|
|
|
} |
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
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!(graphics.buffer[i*2 + 1], !0x88); |
|
|
|
|
assert_eq!(graphics.buffer[i * 2 + 1], !0x88); |
|
|
|
|
} |
|
|
|
|
assert_eq!(graphics.buffer[3*2 + 1], !0xF8);
|
|
|
|
|
assert_eq!(graphics.buffer[3 * 2 + 1], !0xF8); |
|
|
|
|
for i in 4..7 { |
|
|
|
|
assert_eq!(graphics.buffer[i*2 + 1], !0x88); |
|
|
|
|
assert_eq!(graphics.buffer[i * 2 + 1], !0x88); |
|
|
|
|
} |
|
|
|
|
assert_eq!(graphics.buffer[7*2 + 1], Color::White.get_byte_value()); |
|
|
|
|
assert_eq!(graphics.buffer[7 * 2 + 1], Color::White.get_byte_value()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|