Some updates for allowing crate consumers to use useful utility funcs
This commit is contained in:
107
src/graphics.rs
107
src/graphics.rs
@@ -45,10 +45,10 @@ impl GraphicsBuf {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
draw_bresenham_line(0, 0, 127, 0, &mut gfx_buf);
|
self.draw_bresenham_line(0, 0, 127, 0);
|
||||||
draw_bresenham_line(0, 63, 128, 63, &mut gfx_buf);
|
self.draw_bresenham_line(0, 63, 128, 63);
|
||||||
draw_bresenham_line(0, 0, 0, 63, &mut gfx_buf);
|
self.draw_bresenham_line(0, 0, 0, 63);
|
||||||
draw_bresenham_line(127, 0, 127, 63, &mut gfx_buf);
|
self.draw_bresenham_line(127, 0, 127, 63);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_string(&mut self, x: u8, y: u8, string: &str) {
|
pub fn draw_string(&mut self, x: u8, y: u8, string: &str) {
|
||||||
@@ -64,57 +64,68 @@ impl GraphicsBuf {
|
|||||||
pub fn get_px_buffer(&self) -> &[u8; 8192] {
|
pub fn get_px_buffer(&self) -> &[u8; 8192] {
|
||||||
&self.buffer
|
&self.buffer
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn draw_bresenham_line(x0: u8, y0: u8, x1: u8, y1: u8, gfx_buf: &mut [u8; 8192]) {
|
pub fn clr_rect(&mut self, x: u8, y: u8, w: u8, h: u8) {
|
||||||
let x0 = (x0 as i32).clamp(0, 127);
|
let gfx_buf = &mut self.buffer;
|
||||||
let x1 = (x1 as i32).clamp(0, 127);
|
|
||||||
let y0 = (y0 as i32).clamp(0, 63);
|
|
||||||
let y1 = (y1 as i32).clamp(0, 63);
|
|
||||||
|
|
||||||
let mut dx = x1 - x0;
|
for x in x as u32..(x + w) as u32 {
|
||||||
let mut dy = y1 - y0;
|
for y in y as u32..(y + h) as u32 {
|
||||||
|
gfx_buf[(y * 128 + x) as usize] = 0;
|
||||||
let mut x = x0;
|
|
||||||
let mut y = y0;
|
|
||||||
|
|
||||||
let mut err = 0;
|
|
||||||
|
|
||||||
let mut step_x = 1;
|
|
||||||
let mut step_y = 1;
|
|
||||||
|
|
||||||
if dx < 0 {
|
|
||||||
dx = -dx;
|
|
||||||
step_x = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if dy < 0 {
|
|
||||||
dy = -dy;
|
|
||||||
step_y = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if dx > dy {
|
|
||||||
for _ in 0..dx {
|
|
||||||
gfx_buf[(y * 128 + x) as usize] = 1;
|
|
||||||
|
|
||||||
x += step_x;
|
|
||||||
err += 2 * dy;
|
|
||||||
|
|
||||||
if err > dx {
|
|
||||||
y += step_y;
|
|
||||||
err -= 2 * dx;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
for _ in 0..dy {
|
|
||||||
gfx_buf[(y * 128 + x) as usize] = 1;
|
|
||||||
|
|
||||||
y += step_y;
|
pub fn draw_bresenham_line(&mut self, x0: u8, y0: u8, x1: u8, y1: u8) {
|
||||||
err += 2 * dx;
|
let gfx_buf = &mut self.buffer;
|
||||||
|
let x0 = (x0 as i32).clamp(0, 127);
|
||||||
|
let x1 = (x1 as i32).clamp(0, 127);
|
||||||
|
let y0 = (y0 as i32).clamp(0, 63);
|
||||||
|
let y1 = (y1 as i32).clamp(0, 63);
|
||||||
|
|
||||||
|
let mut dx = x1 - x0;
|
||||||
|
let mut dy = y1 - y0;
|
||||||
|
|
||||||
|
let mut x = x0;
|
||||||
|
let mut y = y0;
|
||||||
|
|
||||||
|
let mut err = 0;
|
||||||
|
|
||||||
|
let mut step_x = 1;
|
||||||
|
let mut step_y = 1;
|
||||||
|
|
||||||
|
if dx < 0 {
|
||||||
|
dx = -dx;
|
||||||
|
step_x = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if dy < 0 {
|
||||||
|
dy = -dy;
|
||||||
|
step_y = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if dx > dy {
|
||||||
|
for _ in 0..dx {
|
||||||
|
gfx_buf[(y * 128 + x) as usize] = 1;
|
||||||
|
|
||||||
if err > dy {
|
|
||||||
x += step_x;
|
x += step_x;
|
||||||
err -= 2 * dy;
|
err += 2 * dy;
|
||||||
|
|
||||||
|
if err > dx {
|
||||||
|
y += step_y;
|
||||||
|
err -= 2 * dx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _ in 0..dy {
|
||||||
|
gfx_buf[(y * 128 + x) as usize] = 1;
|
||||||
|
|
||||||
|
y += step_y;
|
||||||
|
err += 2 * dx;
|
||||||
|
|
||||||
|
if err > dy {
|
||||||
|
x += step_x;
|
||||||
|
err -= 2 * dy;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ impl SpriteAtlas {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_str_pixel_width(&self, str: &str) -> usize {
|
pub fn get_str_pixel_width(&self, str: &str) -> usize {
|
||||||
let mut width = 0;
|
let mut width = 0;
|
||||||
|
|
||||||
for c in str.chars() {
|
for c in str.chars() {
|
||||||
|
|||||||
Reference in New Issue
Block a user