Some updates for allowing crate consumers to use useful utility funcs

This commit is contained in:
JP Stringham
2026-03-25 09:11:20 -04:00
parent 5e01dd810a
commit c9cc177bfd
2 changed files with 60 additions and 49 deletions

View File

@@ -45,10 +45,10 @@ impl GraphicsBuf {
}
});
draw_bresenham_line(0, 0, 127, 0, &mut gfx_buf);
draw_bresenham_line(0, 63, 128, 63, &mut gfx_buf);
draw_bresenham_line(0, 0, 0, 63, &mut gfx_buf);
draw_bresenham_line(127, 0, 127, 63, &mut gfx_buf);
self.draw_bresenham_line(0, 0, 127, 0);
self.draw_bresenham_line(0, 63, 128, 63);
self.draw_bresenham_line(0, 0, 0, 63);
self.draw_bresenham_line(127, 0, 127, 63);
}
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] {
&self.buffer
}
}
fn draw_bresenham_line(x0: u8, y0: u8, x1: u8, y1: u8, gfx_buf: &mut [u8; 8192]) {
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);
pub fn clr_rect(&mut self, x: u8, y: u8, w: u8, h: u8) {
let gfx_buf = &mut self.buffer;
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;
x += step_x;
err += 2 * dy;
if err > dx {
y += step_y;
err -= 2 * dx;
for x in x as u32..(x + w) as u32 {
for y in y as u32..(y + h) as u32 {
gfx_buf[(y * 128 + x) as usize] = 0;
}
}
} else {
for _ in 0..dy {
gfx_buf[(y * 128 + x) as usize] = 1;
}
y += step_y;
err += 2 * dx;
pub fn draw_bresenham_line(&mut self, x0: u8, y0: u8, x1: u8, y1: u8) {
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;
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;
}
}
}
}

View File

@@ -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;
for c in str.chars() {