75 lines
2.6 KiB
Rust
75 lines
2.6 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use anymap::AnyMap;
|
|
use glium::{texture::SrgbTexture2d, Display, Program};
|
|
use glium_glyph::{glyph_brush::rusttype::Font, GlyphBrush};
|
|
use glob::glob;
|
|
|
|
pub type Textures = HashMap<String, SrgbTexture2d>;
|
|
pub type Shaders = HashMap<String, Program>;
|
|
|
|
// Set up and load all global resources.
|
|
pub fn load_resources(display: &Display, resources: &mut AnyMap) {
|
|
// Load images
|
|
log::info!("Loading textures");
|
|
let mut textures: Textures = HashMap::new();
|
|
for entry in glob("assets/textures/**/*.png").unwrap() {
|
|
use image::io::Reader;
|
|
|
|
if let Ok(path) = entry {
|
|
let image = Reader::open(&path).unwrap().decode().unwrap().to_rgba8();
|
|
let image_dimensions = image.dimensions();
|
|
|
|
let image = glium::texture::RawImage2d::from_raw_rgba_reversed(
|
|
&image.into_raw(),
|
|
image_dimensions,
|
|
);
|
|
let texture = SrgbTexture2d::new(display, image).unwrap();
|
|
|
|
let name_path = path
|
|
.with_extension("")
|
|
.strip_prefix("assets/textures/")
|
|
.unwrap()
|
|
.to_owned();
|
|
let mut name = String::new();
|
|
for part in name_path.iter() {
|
|
name.push_str(part.to_str().unwrap());
|
|
name.push(':');
|
|
}
|
|
name.pop();
|
|
|
|
textures.insert(name.to_string(), texture);
|
|
log::info!("Loaded texture {:?} from {:?}.", name, path);
|
|
}
|
|
}
|
|
resources.insert(textures);
|
|
|
|
// Load shaders
|
|
log::info!("Loading shaders.");
|
|
let mut shaders: HashMap<String, Program> = HashMap::new();
|
|
for entry in glob("assets/shaders/*").unwrap() {
|
|
if let Ok(path) = entry {
|
|
// Operate only on directories
|
|
if path.is_file() {
|
|
continue;
|
|
}
|
|
|
|
let vert_source = std::fs::read_to_string(path.join("vert.glsl")).unwrap();
|
|
let frag_source = std::fs::read_to_string(path.join("frag.glsl")).unwrap();
|
|
let program = Program::from_source(display, &vert_source, &frag_source, None).unwrap();
|
|
|
|
let name = path.file_name().unwrap().to_str().unwrap().to_owned();
|
|
shaders.insert(name.to_string(), program);
|
|
log::info!("Loaded shader {:?} from {:?}.", name, path);
|
|
}
|
|
}
|
|
resources.insert(shaders);
|
|
|
|
// Set up font stuff
|
|
let dejavu = std::fs::read("assets/fonts/DejaVuSans.ttf").unwrap();
|
|
let fonts = vec![Font::from_bytes(dejavu).unwrap()];
|
|
let glyph_brush = GlyphBrush::new(display, fonts);
|
|
log::info!("Set up font.");
|
|
resources.insert(glyph_brush);
|
|
}
|