From 5ba778e8947e6f348cfadb404e19b90b0e63f0ca Mon Sep 17 00:00:00 2001 From: "0m.ax" Date: Sat, 19 Jul 2025 01:25:09 +0200 Subject: [PATCH] move drawable --- src/main.rs | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 65b2fbc..b030c7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,6 +56,7 @@ trait Drawable { fn draw_and_move(&mut self, display: &mut Display); + fn draw_png(&mut self, display: &mut Display, x: i32, y: i32); } impl BouncingImage { /// Initializes a new BouncingImage. @@ -81,11 +82,29 @@ impl BouncingImage { } bb } + + + + } 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. 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.y += self.move_y; @@ -161,19 +180,7 @@ impl Display { self.flush_frame(); } } - - /// 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. #[allow(dead_code)]