made vec2 and rect nicer :)
This commit is contained in:
parent
efe476fdda
commit
d264935ca3
|
@ -74,11 +74,7 @@ impl Image {
|
|||
//todo: write proper implementation later
|
||||
for i in Vec2::zero().iter_to(src_rect.size) {
|
||||
//todo: implement better(very stupid to do per pixel)
|
||||
if i.x + pos.x >= 0
|
||||
&& i.y + pos.y >= 0
|
||||
&& i.x + pos.x < self.size.x
|
||||
&& i.y + pos.y < self.size.y
|
||||
{
|
||||
if self.size.to_rect().contains(i + pos) {
|
||||
let p = image.get_pixel(i + src_rect.pos);
|
||||
if p > 0 {
|
||||
self.set_pixel(i + pos, p);
|
||||
|
|
13
src/rect.rs
13
src/rect.rs
|
@ -2,16 +2,25 @@ use crate::vec2::Vec2;
|
|||
use num::{Num, ToPrimitive};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Rect<T: Num + Copy + ToPrimitive> {
|
||||
pub struct Rect<T: Num + Copy + ToPrimitive + PartialOrd> {
|
||||
pub pos: Vec2<T>,
|
||||
pub size: Vec2<T>,
|
||||
}
|
||||
|
||||
impl<T: Num + Copy + ToPrimitive> Rect<T> {
|
||||
impl<T: Num + Copy + ToPrimitive + PartialOrd> Rect<T> {
|
||||
pub fn new(x: T, y: T, w: T, h: T) -> Rect<T> {
|
||||
Rect {
|
||||
pos: Vec2 { x, y },
|
||||
size: Vec2 { x: w, y: h },
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pos2(&self) -> Vec2<T> {
|
||||
self.pos + self.size
|
||||
}
|
||||
|
||||
pub fn contains(&self, point: Vec2<T>) -> bool {
|
||||
let p2 = self.pos2();
|
||||
point.x >= self.pos.x && point.y >= self.pos.y && point.x < p2.x && point.y < p2.y
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,23 +20,21 @@ impl Tileset {
|
|||
x: img.size().x / tile_count.x,
|
||||
y: img.size().y / tile_count.y,
|
||||
};
|
||||
for y in 0..tile_count.y {
|
||||
for x in 0..tile_count.x {
|
||||
for tile in Vec2::zero().iter_to(tile_count) {
|
||||
let mut image = Image::new(size);
|
||||
image.draw_image_partial(
|
||||
Vec2::zero(),
|
||||
&img,
|
||||
Rect {
|
||||
pos: Vec2 {
|
||||
x: x * size.x,
|
||||
y: y * size.y,
|
||||
x: tile.x * size.x,
|
||||
y: tile.y * size.y,
|
||||
},
|
||||
size,
|
||||
},
|
||||
);
|
||||
images.push(image);
|
||||
}
|
||||
}
|
||||
Tileset {
|
||||
count: images.len() as i32,
|
||||
size,
|
||||
|
@ -48,4 +46,8 @@ impl Tileset {
|
|||
pub fn get(&self, idx: i32) -> &Image {
|
||||
&self.images[idx as usize]
|
||||
}
|
||||
|
||||
pub fn tile_size(&self) -> Vec2<i32> {
|
||||
self.size
|
||||
}
|
||||
}
|
||||
|
|
28
src/vec2.rs
28
src/vec2.rs
|
@ -1,23 +1,23 @@
|
|||
use crate::Rect;
|
||||
use num::{Num, ToPrimitive};
|
||||
use std::cmp::Ordering;
|
||||
use std::cmp::Ordering::{Greater, Less};
|
||||
use std::iter::Step;
|
||||
use std::ops::{Add, AddAssign, Range, Sub, SubAssign};
|
||||
use Ordering::Equal;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Vec2<T: Num + Copy + ToPrimitive> {
|
||||
pub struct Vec2<T: Num + Copy + ToPrimitive + PartialOrd> {
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
}
|
||||
|
||||
pub struct Vec2Iter<T: Num + Copy + ToPrimitive> {
|
||||
pub struct Vec2Iter<T: Num + Copy + ToPrimitive + PartialOrd> {
|
||||
start: Vec2<T>,
|
||||
end: Vec2<T>,
|
||||
current: Vec2<T>,
|
||||
}
|
||||
|
||||
impl<T: Num + Copy + ToPrimitive> Vec2<T> {
|
||||
impl<T: Num + Copy + ToPrimitive + PartialOrd> Vec2<T> {
|
||||
//pub static
|
||||
pub fn zero() -> Self {
|
||||
Vec2 {
|
||||
|
@ -37,12 +37,20 @@ impl<T: Num + Copy + ToPrimitive> Vec2<T> {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn size(&self) -> usize {
|
||||
(self.x * self.y).to_usize().unwrap()
|
||||
}
|
||||
|
||||
pub fn to_rect(self) -> Rect<T> {
|
||||
Rect {
|
||||
pos: Self::zero(),
|
||||
size: self,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Num + Copy + ToPrimitive + Default> Default for Vec2<T> {
|
||||
impl<T: Num + Copy + ToPrimitive + PartialOrd + Default> Default for Vec2<T> {
|
||||
fn default() -> Self {
|
||||
Vec2 {
|
||||
x: T::default(),
|
||||
|
@ -51,7 +59,7 @@ impl<T: Num + Copy + ToPrimitive + Default> Default for Vec2<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Num + Copy + ToPrimitive> PartialEq for Vec2<T> {
|
||||
impl<T: Num + Copy + ToPrimitive + PartialOrd> PartialEq for Vec2<T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.x == other.x && self.y == other.y
|
||||
}
|
||||
|
@ -73,7 +81,7 @@ impl<T: Num + Copy + ToPrimitive + PartialOrd> Iterator for Vec2Iter<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Num + Copy + ToPrimitive> Add for Vec2<T> {
|
||||
impl<T: Num + Copy + ToPrimitive + PartialOrd> Add for Vec2<T> {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
|
@ -84,7 +92,7 @@ impl<T: Num + Copy + ToPrimitive> Add for Vec2<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Num + Copy + ToPrimitive> Sub for Vec2<T> {
|
||||
impl<T: Num + Copy + ToPrimitive + PartialOrd> Sub for Vec2<T> {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self::Output {
|
||||
|
@ -95,14 +103,14 @@ impl<T: Num + Copy + ToPrimitive> Sub for Vec2<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Num + Copy + ToPrimitive + AddAssign> AddAssign for Vec2<T> {
|
||||
impl<T: Num + Copy + ToPrimitive + PartialOrd + AddAssign> AddAssign for Vec2<T> {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
self.x += rhs.x;
|
||||
self.y += rhs.y;
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Num + Copy + ToPrimitive + SubAssign> SubAssign for Vec2<T> {
|
||||
impl<T: Num + Copy + ToPrimitive + PartialOrd + SubAssign> SubAssign for Vec2<T> {
|
||||
fn sub_assign(&mut self, rhs: Self) {
|
||||
self.x -= rhs.x;
|
||||
self.y -= rhs.y;
|
||||
|
|
Loading…
Reference in New Issue