lifetimes are too hard, assets are now ref-counted

This commit is contained in:
dani 2023-07-02 08:54:24 +00:00
parent 150233728c
commit 707a71b432
3 changed files with 11 additions and 6 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

@ -2,6 +2,7 @@ use crate::vec2::Vec2;
use crate::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) {

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 {