From 3be9730b52920d74d50d7edbdc4f26348817655f Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 13 Feb 2024 02:30:16 -0800 Subject: [PATCH] allow texture loading failure --- src/graphics.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/graphics.rs b/src/graphics.rs index e3ee6ad9..be53af18 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -149,8 +149,8 @@ impl GraphicsState{ } pub fn generate_models(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,map:&map::CompleteMap,textures:strafesnet_texture_loader::texture_loader::Textures){ //generate texture view per texture - let texture_views:Vec=textures.into_iter().map(|(texture_id,texture_data)|{ - let image=ddsfile::Dds::read(std::io::Cursor::new(texture_data)).unwrap(); + let texture_views:HashMap=textures.into_iter().filter_map(|(texture_id,texture_data)|{ + let image=ddsfile::Dds::read(std::io::Cursor::new(texture_data)).ok()?; let (mut width,mut height)=(image.get_width(),image.get_height()); @@ -192,11 +192,11 @@ impl GraphicsState{ wgpu::util::TextureDataOrder::LayerMajor, &image.data, ); - texture.create_view(&wgpu::TextureViewDescriptor{ + Some((texture_id,texture.create_view(&wgpu::TextureViewDescriptor{ label:Some(format!("Texture{} View",texture_id.get()).as_str()), dimension:Some(wgpu::TextureViewDimension::D2), ..wgpu::TextureViewDescriptor::default() - }) + }))) }).collect(); let num_textures=texture_views.len(); @@ -488,10 +488,9 @@ impl GraphicsState{ usage:wgpu::BufferUsages::UNIFORM|wgpu::BufferUsages::COPY_DST, }); let render_config=&map.render_configs[model.render_config.get() as usize]; - let texture_view=match render_config.texture{ - Some(texture_id)=>&texture_views[texture_id.get() as usize], - None=>&self.temp_squid_texture_view, - }; + let texture_view=render_config.texture.and_then(|texture_id| + texture_views.get(&texture_id) + ).unwrap_or(&self.temp_squid_texture_view); let bind_group=device.create_bind_group(&wgpu::BindGroupDescriptor{ layout:&self.bind_group_layouts.model, entries:&[