From 4238ea033a2b44ada5cc0a0b91de07220d11bcee Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Tue, 18 May 2021 00:05:39 +0100 Subject: [PATCH] music; load palettes --- src/lib.rs | 36 +++++++++++++++++++++++++----------- src/music.rs | 18 ++++++++++++++++++ 2 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 src/music.rs diff --git a/src/lib.rs b/src/lib.rs index 134fce7..211eedb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,16 +1,17 @@ use std::fs; use std::path::PathBuf; -use serde_derive::{Serialize, Deserialize}; -use crate::config::Config; mod colour; mod config; mod image; mod mock; +mod music; mod palette; mod scene; pub use colour::Colour; +pub use config::Config; +pub use music::Music; pub use palette::Palette; pub use scene::Scene; @@ -100,12 +101,12 @@ pub struct Position { // } // } -#[derive(Serialize, Deserialize)] pub struct Game { config: Config, -// palettes: Vec, + palettes: Vec, // variables: Vec, // triggers: HashMap, + music: Vec, } #[derive(Debug)] @@ -115,17 +116,34 @@ impl Game { pub fn from(path: String) -> Result { let path = PathBuf::from(path); + let mut music = Vec::new(); + let mut palettes = Vec::new(); + + let mut music_dir = path.clone(); + music_dir.push("music"); + + let music_files = music_dir.read_dir(); + + if music_files.is_ok() { + for file in music_files.unwrap() { + let file = file.unwrap(); + music.push(Music::from_file(file.path())); + println!("music found: {:?}", file.file_name()); + } + } + let mut palettes_dir = path.clone(); palettes_dir.push("palettes"); + let palette_files = palettes_dir.read_dir() - .expect("couldn't find any palettes"); + .expect("couldn't read palettes dir"); for file in palette_files { let file = file.unwrap(); + palettes.push(Palette::from_file(file.path())); println!("palette found: {:?}", file.file_name()); } - // todo load config let mut game_config = path.clone(); game_config.push("game.toml"); let config = fs::read_to_string(game_config) @@ -133,10 +151,6 @@ impl Game { let config: Config = toml::from_str(&config) .expect("Couldn't parse game config"); - Ok( - Game { - config, - } - ) + Ok(Game { config, palettes, music }) } } diff --git a/src/music.rs b/src/music.rs new file mode 100644 index 0000000..f335b52 --- /dev/null +++ b/src/music.rs @@ -0,0 +1,18 @@ +use std::fs; +use std::path::PathBuf; + +/// an XM music file. +/// name is derived from the file stem. +pub struct Music { + pub name: String, + pub bytes: Vec, +} + +impl Music { + pub fn from_file(path: PathBuf) -> Self { + Self { + name: path.file_stem().unwrap().to_str().unwrap().into(), + bytes: fs::read(path).unwrap() + } + } +}