move drawable
This commit is contained in:
		
							parent
							
								
									24492eb158
								
							
						
					
					
						commit
						5ba778e894
					
				
					 1 changed files with 21 additions and 14 deletions
				
			
		
							
								
								
									
										35
									
								
								src/main.rs
									
										
									
									
									
								
							
							
						
						
									
										35
									
								
								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)]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue