upgrade to rust alpha 1.0.0

This commit is contained in:
2015-01-12 12:51:13 +01:00
parent 2d53866272
commit 1c9af73baa
4 changed files with 223 additions and 140 deletions

View File

@@ -1,7 +1,7 @@
use std::iter::repeat;
use std::cmp::{min, max};
use sdl2::rect::Rect;
use sdl2::rect::{Point, Rect};
pub struct Layer<T> {
@@ -15,7 +15,7 @@ pub struct Layer<T> {
impl<T> Layer<T> where T: Clone {
pub fn new(width: i32, height: i32, tile_width: i32, tile_height: i32, tile: T) -> Layer<T> {
Layer {
tiles: repeat(tile).take((width * height) as uint).collect(),
tiles: repeat(tile).take((width * height) as usize).collect(),
width: width,
height: height,
tile_width: tile_width,
@@ -24,41 +24,44 @@ impl<T> Layer<T> where T: Clone {
}
pub fn get_tile(&self, x: i32, y: i32) -> &T {
let offset = (x + y * self.width) as uint;
let offset = (x + y * self.width) as usize;
&self.tiles[offset]
}
pub fn set_tile(&mut self, x: i32, y: i32, tile: T) {
let offset = (x + y * self.width) as uint;
let offset = (x + y * self.width) as usize;
self.tiles[offset] = tile;
}
pub fn find_intersecting(&self, rect: &Rect) -> Rect {
pub fn find_intersecting(&self, rect: &Rect) -> Option<Rect> {
let x1 = max(rect.x / self.tile_width, 0);
let y1 = max(rect.y / self.tile_height, 0);
let x2 = min((rect.x + rect.w - 1) / self.tile_width, self.width - 1);
let y2 = min((rect.y + rect.h - 1) / self.tile_height, self.height - 1);
Rect::new(x1, y1, x2 - x1, y2 - y1)
println!("{}, {}, {}, {}", x1, y1, x2, y2);
if x1 < 0 || x2 >= self.width {
None
}
else if y1 < 0 || y2 >= self.height {
None
}
else {
Some(Rect::new(x1, y1, x2 - x1, y2 - y1))
}
}
pub fn for_each_intersecting<F>(&self, rect: &Rect, mut f: F) where F: FnMut(&T, &Rect) {
let intersect = self.find_intersecting(rect);
pub fn for_each_intersecting<F: FnMut(&T, &Rect)>(&self, rect: &Rect, mut f: F) {
if let Some(intersect) = self.find_intersecting(rect) {
for y in range(intersect.y, intersect.y + intersect.h + 1) {
for x in range(intersect.x, intersect.x + intersect.w + 1) {
let position = Rect::new(x * self.tile_width, y * self.tile_height, self.tile_width, self.tile_height);
if intersect.x < 0 || intersect.x + intersect.w > self.width {
return;
}
else if intersect.y < 0 || intersect.y + intersect.h > self.height {
return;
}
for y in range(intersect.y, intersect.y + intersect.h + 1) {
for x in range(intersect.x, intersect.x + intersect.w + 1) {
let position = Rect::new(x * self.tile_width, y * self.tile_height, self.tile_width, self.tile_height);
f(self.get_tile(x, y), &position);
f(self.get_tile(x, y), &position);
}
}
}
}