trait
This commit is contained in:
parent
5ba778e894
commit
ca1a207c99
1 changed files with 11 additions and 9 deletions
20
src/main.rs
20
src/main.rs
|
@ -53,7 +53,7 @@ struct BouncingImage {
|
||||||
|
|
||||||
trait Drawable {
|
trait Drawable {
|
||||||
// Associated function signature; `Self` refers to the implementor type.
|
// 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);
|
||||||
|
|
||||||
fn draw_png(&mut self, display: &mut Display, x: i32, y: i32);
|
fn draw_png(&mut self, display: &mut Display, x: i32, y: i32);
|
||||||
|
@ -88,7 +88,9 @@ impl BouncingImage {
|
||||||
|
|
||||||
}
|
}
|
||||||
impl Drawable for BouncingImage {
|
impl Drawable for BouncingImage {
|
||||||
|
fn rate(&self) -> u32 {
|
||||||
|
return self.rate;
|
||||||
|
}
|
||||||
/// Draws a PNG image at the given coordinates.
|
/// Draws a PNG image at the given coordinates.
|
||||||
fn draw_png(&mut self, display: &mut Display, x: i32, y: i32) {
|
fn draw_png(&mut self, display: &mut Display, x: i32, y: i32) {
|
||||||
|
|
||||||
|
@ -195,12 +197,12 @@ impl Display {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut images = vec![
|
let mut images:Vec<Box<dyn Drawable>> = vec![
|
||||||
BouncingImage::new("images/unicorn_cc.png", 13, -10, 1, -1, -1),
|
Box::new(BouncingImage::new("images/unicorn_cc.png", 13, -10, 1, -1, -1)),
|
||||||
BouncingImage::new("images/windows_logo.png", -8, 3, 2, -1, -1),
|
Box::new(BouncingImage::new("images/windows_logo.png", -8, 3, 2, -1, -1)),
|
||||||
BouncingImage::new("images/spade.png", 32, -12, 1, 0, 0),
|
Box::new(BouncingImage::new("images/spade.png", 32, -12, 1, 0, 0)),
|
||||||
BouncingImage::new("images/dvdvideo.png", 20, 6, 5, 1000, 800),
|
Box::new(BouncingImage::new("images/dvdvideo.png", 20, 6, 5, 1000, 800)),
|
||||||
BouncingImage::new("images/hackaday.png", 40, 18, 3, 500, 800),
|
Box::new(BouncingImage::new("images/hackaday.png", 40, 18, 3, 500, 800)),
|
||||||
];
|
];
|
||||||
|
|
||||||
let mut display = Display::new(DISPLAY_HOST, DISPLAY_PORT);
|
let mut display = Display::new(DISPLAY_HOST, DISPLAY_PORT);
|
||||||
|
@ -210,7 +212,7 @@ fn main() {
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
for (i, bb) in images.iter_mut().enumerate() {
|
for (i, bb) in images.iter_mut().enumerate() {
|
||||||
if bb.rate > 0 && frame_counter % bb.rate != 0 {
|
if bb.rate() > 0 && frame_counter % bb.rate() != 0 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
bb.draw_and_move(&mut display);
|
bb.draw_and_move(&mut display);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue