move drawable

This commit is contained in:
0m.ax 2025-07-19 01:25:09 +02:00
parent 24492eb158
commit 5ba778e894

View file

@ -56,6 +56,7 @@ trait Drawable {
fn draw_and_move(&mut self, display: &mut Display); fn draw_and_move(&mut self, display: &mut Display);
fn draw_png(&mut self, display: &mut Display, x: i32, y: i32);
} }
impl BouncingImage { impl BouncingImage {
/// Initializes a new BouncingImage. /// Initializes a new BouncingImage.
@ -81,11 +82,29 @@ impl BouncingImage {
} }
bb bb
} }
} }
impl Drawable for BouncingImage { impl Drawable for BouncingImage {
/// Draws a PNG image at the given coordinates.
fn draw_png(&mut self, display: &mut Display, x: i32, y: i32) {
for sy in 0..self.img.height {
for sx in 0..self.img.width {
let index = (sy * self.img.width + sx) as usize * 4;
let rgba = &self.img.pixels[index..index + 4];
if rgba[3] > 0 { // Check alpha channel
display.set_pixel((x + sx as i32) as u16, (y + sy as i32) as u16, rgba[0], rgba[1], rgba[2]);
}
}
}
}
/// Draws the image and updates its position. /// Draws the image and updates its position.
fn draw_and_move(&mut self, display: &mut Display) { fn draw_and_move(&mut self, display: &mut Display) {
display.draw_png(&self.img, self.x, self.y); self.draw_png(display, self.x, self.y);
self.x += self.move_x; self.x += self.move_x;
self.y += self.move_y; self.y += self.move_y;
@ -162,18 +181,6 @@ impl Display {
} }
} }
/// Draws a PNG image at the given coordinates.
fn draw_png(&mut self, png: &PngData, x: i32, y: i32) {
for sy in 0..png.height {
for sx in 0..png.width {
let index = (sy * png.width + sx) as usize * 4;
let rgba = &png.pixels[index..index + 4];
if rgba[3] > 0 { // Check alpha channel
self.set_pixel((x + sx as i32) as u16, (y + sy as i32) as u16, rgba[0], rgba[1], rgba[2]);
}
}
}
}
/// Clears the entire screen to black. /// Clears the entire screen to black.
#[allow(dead_code)] #[allow(dead_code)]