music; load palettes

This commit is contained in:
Max Bradbury 2021-05-18 00:05:39 +01:00
parent 5bb6760568
commit 4238ea033a
2 changed files with 43 additions and 11 deletions

View File

@ -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<Palette>,
palettes: Vec<Palette>,
// variables: Vec<Variable>,
// triggers: HashMap<String, ScriptCollection>,
music: Vec<Music>,
}
#[derive(Debug)]
@ -115,17 +116,34 @@ impl Game {
pub fn from(path: String) -> Result<Game, GameParseError> {
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 })
}
}

18
src/music.rs Normal file
View File

@ -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<u8>,
}
impl Music {
pub fn from_file(path: PathBuf) -> Self {
Self {
name: path.file_stem().unwrap().to_str().unwrap().into(),
bytes: fs::read(path).unwrap()
}
}
}