X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fboll.rs;h=5d095633529e879bb501c1fd9702a5997573ca88;hb=60058b918569190f437fe996dfc79daf5a431b91;hp=f936fca261df8cc6142e896db502a171e9e2fbd8;hpb=c315bb317cbd779063cb0135abe731886de30838;p=kaka%2Frust-sdl-test.git diff --git a/src/boll.rs b/src/boll.rs index f936fca..5d09563 100644 --- a/src/boll.rs +++ b/src/boll.rs @@ -1,20 +1,18 @@ -use sdl2::pixels::Color; -use sdl2::rect::Point; +use core::render::Renderer; use sdl2::rect::Rect; -use sdl2::render::Canvas; -use sdl2::video::Window; +use common::Point; +use sdl2::gfx::primitives::DrawRenderer; use {SCREEN_HEIGHT, SCREEN_WIDTH}; -use common::Point2D; pub trait Boll { fn update(&mut self); - fn draw(&mut self, canvas: &mut Canvas, size: u32); + fn draw(&self, renderer: &mut Renderer, size: u32); } pub struct SquareBoll { - pub pos: Point2D, - pub vel: Point2D, + pub pos: Point, + pub vel: Point, } impl Boll for SquareBoll { @@ -40,14 +38,46 @@ impl Boll for SquareBoll { } } - fn draw(&mut self, canvas: &mut Canvas, size: u32) { - canvas.set_draw_color(Color::RGBA( + fn draw(&self, renderer: &mut Renderer, size: u32) { + renderer.canvas().set_draw_color(( 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)); - canvas.fill_rect(r).unwrap(); + r.center_on((self.pos.x as i32, self.pos.y as i32)); + renderer.canvas().fill_rect(r).unwrap(); + } +} + +pub struct CircleBoll { + pub boll: SquareBoll, +} + +impl CircleBoll { + pub fn new(pos: Point, vel: Point) -> CircleBoll { + CircleBoll { + boll: SquareBoll { pos, vel }, + } + } +} + +impl Boll for CircleBoll { + fn update(&mut self) { + self.boll.update(); + } + + fn draw(&self, renderer: &mut Renderer, size: u32) { + let val = 255 - std::cmp::min(255, (self.boll.vel.length() * 20.0) as u8); + renderer + .canvas() + .filled_circle( + self.boll.pos.x as i16, + self.boll.pos.y as i16, + size as i16, + (val, val, val, 128), + ) + .unwrap(); } }