Skip to content

Commit

Permalink
Merge pull request #133 from boozook/api/cached-end-point
Browse files Browse the repository at this point in the history
Add cached api end points (part 3) - sound api refactoring
  • Loading branch information
boozook authored Sep 28, 2023
2 parents 85d68f3 + 88fa7d6 commit e4c7b94
Show file tree
Hide file tree
Showing 25 changed files with 1,546 additions and 911 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ display = { version = "0.3", path = "api/display", package = "playdate-display",
fs = { version = "0.2", path = "api/fs", package = "playdate-fs", default-features = false }
gfx = { version = "0.3", path = "api/gfx", package = "playdate-graphics", default-features = false }
menu = { version = "0.1", path = "api/menu", package = "playdate-menu", default-features = false }
sound = { version = "0.1", path = "api/sound", package = "playdate-sound", default-features = false }
sound = { version = "0.2", path = "api/sound", package = "playdate-sound", default-features = false }
sprite = { version = "0.1", path = "api/sprite", package = "playdate-sprite", default-features = false }
system = { version = "0.3", path = "api/system", package = "playdate-system", default-features = false }
sys = { version = "0.1", path = "api/sys", package = "playdate-sys", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion api/gfx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "playdate-graphics"
version = "0.3.1"
version = "0.3.2"
readme = "README.md"
description = "High-level graphics API built on-top of Playdate API"
keywords = ["playdate", "sdk", "api", "gamedev"]
Expand Down
10 changes: 10 additions & 0 deletions api/gfx/src/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ impl<Api: api::Api, const FOD: bool> VideoPlayer<Api, FOD> {

/// Retrieves information about the video, by passing optional mutable references.
///
/// Example:
/// ```no_run
/// let mut frame_count = Some(0);
/// let mut current_frame = Some(0);
/// player.info_raw(None, None, None,
/// frame_count.as_mut(),
/// current_frame.as_mut()
/// );
/// println!( "{}/{}", current_frame.unwrap(), frame_count.unwrap());
/// ```
/// Calls [`sys::ffi::playdate_video::renderFrame`].
#[doc(alias = "sys::ffi::playdate_video::renderFrame")]
pub fn info_raw(&self,
Expand Down
2 changes: 1 addition & 1 deletion api/playdate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "playdate"
version = "0.1.3"
version = "0.1.4"
readme = "README.md"
description = "High-level Playdate API"
keywords = ["playdate", "sdk", "api", "gamedev"]
Expand Down
2 changes: 1 addition & 1 deletion api/playdate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ Just run `cargo new <your options>` and add do following:

- - -

Made with ❤️‍🔥 by [my](https://a.koz.world).
Made with ❤️‍🔥 by [me](https://a.koz.world).

This software is not sponsored or supported by Panic.
8 changes: 4 additions & 4 deletions api/playdate/examples/hello-world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ impl State {
// Background music

// Create player
let player = SamplePlayer::try_new().unwrap();
let player = SamplePlayer::new().unwrap();

// load sound
let sample = Sample::new_from_file(SOUND_PATH);
player.try_set_sample(&sample).unwrap();
let sample = Sample::new_from_file(SOUND_PATH).unwrap();
player.set_sample(&sample);

// start playback
player.try_play(Repeat::LoopsEndlessly, 1.0).unwrap();
player.play(Repeat::LoopsEndlessly, 1.0);

// Finally store it all in the state
Self { location: Point::new(CENTER_X as _, CENTER_Y as _),
Expand Down
41 changes: 21 additions & 20 deletions api/playdate/examples/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,27 @@ fn event_handler(api: NonNull<PlaydateAPI>, event: SystemEvent, _sim_key_code: u
player.use_screen_context();

// Register update handler
api.system().set_update_callback_boxed(
|state| {
// Draw current frame of the player
state.player.render_frame(state.current).unwrap();

// Advance to the next frame
state.current += 1;
if state.current >= state.length {
state.current = 0;
}

// Draw FPS on-top of the player's render
System::Default().draw_fps(0, 0);

// Continue
true
},
State { length: player.info().frame_count,
current: 0,
player, },
let system = api.system();
system.set_update_callback_boxed(
move |state| {
// Draw current frame of the player
state.player.render_frame(state.current).unwrap();

// Advance to the next frame
state.current += 1;
if state.current >= state.length {
state.current = 0;
}

// Draw FPS on-top of the player's render
system.draw_fps(0, 0);

// Continue
true
},
State { length: player.info().frame_count,
current: 0,
player, },
);

true
Expand Down
12 changes: 11 additions & 1 deletion api/playdate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ pub mod ext {
/// Playdate Display API.
fn display(&self) -> display::Display<display::api::Cache>;

// fn sound() -> sound::Sound;
/// Playdate Sound API.
fn sound(&self) -> sound::Sound<sound::api::Cache>;

// fn lua() -> lua::Lua;
// fn json() -> json::Json;
// fn scoreboards() -> scoreboards::Scoreboards;
Expand All @@ -78,6 +80,10 @@ pub mod ext {
fn display(&self) -> display::Display<display::api::Cache> {
display::Display::new_with(display::api::Cache::from(unsafe { self.as_ref() }.display))
}

fn sound(&self) -> sound::Sound<sound::api::Cache> {
sound::Sound::new_with(sound::api::Cache::from(unsafe { self.as_ref() }.sound))
}
}

impl PlaydateAPIExt for *const sys::ffi::PlaydateAPI {
Expand All @@ -96,5 +102,9 @@ pub mod ext {
fn display(&self) -> display::Display<display::api::Cache> {
display::Display::new_with(display::api::Cache::from(unsafe { self.as_ref() }.expect("api").display))
}

fn sound(&self) -> sound::Sound<sound::api::Cache> {
sound::Sound::new_with(sound::api::Cache::from(unsafe { self.as_ref() }.expect("api").sound))
}
}
}
8 changes: 1 addition & 7 deletions api/sound/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "playdate-sound"
version = "0.1.6"
version = "0.2.0"
readme = "README.md"
description = "High-level sound API built on-top of Playdate API"
keywords = ["playdate", "sdk", "api", "gamedev"]
Expand Down Expand Up @@ -44,12 +44,6 @@ name = "sp-simple"
crate-type = ["dylib", "staticlib"]
path = "examples/sp-simple.rs"

# [[example]]
# name = "sp-cached"
# crate-type = ["dylib", "staticlib"]
# path = "examples/sp-cached.rs"
# required-features = ["bindings-derive-cache"]

[[example]]
name = "fp-simple"
crate-type = ["dylib", "staticlib"]
Expand Down
8 changes: 5 additions & 3 deletions api/sound/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
High-level sound API built on-top of [playdate-sys][].

Covered components:
- FilePlayer
- SamplePlayer
- File Player
- Sample Player
- Sample
- Sound Source
- Headphones and microphone (incomplete) 🤏

⚠️ __Incomplete__, WiP.

🚨 Before the version `0.3` API is highly unstable and subject to change. After that old versions will yanked.
Before the version `0.3` API is unstable and can be changed.


## Prerequisites
Expand Down
10 changes: 5 additions & 5 deletions api/sound/examples/fp-simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ impl State {
/// Updates the state
fn update(&mut self) -> Option<()> {
let text: Cow<str> = if let Some(player) = self.player.as_ref() {
let offset = player.try_get_offset().ok()?;
let length = player.try_get_length().ok()?;
let offset = player.offset();
let length = player.length();
format!("{:.2} / {:.2}", offset, length).into()
} else {
"no player".into()
Expand Down Expand Up @@ -76,15 +76,15 @@ impl State {
(*(*sys::API).display).setRefreshRate?(60.0);

// create player
self.player = Player::try_new().ok()?.into();
self.player = Player::new().ok()?.into();
let player = self.player.as_ref()?;

// load sound
const SOUND_PATH: &Path = "sfx/main_theme.pda";
player.try_load_into_player(SOUND_PATH).ok()?;
player.load_into_player(SOUND_PATH).ok()?;

// start playback
player.try_play(Repeat::LoopsEndlessly).ok()?;
player.play(Repeat::LoopsEndlessly);
},
_ => {},
}
Expand Down
12 changes: 6 additions & 6 deletions api/sound/examples/sp-simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl State {
/// Updates the state
fn update(&mut self) -> Option<()> {
let text: Cow<str> = if let Some(player) = self.player.as_ref() {
let offset = player.try_get_offset().ok()?;
let length = player.try_get_length().ok()?;
let offset = player.offset();
let length = player.length();
format!("{:.2} / {:.2}", offset, length).into()
} else {
"no player".into()
Expand Down Expand Up @@ -77,16 +77,16 @@ impl State {
(*(*sys::API).display).setRefreshRate?(60.0);

// create player
self.player = Player::try_new().ok()?.into();
self.player = Player::new().ok()?.into();
let player = self.player.as_ref()?;

// load sound
const SOUND_PATH: &Path = "sfx/main_theme.pda";
let sample = Sample::new_from_file(SOUND_PATH);
player.try_set_sample(&sample).ok()?;
let sample = Sample::new_from_file(SOUND_PATH).ok()?;
player.set_sample(&sample);

// start playback
player.try_play(Repeat::LoopsEndlessly, 1.0).ok()?;
player.play(Repeat::LoopsEndlessly, 1.0);
},
_ => {},
}
Expand Down
12 changes: 11 additions & 1 deletion api/sound/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ pub type ApiError = sys::error::Error<self::Error>;

#[derive(Debug)]
pub enum Error {
/// The file does not exist.
FileNotExist,

/// Causes when allocation failed and/or null-ptr returned.
Alloc,

/// Error caused by the file system.
Fs(fs::error::Error),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self {
Error::FileNotExist => write!(f, "Snd: File doesn't exist"),
Error::Alloc => write!(f, "Snd: Allocation failed"),
Error::FileNotExist => write!(f, "Snd: File doesn't exist"),
Error::Fs(err) => err.fmt(f),
}
}
}
Expand All @@ -25,5 +31,9 @@ impl Into<ApiError> for Error {
fn into(self) -> ApiError { ApiError::Api(self) }
}

impl From<fs::error::Error> for Error {
fn from(err: fs::error::Error) -> Self { Self::Fs(err) }
}


impl core::error::Error for Error {}
Loading

0 comments on commit e4c7b94

Please sign in to comment.