forked from karx/catbox
added music theory
This commit is contained in:
parent
7c98b6f830
commit
8658fabc8e
|
@ -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"]
|
||||
|
|
21
src/lib.rs
21
src/lib.rs
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue