color
This commit is contained in:
parent
bccb1be2d4
commit
7316805ad7
1 changed files with 19 additions and 22 deletions
41
src/main.rs
41
src/main.rs
|
@ -54,7 +54,7 @@ struct BouncingImage {
|
|||
trait Drawable {
|
||||
// Associated function signature; `Self` refers to the implementor type.
|
||||
fn rate(&self) -> u32;
|
||||
fn draw_and_move(&mut self, display: &mut Display);
|
||||
fn draw_and_move(&mut self, display: &mut Display,tick:u32);
|
||||
|
||||
}
|
||||
impl BouncingImage {
|
||||
|
@ -104,7 +104,7 @@ impl Drawable for BouncingImage {
|
|||
}
|
||||
|
||||
/// 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,_: u32) {
|
||||
self.draw_png(display, self.x, self.y);
|
||||
|
||||
self.x += self.move_x;
|
||||
|
@ -122,15 +122,13 @@ impl Drawable for BouncingImage {
|
|||
struct Circle {
|
||||
x: u32,
|
||||
y: u32,
|
||||
radius: i32
|
||||
}
|
||||
impl Circle {
|
||||
|
||||
fn new(x:u32, y: u32,radius:i32) -> Self {
|
||||
fn new(x:u32, y: u32) -> Self {
|
||||
Circle {
|
||||
x,
|
||||
y,
|
||||
radius
|
||||
y
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -153,16 +151,16 @@ impl Circle {
|
|||
/// * `center_y`: The y-coordinate of the circle's center.
|
||||
/// * `radius`: The radius of the circle. Must be non-negative.
|
||||
/// * `r`, `g`, `b`: The RGB color components for the circle.
|
||||
pub fn draw_circle(&mut self, display: &mut Display, center_x: u32, center_y: u32, radius: i32, r: u8, g: u8, b: u8) {
|
||||
pub fn draw_circle(&mut self, display: &mut Display, center_x: u32, center_y: u32, radius: u32, r: u8, g: u8, b: u8) {
|
||||
if radius < 0 {
|
||||
// Or return an error: Err("Radius cannot be negative".into())
|
||||
return;
|
||||
}
|
||||
|
||||
let mut x = 0;
|
||||
let mut y = radius;
|
||||
let radius_i32:i32 = radius.try_into().unwrap();
|
||||
let mut x:i32 = 0;
|
||||
let mut y:i32 = radius_i32;
|
||||
// Initial decision parameter
|
||||
let mut d = 3 - 2 * radius;
|
||||
let mut d:i32 = 3 - 2 * radius_i32;
|
||||
|
||||
// Iterate through the first octant and draw points in all 8 octants
|
||||
while y >= x {
|
||||
|
@ -188,18 +186,16 @@ impl Drawable for Circle {
|
|||
}
|
||||
|
||||
/// Helper method to draw the 8 symmetric points for a given (x, y) offset.
|
||||
fn draw_and_move(&mut self, display: &mut Display) {
|
||||
fn draw_and_move(&mut self, display: &mut Display,tick:u32) {
|
||||
let hsv_color = color::Hsv {
|
||||
h: (self.radius%360).try_into().unwrap(),
|
||||
h: (tick%360).try_into().unwrap(),
|
||||
s: 1.0,
|
||||
v: 1.0,
|
||||
};
|
||||
let rgb: color::Rgb = hsv_color.into();
|
||||
self.draw_circle(display,self.x,self.y,self.radius,rgb.r,rgb.g,rgb.b);
|
||||
self.radius = self.radius + 1;
|
||||
if self.radius > 1080/2 {
|
||||
self.radius = 1;
|
||||
}
|
||||
|
||||
let radius = tick % 128;
|
||||
self.draw_circle(display,self.x,self.y,radius.try_into().unwrap(),rgb.r,rgb.g,rgb.b);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -286,22 +282,23 @@ fn main() {
|
|||
// Box::new(BouncingImage::new("images/spade.png", 32, -12, 1, 0, 0)),
|
||||
// Box::new(BouncingImage::new("images/dvdvideo.png", 20, 6, 5, 1000, 800)),
|
||||
// Box::new(BouncingImage::new("images/hackaday.png", 40, 18, 3, 500, 800)),
|
||||
Box::new(Circle::new(1920/2,1080/2,128))
|
||||
Box::new(Circle::new(1920/2,1080/2))
|
||||
];
|
||||
|
||||
let mut display = Display::new(DISPLAY_HOST, DISPLAY_PORT);
|
||||
let mut frame_counter: u32 = 0;
|
||||
|
||||
// display.blank_screen();
|
||||
|
||||
let mut tick:u32 = 0;
|
||||
loop {
|
||||
for (i, bb) in images.iter_mut().enumerate() {
|
||||
if bb.rate() > 0 && frame_counter % bb.rate() != 0 {
|
||||
continue;
|
||||
}
|
||||
bb.draw_and_move(&mut display);
|
||||
}
|
||||
bb.draw_and_move(&mut display,tick);
|
||||
}
|
||||
display.flush_frame();
|
||||
tick+=1;
|
||||
frame_counter += 1;
|
||||
|
||||
// A small delay to control the frame rate
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue