forked from karx/catbox
Merge pull request 'master' (#1) from karx/catbox:master into master
Reviewed-on: gallant/catbox#1
This commit is contained in:
commit
71aa76b7c2
80
src/lib.rs
80
src/lib.rs
|
@ -90,17 +90,13 @@
|
||||||
clippy::module_name_repetitions,
|
clippy::module_name_repetitions,
|
||||||
clippy::missing_errors_doc
|
clippy::missing_errors_doc
|
||||||
)]
|
)]
|
||||||
|
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||||
|
|
||||||
pub mod physics;
|
pub mod physics;
|
||||||
pub mod vec2;
|
pub mod vec2;
|
||||||
|
|
||||||
use std::{
|
#[cfg(feature = "audio")]
|
||||||
cell::Cell,
|
use rodio::{self, source::Source, Decoder, OutputStream};
|
||||||
ops::{Deref, DerefMut},
|
|
||||||
path::Path,
|
|
||||||
slice::IterMut,
|
|
||||||
};
|
|
||||||
use std::{thread};
|
|
||||||
use sdl2::{
|
use sdl2::{
|
||||||
image::ImageRWops,
|
image::ImageRWops,
|
||||||
mouse::MouseButton,
|
mouse::MouseButton,
|
||||||
|
@ -112,10 +108,12 @@ use sdl2::{
|
||||||
video::{Window, WindowBuildError, WindowContext},
|
video::{Window, WindowBuildError, WindowContext},
|
||||||
EventPump, IntegerOrSdlError,
|
EventPump, IntegerOrSdlError,
|
||||||
};
|
};
|
||||||
use std::fs::File;
|
use std::{
|
||||||
use std::io::BufReader;
|
cell::Cell,
|
||||||
#[cfg(feature = "audio")]
|
ops::{Deref, DerefMut},
|
||||||
use rodio::{self, Decoder, OutputStream, source::Source};
|
path::Path,
|
||||||
|
slice::IterMut,
|
||||||
|
};
|
||||||
use vec2::Vec2Int;
|
use vec2::Vec2Int;
|
||||||
|
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
|
@ -165,6 +163,14 @@ error_from_format! {
|
||||||
InitError
|
InitError
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "audio")]
|
||||||
|
error_from_format! {
|
||||||
|
rodio::StreamError,
|
||||||
|
std::io::Error,
|
||||||
|
rodio::decoder::DecoderError,
|
||||||
|
rodio::PlayError
|
||||||
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for CatboxError {
|
impl std::fmt::Display for CatboxError {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
self.0.fmt(f)
|
self.0.fmt(f)
|
||||||
|
@ -761,7 +767,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;
|
||||||
|
@ -773,23 +779,35 @@ 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,
|
#[cfg(feature = "audio")]
|
||||||
// so we need to keep the main thread alive while it's playing.
|
#[cfg_attr(docsrs, doc(cfg(feature = "audio")))]
|
||||||
std::thread::sleep(std::time::Duration::from_secs(y));
|
/// 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(())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#![warn(clippy::pedantic)]
|
#![warn(clippy::pedantic)]
|
||||||
|
|
||||||
use cat_box::{draw_text, get_keyboard_state, get_mouse_state, Game, Sprite, SpriteCollection, play};
|
use cat_box::{draw_text, get_keyboard_state, get_mouse_state, Game, Sprite, SpriteCollection};
|
||||||
use sdl2::keyboard::Scancode;
|
use sdl2::keyboard::Scancode;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -19,7 +19,8 @@ fn main() {
|
||||||
coll.push(x);
|
coll.push(x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
play(String::from("output.mp3"), 120);
|
#[cfg(feature = "audio")]
|
||||||
|
cat_box::play("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