Skip to content

Commit

Permalink
Add cached api end points (part 1) (#130)
Browse files Browse the repository at this point in the history
* add cached api end-point for display

* add cached api end-point for system

* add cached api end-point for gfx

* gfx refactoring, add video api

* add minimalistic video example

* fix error handling

* fix missed gated derive `Debug`

* CI: fix dry-run condition

* update deps, bump crates
  • Loading branch information
boozook authored Sep 26, 2023
1 parent 9b92f1c commit 65f3e25
Show file tree
Hide file tree
Showing 21 changed files with 2,662 additions and 255 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
with:
# From docs: `github.head_ref` is only available when the event that triggers
# a workflow run is either `pull_request` or `pull_request_target`.
dry-run: ${{ github.event.inputs.dry-run == 'true' || github.head_ref }}
dry-run: ${{ github.event.inputs.dry-run == 'true' || (github.head_ref && 'true') || 'false' }}
check-repo: ${{ (github.event.inputs && github.event.inputs.check-repo == 'true') || true }}
ignore-unpublished-changes: ${{ github.head_ref == 'false' }}
registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
20 changes: 10 additions & 10 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ repository = "https://github.com/boozook/playdate.git"
[workspace.dependencies]
color = { version = "0.1", path = "api/color", package = "playdate-color", default-features = false }
ctrl = { version = "0.1", path = "api/ctrl", package = "playdate-controls", default-features = false }
display = { version = "0.2", path = "api/display", package = "playdate-display", default-features = false }
display = { version = "0.3", path = "api/display", package = "playdate-display", default-features = false }
fs = { version = "0.1", path = "api/fs", package = "playdate-fs", default-features = false }
gfx = { version = "0.2", path = "api/gfx", package = "playdate-graphics", 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 }
sprite = { version = "0.1", path = "api/sprite", package = "playdate-sprite", default-features = false }
system = { version = "0.2", path = "api/system", package = "playdate-system", 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 }

build = { version = "0.2", path = "support/build", package = "playdate-build", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion api/display/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "playdate-display"
version = "0.2.0"
version = "0.3.0"
readme = "README.md"
description = "High-level Display API built on-top of Playdate API"
keywords = ["playdate", "sdk", "api", "gamedev"]
Expand Down
93 changes: 92 additions & 1 deletion api/display/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ impl Display<api::Default> {
pub fn Default() -> Self { Self(Default::default()) }
}

impl Display<api::Cache> {
/// Creates [`Display`] without type parameter requirement.
///
/// Uses [`api::Cache`].
#[allow(non_snake_case)]
pub fn Cached() -> Self { Self(Default::default()) }
}

impl<Api: Default + api::Api> Default for Display<Api> {
fn default() -> Self { Self(Default::default()) }
}
Expand Down Expand Up @@ -169,14 +177,97 @@ pub mod api {
use core::ffi::c_float;
use core::ffi::c_int;
use core::ffi::c_uint;
use core::ptr::NonNull;
use sys::ffi::playdate_display;


/// Default display api end-point, ZST.
///
/// All calls approximately costs ~3 derefs.
#[derive(Debug, Clone, Copy, core::default::Default)]
pub struct Default;

impl Api for Default {}


/// Cached display api end-point.
///
/// Stores one reference, so size on stack is eq `usize`.
///
/// All calls approximately costs ~1 deref.
#[derive(Clone, Copy)]
#[cfg_attr(feature = "bindings-derive-debug", derive(Debug))]
pub struct Cache(&'static playdate_display);

impl core::default::Default for Cache {
fn default() -> Self { Self(sys::api!(display)) }
}

impl From<*const playdate_display> for Cache {
#[inline(always)]
fn from(ptr: *const playdate_display) -> Self { Self(unsafe { ptr.as_ref() }.expect("display")) }
}

impl From<&'static playdate_display> for Cache {
#[inline(always)]
fn from(r: &'static playdate_display) -> Self { Self(r) }
}

impl From<NonNull<playdate_display>> for Cache {
#[inline(always)]
fn from(ptr: NonNull<playdate_display>) -> Self { Self(unsafe { ptr.as_ref() }) }
}

impl From<&'_ NonNull<playdate_display>> for Cache {
#[inline(always)]
fn from(ptr: &NonNull<playdate_display>) -> Self { Self(unsafe { ptr.as_ref() }) }
}


impl Api for Cache {
/// Equivalent to [`sys::ffi::playdate_display::getWidth`]
#[doc(alias = "sys::ffi::playdate_display::getWidth")]
#[inline(always)]
fn get_width(&self) -> unsafe extern "C" fn() -> c_int { self.0.getWidth.expect("getWidth") }

/// Equivalent to [`sys::ffi::playdate_display::getHeight`]
#[doc(alias = "sys::ffi::playdate_display::getHeight")]
#[inline(always)]
fn get_height(&self) -> unsafe extern "C" fn() -> c_int { self.0.getHeight.expect("getHeight") }

/// Equivalent to [`sys::ffi::playdate_display::setRefreshRate`]
#[doc(alias = "sys::ffi::playdate_display::setRefreshRate")]
#[inline(always)]
fn set_refresh_rate(&self) -> unsafe extern "C" fn(rate: c_float) {
self.0.setRefreshRate.expect("setRefreshRate")
}

/// Equivalent to [`sys::ffi::playdate_display::setInverted`]
#[doc(alias = "sys::ffi::playdate_display::setInverted")]
#[inline(always)]
fn set_inverted(&self) -> unsafe extern "C" fn(flag: c_int) { self.0.setInverted.expect("setInverted") }

/// Equivalent to [`sys::ffi::playdate_display::setScale`]
#[doc(alias = "sys::ffi::playdate_display::setScale")]
#[inline(always)]
fn set_scale(&self) -> unsafe extern "C" fn(s: c_uint) { self.0.setScale.expect("setScale") }

/// Equivalent to [`sys::ffi::playdate_display::setMosaic`]
#[doc(alias = "sys::ffi::playdate_display::setMosaic")]
#[inline(always)]
fn set_mosaic(&self) -> unsafe extern "C" fn(x: c_uint, y: c_uint) { self.0.setMosaic.expect("setMosaic") }

/// Equivalent to [`sys::ffi::playdate_display::setFlipped`]
#[doc(alias = "sys::ffi::playdate_display::setFlipped")]
#[inline(always)]
fn set_flipped(&self) -> unsafe extern "C" fn(x: c_int, y: c_int) { self.0.setFlipped.expect("setFlipped") }

/// Equivalent to [`sys::ffi::playdate_display::setOffset`]
#[doc(alias = "sys::ffi::playdate_display::setOffset")]
#[inline(always)]
fn set_offset(&self) -> unsafe extern "C" fn(x: c_int, y: c_int) { self.0.setOffset.expect("setOffset") }
}


pub trait Api {
/// Equivalent to [`sys::ffi::playdate_display::getWidth`]
#[doc(alias = "sys::ffi::playdate_display::getWidth")]
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.2.3"
version = "0.3.0"
readme = "README.md"
description = "High-level graphics API built on-top of Playdate API"
keywords = ["playdate", "sdk", "api", "gamedev"]
Expand Down
Loading

0 comments on commit 65f3e25

Please sign in to comment.