Compare commits
9 commits
7c98b6f830
...
07198829cf
Author | SHA1 | Date | |
---|---|---|---|
Yash Karandikar | 07198829cf | ||
Yash Karandikar | 884b23c5a5 | ||
Yash Karandikar | b891ff76b4 | ||
Yash Karandikar | bf1b7ed39c | ||
Yash Karandikar | b42a12465a | ||
gallant | d372aedfa2 | ||
gallant | 22b71a3ffe | ||
gallant | b8ddcdb423 | ||
gallant | 8658fabc8e |
|
@ -14,6 +14,10 @@ exclude = ["src/main.rs"]
|
|||
version = "0.35.2"
|
||||
features = ["image", "ttf"]
|
||||
|
||||
[dependencies]
|
||||
rodio = { version = "0.15.0", optional = true}
|
||||
|
||||
[features]
|
||||
default = []
|
||||
default = ["audio"]
|
||||
static = ["sdl2/static-link", "sdl2/bundled"]
|
||||
audio = ["dep:rodio"]
|
||||
|
|
BIN
output.mp3
Normal file
BIN
output.mp3
Normal file
Binary file not shown.
57
src/lib.rs
57
src/lib.rs
|
@ -90,17 +90,13 @@
|
|||
clippy::module_name_repetitions,
|
||||
clippy::missing_errors_doc
|
||||
)]
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
|
||||
pub mod physics;
|
||||
pub mod vec2;
|
||||
|
||||
use std::{
|
||||
cell::Cell,
|
||||
ops::{Deref, DerefMut},
|
||||
path::Path,
|
||||
slice::IterMut,
|
||||
};
|
||||
|
||||
#[cfg(feature = "audio")]
|
||||
use rodio::{self, source::Source, Decoder, OutputStream};
|
||||
use sdl2::{
|
||||
image::ImageRWops,
|
||||
mouse::MouseButton,
|
||||
|
@ -112,7 +108,12 @@ use sdl2::{
|
|||
video::{Window, WindowBuildError, WindowContext},
|
||||
EventPump, IntegerOrSdlError,
|
||||
};
|
||||
|
||||
use std::{
|
||||
cell::Cell,
|
||||
ops::{Deref, DerefMut},
|
||||
path::Path,
|
||||
slice::IterMut,
|
||||
};
|
||||
use vec2::Vec2Int;
|
||||
|
||||
#[doc(no_inline)]
|
||||
|
@ -162,6 +163,14 @@ error_from_format! {
|
|||
InitError
|
||||
}
|
||||
|
||||
#[cfg(feature = "audio")]
|
||||
error_from_format! {
|
||||
rodio::StreamError,
|
||||
std::io::Error,
|
||||
rodio::decoder::DecoderError,
|
||||
rodio::PlayError
|
||||
}
|
||||
|
||||
impl std::fmt::Display for CatboxError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
|
@ -770,3 +779,35 @@ impl Game {
|
|||
self.stopped.set(true);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "audio")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "audio")))]
|
||||
/// Plays an audio file given the path of file and plays it for y seconds
|
||||
/// ```no_run
|
||||
/// # use cat_box::play;
|
||||
/// play("/path/to/song.mp3", 15);
|
||||
/// ```
|
||||
pub fn play<P: AsRef<Path> + Send + 'static>(
|
||||
path: P,
|
||||
time: u64,
|
||||
) -> std::thread::JoinHandle<Result<()>> {
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::thread;
|
||||
|
||||
thread::spawn(move || {
|
||||
let (_stream, stream_handle) = OutputStream::try_default()?;
|
||||
// Load a sound from a file, using a path relative to Cargo.toml
|
||||
let file = BufReader::new(File::open(path)?);
|
||||
// Decode that sound file into a source
|
||||
let source = Decoder::new(file)?;
|
||||
// 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(time));
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
|
|
@ -19,6 +19,8 @@ fn main() {
|
|||
coll.push(x);
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "audio")]
|
||||
cat_box::play("output.mp3", 120);
|
||||
game.run(|ctx| {
|
||||
i = (i + 1) % 255;
|
||||
ctx.set_background_colour(i as u8, 64, 255);
|
||||
|
|
Loading…
Reference in a new issue