From ac57e9ee384ee441d55c2bd01549efb83630b5d1 Mon Sep 17 00:00:00 2001 From: logaritmisk Date: Tue, 21 Nov 2017 09:50:59 +0100 Subject: [PATCH] Update crates. Fmt. Clippy. --- Cargo.lock | 101 ++++++++++++-------- Cargo.toml | 2 +- src/camera.rs | 10 +- src/command.rs | 4 +- src/component.rs | 5 +- src/game_object.rs | 22 +++-- src/keyboard.rs | 10 +- src/main.rs | 86 ++++++++++------- src/player_components.rs | 19 ++-- src/sprite.rs | 46 +++++---- src/tile.rs | 197 ++++++++++++++++++++++++++------------- src/tiler.rs | 8 +- src/timer.rs | 4 +- 13 files changed, 320 insertions(+), 194 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f9422ad..f2f8f02 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,94 +1,119 @@ -[root] -name = "super-matte-bros" -version = "0.0.1" -dependencies = [ - "sdl2 0.28.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "bitflags" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "cfg-if" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "fuchsia-zircon" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fuchsia-zircon-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "fuchsia-zircon-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "lazy_static" -version = "0.2.2" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "libc" -version = "0.2.19" +version = "0.2.33" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "num" -version = "0.1.36" +version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "num-integer" -version = "0.1.32" +version = "0.1.35" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "num-iter" -version = "0.1.32" +version = "0.1.34" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "num-traits" -version = "0.1.36" +version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "rand" -version = "0.3.15" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-zircon 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "sdl2" -version = "0.28.0" +version = "0.31.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", - "num 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", - "sdl2-sys 0.27.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", + "num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", + "sdl2-sys 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "sdl2-sys" -version = "0.27.2" +version = "0.31.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "super-matte-bros" +version = "0.0.1" +dependencies = [ + "sdl2 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [metadata] "checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" -"checksum lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6abe0ee2e758cd6bc8a2cd56726359007748fbf4128da998b65d0b70f881e19b" -"checksum libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)" = "9e030dc72013ed68994d1b2cbf36a94dd0e58418ba949c4b0db7eeb70a7a6352" -"checksum num 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "bde7c03b09e7c6a301ee81f6ddf66d7a28ec305699e3d3b056d2fc56470e3120" -"checksum num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "fb24d9bfb3f222010df27995441ded1e954f8f69cd35021f6bef02ca9552fb92" -"checksum num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "287a1c9969a847055e1122ec0ea7a5c5d6f72aad97934e131c83d5c08ab4e45c" -"checksum num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "a16a42856a256b39c6d3484f097f6713e14feacd9bfb02290917904fae46c81c" -"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" -"checksum sdl2 0.28.0 (registry+https://github.com/rust-lang/crates.io-index)" = "29301273acc73295e4864a069bf9ad791ef0f819f4fc2a4653aa55818f1cbb05" -"checksum sdl2-sys 0.27.2 (registry+https://github.com/rust-lang/crates.io-index)" = "37cfe343db4cd2159cab098096fff52e92d513c2a5b1ee06abbfd5db5323a64d" +"checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de" +"checksum fuchsia-zircon 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f6c0581a4e363262e52b87f59ee2afe3415361c6ec35e665924eb08afe8ff159" +"checksum fuchsia-zircon-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "43f3795b4bae048dc6123a6b972cadde2e676f9ded08aef6bb77f5f157684a82" +"checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" +"checksum libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "5ba3df4dcb460b9dfbd070d41c94c19209620c191b0340b929ce748a2bcd42d2" +"checksum num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "a311b77ebdc5dd4cf6449d81e4135d9f0e3b153839ac90e648a8ef538f923525" +"checksum num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "d1452e8b06e448a07f0e6ebb0bb1d92b8890eea63288c0b627331d53514d0fba" +"checksum num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "7485fcc84f85b4ecd0ea527b14189281cf27d60e583ae65ebc9c088b13dffe01" +"checksum num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "99843c856d68d8b4313b03a17e33c4bb42ae8f6610ea81b28abe076ac721b9b0" +"checksum rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6475140dfd8655aeb72e1fd4b7a1cc1c202be65d71669476e392fe62532b9edd" +"checksum sdl2 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a74c2a98a354b20713b90cce70aef9e927e46110d1bc4ef728fd74e0d53eba60" +"checksum sdl2-sys 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5c543ce8a6e33a30cb909612eeeb22e693848211a84558d5a00bb11e791b7ab7" diff --git a/Cargo.toml b/Cargo.toml index 6c57ace..03c3834 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.0.1" authors = ["logaritmisk "] [dependencies.sdl2] -version = "0.28" +version = "0.31" default-features = false features = ["image"] diff --git a/src/camera.rs b/src/camera.rs index cedffe2..ba83f58 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -41,10 +41,12 @@ impl Camera { } pub fn to_relative_rect(&self, rect: &Rect) -> Rect { - Rect::new(rect.x() - self.x, - rect.y() - self.y, - rect.width(), - rect.height()) + Rect::new( + rect.x() - self.x, + rect.y() - self.y, + rect.width(), + rect.height(), + ) } pub fn to_rect(&self) -> Rect { diff --git a/src/command.rs b/src/command.rs index 0392275..5a65115 100644 --- a/src/command.rs +++ b/src/command.rs @@ -15,7 +15,9 @@ pub struct PlayerJumpCommand<'a> { impl<'a> PlayerJumpCommand<'a> { pub fn new(game_object: &'a mut GameObject<'a>) -> PlayerJumpCommand<'a> { - PlayerJumpCommand { game_object: RefCell::new(game_object) } + PlayerJumpCommand { + game_object: RefCell::new(game_object), + } } } diff --git a/src/component.rs b/src/component.rs index d6c4baf..e68b173 100644 --- a/src/component.rs +++ b/src/component.rs @@ -1,5 +1,6 @@ use sdl2::rect::Rect; -use sdl2::render::Renderer; +use sdl2::render::Canvas; +use sdl2::video::Window; use game_object::GameObject; @@ -9,5 +10,5 @@ pub trait Updatable { } pub trait Renderable { - fn render(&self, object: &GameObject, f64, &mut Renderer, &Rect); + fn render(&self, object: &GameObject, f64, &mut Canvas, &Rect); } diff --git a/src/game_object.rs b/src/game_object.rs index f405727..e2514af 100644 --- a/src/game_object.rs +++ b/src/game_object.rs @@ -1,7 +1,8 @@ use sdl2::rect::Rect; -use sdl2::render::Renderer; +use sdl2::render::Canvas; +use sdl2::video::Window; -use component::{Updatable, Renderable}; +use component::{Renderable, Updatable}; pub struct GameObject<'a> { @@ -9,8 +10,8 @@ pub struct GameObject<'a> { pub y: f32, pub w: u32, pub h: u32, - pub dx: f32, // TODO moved to PlayerPhysicsComponent - pub dy: f32, // TODO moved to PlayerPhysicsComponent + pub dx: f32, // TODO moved to PlayerPhysicsComponent + pub dy: f32, // TODO moved to PlayerPhysicsComponent pub gravity: f32, // TODO moved to PlayerPhysicsComponent pub on_ground: bool, physics: Box, @@ -18,11 +19,12 @@ pub struct GameObject<'a> { } impl<'a> GameObject<'a> { - pub fn new(x: f32, - y: f32, - physics: Box, - graphics: Box) - -> GameObject<'a> { + pub fn new( + x: f32, + y: f32, + physics: Box, + graphics: Box, + ) -> GameObject<'a> { GameObject { x: x, y: y, @@ -43,7 +45,7 @@ impl<'a> GameObject<'a> { } #[inline] - pub fn render(&self, elapsed: f64, renderer: &mut Renderer, destination: &Rect) { + pub fn render(&self, elapsed: f64, renderer: &mut Canvas, destination: &Rect) { self.graphics.render(self, elapsed, renderer, destination); } diff --git a/src/keyboard.rs b/src/keyboard.rs index 74be056..1e3e000 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -20,11 +20,11 @@ impl KeyboardHandler { pub fn process(&mut self, event: &Event) { match *event { - Event::KeyDown { keycode, repeat, .. } => { - if !repeat { - self.key_down(keycode.unwrap()); - } - } + Event::KeyDown { + keycode, repeat, .. + } => if !repeat { + self.key_down(keycode.unwrap()); + }, Event::KeyUp { keycode, .. } => { self.key_up(keycode.unwrap()); } diff --git a/src/main.rs b/src/main.rs index 13deb82..f23f155 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use keyboard::KeyboardHandler; use sprite::{Sprite, StaticSprite}; use timer::Timer; use game_object::GameObject; -use player_components::{PlayerPhysicsComponent, PlayerGraphicsComponent}; +use player_components::{PlayerGraphicsComponent, PlayerPhysicsComponent}; mod timer; @@ -59,43 +59,46 @@ fn main() { let _ = sdl2::image::init(sdl2::image::INIT_PNG).unwrap(); - let window = video_subsystem.window("Super Matte Bros", SCREEN_WIDTH, SCREEN_HEIGHT) + let window = video_subsystem + .window("Super Matte Bros", SCREEN_WIDTH, SCREEN_HEIGHT) .position_centered() .build() .unwrap(); - let mut renderer = window.renderer() - .software() - .build() - .unwrap(); + let mut canvas = window.into_canvas().software().build().unwrap(); + let creator = canvas.texture_creator(); - let world_sprites = renderer.load_texture(Path::new("gfx/world.png")).unwrap(); + let world_sprites = creator.load_texture(Path::new("gfx/world.png")).unwrap(); let _foo = tiler::Set { - image: renderer.load_texture(Path::new("gfx/world.png")).unwrap(), + image: creator.load_texture(Path::new("gfx/world.png")).unwrap(), width: 16, height: 16, - tiles: vec![tiler::Tile { - id: 0, - animation: None, - }, - tiler::Tile { - id: 1, - animation: None, - }], + tiles: vec![ + tiler::Tile { + id: 0, + animation: None, + }, + tiler::Tile { + id: 1, + animation: None, + }, + ], }; let floor_sprite = StaticSprite::new(&world_sprites, 16 * 0, 16 * 0); let brick_sprite = StaticSprite::new(&world_sprites, 16 * 1, 16 * 0); - let player_sprites = renderer.load_texture(Path::new("gfx/mario.png")).unwrap(); + let player_sprites = creator.load_texture(Path::new("gfx/mario.png")).unwrap(); let timer = Timer::new(); - let mut player = GameObject::new(390.0, - 390.0, - Box::new(PlayerPhysicsComponent::new()), - Box::new(PlayerGraphicsComponent::new(&player_sprites))); + let mut player = GameObject::new( + 390.0, + 390.0, + Box::new(PlayerPhysicsComponent::new()), + Box::new(PlayerGraphicsComponent::new(&player_sprites)), + ); let mut keyboard = KeyboardHandler::new(); @@ -279,7 +282,11 @@ fn main() { d = match *layer.get_tile(x, y) { Tile::Floor(_) => d.min(t), - Tile::Static(_, solid) => if solid { d.min(t) } else { d }, + Tile::Static(_, solid) => if solid { + d.min(t) + } else { + d + }, _ => d, }; @@ -311,7 +318,11 @@ fn main() { d = match *layer.get_tile(x, y) { Tile::Floor(_) => d.max(t), - Tile::Static(_, solid) => if solid { d.max(t) } else { d }, + Tile::Static(_, solid) => if solid { + d.max(t) + } else { + d + }, _ => d, }; @@ -345,7 +356,11 @@ fn main() { d = match *layer.get_tile(x, y) { Tile::Floor(_) => d.min(t), - Tile::Static(_, solid) => if solid { d.min(t) } else { d }, + Tile::Static(_, solid) => if solid { + d.min(t) + } else { + d + }, _ => d, }; @@ -381,7 +396,11 @@ fn main() { d = match *layer.get_tile(x, y) { Tile::Floor(_) => d.max(t), - Tile::Static(_, solid) => if solid { d.max(t) } else { d }, + Tile::Static(_, solid) => if solid { + d.max(t) + } else { + d + }, _ => d, }; @@ -407,17 +426,18 @@ fn main() { lag -= MS_PER_UPDATE; } - renderer.set_draw_color(Color::RGB(93, 148, 251)); - renderer.clear(); + canvas.set_draw_color(Color::RGB(93, 148, 251)); + canvas.clear(); layer.for_each_intersecting(&camera.to_rect(), |tile: &Tile, position: &Rect| { let object = camera.to_relative_rect(position); match *tile { - Tile::Background(src) | - Tile::Floor(src) => renderer.copy(&world_sprites, Some(src), Some(object)).unwrap(), - Tile::Static(ref sprite, _) => { - sprite.render(lag / MS_PER_UPDATE, &mut renderer, &object) + Tile::Background(src) | Tile::Floor(src) => canvas + .copy(&world_sprites, Some(src), Some(object)) + .unwrap(), + Tile::Static(sprite, _) => { + sprite.render(lag / MS_PER_UPDATE, &mut canvas, &object) } _ => (), } @@ -425,8 +445,8 @@ fn main() { let player_rect = camera.to_relative_rect(&player.to_rect()); - player.render(elapsed, &mut renderer, &player_rect); + player.render(elapsed, &mut canvas, &player_rect); - renderer.present(); + canvas.present(); } } diff --git a/src/player_components.rs b/src/player_components.rs index 3592ae7..4abae90 100644 --- a/src/player_components.rs +++ b/src/player_components.rs @@ -1,11 +1,12 @@ use std::cell::{Cell, RefCell}; use sdl2::rect::Rect; -use sdl2::render::{Renderer, Texture}; +use sdl2::render::{Canvas, Texture}; +use sdl2::video::Window; -use sprite::{Sprite, StaticSprite, AnimatedSprite}; +use sprite::{AnimatedSprite, Sprite, StaticSprite}; use game_object::GameObject; -use component::{Updatable, Renderable}; +use component::{Renderable, Updatable}; pub struct PlayerPhysicsComponent { @@ -48,11 +49,13 @@ impl<'a> PlayerGraphicsComponent<'a> { } impl<'a> Renderable for PlayerGraphicsComponent<'a> { - fn render(&self, - object: &GameObject, - elapsed: f64, - renderer: &mut Renderer, - destination: &Rect) { + fn render( + &self, + object: &GameObject, + elapsed: f64, + renderer: &mut Canvas, + destination: &Rect, + ) { if !object.on_ground { let mut sprite = self.sprite_jumping.borrow_mut(); diff --git a/src/sprite.rs b/src/sprite.rs index 4903ce2..bed5ac1 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -1,14 +1,16 @@ use std::cell::RefCell; -use sdl2::render::{Texture, Renderer}; +use sdl2::render::{Canvas, Texture}; +use sdl2::video::Window; use sdl2::rect::Rect; + pub trait Sprite { - fn render(&self, f64, &mut Renderer, &Rect); + fn render(&self, f64, &mut Canvas, &Rect); } pub struct StaticSprite<'a> { - texture: &'a Texture, + texture: &'a Texture<'a>, x: i32, y: i32, pub flip_horizontal: bool, @@ -28,19 +30,21 @@ impl<'a> StaticSprite<'a> { } impl<'a> Sprite for StaticSprite<'a> { - fn render(&self, _: f64, drawer: &mut Renderer, destination: &Rect) { - let _ = drawer.copy_ex(self.texture, - Some(Rect::new(self.x, self.y, 16, 16)), - Some(*destination), - 0.0, - None, - self.flip_horizontal, - self.flip_vertical); + fn render(&self, _: f64, drawer: &mut Canvas, destination: &Rect) { + let _ = drawer.copy_ex( + self.texture, + Some(Rect::new(self.x, self.y, 16, 16)), + Some(*destination), + 0.0, + None, + self.flip_horizontal, + self.flip_vertical, + ); } } pub struct AnimatedSprite<'a> { - texture: &'a Texture, + texture: &'a Texture<'a>, x: i32, y: i32, pub flip_horizontal: bool, @@ -68,7 +72,7 @@ impl<'a> AnimatedSprite<'a> { } impl<'a> Sprite for AnimatedSprite<'a> { - fn render(&self, elapsed: f64, drawer: &mut Renderer, destination: &Rect) { + fn render(&self, elapsed: f64, drawer: &mut Canvas, destination: &Rect) { let mut time = self.time.borrow_mut(); let mut frame = self.frame.borrow_mut(); @@ -79,12 +83,14 @@ impl<'a> Sprite for AnimatedSprite<'a> { let x = self.x + (*frame * 16) as i32; - let _ = drawer.copy_ex(self.texture, - Some(Rect::new(x, self.y, 16, 16)), - Some(*destination), - 0.0, - None, - self.flip_horizontal, - self.flip_vertical); + let _ = drawer.copy_ex( + self.texture, + Some(Rect::new(x, self.y, 16, 16)), + Some(*destination), + 0.0, + None, + self.flip_horizontal, + self.flip_vertical, + ); } } diff --git a/src/tile.rs b/src/tile.rs index 8e2b7b4..80cd2a5 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -11,7 +11,8 @@ pub struct Layer { } impl Layer - where T: Clone +where + T: Clone, { // TODO Change u32 to usize for width and height? pub fn new(width: u32, height: u32, tile_width: u32, tile_height: u32, tile: T) -> Layer { @@ -28,7 +29,7 @@ impl Layer pub fn get_tile(&self, x: i32, y: i32) -> &T { let offset = (x + y * self.width as i32) as usize; - match *self.tiles.get(offset).unwrap() { + match self.tiles[offset] { Some(ref tile) => tile, None => &self.default, } @@ -41,12 +42,13 @@ impl Layer } pub fn find_intersecting(&self, rect: &Rect) -> Option { - if rect.x() + rect.width() as i32 <= 0 || - rect.x() >= (self.width * self.tile_width) as i32 { + if rect.x() + rect.width() as i32 <= 0 || rect.x() >= (self.width * self.tile_width) as i32 + { return None; } - if rect.y() + rect.height() as i32 <= 0 || - rect.y() >= (self.height * self.tile_height) as i32 { + if rect.y() + rect.height() as i32 <= 0 + || rect.y() >= (self.height * self.tile_height) as i32 + { return None; } @@ -56,17 +58,24 @@ impl Layer let x2 = (rect.x() + rect.width() as i32 - 1) / self.tile_width as i32; let y2 = (rect.y() + rect.height() as i32 - 1) / self.tile_height as i32; - Some(Rect::new(x1, y1, (x2 - x1 + 1) as u32, (y2 - y1 + 1) as u32)) + Some(Rect::new( + x1, + y1, + (x2 - x1 + 1) as u32, + (y2 - y1 + 1) as u32, + )) } pub fn for_each_intersecting(&self, rect: &Rect, mut f: F) { if let Some(intersect) = self.find_intersecting(rect) { for y in intersect.y()..(intersect.y() + intersect.height() as i32) { for x in intersect.x()..(intersect.x() + intersect.width() as i32) { - let position = Rect::new(x * self.tile_width as i32, - y * self.tile_height as i32, - self.tile_width, - self.tile_height); + let position = Rect::new( + x * self.tile_width as i32, + y * self.tile_height as i32, + self.tile_width, + self.tile_height, + ); f(self.get_tile(x, y), &position); } @@ -75,10 +84,12 @@ impl Layer } pub fn to_rect(&self) -> Rect { - Rect::new(0, - 0, - self.width * self.tile_width, - self.height * self.tile_height) + Rect::new( + 0, + 0, + self.width * self.tile_width, + self.height * self.tile_height, + ) } } @@ -102,71 +113,123 @@ mod tests { assert_eq!(layer.find_intersecting(&Rect::new(9, 9, 1, 1)), None); // middle of tile. - assert_eq!(layer.find_intersecting(&Rect::new(1, 1, 1, 1)), - Some(Rect::new(0, 0, 1, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(4, 1, 1, 1)), - Some(Rect::new(1, 0, 1, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(7, 1, 1, 1)), - Some(Rect::new(2, 0, 1, 1))); + assert_eq!( + layer.find_intersecting(&Rect::new(1, 1, 1, 1)), + Some(Rect::new(0, 0, 1, 1)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(4, 1, 1, 1)), + Some(Rect::new(1, 0, 1, 1)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(7, 1, 1, 1)), + Some(Rect::new(2, 0, 1, 1)) + ); - assert_eq!(layer.find_intersecting(&Rect::new(1, 4, 1, 1)), - Some(Rect::new(0, 1, 1, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(4, 4, 1, 1)), - Some(Rect::new(1, 1, 1, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(7, 4, 1, 1)), - Some(Rect::new(2, 1, 1, 1))); + assert_eq!( + layer.find_intersecting(&Rect::new(1, 4, 1, 1)), + Some(Rect::new(0, 1, 1, 1)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(4, 4, 1, 1)), + Some(Rect::new(1, 1, 1, 1)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(7, 4, 1, 1)), + Some(Rect::new(2, 1, 1, 1)) + ); - assert_eq!(layer.find_intersecting(&Rect::new(1, 7, 1, 1)), - Some(Rect::new(0, 2, 1, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(4, 7, 1, 1)), - Some(Rect::new(1, 2, 1, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(7, 7, 1, 1)), - Some(Rect::new(2, 2, 1, 1))); + assert_eq!( + layer.find_intersecting(&Rect::new(1, 7, 1, 1)), + Some(Rect::new(0, 2, 1, 1)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(4, 7, 1, 1)), + Some(Rect::new(1, 2, 1, 1)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(7, 7, 1, 1)), + Some(Rect::new(2, 2, 1, 1)) + ); // interlaps 4 tiles. - assert_eq!(layer.find_intersecting(&Rect::new(2, 2, 2, 2)), - Some(Rect::new(0, 0, 2, 2))); - assert_eq!(layer.find_intersecting(&Rect::new(5, 2, 2, 2)), - Some(Rect::new(1, 0, 2, 2))); - assert_eq!(layer.find_intersecting(&Rect::new(2, 5, 2, 2)), - Some(Rect::new(0, 1, 2, 2))); - assert_eq!(layer.find_intersecting(&Rect::new(5, 5, 2, 2)), - Some(Rect::new(1, 1, 2, 2))); + assert_eq!( + layer.find_intersecting(&Rect::new(2, 2, 2, 2)), + Some(Rect::new(0, 0, 2, 2)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(5, 2, 2, 2)), + Some(Rect::new(1, 0, 2, 2)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(2, 5, 2, 2)), + Some(Rect::new(0, 1, 2, 2)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(5, 5, 2, 2)), + Some(Rect::new(1, 1, 2, 2)) + ); // interlaps 2 tiles horizontal. - assert_eq!(layer.find_intersecting(&Rect::new(2, 1, 2, 1)), - Some(Rect::new(0, 0, 2, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(5, 1, 2, 1)), - Some(Rect::new(1, 0, 2, 1))); + assert_eq!( + layer.find_intersecting(&Rect::new(2, 1, 2, 1)), + Some(Rect::new(0, 0, 2, 1)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(5, 1, 2, 1)), + Some(Rect::new(1, 0, 2, 1)) + ); - assert_eq!(layer.find_intersecting(&Rect::new(2, 4, 2, 1)), - Some(Rect::new(0, 1, 2, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(5, 4, 2, 1)), - Some(Rect::new(1, 1, 2, 1))); + assert_eq!( + layer.find_intersecting(&Rect::new(2, 4, 2, 1)), + Some(Rect::new(0, 1, 2, 1)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(5, 4, 2, 1)), + Some(Rect::new(1, 1, 2, 1)) + ); - assert_eq!(layer.find_intersecting(&Rect::new(2, 7, 2, 1)), - Some(Rect::new(0, 2, 2, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(5, 7, 2, 1)), - Some(Rect::new(1, 2, 2, 1))); + assert_eq!( + layer.find_intersecting(&Rect::new(2, 7, 2, 1)), + Some(Rect::new(0, 2, 2, 1)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(5, 7, 2, 1)), + Some(Rect::new(1, 2, 2, 1)) + ); // interlaps 2 tiles vertical. - assert_eq!(layer.find_intersecting(&Rect::new(1, 2, 1, 2)), - Some(Rect::new(0, 0, 1, 2))); - assert_eq!(layer.find_intersecting(&Rect::new(1, 5, 1, 2)), - Some(Rect::new(0, 1, 1, 2))); + assert_eq!( + layer.find_intersecting(&Rect::new(1, 2, 1, 2)), + Some(Rect::new(0, 0, 1, 2)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(1, 5, 1, 2)), + Some(Rect::new(0, 1, 1, 2)) + ); - assert_eq!(layer.find_intersecting(&Rect::new(4, 2, 1, 2)), - Some(Rect::new(1, 0, 1, 2))); - assert_eq!(layer.find_intersecting(&Rect::new(4, 5, 1, 2)), - Some(Rect::new(1, 1, 1, 2))); + assert_eq!( + layer.find_intersecting(&Rect::new(4, 2, 1, 2)), + Some(Rect::new(1, 0, 1, 2)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(4, 5, 1, 2)), + Some(Rect::new(1, 1, 1, 2)) + ); - assert_eq!(layer.find_intersecting(&Rect::new(7, 2, 1, 2)), - Some(Rect::new(2, 0, 1, 2))); - assert_eq!(layer.find_intersecting(&Rect::new(7, 5, 1, 2)), - Some(Rect::new(2, 1, 1, 2))); + assert_eq!( + layer.find_intersecting(&Rect::new(7, 2, 1, 2)), + Some(Rect::new(2, 0, 1, 2)) + ); + assert_eq!( + layer.find_intersecting(&Rect::new(7, 5, 1, 2)), + Some(Rect::new(2, 1, 1, 2)) + ); // exactly one tile. - assert_eq!(layer.find_intersecting(&Rect::new(0, 0, 3, 3)), - Some(Rect::new(0, 0, 1, 1))); + assert_eq!( + layer.find_intersecting(&Rect::new(0, 0, 3, 3)), + Some(Rect::new(0, 0, 1, 1)) + ); } } diff --git a/src/tiler.rs b/src/tiler.rs index 03128b0..db366e6 100644 --- a/src/tiler.rs +++ b/src/tiler.rs @@ -6,16 +6,16 @@ use sdl2::render::Texture; type TileId = usize; -pub struct Map { +pub struct Map<'a> { width: usize, height: usize, - sets: Vec, + sets: Vec>, layers: Vec, } -pub struct Set { - pub image: Texture, +pub struct Set<'a> { + pub image: Texture<'a>, pub width: usize, pub height: usize, pub tiles: Vec, diff --git a/src/timer.rs b/src/timer.rs index 53ad1d3..47d5722 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -6,7 +6,9 @@ pub struct Timer { impl Timer { pub fn new() -> Timer { - Timer { time: Instant::now() } + Timer { + time: Instant::now(), + } } pub fn current_time(&self) -> f64 {