diff --git a/src/shader.wgsl b/res/shader.wgsl similarity index 100% rename from src/shader.wgsl rename to res/shader.wgsl diff --git a/src/main.rs b/src/main.rs index 0b95b4e..4abedf9 100755 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod system; mod game; mod renderer; mod input_event; +mod resources; use game::Game; use specs::*; diff --git a/src/renderer.rs b/src/renderer.rs index 6700a55..f0e4f7a 100755 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -1,4 +1,5 @@ use std::sync::Arc; +use std::borrow::Cow; use async_trait::async_trait; @@ -6,6 +7,8 @@ use async_trait::async_trait; //use winit::window::Window; use winit::{window::Window, event::WindowEvent}; +use crate::resources; + #[async_trait] pub trait Renderer { //fn new(surface: wgpu::Surface, config: wgpu::SurfaceConfiguration, device: wgpu::Device) @@ -84,9 +87,10 @@ impl BasicRenderer { }; surface.configure(&device, &config); + let shader_src = resources::load_string("shader.wgsl").await.expect("Failed to load shader!"); let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("Shader"), - source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()), + source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(&shader_src)), }); // Create a render pipeline. At some point this will be created by something else and given to the renderer diff --git a/src/resources.rs b/src/resources.rs new file mode 100644 index 0000000..05e1dfd --- /dev/null +++ b/src/resources.rs @@ -0,0 +1,28 @@ +use cfg_if::cfg_if; + +/// Load bytes from a file. +pub async fn load_bytes(file_name: &str) -> anyhow::Result> { + cfg_if! { + if #[cfg(target_arch = "wasm32")] { + let url = format_url(file_name); + let bytes = reqwest::get(url) + .await? + .bytes() + .await?; + } else { + let out_dir = std::env::var("OUT_DIR").unwrap_or(String::new()); + let path = std::path::Path::new(&out_dir) + .join("res") + .join(file_name); + let bytes = std::fs::read(path)?; + } + } + + Ok(bytes) +} + +/// Load the string content from a file. +pub async fn load_string(file_name: &str) -> anyhow::Result { + let bytes = load_bytes(file_name).await?; + Ok(String::from(std::str::from_utf8(&bytes)?)) +} \ No newline at end of file