Compare commits

..

2 Commits

Author SHA1 Message Date
dani efe476fdda added some traits to Vec2 2023-07-02 11:21:50 +00:00
dani 707a71b432 lifetimes are too hard, assets are now ref-counted 2023-07-02 08:54:24 +00:00
4 changed files with 105 additions and 28 deletions

View File

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

View File

@ -1,7 +1,8 @@
use crate::vec2::Vec2;
use crate::Rect;
use crate::{HexMap, Rect};
use std::fs;
use std::fs::File;
use std::rc::Rc;
//todo: make dynamically different bitdepths
pub struct Image {
@ -17,11 +18,11 @@ impl Image {
}
}
pub fn load(path: &str) -> Self {
pub fn load(path: &str) -> Rc<Self> {
Self::load_data(fs::read(path).unwrap().as_slice())
}
pub fn load_data(data: &[u8]) -> Self {
pub fn load_data(data: &[u8]) -> Rc<Self> {
let mut options = gif::DecodeOptions::new();
options.set_color_output(gif::ColorOutput::Indexed);
let mut decoder = options.read_info(data).unwrap();
@ -39,6 +40,7 @@ impl Image {
y: y as i32,
},
}
.into()
}
pub fn clear(&mut self) {
@ -53,6 +55,10 @@ impl Image {
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) {
self.draw_image_partial(
pos,
@ -66,27 +72,16 @@ impl Image {
pub fn draw_image_partial(&mut self, pos: Vec2<i32>, image: &Image, src_rect: Rect<i32>) {
//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);
}
}
}

View File

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

View File

@ -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<T: Num + Copy + ToPrimitive> {
@ -6,6 +11,12 @@ pub struct Vec2<T: Num + Copy + ToPrimitive> {
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> {
//pub static
pub fn zero() -> Self {
@ -16,6 +27,16 @@ impl<T: Num + Copy + ToPrimitive> Vec2<T> {
}
//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 {
(self.x * self.y).to_usize().unwrap()
}
@ -29,3 +50,61 @@ 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;
}
}