diff --git a/src/image.rs b/src/image.rs index 26ee5b5..a0a77b9 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,5 +1,5 @@ use crate::vec2::Vec2; -use crate::Rect; +use crate::{HexMap, Rect}; use std::fs; use std::fs::File; use std::rc::Rc; @@ -55,6 +55,10 @@ impl Image { self.data.as_slice() } + //pub fn draw_hexmap(&mut self, pos: Vec2, hexmap: &HexMap) { + // for pos in Vec2::zero()..pos {} + //} + pub fn draw_image(&mut self, pos: Vec2, image: &Image) { self.draw_image_partial( pos, @@ -68,27 +72,16 @@ impl Image { pub fn draw_image_partial(&mut self, pos: Vec2, image: &Image, src_rect: Rect) { //todo: write proper implementation later - for y in 0..src_rect.size.y { - for x in 0..src_rect.size.x { - //todo: implement better(very stupid to do per pixel) - if x + pos.x >= 0 - && y + pos.y >= 0 - && x + pos.x < self.size.x - && y + pos.y < self.size.y - { - let p = image.get_pixel(Vec2 { - x: x + src_rect.pos.x, - y: y + src_rect.pos.y, - }); - if p > 0 { - self.set_pixel( - Vec2 { - x: x + pos.x, - y: y + pos.y, - }, - p, - ); - } + 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 + { + let p = image.get_pixel(i + src_rect.pos); + if p > 0 { + self.set_pixel(i + pos, p); } } } diff --git a/src/vec2.rs b/src/vec2.rs index fb0740d..b71612e 100644 --- a/src/vec2.rs +++ b/src/vec2.rs @@ -1,4 +1,9 @@ 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 { @@ -6,6 +11,12 @@ pub struct Vec2 { pub y: T, } +pub struct Vec2Iter { + start: Vec2, + end: Vec2, + current: Vec2, +} + impl Vec2 { //pub static pub fn zero() -> Self { @@ -16,6 +27,16 @@ impl Vec2 { } //pub + pub fn iter_to(self, other: Self) -> Vec2Iter { + Vec2Iter { + start: self, + end: other, + current: Vec2 { + x: self.x - T::one(), + y: self.y, + }, + } + } pub fn size(&self) -> usize { (self.x * self.y).to_usize().unwrap() } @@ -29,3 +50,61 @@ impl Default for Vec2 { } } } + +impl PartialEq for Vec2 { + fn eq(&self, other: &Self) -> bool { + self.x == other.x && self.y == other.y + } +} + +impl Iterator for Vec2Iter { + type Item = Vec2; + + fn next(&mut self) -> Option { + self.current.x = self.current.x + T::one(); + if self.current.x >= self.end.x { + self.current.y = self.current.y + T::one(); + self.current.x = self.start.x; + if self.current.y >= self.end.y { + return None; + } + } + Some(self.current) + } +} + +impl Add for Vec2 { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + Self { + x: self.x + rhs.x, + y: self.y + rhs.y, + } + } +} + +impl Sub for Vec2 { + type Output = Self; + + fn sub(self, rhs: Self) -> Self::Output { + Self { + x: self.x - rhs.x, + y: self.y - rhs.y, + } + } +} + +impl AddAssign for Vec2 { + fn add_assign(&mut self, rhs: Self) { + self.x += rhs.x; + self.y += rhs.y; + } +} + +impl SubAssign for Vec2 { + fn sub_assign(&mut self, rhs: Self) { + self.x -= rhs.x; + self.y -= rhs.y; + } +}