Upgrade SDL2 and SDL2 Image.
This commit is contained in:
35
Cargo.lock
generated
35
Cargo.lock
generated
@@ -2,9 +2,9 @@
|
|||||||
name = "super-matte-bros"
|
name = "super-matte-bros"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"sdl2 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"sdl2 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"sdl2_image 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"sdl2_image 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"time 0.1.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
"time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -23,13 +23,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kernel32-sys"
|
name = "kernel32-sys"
|
||||||
version = "0.1.4"
|
version = "0.2.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
"winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazy_static"
|
||||||
|
version = "0.1.16"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.1.10"
|
version = "0.1.10"
|
||||||
@@ -66,43 +71,43 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sdl2"
|
name = "sdl2"
|
||||||
version = "0.12.0"
|
version = "0.16.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)",
|
"num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"rand 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"rand 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"sdl2-sys 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"sdl2-sys 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sdl2-sys"
|
name = "sdl2-sys"
|
||||||
version = "0.7.0"
|
version = "0.8.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sdl2_image"
|
name = "sdl2_image"
|
||||||
version = "0.6.0"
|
version = "0.16.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"sdl2 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"sdl2 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"sdl2-sys 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"sdl2-sys 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "time"
|
name = "time"
|
||||||
version = "0.1.33"
|
version = "0.1.35"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"kernel32-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
"kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
"winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ version = "0.0.1"
|
|||||||
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
|
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
time = "0.1"
|
time = "0.1.35"
|
||||||
sdl2 = "0.12.0"
|
sdl2 = "0.16.0"
|
||||||
sdl2_image = "0.6.0"
|
sdl2_image = "0.16.0"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "super-matte-bros"
|
name = "super-matte-bros"
|
||||||
|
|||||||
@@ -40,7 +40,11 @@ impl Camera {
|
|||||||
self.y = y;
|
self.y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_relative_rect(&self, rect: &Rect) -> Rect {
|
||||||
|
Rect::new(rect.x() - self.x, rect.y() - self.y, rect.width(), rect.height())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn to_rect(&self) -> Rect {
|
pub fn to_rect(&self) -> Rect {
|
||||||
Rect::new_unwrap(self.x, self.y, self.width, self.height)
|
Rect::new(self.x, self.y, self.width, self.height)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
240
src/main.rs
240
src/main.rs
@@ -4,8 +4,9 @@ extern crate sdl2_image;
|
|||||||
|
|
||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::thread::sleep;
|
// use std::thread::sleep;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use sdl2_image::LoadTexture;
|
use sdl2_image::LoadTexture;
|
||||||
use sdl2::rect::Rect;
|
use sdl2::rect::Rect;
|
||||||
@@ -14,6 +15,7 @@ use sdl2::pixels::Color;
|
|||||||
use sdl2::render::{Renderer, Texture};
|
use sdl2::render::{Renderer, Texture};
|
||||||
|
|
||||||
|
|
||||||
|
// use timer::current_time;
|
||||||
use tile::Layer;
|
use tile::Layer;
|
||||||
use camera::Camera;
|
use camera::Camera;
|
||||||
// use player::Player;
|
// use player::Player;
|
||||||
@@ -21,6 +23,7 @@ use keyboard::KeyboardHandler;
|
|||||||
use sprite::{Sprite, StaticSprite, AnimatedSprite};
|
use sprite::{Sprite, StaticSprite, AnimatedSprite};
|
||||||
|
|
||||||
|
|
||||||
|
mod timer;
|
||||||
mod tile;
|
mod tile;
|
||||||
mod camera;
|
mod camera;
|
||||||
mod player;
|
mod player;
|
||||||
@@ -34,7 +37,7 @@ const SCREEN_HEIGHT : u32 = 640;
|
|||||||
const TILE_WIDTH : u32 = 32;
|
const TILE_WIDTH : u32 = 32;
|
||||||
const TILE_HEIGHT : u32 = 32;
|
const TILE_HEIGHT : u32 = 32;
|
||||||
|
|
||||||
const MS_PER_UPDATE : u64 = 10;
|
const MS_PER_UPDATE : f64 = 10.0;
|
||||||
|
|
||||||
const PLAYER_SPEED_X : f32 = 4.0;
|
const PLAYER_SPEED_X : f32 = 4.0;
|
||||||
const PLAYER_THRESHOLD_X : f32 = 0.2;
|
const PLAYER_THRESHOLD_X : f32 = 0.2;
|
||||||
@@ -53,13 +56,14 @@ struct GameObject<'a> {
|
|||||||
pub dy: f32,
|
pub dy: f32,
|
||||||
pub gravity: f32,
|
pub gravity: f32,
|
||||||
pub on_ground: bool,
|
pub on_ground: bool,
|
||||||
pub flip: (bool, bool),
|
pub flip_horizontal: bool,
|
||||||
physics: Box<PhysicsComponent + 'a>,
|
pub flip_vertical: bool,
|
||||||
graphics: Box<GraphicsComponent + 'a>
|
physics: Box<Updatable + 'a>,
|
||||||
|
graphics: Box<Renderable + 'a>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> GameObject<'a> {
|
impl<'a> GameObject<'a> {
|
||||||
pub fn new(x: f32, y: f32, physics: Box<PhysicsComponent + 'a>, graphics: Box<GraphicsComponent + 'a>) -> GameObject<'a> {
|
pub fn new(x: f32, y: f32, physics: Box<Updatable + 'a>, graphics: Box<Renderable + 'a>) -> GameObject<'a> {
|
||||||
GameObject {
|
GameObject {
|
||||||
x: x,
|
x: x,
|
||||||
y: y,
|
y: y,
|
||||||
@@ -69,48 +73,40 @@ impl<'a> GameObject<'a> {
|
|||||||
dy: 0.0,
|
dy: 0.0,
|
||||||
gravity: 0.3,
|
gravity: 0.3,
|
||||||
on_ground: false,
|
on_ground: false,
|
||||||
flip: (false, false),
|
flip_horizontal: false,
|
||||||
|
flip_vertical: false,
|
||||||
physics: physics,
|
physics: physics,
|
||||||
graphics: graphics
|
graphics: graphics
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&self, elapsed: u64) {
|
pub fn update(&self) {
|
||||||
self.physics.update(self, elapsed);
|
self.physics.update(self);
|
||||||
self.graphics.update(self, elapsed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&self, renderer: &mut Renderer, destination: &Rect) {
|
pub fn render(&self, elapsed: f64, renderer: &mut Renderer, destination: &Rect) {
|
||||||
self.physics.render(self, renderer, destination);
|
self.graphics.render(self, elapsed, renderer, destination);
|
||||||
self.graphics.render(self, renderer, destination);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_rect(&self) -> Rect {
|
pub fn to_rect(&self) -> Rect {
|
||||||
Rect::new_unwrap(self.x as i32, self.y as i32, 32, 32)
|
Rect::new(self.x as i32, self.y as i32, 32, 32)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trait Updatable {
|
||||||
|
fn update(&self, &GameObject);
|
||||||
|
}
|
||||||
|
|
||||||
trait PhysicsComponent {
|
trait Renderable {
|
||||||
fn update(&self, &GameObject, u64);
|
fn render(&self, &GameObject, f64, &mut Renderer, &Rect);
|
||||||
fn render(&self, &GameObject, &mut Renderer, &Rect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct PlayerPhysicsComponent;
|
struct PlayerPhysicsComponent;
|
||||||
|
|
||||||
impl PhysicsComponent for PlayerPhysicsComponent {
|
impl Updatable for PlayerPhysicsComponent {
|
||||||
fn update(&self, object: &GameObject, elapsed: u64) {
|
fn update(&self, object: &GameObject) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render(&self, object: &GameObject, renderer: &mut Renderer, destination: &Rect) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
trait GraphicsComponent {
|
|
||||||
fn update(&self, &GameObject, u64);
|
|
||||||
fn render(&self, &GameObject, &mut Renderer, &Rect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -121,22 +117,19 @@ struct PlayerGraphicsComponent<'a> {
|
|||||||
impl<'a> PlayerGraphicsComponent<'a> {
|
impl<'a> PlayerGraphicsComponent<'a> {
|
||||||
pub fn new(texture: &'a Texture) -> PlayerGraphicsComponent<'a> {
|
pub fn new(texture: &'a Texture) -> PlayerGraphicsComponent<'a> {
|
||||||
PlayerGraphicsComponent {
|
PlayerGraphicsComponent {
|
||||||
sprite_running: RefCell::new(AnimatedSprite::new(&texture, 96, 32, 3, 10))
|
sprite_running: RefCell::new(AnimatedSprite::new(&texture, 96, 32, 3, 10.0))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> GraphicsComponent for PlayerGraphicsComponent<'a> {
|
impl<'a> Renderable for PlayerGraphicsComponent<'a> {
|
||||||
fn update(&self, object: &GameObject, elapsed: u64) {
|
fn render(&self, object: &GameObject, elapsed: f64, renderer: &mut Renderer, destination: &Rect) {
|
||||||
self.sprite_running.borrow_mut().update(elapsed);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render(&self, object: &GameObject, renderer: &mut Renderer, destination: &Rect) {
|
|
||||||
let mut sprite = self.sprite_running.borrow_mut();
|
let mut sprite = self.sprite_running.borrow_mut();
|
||||||
|
|
||||||
sprite.flip = object.flip;
|
sprite.flip_horizontal = object.flip_horizontal;
|
||||||
|
sprite.flip_vertical = object.flip_vertical;
|
||||||
|
|
||||||
sprite.render(renderer, destination);
|
sprite.render(elapsed, renderer, destination);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,16 +142,31 @@ enum Tile<'a> {
|
|||||||
Floor(Rect)
|
Floor(Rect)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct TextureManager<'a> {
|
||||||
|
renderer: &'a mut Renderer<'a>,
|
||||||
|
textures: HashMap<&'static str, Texture>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TextureManager<'a> {
|
||||||
|
fn new(renderer: &'a mut Renderer<'a>) -> TextureManager<'a> {
|
||||||
|
TextureManager {
|
||||||
|
renderer: renderer,
|
||||||
|
textures: HashMap::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let sdl_context = sdl2::init().unwrap();
|
let sdl_context = sdl2::init().unwrap();
|
||||||
let video_subsystem = sdl_context.video().unwrap();
|
let video_subsystem = sdl_context.video().unwrap();
|
||||||
|
|
||||||
sdl2_image::init(sdl2_image::INIT_PNG);
|
let _image_context = sdl2_image::init(sdl2_image::INIT_PNG).unwrap();
|
||||||
|
|
||||||
let window = video_subsystem.window("Super Matte Bros", SCREEN_WIDTH, SCREEN_HEIGHT).position_centered().build().unwrap();
|
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 renderer = window.renderer().software().build().unwrap();
|
||||||
|
|
||||||
|
// let textures = TextureManager::new(&mut renderer);
|
||||||
|
|
||||||
let world_sprites = renderer.load_texture(&Path::new("gfx/world.png")).unwrap();
|
let world_sprites = renderer.load_texture(&Path::new("gfx/world.png")).unwrap();
|
||||||
|
|
||||||
let floor_sprite = StaticSprite::new(&world_sprites, 16 * 0, 16 * 0);
|
let floor_sprite = StaticSprite::new(&world_sprites, 16 * 0, 16 * 0);
|
||||||
@@ -173,100 +181,100 @@ fn main() {
|
|||||||
let mut layer = Layer::new(212, 20, TILE_WIDTH, TILE_HEIGHT, Tile::Empty);
|
let mut layer = Layer::new(212, 20, TILE_WIDTH, TILE_HEIGHT, Tile::Empty);
|
||||||
|
|
||||||
|
|
||||||
layer.set_tile(2, 15, Tile::Background(Rect::new_unwrap(16 * 9, 16 * 8, 16, 16)));
|
layer.set_tile(2, 15, Tile::Background(Rect::new(16 * 9, 16 * 8, 16, 16)));
|
||||||
|
|
||||||
layer.set_tile(1, 16, Tile::Background(Rect::new_unwrap(16 * 8, 16 * 8, 16, 16)));
|
layer.set_tile(1, 16, Tile::Background(Rect::new(16 * 8, 16 * 8, 16, 16)));
|
||||||
layer.set_tile(2, 16, Tile::Background(Rect::new_unwrap(16 * 8, 16 * 9, 16, 16)));
|
layer.set_tile(2, 16, Tile::Background(Rect::new(16 * 8, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(3, 16, Tile::Background(Rect::new_unwrap(16 * 10, 16 * 8, 16, 16)));
|
layer.set_tile(3, 16, Tile::Background(Rect::new(16 * 10, 16 * 8, 16, 16)));
|
||||||
|
|
||||||
layer.set_tile(0, 17, Tile::Background(Rect::new_unwrap(16 * 8, 16 * 8, 16, 16)));
|
layer.set_tile(0, 17, Tile::Background(Rect::new(16 * 8, 16 * 8, 16, 16)));
|
||||||
layer.set_tile(1, 17, Tile::Background(Rect::new_unwrap(16 * 8, 16 * 9, 16, 16)));
|
layer.set_tile(1, 17, Tile::Background(Rect::new(16 * 8, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(2, 17, Tile::Background(Rect::new_unwrap(16 * 9, 16 * 9, 16, 16)));
|
layer.set_tile(2, 17, Tile::Background(Rect::new(16 * 9, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(3, 17, Tile::Background(Rect::new_unwrap(16 * 8, 16 * 9, 16, 16)));
|
layer.set_tile(3, 17, Tile::Background(Rect::new(16 * 8, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(4, 17, Tile::Background(Rect::new_unwrap(16 * 10, 16 * 8, 16, 16)));
|
layer.set_tile(4, 17, Tile::Background(Rect::new(16 * 10, 16 * 8, 16, 16)));
|
||||||
|
|
||||||
|
|
||||||
layer.set_tile(11, 17, Tile::Background(Rect::new_unwrap(16 * 11, 16 * 9, 16, 16)));
|
layer.set_tile(11, 17, Tile::Background(Rect::new(16 * 11, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(12, 17, Tile::Background(Rect::new_unwrap(16 * 12, 16 * 9, 16, 16)));
|
layer.set_tile(12, 17, Tile::Background(Rect::new(16 * 12, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(13, 17, Tile::Background(Rect::new_unwrap(16 * 12, 16 * 9, 16, 16)));
|
layer.set_tile(13, 17, Tile::Background(Rect::new(16 * 12, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(14, 17, Tile::Background(Rect::new_unwrap(16 * 12, 16 * 9, 16, 16)));
|
layer.set_tile(14, 17, Tile::Background(Rect::new(16 * 12, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(15, 17, Tile::Background(Rect::new_unwrap(16 * 13, 16 * 9, 16, 16)));
|
layer.set_tile(15, 17, Tile::Background(Rect::new(16 * 13, 16 * 9, 16, 16)));
|
||||||
|
|
||||||
|
|
||||||
layer.set_tile(16, 14, Tile::Floor(Rect::new_unwrap(16 * 24, 16 * 0, 16, 16)));
|
layer.set_tile(16, 14, Tile::Floor(Rect::new(16 * 24, 16 * 0, 16, 16)));
|
||||||
|
|
||||||
|
|
||||||
layer.set_tile(17, 16, Tile::Background(Rect::new_unwrap(16 * 9, 16 * 8, 16, 16)));
|
layer.set_tile(17, 16, Tile::Background(Rect::new(16 * 9, 16 * 8, 16, 16)));
|
||||||
|
|
||||||
layer.set_tile(16, 17, Tile::Background(Rect::new_unwrap(16 * 8, 16 * 8, 16, 16)));
|
layer.set_tile(16, 17, Tile::Background(Rect::new(16 * 8, 16 * 8, 16, 16)));
|
||||||
layer.set_tile(17, 17, Tile::Background(Rect::new_unwrap(16 * 8, 16 * 9, 16, 16)));
|
layer.set_tile(17, 17, Tile::Background(Rect::new(16 * 8, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(18, 17, Tile::Background(Rect::new_unwrap(16 * 10, 16 * 8, 16, 16)));
|
layer.set_tile(18, 17, Tile::Background(Rect::new(16 * 10, 16 * 8, 16, 16)));
|
||||||
|
|
||||||
|
|
||||||
layer.set_tile(20, 14, Tile::Static(&brick_sprite, true));
|
layer.set_tile(20, 14, Tile::Static(&brick_sprite, true));
|
||||||
layer.set_tile(21, 14, Tile::Floor(Rect::new_unwrap(16 * 24, 16 * 0, 16, 16)));
|
layer.set_tile(21, 14, Tile::Floor(Rect::new(16 * 24, 16 * 0, 16, 16)));
|
||||||
layer.set_tile(22, 14, Tile::Static(&brick_sprite, true));
|
layer.set_tile(22, 14, Tile::Static(&brick_sprite, true));
|
||||||
layer.set_tile(23, 14, Tile::Floor(Rect::new_unwrap(16 * 24, 16 * 0, 16, 16)));
|
layer.set_tile(23, 14, Tile::Floor(Rect::new(16 * 24, 16 * 0, 16, 16)));
|
||||||
layer.set_tile(24, 14, Tile::Static(&brick_sprite, true));
|
layer.set_tile(24, 14, Tile::Static(&brick_sprite, true));
|
||||||
|
|
||||||
|
|
||||||
layer.set_tile(22, 10, Tile::Floor(Rect::new_unwrap(16 * 24, 16 * 0, 16, 16)));
|
layer.set_tile(22, 10, Tile::Floor(Rect::new(16 * 24, 16 * 0, 16, 16)));
|
||||||
|
|
||||||
|
|
||||||
layer.set_tile(19, 7, Tile::Floor(Rect::new_unwrap(16 * 0, 16 * 20, 16, 16)));
|
layer.set_tile(19, 7, Tile::Floor(Rect::new(16 * 0, 16 * 20, 16, 16)));
|
||||||
layer.set_tile(20, 7, Tile::Floor(Rect::new_unwrap(16 * 1, 16 * 20, 16, 16)));
|
layer.set_tile(20, 7, Tile::Floor(Rect::new(16 * 1, 16 * 20, 16, 16)));
|
||||||
layer.set_tile(21, 7, Tile::Floor(Rect::new_unwrap(16 * 2, 16 * 20, 16, 16)));
|
layer.set_tile(21, 7, Tile::Floor(Rect::new(16 * 2, 16 * 20, 16, 16)));
|
||||||
layer.set_tile(19, 8, Tile::Floor(Rect::new_unwrap(16 * 0, 16 * 21, 16, 16)));
|
layer.set_tile(19, 8, Tile::Floor(Rect::new(16 * 0, 16 * 21, 16, 16)));
|
||||||
layer.set_tile(20, 8, Tile::Floor(Rect::new_unwrap(16 * 1, 16 * 21, 16, 16)));
|
layer.set_tile(20, 8, Tile::Floor(Rect::new(16 * 1, 16 * 21, 16, 16)));
|
||||||
layer.set_tile(21, 8, Tile::Floor(Rect::new_unwrap(16 * 2, 16 * 21, 16, 16)));
|
layer.set_tile(21, 8, Tile::Floor(Rect::new(16 * 2, 16 * 21, 16, 16)));
|
||||||
|
|
||||||
|
|
||||||
layer.set_tile(23, 17, Tile::Background(Rect::new_unwrap(16 * 11, 16 * 9, 16, 16)));
|
layer.set_tile(23, 17, Tile::Background(Rect::new(16 * 11, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(24, 17, Tile::Background(Rect::new_unwrap(16 * 12, 16 * 9, 16, 16)));
|
layer.set_tile(24, 17, Tile::Background(Rect::new(16 * 12, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(25, 17, Tile::Background(Rect::new_unwrap(16 * 13, 16 * 9, 16, 16)));
|
layer.set_tile(25, 17, Tile::Background(Rect::new(16 * 13, 16 * 9, 16, 16)));
|
||||||
|
|
||||||
|
|
||||||
layer.set_tile(28, 16, Tile::Floor(Rect::new_unwrap(16 * 0, 16 * 8, 16, 16)));
|
layer.set_tile(28, 16, Tile::Floor(Rect::new(16 * 0, 16 * 8, 16, 16)));
|
||||||
layer.set_tile(29, 16, Tile::Floor(Rect::new_unwrap(16 * 1, 16 * 8, 16, 16)));
|
layer.set_tile(29, 16, Tile::Floor(Rect::new(16 * 1, 16 * 8, 16, 16)));
|
||||||
layer.set_tile(28, 17, Tile::Floor(Rect::new_unwrap(16 * 0, 16 * 9, 16, 16)));
|
layer.set_tile(28, 17, Tile::Floor(Rect::new(16 * 0, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(29, 17, Tile::Floor(Rect::new_unwrap(16 * 1, 16 * 9, 16, 16)));
|
layer.set_tile(29, 17, Tile::Floor(Rect::new(16 * 1, 16 * 9, 16, 16)));
|
||||||
|
|
||||||
|
|
||||||
layer.set_tile(38, 15, Tile::Floor(Rect::new_unwrap(16 * 0, 16 * 8, 16, 16)));
|
layer.set_tile(38, 15, Tile::Floor(Rect::new(16 * 0, 16 * 8, 16, 16)));
|
||||||
layer.set_tile(39, 15, Tile::Floor(Rect::new_unwrap(16 * 1, 16 * 8, 16, 16)));
|
layer.set_tile(39, 15, Tile::Floor(Rect::new(16 * 1, 16 * 8, 16, 16)));
|
||||||
layer.set_tile(38, 16, Tile::Floor(Rect::new_unwrap(16 * 0, 16 * 9, 16, 16)));
|
layer.set_tile(38, 16, Tile::Floor(Rect::new(16 * 0, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(39, 16, Tile::Floor(Rect::new_unwrap(16 * 1, 16 * 9, 16, 16)));
|
layer.set_tile(39, 16, Tile::Floor(Rect::new(16 * 1, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(38, 17, Tile::Floor(Rect::new_unwrap(16 * 0, 16 * 9, 16, 16)));
|
layer.set_tile(38, 17, Tile::Floor(Rect::new(16 * 0, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(39, 17, Tile::Floor(Rect::new_unwrap(16 * 1, 16 * 9, 16, 16)));
|
layer.set_tile(39, 17, Tile::Floor(Rect::new(16 * 1, 16 * 9, 16, 16)));
|
||||||
|
|
||||||
|
|
||||||
layer.set_tile(41, 17, Tile::Background(Rect::new_unwrap(16 * 11, 16 * 9, 16, 16)));
|
layer.set_tile(41, 17, Tile::Background(Rect::new(16 * 11, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(42, 17, Tile::Background(Rect::new_unwrap(16 * 12, 16 * 9, 16, 16)));
|
layer.set_tile(42, 17, Tile::Background(Rect::new(16 * 12, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(43, 17, Tile::Background(Rect::new_unwrap(16 * 12, 16 * 9, 16, 16)));
|
layer.set_tile(43, 17, Tile::Background(Rect::new(16 * 12, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(44, 17, Tile::Background(Rect::new_unwrap(16 * 13, 16 * 9, 16, 16)));
|
layer.set_tile(44, 17, Tile::Background(Rect::new(16 * 13, 16 * 9, 16, 16)));
|
||||||
|
|
||||||
|
|
||||||
layer.set_tile(46, 14, Tile::Floor(Rect::new_unwrap(16 * 0, 16 * 8, 16, 16)));
|
layer.set_tile(46, 14, Tile::Floor(Rect::new(16 * 0, 16 * 8, 16, 16)));
|
||||||
layer.set_tile(47, 14, Tile::Floor(Rect::new_unwrap(16 * 1, 16 * 8, 16, 16)));
|
layer.set_tile(47, 14, Tile::Floor(Rect::new(16 * 1, 16 * 8, 16, 16)));
|
||||||
layer.set_tile(46, 15, Tile::Floor(Rect::new_unwrap(16 * 0, 16 * 9, 16, 16)));
|
layer.set_tile(46, 15, Tile::Floor(Rect::new(16 * 0, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(47, 15, Tile::Floor(Rect::new_unwrap(16 * 1, 16 * 9, 16, 16)));
|
layer.set_tile(47, 15, Tile::Floor(Rect::new(16 * 1, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(46, 16, Tile::Floor(Rect::new_unwrap(16 * 0, 16 * 9, 16, 16)));
|
layer.set_tile(46, 16, Tile::Floor(Rect::new(16 * 0, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(47, 16, Tile::Floor(Rect::new_unwrap(16 * 1, 16 * 9, 16, 16)));
|
layer.set_tile(47, 16, Tile::Floor(Rect::new(16 * 1, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(46, 17, Tile::Floor(Rect::new_unwrap(16 * 0, 16 * 9, 16, 16)));
|
layer.set_tile(46, 17, Tile::Floor(Rect::new(16 * 0, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(47, 17, Tile::Floor(Rect::new_unwrap(16 * 1, 16 * 9, 16, 16)));
|
layer.set_tile(47, 17, Tile::Floor(Rect::new(16 * 1, 16 * 9, 16, 16)));
|
||||||
|
|
||||||
|
|
||||||
layer.set_tile(50, 15, Tile::Background(Rect::new_unwrap(16 * 9, 16 * 8, 16, 16)));
|
layer.set_tile(50, 15, Tile::Background(Rect::new(16 * 9, 16 * 8, 16, 16)));
|
||||||
|
|
||||||
layer.set_tile(49, 16, Tile::Background(Rect::new_unwrap(16 * 8, 16 * 8, 16, 16)));
|
layer.set_tile(49, 16, Tile::Background(Rect::new(16 * 8, 16 * 8, 16, 16)));
|
||||||
layer.set_tile(50, 16, Tile::Background(Rect::new_unwrap(16 * 8, 16 * 9, 16, 16)));
|
layer.set_tile(50, 16, Tile::Background(Rect::new(16 * 8, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(51, 16, Tile::Background(Rect::new_unwrap(16 * 10, 16 * 8, 16, 16)));
|
layer.set_tile(51, 16, Tile::Background(Rect::new(16 * 10, 16 * 8, 16, 16)));
|
||||||
|
|
||||||
layer.set_tile(48, 17, Tile::Background(Rect::new_unwrap(16 * 8, 16 * 8, 16, 16)));
|
layer.set_tile(48, 17, Tile::Background(Rect::new(16 * 8, 16 * 8, 16, 16)));
|
||||||
layer.set_tile(49, 17, Tile::Background(Rect::new_unwrap(16 * 8, 16 * 9, 16, 16)));
|
layer.set_tile(49, 17, Tile::Background(Rect::new(16 * 8, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(50, 17, Tile::Background(Rect::new_unwrap(16 * 9, 16 * 9, 16, 16)));
|
layer.set_tile(50, 17, Tile::Background(Rect::new(16 * 9, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(51, 17, Tile::Background(Rect::new_unwrap(16 * 8, 16 * 9, 16, 16)));
|
layer.set_tile(51, 17, Tile::Background(Rect::new(16 * 8, 16 * 9, 16, 16)));
|
||||||
layer.set_tile(52, 17, Tile::Background(Rect::new_unwrap(16 * 10, 16 * 8, 16, 16)));
|
layer.set_tile(52, 17, Tile::Background(Rect::new(16 * 10, 16 * 8, 16, 16)));
|
||||||
|
|
||||||
|
|
||||||
for x in 0..212 {
|
for x in 0..212 {
|
||||||
@@ -277,15 +285,15 @@ fn main() {
|
|||||||
|
|
||||||
let mut camera = Camera::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, layer.to_rect());
|
let mut camera = Camera::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, layer.to_rect());
|
||||||
|
|
||||||
let mut current : u64;
|
let mut current : f64;
|
||||||
let mut elapsed : u64;
|
let mut elapsed : f64;
|
||||||
let mut previous : u64 = time::precise_time_ns() / 1_000_000;
|
let mut previous : f64 = timer::current_time();
|
||||||
let mut lag : u64 = 0;
|
let mut lag : f64 = 0.0;
|
||||||
|
|
||||||
let mut event_pump = sdl_context.event_pump().unwrap();
|
let mut event_pump = sdl_context.event_pump().unwrap();
|
||||||
|
|
||||||
'main : loop {
|
'main : loop {
|
||||||
current = time::precise_time_ns() / 1_000_000;
|
current = timer::current_time();
|
||||||
elapsed = current - previous;
|
elapsed = current - previous;
|
||||||
previous = current;
|
previous = current;
|
||||||
lag += elapsed;
|
lag += elapsed;
|
||||||
@@ -321,7 +329,7 @@ fn main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
player.dx = a * PLAYER_SPEED_X + (1.0 - a) * player.dx;
|
player.dx = a * PLAYER_SPEED_X + (1.0 - a) * player.dx;
|
||||||
player.flip = (false, false);
|
player.flip_horizontal = false;
|
||||||
} else if keyboard.is_held(Keycode::Left) && (player.dx <= 0.0 || player.on_ground) {
|
} else if keyboard.is_held(Keycode::Left) && (player.dx <= 0.0 || player.on_ground) {
|
||||||
let a = if player.dx < 0.0 {
|
let a = if player.dx < 0.0 {
|
||||||
PLAYER_ACCELERATION_X_START
|
PLAYER_ACCELERATION_X_START
|
||||||
@@ -330,7 +338,7 @@ fn main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
player.dx = a * -PLAYER_SPEED_X + (1.0 - a) * player.dx;
|
player.dx = a * -PLAYER_SPEED_X + (1.0 - a) * player.dx;
|
||||||
player.flip = (true, false);
|
player.flip_horizontal = true;
|
||||||
} else if player.on_ground {
|
} else if player.on_ground {
|
||||||
player.dx = (1.0 - PLAYER_ACCELERATION_X_STOP) * player.dx;
|
player.dx = (1.0 - PLAYER_ACCELERATION_X_STOP) * player.dx;
|
||||||
|
|
||||||
@@ -518,7 +526,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
player.update(elapsed);
|
player.update();
|
||||||
|
|
||||||
camera.center(&player.to_rect());
|
camera.center(&player.to_rect());
|
||||||
|
|
||||||
@@ -529,7 +537,7 @@ fn main() {
|
|||||||
renderer.clear();
|
renderer.clear();
|
||||||
|
|
||||||
layer.for_each_intersecting(&camera.to_rect(), |tile: &Tile, position: &Rect| {
|
layer.for_each_intersecting(&camera.to_rect(), |tile: &Tile, position: &Rect| {
|
||||||
let object = camera_relative_rect(&camera.to_rect(), position);
|
let object = camera.to_relative_rect(position);
|
||||||
|
|
||||||
match *tile {
|
match *tile {
|
||||||
Tile::Background(src) => {
|
Tile::Background(src) => {
|
||||||
@@ -538,23 +546,17 @@ fn main() {
|
|||||||
Tile::Floor(src) => {
|
Tile::Floor(src) => {
|
||||||
renderer.copy(&world_sprites, Some(src), Some(object));
|
renderer.copy(&world_sprites, Some(src), Some(object));
|
||||||
},
|
},
|
||||||
Tile::Static(ref sprite, _) => sprite.render(&mut renderer, &object),
|
Tile::Static(ref sprite, _) => sprite.render(lag / MS_PER_UPDATE, &mut renderer, &object),
|
||||||
_ => ()
|
_ => ()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let player_rect = camera_relative_rect(&camera.to_rect(), &player.to_rect());
|
let player_rect = camera.to_relative_rect(&player.to_rect());
|
||||||
|
|
||||||
player.render(&mut renderer, &player_rect);
|
player.render(lag / MS_PER_UPDATE, &mut renderer, &player_rect);
|
||||||
|
|
||||||
renderer.present();
|
renderer.present();
|
||||||
|
|
||||||
sleep(std::time::Duration::from_millis(5));
|
// sleep(std::time::Duration::from_millis(5));
|
||||||
}
|
}
|
||||||
|
|
||||||
sdl2_image::quit();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn camera_relative_rect(camera: &Rect, other: &Rect) -> Rect {
|
|
||||||
Rect::new_unwrap(other.x() - camera.x(), other.y() - camera.y(), other.width(), other.height())
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,6 @@ impl Player {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_rect(&self) -> Rect {
|
pub fn to_rect(&self) -> Rect {
|
||||||
Rect::new_unwrap(self.x as i32, self.y as i32, self.w, self.h)
|
Rect::new(self.x as i32, self.y as i32, self.w, self.h)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
use sdl2::render::{Texture, Renderer};
|
use sdl2::render::{Texture, Renderer};
|
||||||
use sdl2::rect::Rect;
|
use sdl2::rect::Rect;
|
||||||
|
|
||||||
|
|
||||||
pub trait Sprite {
|
pub trait Sprite {
|
||||||
fn update(&mut self, u64) {}
|
fn render(&self, f64, &mut Renderer, &Rect);
|
||||||
fn render(&self, &mut Renderer, &Rect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -12,7 +13,8 @@ pub struct StaticSprite<'a> {
|
|||||||
texture: &'a Texture,
|
texture: &'a Texture,
|
||||||
x: i32,
|
x: i32,
|
||||||
y: i32,
|
y: i32,
|
||||||
pub flip: (bool, bool)
|
pub flip_horizontal: bool,
|
||||||
|
pub flip_vertical: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> StaticSprite<'a> {
|
impl<'a> StaticSprite<'a> {
|
||||||
@@ -21,14 +23,15 @@ impl<'a> StaticSprite<'a> {
|
|||||||
texture: texture,
|
texture: texture,
|
||||||
x: x,
|
x: x,
|
||||||
y: y,
|
y: y,
|
||||||
flip: (false, false)
|
flip_horizontal: false,
|
||||||
|
flip_vertical: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Sprite for StaticSprite<'a> {
|
impl<'a> Sprite for StaticSprite<'a> {
|
||||||
fn render(&self, drawer: &mut Renderer, destination: &Rect) {
|
fn render(&self, elapsed: f64, drawer: &mut Renderer, destination: &Rect) {
|
||||||
drawer.copy_ex(self.texture, Some(Rect::new_unwrap(self.x, self.y, 16, 16)), Some(*destination), 0.0, None, self.flip);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,39 +40,42 @@ pub struct AnimatedSprite<'a> {
|
|||||||
texture: &'a Texture,
|
texture: &'a Texture,
|
||||||
x: i32,
|
x: i32,
|
||||||
y: i32,
|
y: i32,
|
||||||
pub flip: (bool, bool),
|
pub flip_horizontal: bool,
|
||||||
frame: u32,
|
pub flip_vertical: bool,
|
||||||
frames: u32,
|
frames: u32,
|
||||||
time: u64,
|
frame_time: f64,
|
||||||
frame_time: u64
|
frame: RefCell<u32>,
|
||||||
|
time: RefCell<f64>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> AnimatedSprite<'a> {
|
impl<'a> AnimatedSprite<'a> {
|
||||||
pub fn new(texture: &'a Texture, x: i32, y: i32, frames: u32, fps: u32) -> AnimatedSprite<'a> {
|
pub fn new(texture: &'a Texture, x: i32, y: i32, frames: u32, fps: f32) -> AnimatedSprite<'a> {
|
||||||
AnimatedSprite {
|
AnimatedSprite {
|
||||||
texture: texture,
|
texture: texture,
|
||||||
x: x,
|
x: x,
|
||||||
y: y,
|
y: y,
|
||||||
flip: (false, false),
|
flip_horizontal: false,
|
||||||
frame: 0,
|
flip_vertical: false,
|
||||||
|
frame: RefCell::new(0),
|
||||||
frames: frames,
|
frames: frames,
|
||||||
time: 0,
|
time: RefCell::new(0.0),
|
||||||
frame_time: 1000 / fps as u64
|
frame_time: 1000.0 / fps as f64
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Sprite for AnimatedSprite<'a> {
|
impl<'a> Sprite for AnimatedSprite<'a> {
|
||||||
fn update(&mut self, elapsed: u64) {
|
fn render(&self, elapsed: f64, drawer: &mut Renderer, destination: &Rect) {
|
||||||
self.time += elapsed;
|
let mut time = self.time.borrow_mut();
|
||||||
self.frame += (self.time / self.frame_time) as u32;
|
let mut frame = self.frame.borrow_mut();
|
||||||
self.frame %= self.frames;
|
|
||||||
self.time %= self.frame_time;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render(&self, drawer: &mut Renderer, destination: &Rect) {
|
*time += 10.0 + elapsed;
|
||||||
let x = self.x + (self.frame * 16) as i32;
|
*frame += (*time / self.frame_time) as u32;
|
||||||
|
*time %= self.frame_time;
|
||||||
|
*frame %= self.frames;
|
||||||
|
|
||||||
drawer.copy_ex(self.texture, Some(Rect::new_unwrap(x, self.y, 16, 16)), Some(*destination), 0.0, None, self.flip);
|
let x = self.x + (*frame * 16) as i32;
|
||||||
|
|
||||||
|
drawer.copy_ex(self.texture, Some(Rect::new(x, self.y, 16, 16)), Some(*destination), 0.0, None, self.flip_horizontal, self.flip_vertical);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
70
src/tile.rs
70
src/tile.rs
@@ -52,14 +52,14 @@ impl<T> Layer<T> where T: Clone {
|
|||||||
let x2 = (rect.x() + rect.width() as i32 - 1) / self.tile_width as i32;
|
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;
|
let y2 = (rect.y() + rect.height() as i32 - 1) / self.tile_height as i32;
|
||||||
|
|
||||||
Some(Rect::new_unwrap(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<F: FnMut(&T, &Rect)>(&self, rect: &Rect, mut f: F) {
|
pub fn for_each_intersecting<F: FnMut(&T, &Rect)>(&self, rect: &Rect, mut f: F) {
|
||||||
if let Some(intersect) = self.find_intersecting(rect) {
|
if let Some(intersect) = self.find_intersecting(rect) {
|
||||||
for y in intersect.y()..(intersect.y() + intersect.height() as i32) {
|
for y in intersect.y()..(intersect.y() + intersect.height() as i32) {
|
||||||
for x in intersect.x()..(intersect.x() + intersect.width() as i32) {
|
for x in intersect.x()..(intersect.x() + intersect.width() as i32) {
|
||||||
let position = Rect::new_unwrap(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).unwrap(), &position);
|
f(self.get_tile(x, y).unwrap(), &position);
|
||||||
}
|
}
|
||||||
@@ -68,7 +68,7 @@ impl<T> Layer<T> where T: Clone {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_rect(&self) -> Rect {
|
pub fn to_rect(&self) -> Rect {
|
||||||
Rect::new_unwrap(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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,54 +84,54 @@ mod tests {
|
|||||||
let layer = Layer::new(3, 3, 3, 3, ());
|
let layer = Layer::new(3, 3, 3, 3, ());
|
||||||
|
|
||||||
// out of bounds.
|
// out of bounds.
|
||||||
assert_eq!(layer.find_intersecting(&Rect::new_unwrap(-1, 1, 1, 1)), None);
|
assert_eq!(layer.find_intersecting(&Rect::new(-1, 1, 1, 1)), None);
|
||||||
assert_eq!(layer.find_intersecting(&Rect::new_unwrap( 1, -1, 1, 1)), None);
|
assert_eq!(layer.find_intersecting(&Rect::new( 1, -1, 1, 1)), None);
|
||||||
assert_eq!(layer.find_intersecting(&Rect::new_unwrap(-1, -1, 1, 1)), None);
|
assert_eq!(layer.find_intersecting(&Rect::new(-1, -1, 1, 1)), None);
|
||||||
|
|
||||||
assert_eq!(layer.find_intersecting(&Rect::new_unwrap(9, 7, 1, 1)), None);
|
assert_eq!(layer.find_intersecting(&Rect::new(9, 7, 1, 1)), None);
|
||||||
assert_eq!(layer.find_intersecting(&Rect::new_unwrap(7, 9, 1, 1)), None);
|
assert_eq!(layer.find_intersecting(&Rect::new(7, 9, 1, 1)), None);
|
||||||
assert_eq!(layer.find_intersecting(&Rect::new_unwrap(9, 9, 1, 1)), None);
|
assert_eq!(layer.find_intersecting(&Rect::new(9, 9, 1, 1)), None);
|
||||||
|
|
||||||
// middle of tile.
|
// middle of tile.
|
||||||
assert_eq!(layer.find_intersecting(&Rect::new_unwrap(1, 1, 1, 1)), Some(Rect::new_unwrap(0, 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_unwrap(4, 1, 1, 1)), Some(Rect::new_unwrap(1, 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_unwrap(7, 1, 1, 1)), Some(Rect::new_unwrap(2, 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_unwrap(1, 4, 1, 1)), Some(Rect::new_unwrap(0, 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_unwrap(4, 4, 1, 1)), Some(Rect::new_unwrap(1, 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_unwrap(7, 4, 1, 1)), Some(Rect::new_unwrap(2, 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_unwrap(1, 7, 1, 1)), Some(Rect::new_unwrap(0, 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_unwrap(4, 7, 1, 1)), Some(Rect::new_unwrap(1, 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_unwrap(7, 7, 1, 1)), Some(Rect::new_unwrap(2, 2, 1, 1)));
|
assert_eq!(layer.find_intersecting(&Rect::new(7, 7, 1, 1)), Some(Rect::new(2, 2, 1, 1)));
|
||||||
|
|
||||||
// interlaps 4 tiles.
|
// interlaps 4 tiles.
|
||||||
assert_eq!(layer.find_intersecting(&Rect::new_unwrap(2, 2, 2, 2)), Some(Rect::new_unwrap(0, 0, 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_unwrap(5, 2, 2, 2)), Some(Rect::new_unwrap(1, 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_unwrap(2, 5, 2, 2)), Some(Rect::new_unwrap(0, 1, 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_unwrap(5, 5, 2, 2)), Some(Rect::new_unwrap(1, 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.
|
// interlaps 2 tiles horizontal.
|
||||||
assert_eq!(layer.find_intersecting(&Rect::new_unwrap(2, 1, 2, 1)), Some(Rect::new_unwrap(0, 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_unwrap(5, 1, 2, 1)), Some(Rect::new_unwrap(1, 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_unwrap(2, 4, 2, 1)), Some(Rect::new_unwrap(0, 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_unwrap(5, 4, 2, 1)), Some(Rect::new_unwrap(1, 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_unwrap(2, 7, 2, 1)), Some(Rect::new_unwrap(0, 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_unwrap(5, 7, 2, 1)), Some(Rect::new_unwrap(1, 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.
|
// interlaps 2 tiles vertical.
|
||||||
assert_eq!(layer.find_intersecting(&Rect::new_unwrap(1, 2, 1, 2)), Some(Rect::new_unwrap(0, 0, 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_unwrap(1, 5, 1, 2)), Some(Rect::new_unwrap(0, 1, 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_unwrap(4, 2, 1, 2)), Some(Rect::new_unwrap(1, 0, 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_unwrap(4, 5, 1, 2)), Some(Rect::new_unwrap(1, 1, 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_unwrap(7, 2, 1, 2)), Some(Rect::new_unwrap(2, 0, 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_unwrap(7, 5, 1, 2)), Some(Rect::new_unwrap(2, 1, 1, 2)));
|
assert_eq!(layer.find_intersecting(&Rect::new(7, 5, 1, 2)), Some(Rect::new(2, 1, 1, 2)));
|
||||||
|
|
||||||
// exactly one tile.
|
// exactly one tile.
|
||||||
assert_eq!(layer.find_intersecting(&Rect::new_unwrap(0, 0, 3, 3)), Some(Rect::new_unwrap(0, 0, 1, 1)));
|
assert_eq!(layer.find_intersecting(&Rect::new(0, 0, 3, 3)), Some(Rect::new(0, 0, 1, 1)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
5
src/timer.rs
Normal file
5
src/timer.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
extern crate time;
|
||||||
|
|
||||||
|
pub fn current_time() -> f64 {
|
||||||
|
(time::precise_time_ns() / 1_000_000) as f64
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user