strafe-project/src/legacy.rs

34 lines
1.1 KiB
Rust
Raw Normal View History

2024-02-13 08:13:04 +00:00
use std::io::Read;
use std::collections::HashMap;
use crate::texture_loader::{Texture,Textures};
use strafesnet_common::model::TextureId;
pub struct TextureLoader{
texture_names:HashMap<String,TextureId>,
}
impl TextureLoader{
pub fn new()->Self{
Self{
texture_names:HashMap::new(),
}
}
}
impl crate::texture_loader::TextureLoaderTrait for TextureLoader{
type Error=std::io::Error;
fn acquire_id(&mut self,name:&str)->TextureId{
let texture_id=TextureId::new(self.texture_names.len() as u32);
*self.texture_names.entry(name.to_owned()).or_insert(texture_id)
}
fn load(&self)->Result<Textures,Self::Error>{
let mut texture_data=vec![Vec::<u8>::new();self.texture_names.len()];
for (texture_name,texture_id) in &self.texture_names{
let path=std::path::PathBuf::from(format!("textures/{}.dds",texture_name.as_str()));
if let Ok(mut file)=std::fs::File::open(path){
//TODO: parallel
file.read_to_end(texture_data.get_mut(texture_id.get() as usize).unwrap())?;
}
}
Ok(Textures::new(texture_data.into_iter().map(Texture::ImageDDS).collect()))
}
}