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
|
//todo: write proper implementation later
|
||||||
for i in Vec2::zero().iter_to(src_rect.size) {
|
for i in Vec2::zero().iter_to(src_rect.size) {
|
||||||
//todo: implement better(very stupid to do per pixel)
|
//todo: implement better(very stupid to do per pixel)
|
||||||
if i.x + pos.x >= 0
|
if self.size.to_rect().contains(i + pos) {
|
||||||
&& i.y + pos.y >= 0
|
|
||||||
&& i.x + pos.x < self.size.x
|
|
||||||
&& i.y + pos.y < self.size.y
|
|
||||||
{
|
|
||||||
let p = image.get_pixel(i + src_rect.pos);
|
let p = image.get_pixel(i + src_rect.pos);
|
||||||
if p > 0 {
|
if p > 0 {
|
||||||
self.set_pixel(i + pos, p);
|
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};
|
use num::{Num, ToPrimitive};
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct Rect<T: Num + Copy + ToPrimitive> {
|
pub struct Rect<T: Num + Copy + ToPrimitive + PartialOrd> {
|
||||||
pub pos: Vec2<T>,
|
pub pos: Vec2<T>,
|
||||||
pub size: 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> {
|
pub fn new(x: T, y: T, w: T, h: T) -> Rect<T> {
|
||||||
Rect {
|
Rect {
|
||||||
pos: Vec2 { x, y },
|
pos: Vec2 { x, y },
|
||||||
size: Vec2 { x: w, y: h },
|
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,
|
x: img.size().x / tile_count.x,
|
||||||
y: img.size().y / tile_count.y,
|
y: img.size().y / tile_count.y,
|
||||||
};
|
};
|
||||||
for y in 0..tile_count.y {
|
for tile in Vec2::zero().iter_to(tile_count) {
|
||||||
for x in 0..tile_count.x {
|
|
||||||
let mut image = Image::new(size);
|
let mut image = Image::new(size);
|
||||||
image.draw_image_partial(
|
image.draw_image_partial(
|
||||||
Vec2::zero(),
|
Vec2::zero(),
|
||||||
&img,
|
&img,
|
||||||
Rect {
|
Rect {
|
||||||
pos: Vec2 {
|
pos: Vec2 {
|
||||||
x: x * size.x,
|
x: tile.x * size.x,
|
||||||
y: y * size.y,
|
y: tile.y * size.y,
|
||||||
},
|
},
|
||||||
size,
|
size,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
images.push(image);
|
images.push(image);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Tileset {
|
Tileset {
|
||||||
count: images.len() as i32,
|
count: images.len() as i32,
|
||||||
size,
|
size,
|
||||||
|
@ -48,4 +46,8 @@ impl Tileset {
|
||||||
pub fn get(&self, idx: i32) -> &Image {
|
pub fn get(&self, idx: i32) -> &Image {
|
||||||
&self.images[idx as usize]
|
&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 num::{Num, ToPrimitive};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::cmp::Ordering::{Greater, Less};
|
use std::cmp::Ordering::{Greater, Less};
|
||||||
use std::iter::Step;
|
|
||||||
use std::ops::{Add, AddAssign, Range, Sub, SubAssign};
|
use std::ops::{Add, AddAssign, Range, Sub, SubAssign};
|
||||||
use Ordering::Equal;
|
use Ordering::Equal;
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct Vec2<T: Num + Copy + ToPrimitive> {
|
pub struct Vec2<T: Num + Copy + ToPrimitive + PartialOrd> {
|
||||||
pub x: T,
|
pub x: T,
|
||||||
pub y: T,
|
pub y: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Vec2Iter<T: Num + Copy + ToPrimitive> {
|
pub struct Vec2Iter<T: Num + Copy + ToPrimitive + PartialOrd> {
|
||||||
start: Vec2<T>,
|
start: Vec2<T>,
|
||||||
end: Vec2<T>,
|
end: Vec2<T>,
|
||||||
current: Vec2<T>,
|
current: Vec2<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Num + Copy + ToPrimitive> Vec2<T> {
|
impl<T: Num + Copy + ToPrimitive + PartialOrd> Vec2<T> {
|
||||||
//pub static
|
//pub static
|
||||||
pub fn zero() -> Self {
|
pub fn zero() -> Self {
|
||||||
Vec2 {
|
Vec2 {
|
||||||
|
@ -37,12 +37,20 @@ impl<T: Num + Copy + ToPrimitive> Vec2<T> {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn size(&self) -> usize {
|
pub fn size(&self) -> usize {
|
||||||
(self.x * self.y).to_usize().unwrap()
|
(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 {
|
fn default() -> Self {
|
||||||
Vec2 {
|
Vec2 {
|
||||||
x: T::default(),
|
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 {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
self.x == other.x && self.y == other.y
|
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;
|
type Output = Self;
|
||||||
|
|
||||||
fn add(self, rhs: Self) -> Self::Output {
|
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;
|
type Output = Self;
|
||||||
|
|
||||||
fn sub(self, rhs: Self) -> Self::Output {
|
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) {
|
fn add_assign(&mut self, rhs: Self) {
|
||||||
self.x += rhs.x;
|
self.x += rhs.x;
|
||||||
self.y += rhs.y;
|
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) {
|
fn sub_assign(&mut self, rhs: Self) {
|
||||||
self.x -= rhs.x;
|
self.x -= rhs.x;
|
||||||
self.y -= rhs.y;
|
self.y -= rhs.y;
|
||||||
|
|
Loading…
Reference in New Issue