Merge pull request 'added music theory' (#5) from gallant/catbox:master into audio
Reviewed-on: #5
This commit is contained in:
commit
b42a12465a
|
@ -14,6 +14,10 @@ exclude = ["src/main.rs"]
|
||||||
version = "0.35.2"
|
version = "0.35.2"
|
||||||
features = ["image", "ttf"]
|
features = ["image", "ttf"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rodio = { version = "0.15.0", optional = true}
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = ["audio"]
|
||||||
static = ["sdl2/static-link", "sdl2/bundled"]
|
static = ["sdl2/static-link", "sdl2/bundled"]
|
||||||
|
audio = ["dep:rodio"]
|
||||||
|
|
BIN
output.mp3
Normal file
BIN
output.mp3
Normal file
Binary file not shown.
29
src/lib.rs
29
src/lib.rs
|
@ -100,7 +100,7 @@ use std::{
|
||||||
path::Path,
|
path::Path,
|
||||||
slice::IterMut,
|
slice::IterMut,
|
||||||
};
|
};
|
||||||
|
use std::{thread};
|
||||||
use sdl2::{
|
use sdl2::{
|
||||||
image::ImageRWops,
|
image::ImageRWops,
|
||||||
mouse::MouseButton,
|
mouse::MouseButton,
|
||||||
|
@ -112,7 +112,10 @@ use sdl2::{
|
||||||
video::{Window, WindowBuildError, WindowContext},
|
video::{Window, WindowBuildError, WindowContext},
|
||||||
EventPump, IntegerOrSdlError,
|
EventPump, IntegerOrSdlError,
|
||||||
};
|
};
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::BufReader;
|
||||||
|
#[cfg(feature = "audio")]
|
||||||
|
use rodio::{self, Decoder, OutputStream, source::Source};
|
||||||
use vec2::Vec2Int;
|
use vec2::Vec2Int;
|
||||||
|
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
|
@ -758,7 +761,7 @@ impl Game {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stops the game loop. This method should be called inside the closure that you passed to [`Self::run()`].
|
/// Stops the game loop. This method should be called inside the closure that you passed to [`Self::run()`].
|
||||||
/// ```
|
/// ```
|
||||||
/// # use cat_box::Game;
|
/// # use cat_box::Game;
|
||||||
|
@ -770,3 +773,23 @@ impl Game {
|
||||||
self.stopped.set(true);
|
self.stopped.set(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// Plays an audio file given the path of file and plays it for y seconds
|
||||||
|
/// ```
|
||||||
|
/// play(String::from("/path/to/song.mp3", 15));
|
||||||
|
/// ```
|
||||||
|
#[cfg(feature = "audio")]
|
||||||
|
pub fn play<P: AsRef<Path> + std::marker::Send + 'static>(x: P, y: u64) -> thread::JoinHandle<()> {
|
||||||
|
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()).unwrap();
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#![warn(clippy::pedantic)]
|
#![warn(clippy::pedantic)]
|
||||||
|
|
||||||
use cat_box::{draw_text, get_keyboard_state, get_mouse_state, Game, Sprite, SpriteCollection};
|
use cat_box::{draw_text, get_keyboard_state, get_mouse_state, Game, Sprite, SpriteCollection, play};
|
||||||
use sdl2::keyboard::Scancode;
|
use sdl2::keyboard::Scancode;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -19,6 +19,7 @@ fn main() {
|
||||||
coll.push(x);
|
coll.push(x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
play(String::from("output.mp3"), 120);
|
||||||
game.run(|ctx| {
|
game.run(|ctx| {
|
||||||
i = (i + 1) % 255;
|
i = (i + 1) % 255;
|
||||||
ctx.set_background_colour(i as u8, 64, 255);
|
ctx.set_background_colour(i as u8, 64, 255);
|
||||||
|
|
Loading…
Reference in a new issue