2024-02-14 03:48:53 +00:00
|
|
|
use std::io::Read;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use crate::texture::{Texture,Textures};
|
|
|
|
use strafesnet_common::model::{MeshId,TextureId};
|
|
|
|
|
|
|
|
pub struct TextureLoader{
|
2024-02-14 22:45:10 +00:00
|
|
|
texture_paths:HashMap<Box<str>,TextureId>,
|
2024-02-14 03:48:53 +00:00
|
|
|
}
|
|
|
|
impl TextureLoader{
|
|
|
|
pub fn acquire_texture_id(&mut self,name:&str)->TextureId{
|
|
|
|
let texture_id=TextureId::new(self.texture_paths.len() as u32);
|
|
|
|
*self.texture_paths.entry(name.into()).or_insert(texture_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub struct MeshLoader{
|
2024-02-14 22:45:10 +00:00
|
|
|
mesh_paths:HashMap<Box<str>,MeshId>,
|
2024-02-14 03:48:53 +00:00
|
|
|
}
|
|
|
|
impl MeshLoader{
|
|
|
|
pub fn acquire_mesh_id(&mut self,name:&str)->MeshId{
|
|
|
|
let texture_id=MeshId::new(self.mesh_paths.len() as u32);
|
|
|
|
*self.mesh_paths.entry(name.into()).or_insert(texture_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Loader{
|
|
|
|
texture_loader:TextureLoader,
|
|
|
|
mesh_loader:MeshLoader,
|
|
|
|
}
|
|
|
|
impl Loader{
|
|
|
|
pub fn new()->Self{
|
|
|
|
Self{
|
|
|
|
texture_loader:TextureLoader{texture_paths:HashMap::new()},
|
|
|
|
mesh_loader:MeshLoader{mesh_paths:HashMap::new()},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn get_inner_mut(&mut self)->(&mut TextureLoader,&mut MeshLoader){
|
|
|
|
(&mut self.texture_loader,&mut self.mesh_loader)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Loader{
|
|
|
|
pub fn load_textures(&self)->Result<Textures,std::io::Error>{
|
2024-02-14 08:35:31 +00:00
|
|
|
let mut texture_data=vec![Vec::<u8>::new();self.texture_loader.texture_paths.len()];
|
|
|
|
for (texture_path,texture_id) in &self.texture_loader.texture_paths{
|
2024-02-14 22:45:10 +00:00
|
|
|
let path=std::path::PathBuf::from(format!("textures/{}.dds",texture_path));
|
2024-02-14 08:35:31 +00:00
|
|
|
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()))
|
2024-02-14 03:48:53 +00:00
|
|
|
}
|
|
|
|
//load_meshes should look like load_textures
|
|
|
|
/*
|
|
|
|
pub fn load_meshes(&mut self)->Result<Meshes,std::io::Error>{
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|