From d264935ca3d172e5a711afdda695af69dd1d5413 Mon Sep 17 00:00:00 2001 From: dani Date: Sun, 2 Jul 2023 12:01:07 +0000 Subject: [PATCH] made vec2 and rect nicer :) --- src/image.rs | 6 +----- src/rect.rs | 13 +++++++++++-- src/tileset.rs | 32 +++++++++++++++++--------------- src/vec2.rs | 28 ++++++++++++++++++---------- 4 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/image.rs b/src/image.rs index a0a77b9..936e22d 100644 --- a/src/image.rs +++ b/src/image.rs @@ -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); diff --git a/src/rect.rs b/src/rect.rs index f30fad8..cb7fbd0 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -2,16 +2,25 @@ use crate::vec2::Vec2; use num::{Num, ToPrimitive}; #[derive(Copy, Clone)] -pub struct Rect { +pub struct Rect { pub pos: Vec2, pub size: Vec2, } -impl Rect { +impl Rect { pub fn new(x: T, y: T, w: T, h: T) -> Rect { Rect { pos: Vec2 { x, y }, size: Vec2 { x: w, y: h }, } } + + pub fn pos2(&self) -> Vec2 { + self.pos + self.size + } + + pub fn contains(&self, point: Vec2) -> bool { + let p2 = self.pos2(); + point.x >= self.pos.x && point.y >= self.pos.y && point.x < p2.x && point.y < p2.y + } } diff --git a/src/tileset.rs b/src/tileset.rs index e928f8e..b267109 100644 --- a/src/tileset.rs +++ b/src/tileset.rs @@ -20,22 +20,20 @@ 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 { - let mut image = Image::new(size); - image.draw_image_partial( - Vec2::zero(), - &img, - Rect { - pos: Vec2 { - x: x * size.x, - y: y * size.y, - }, - size, + 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: tile.x * size.x, + y: tile.y * size.y, }, - ); - images.push(image); - } + size, + }, + ); + images.push(image); } Tileset { count: images.len() as i32, @@ -48,4 +46,8 @@ impl Tileset { pub fn get(&self, idx: i32) -> &Image { &self.images[idx as usize] } + + pub fn tile_size(&self) -> Vec2 { + self.size + } } diff --git a/src/vec2.rs b/src/vec2.rs index b71612e..1f2522b 100644 --- a/src/vec2.rs +++ b/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 { +pub struct Vec2 { pub x: T, pub y: T, } -pub struct Vec2Iter { +pub struct Vec2Iter { start: Vec2, end: Vec2, current: Vec2, } -impl Vec2 { +impl Vec2 { //pub static pub fn zero() -> Self { Vec2 { @@ -37,12 +37,20 @@ impl Vec2 { }, } } + pub fn size(&self) -> usize { (self.x * self.y).to_usize().unwrap() } + + pub fn to_rect(self) -> Rect { + Rect { + pos: Self::zero(), + size: self, + } + } } -impl Default for Vec2 { +impl Default for Vec2 { fn default() -> Self { Vec2 { x: T::default(), @@ -51,7 +59,7 @@ impl Default for Vec2 { } } -impl PartialEq for Vec2 { +impl PartialEq for Vec2 { fn eq(&self, other: &Self) -> bool { self.x == other.x && self.y == other.y } @@ -73,7 +81,7 @@ impl Iterator for Vec2Iter { } } -impl Add for Vec2 { +impl Add for Vec2 { type Output = Self; fn add(self, rhs: Self) -> Self::Output { @@ -84,7 +92,7 @@ impl Add for Vec2 { } } -impl Sub for Vec2 { +impl Sub for Vec2 { type Output = Self; fn sub(self, rhs: Self) -> Self::Output { @@ -95,14 +103,14 @@ impl Sub for Vec2 { } } -impl AddAssign for Vec2 { +impl AddAssign for Vec2 { fn add_assign(&mut self, rhs: Self) { self.x += rhs.x; self.y += rhs.y; } } -impl SubAssign for Vec2 { +impl SubAssign for Vec2 { fn sub_assign(&mut self, rhs: Self) { self.x -= rhs.x; self.y -= rhs.y;