From dcf41c0ab884c124ffda8f616ec617c1e584255e Mon Sep 17 00:00:00 2001 From: Raymond Hogenson Date: Sat, 18 Aug 2018 11:01:49 -0400 Subject: Add textures These were fun to make --- src/state.rs | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) (limited to 'src/state.rs') diff --git a/src/state.rs b/src/state.rs index da53054..f70c0ba 100644 --- a/src/state.rs +++ b/src/state.rs @@ -239,42 +239,33 @@ impl Positioned for Enemy { } pub trait Drawable : Positioned { - fn color(&self) -> sdl2::pixels::Color; - fn draw(&self, canvas: &mut sdl2::render::WindowCanvas) { - let x = self.x(); - let y = self.y(); - let r = self.radius(); - let r = r as i32; - let x_min = x - r; - let x_max = x + r; - let y_min = y - r; - let y_max = y + r; - canvas.set_draw_color(self.color()); - for x in x_min..x_max + 1 { - for y in y_min..y_max + 1 { - if self.contains(x, y) { - canvas.fill_rect(sdl2::rect::Rect::new(x, y, 1, 1)).unwrap(); - } - } - } + fn center(&self) -> (u32, u32); + fn draw(&self, canvas: &mut sdl2::render::WindowCanvas, surface: &sdl2::surface::Surface) { + let texture_creator = canvas.texture_creator(); + let texture = texture_creator.create_texture_from_surface(surface).unwrap(); + let tw = texture.query().width; + let th = texture.query().height; + let (cx, cy) = self.center(); + let x0 = self.x() - cx as i32; + let y0 = self.y() - cy as i32; + canvas.copy(&texture, None, Some(sdl2::rect::Rect::new(x0, y0, tw, th))).unwrap(); } } impl Drawable for Enemy { - fn color(&self) -> sdl2::pixels::Color { - let red_scale = self.health as f64 / STARTING_HEALTH as f64 * 255.; - sdl2::pixels::Color::RGB(red_scale as u8, 255 - red_scale as u8, 0) + fn center(&self) -> (u32, u32) { + (0, 0) } } impl Drawable for EBullet { - fn color(&self) -> sdl2::pixels::Color { - sdl2::pixels::Color::RGB(0, 0, 255) + fn center(&self) -> (u32, u32) { + (20, 20) } } impl Drawable for PBullet { - fn color(&self) -> sdl2::pixels::Color { - sdl2::pixels::Color::RGB(230, 255, 230) + fn center(&self) -> (u32, u32) { + (16, 22) } } -- cgit v1.3.1