Add basic resources module

This commit is contained in:
SeanOMik 2023-04-15 00:01:35 -04:00
parent 3fe294b8b2
commit 9b570a68e3
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
4 changed files with 34 additions and 1 deletions

View File

@ -2,6 +2,7 @@ mod system;
mod game; mod game;
mod renderer; mod renderer;
mod input_event; mod input_event;
mod resources;
use game::Game; use game::Game;
use specs::*; use specs::*;

View File

@ -1,4 +1,5 @@
use std::sync::Arc; use std::sync::Arc;
use std::borrow::Cow;
use async_trait::async_trait; use async_trait::async_trait;
@ -6,6 +7,8 @@ use async_trait::async_trait;
//use winit::window::Window; //use winit::window::Window;
use winit::{window::Window, event::WindowEvent}; use winit::{window::Window, event::WindowEvent};
use crate::resources;
#[async_trait] #[async_trait]
pub trait Renderer { pub trait Renderer {
//fn new(surface: wgpu::Surface, config: wgpu::SurfaceConfiguration, device: wgpu::Device) //fn new(surface: wgpu::Surface, config: wgpu::SurfaceConfiguration, device: wgpu::Device)
@ -84,9 +87,10 @@ impl BasicRenderer {
}; };
surface.configure(&device, &config); 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 { let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("Shader"), 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 // Create a render pipeline. At some point this will be created by something else and given to the renderer

28
src/resources.rs Normal file
View File

@ -0,0 +1,28 @@
use cfg_if::cfg_if;
/// Load bytes from a file.
pub async fn load_bytes(file_name: &str) -> anyhow::Result<Vec<u8>> {
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<String> {
let bytes = load_bytes(file_name).await?;
Ok(String::from(std::str::from_utf8(&bytes)?))
}