added music theory

This commit is contained in:
gallant 2022-10-01 14:15:43 -05:00
parent 7c98b6f830
commit 8658fabc8e
2 changed files with 22 additions and 2 deletions

View File

@ -14,6 +14,9 @@ exclude = ["src/main.rs"]
version = "0.35.2"
features = ["image", "ttf"]
[dependencies]
rodio = "0.15.0"
[features]
default = []
static = ["sdl2/static-link", "sdl2/bundled"]

View File

@ -100,7 +100,7 @@ use std::{
path::Path,
slice::IterMut,
};
use std::{thread, time, process::Command};
use sdl2::{
image::ImageRWops,
mouse::MouseButton,
@ -112,7 +112,9 @@ use sdl2::{
video::{Window, WindowBuildError, WindowContext},
EventPump, IntegerOrSdlError,
};
use std::fs::File;
use std::io::BufReader;
use rodio::{Decoder, OutputStream, source::Source};
use vec2::Vec2Int;
#[doc(no_inline)]
@ -758,7 +760,22 @@ impl Game {
Ok(())
}
pub fn play(x: String, y: u64) {
thread::spawn(move || {
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
// Load a sound from a file, using a path relative to Cargo.toml
let file = BufReader::new(File::open(x).unwrap());
// Decode that sound file into a source
let source = Decoder::new(file).unwrap();
// Play the sound directly on the device
stream_handle.play_raw(source.convert_samples());
// The sound plays in a separate audio thread,
// so we need to keep the main thread alive while it's playing.
std::thread::sleep(std::time::Duration::from_secs(y));
});
}
/// Stops the game loop. This method should be called inside the closure that you passed to [`Self::run()`].
/// ```
/// # use cat_box::Game;