Compare commits

..

No commits in common. "efe476fddad907dc1fa07401111abb7b878febe0" and "150233728c892cefbb7f32566773388114005d84" have entirely different histories.

4 changed files with 28 additions and 105 deletions

View File

@ -1,12 +1,11 @@
use skunk2d::*; use skunk2d::*;
use std::rc::Rc;
const WIDTH: i32 = 1920 / 3; const WIDTH: i32 = 1920 / 3;
const HEIGHT: i32 = 1080 / 3; const HEIGHT: i32 = 1080 / 3;
struct World { struct World {
img: Rc<Image>, img: Image,
font: Rc<Tileset>, font: Tileset,
pos: Vec2<i32>, pos: Vec2<i32>,
} }

View File

@ -1,8 +1,7 @@
use crate::vec2::Vec2; use crate::vec2::Vec2;
use crate::{HexMap, Rect}; use crate::Rect;
use std::fs; use std::fs;
use std::fs::File; use std::fs::File;
use std::rc::Rc;
//todo: make dynamically different bitdepths //todo: make dynamically different bitdepths
pub struct Image { pub struct Image {
@ -18,11 +17,11 @@ impl Image {
} }
} }
pub fn load(path: &str) -> Rc<Self> { pub fn load(path: &str) -> Self {
Self::load_data(fs::read(path).unwrap().as_slice()) Self::load_data(fs::read(path).unwrap().as_slice())
} }
pub fn load_data(data: &[u8]) -> Rc<Self> { pub fn load_data(data: &[u8]) -> Self {
let mut options = gif::DecodeOptions::new(); let mut options = gif::DecodeOptions::new();
options.set_color_output(gif::ColorOutput::Indexed); options.set_color_output(gif::ColorOutput::Indexed);
let mut decoder = options.read_info(data).unwrap(); let mut decoder = options.read_info(data).unwrap();
@ -40,7 +39,6 @@ impl Image {
y: y as i32, y: y as i32,
}, },
} }
.into()
} }
pub fn clear(&mut self) { pub fn clear(&mut self) {
@ -55,10 +53,6 @@ impl Image {
self.data.as_slice() self.data.as_slice()
} }
//pub fn draw_hexmap(&mut self, pos: Vec2<i32>, hexmap: &HexMap) {
// for pos in Vec2::zero()..pos {}
//}
pub fn draw_image(&mut self, pos: Vec2<i32>, image: &Image) { pub fn draw_image(&mut self, pos: Vec2<i32>, image: &Image) {
self.draw_image_partial( self.draw_image_partial(
pos, pos,
@ -72,16 +66,27 @@ impl Image {
pub fn draw_image_partial(&mut self, pos: Vec2<i32>, image: &Image, src_rect: Rect<i32>) { pub fn draw_image_partial(&mut self, pos: Vec2<i32>, image: &Image, src_rect: Rect<i32>) {
//todo: write proper implementation later //todo: write proper implementation later
for i in Vec2::zero().iter_to(src_rect.size) { for y in 0..src_rect.size.y {
//todo: implement better(very stupid to do per pixel) for x in 0..src_rect.size.x {
if i.x + pos.x >= 0 //todo: implement better(very stupid to do per pixel)
&& i.y + pos.y >= 0 if x + pos.x >= 0
&& i.x + pos.x < self.size.x && y + pos.y >= 0
&& i.y + pos.y < self.size.y && x + pos.x < self.size.x
{ && y + pos.y < self.size.y
let p = image.get_pixel(i + src_rect.pos); {
if p > 0 { let p = image.get_pixel(Vec2 {
self.set_pixel(i + pos, p); 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,
);
}
} }
} }
} }

View File

@ -1,6 +1,5 @@
use crate::{Image, Rect, Vec2}; use crate::{Image, Rect, Vec2};
use std::fs; use std::fs;
use std::rc::Rc;
pub struct Tileset { pub struct Tileset {
count: i32, count: i32,
@ -9,11 +8,11 @@ pub struct Tileset {
} }
impl Tileset { impl Tileset {
pub fn load(path: &str, tile_count: Vec2<i32>) -> Rc<Self> { pub fn load(path: &str, tile_count: Vec2<i32>) -> Self {
Self::load_data(fs::read(path).unwrap().as_slice(), tile_count) Self::load_data(fs::read(path).unwrap().as_slice(), tile_count)
} }
pub fn load_data(data: &[u8], tile_count: Vec2<i32>) -> Rc<Self> { pub fn load_data(data: &[u8], tile_count: Vec2<i32>) -> Self {
let img = Image::load_data(data); let img = Image::load_data(data);
let mut images: Vec<Image> = vec![]; let mut images: Vec<Image> = vec![];
let size = Vec2 { let size = Vec2 {
@ -42,7 +41,6 @@ impl Tileset {
size, size,
images, images,
} }
.into()
} }
pub fn get(&self, idx: i32) -> &Image { pub fn get(&self, idx: i32) -> &Image {

View File

@ -1,9 +1,4 @@
use num::{Num, ToPrimitive}; 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)] #[derive(Copy, Clone)]
pub struct Vec2<T: Num + Copy + ToPrimitive> { pub struct Vec2<T: Num + Copy + ToPrimitive> {
@ -11,12 +6,6 @@ pub struct Vec2<T: Num + Copy + ToPrimitive> {
pub y: T, pub y: T,
} }
pub struct Vec2Iter<T: Num + Copy + ToPrimitive> {
start: Vec2<T>,
end: Vec2<T>,
current: Vec2<T>,
}
impl<T: Num + Copy + ToPrimitive> Vec2<T> { impl<T: Num + Copy + ToPrimitive> Vec2<T> {
//pub static //pub static
pub fn zero() -> Self { pub fn zero() -> Self {
@ -27,16 +16,6 @@ impl<T: Num + Copy + ToPrimitive> Vec2<T> {
} }
//pub //pub
pub fn iter_to(self, other: Self) -> Vec2Iter<T> {
Vec2Iter {
start: self,
end: other,
current: Vec2 {
x: self.x - T::one(),
y: self.y,
},
}
}
pub fn size(&self) -> usize { pub fn size(&self) -> usize {
(self.x * self.y).to_usize().unwrap() (self.x * self.y).to_usize().unwrap()
} }
@ -50,61 +29,3 @@ impl<T: Num + Copy + ToPrimitive + Default> Default for Vec2<T> {
} }
} }
} }
impl<T: Num + Copy + ToPrimitive> PartialEq for Vec2<T> {
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y
}
}
impl<T: Num + Copy + ToPrimitive + PartialOrd> Iterator for Vec2Iter<T> {
type Item = Vec2<T>;
fn next(&mut self) -> Option<Self::Item> {
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<T: Num + Copy + ToPrimitive> Add for Vec2<T> {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}
impl<T: Num + Copy + ToPrimitive> Sub for Vec2<T> {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
impl<T: Num + Copy + ToPrimitive + 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> {
fn sub_assign(&mut self, rhs: Self) {
self.x -= rhs.x;
self.y -= rhs.y;
}
}