Fix doctest

This commit is contained in:
Yash Karandikar 2022-10-06 08:50:47 -05:00
parent b42a12465a
commit bf1b7ed39c
2 changed files with 30 additions and 26 deletions

View file

@ -94,13 +94,8 @@
pub mod physics;
pub mod vec2;
use std::{
cell::Cell,
ops::{Deref, DerefMut},
path::Path,
slice::IterMut,
};
use std::{thread};
#[cfg(feature = "audio")]
use rodio::{self, source::Source, Decoder, OutputStream};
use sdl2::{
image::ImageRWops,
mouse::MouseButton,
@ -114,8 +109,13 @@ use sdl2::{
};
use std::fs::File;
use std::io::BufReader;
#[cfg(feature = "audio")]
use rodio::{self, Decoder, OutputStream, source::Source};
use std::thread;
use std::{
cell::Cell,
ops::{Deref, DerefMut},
path::Path,
slice::IterMut,
};
use vec2::Vec2Int;
#[doc(no_inline)]
@ -761,7 +761,7 @@ impl Game {
Ok(())
}
/// Stops the game loop. This method should be called inside the closure that you passed to [`Self::run()`].
/// ```
/// # use cat_box::Game;
@ -773,23 +773,25 @@ impl Game {
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));
/// ```no_run
/// # use cat_box::play;
/// play("/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();
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));
})
}
// 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));
})
}

View file

@ -1,6 +1,8 @@
#![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, play, Game, Sprite, SpriteCollection,
};
use sdl2::keyboard::Scancode;
fn main() {