X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fboll.rs;h=b8e21a798d2fe26a097e8a7fdb490d17d6e79fc4;hb=eca2559123ae3c7ef184bf42ec60680fcddb38f6;hp=35dd97e61fac2032b56d687015c85a557db28123;hpb=6c5dd5cf7007081ce7ea87e59aac21c0e7007a2c;p=kaka%2Frust-sdl-test.git diff --git a/src/boll.rs b/src/boll.rs index 35dd97e..b8e21a7 100644 --- a/src/boll.rs +++ b/src/boll.rs @@ -4,13 +4,13 @@ use sdl2::rect::Rect; use sdl2::render::Canvas; use sdl2::video::Window; -use {SCREEN_HEIGHT, SCREEN_WIDTH}; use common::Point2D; use sdl2::gfx::primitives::DrawRenderer; +use {SCREEN_HEIGHT, SCREEN_WIDTH}; pub trait Boll { fn update(&mut self); - fn draw(&mut self, canvas: &mut Canvas, size: u32); + fn draw(&self, canvas: &mut Canvas, size: u32); } pub struct SquareBoll { @@ -41,11 +41,12 @@ impl Boll for SquareBoll { } } - fn draw(&mut self, canvas: &mut Canvas, size: u32) { + fn draw(&self, canvas: &mut Canvas, size: u32) { canvas.set_draw_color(Color::RGBA( 255 - std::cmp::min(255, (self.vel.length() * 25.0) as u8), (255.0 * (self.pos.x / SCREEN_WIDTH as f64)) as u8, - (255.0 * (self.pos.y / SCREEN_HEIGHT as f64)) as u8, 128 + (255.0 * (self.pos.y / SCREEN_HEIGHT as f64)) as u8, + 128, )); let mut r = Rect::new(0, 0, size, size); r.center_on(Point::new(self.pos.x as i32, self.pos.y as i32)); @@ -53,7 +54,6 @@ impl Boll for SquareBoll { } } - pub struct CircleBoll { pub boll: SquareBoll, } @@ -61,10 +61,7 @@ pub struct CircleBoll { impl CircleBoll { pub fn new(pos: Point2D, vel: Point2D) -> CircleBoll { CircleBoll { - boll: SquareBoll { - pos, - vel, - } + boll: SquareBoll { pos, vel }, } } } @@ -74,13 +71,15 @@ impl Boll for CircleBoll { self.boll.update(); } - fn draw(&mut self, canvas: &mut Canvas, size: u32) { + fn draw(&self, canvas: &mut Canvas, size: u32) { let val = 255 - std::cmp::min(255, (self.boll.vel.length() * 20.0) as u8); - canvas.filled_circle(self.boll.pos.x as i16, self.boll.pos.y as i16, size as i16, Color::RGBA( - val, - val, - val, - 128, - )).unwrap(); + canvas + .filled_circle( + self.boll.pos.x as i16, + self.boll.pos.y as i16, + size as i16, + Color::RGBA(val, val, val, 128), + ) + .unwrap(); } }