Skip to content

Commit

Permalink
add display api (closes #38)
Browse files Browse the repository at this point in the history
  • Loading branch information
boozook committed Sep 13, 2023
1 parent b6132a4 commit fabc504
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ jobs:
cargo test -p=playdate-graphics --lib --no-default-features --features=$FEATURES_1 -- --nocapture
cargo test -p=playdate-graphics --lib --no-default-features --features=$FEATURES_2 -- --nocapture
cargo test -p=playdate-display --lib --no-default-features --features=$FEATURES_1 -- --nocapture
cargo test -p=playdate-display --lib --no-default-features --features=$FEATURES_2 -- --nocapture
- name: Examples
run: |
FEATURES=bindgen-runtime,bindings-derive-debug
Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

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

40 changes: 40 additions & 0 deletions api/display/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
name = "playdate-display"
version = "0.1.0"
edition = "2021"

readme = "README.md"
license = "MIT OR Apache-2.0"
authors = ["Alex Koz <[email protected]>"]
description = "High-level Display API built on-top of Playdate API"
homepage = "https://github.com/boozook/playdate"
repository = "https://github.com/boozook/playdate.git"


[features]
default = ["sys/default"]
# sys- features:
lang-items = ["sys/lang-items"]
allocator = ["sys/allocator"]
panic-handler = ["sys/panic-handler"]
eh-personality = ["sys/eh-personality"]
error-ctx = ["sys/error-ctx"]
bindgen-runtime = ["sys/bindgen-runtime"]
bindgen-static = ["sys/bindgen-static"]
bindings-derive-default = ["sys/bindings-derive-default"]
bindings-derive-eq = ["sys/bindings-derive-eq"]
bindings-derive-copy = ["sys/bindings-derive-copy"]
bindings-derive-debug = ["sys/bindings-derive-debug"]
bindings-derive-hash = ["sys/bindings-derive-hash"]
bindings-derive-ord = ["sys/bindings-derive-ord"]
bindings-derive-partialeq = ["sys/bindings-derive-partialeq"]
bindings-derive-partialord = ["sys/bindings-derive-partialord"]
bindings-derive-constparamty = ["sys/bindings-derive-constparamty"]
bindings-documentation = ["sys/bindings-documentation"]


[dependencies.sys]
version = "0.1"
path = "../sys"
package = "playdate-sys"
default-features = false
25 changes: 25 additions & 0 deletions api/display/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Display API for PlayDate

High-level display API built on-top of [playdate-sys][].


## Usage

```rust
use playdate_display::Display;

let display = Display::new();

let width = display.width();
let height = display.height();
display.set_refresh_rate(30.0);
```


[playdate-sys]: https://crates.io/crates/playdate-sys



- - -

This software is not sponsored or supported by Panic.
124 changes: 124 additions & 0 deletions api/display/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#![cfg_attr(not(test), no_std)]
extern crate sys;

use core::ffi::c_char;
use core::ffi::c_float;
use core::ffi::c_int;
use core::ffi::c_uint;


#[derive(Debug, Clone, Copy)]
pub struct Display<Api = api::Default>(Api);

impl<Api: Default + api::Api> Default for Display<Api> {
fn default() -> Self { Self(Default::default()) }
}

impl<Api: Default + api::Api> Display<Api> {
pub fn new(api: Api) -> Self { Self(api) }
}

impl<Api: api::Api> Display<Api> {
pub fn new_with(api: Api) -> Self { Self(api) }
}


impl<Api: api::Api> Display<Api> {
/// Equivalent to [`sys::ffi::playdate_display::getWidth`]
#[doc(alias = "sys::ffi::playdate_display::getWidth")]
pub fn width(&self) -> c_int {
let f = self.0.get_width();
unsafe { f() }
}

/// Equivalent to [`sys::ffi::playdate_display::getHeight`]
#[doc(alias = "sys::ffi::playdate_display::getHeight")]
pub fn height(&self) -> c_int {
let f = self.0.get_height();
unsafe { f() }
}

/// Equivalent to [`sys::ffi::playdate_display::setRefreshRate`]
#[doc(alias = "sys::ffi::playdate_display::setRefreshRate")]
pub fn set_refresh_rate(&self, rate: c_float) {
let f = self.0.set_refresh_rate();
unsafe { f(rate) }
}

/// Equivalent to [`sys::ffi::playdate_display::setInverted`]
#[doc(alias = "sys::ffi::playdate_display::setInverted")]
pub fn set_inverted(&self, value: bool) {
let f = self.0.set_inverted();
unsafe { f(value as _) }
}

/// Equivalent to [`sys::ffi::playdate_display::setScale`]
#[doc(alias = "sys::ffi::playdate_display::setScale")]
pub fn set_scale(&self, scale: c_uint) {
let f = self.0.set_scale();
unsafe { f(scale) }
}

/// Equivalent to [`sys::ffi::playdate_display::setMosaic`]
#[doc(alias = "sys::ffi::playdate_display::setMosaic")]
pub fn set_mosaic(&self, x: c_uint, y: c_uint) {
let f = self.0.set_mosaic();
unsafe { f(x, y) }
}

/// Equivalent to [`sys::ffi::playdate_display::setFlipped`]
#[doc(alias = "sys::ffi::playdate_display::setFlipped")]
pub fn set_flipped(&self, x: bool, y: bool) {
let f = self.0.set_flipped();
unsafe { f(x as _, y as _) }
}

/// Equivalent to [`sys::ffi::playdate_display::setOffset`]
#[doc(alias = "sys::ffi::playdate_display::setOffset")]
pub fn set_offset(&self, x: c_int, y: c_int) {
let f = self.0.set_offset();
unsafe { f(x, y) }
}
}


pub mod api {
use core::ffi::c_char;
use core::ffi::c_float;
use core::ffi::c_int;
use core::ffi::c_uint;


#[derive(Debug, Clone, Copy, core::default::Default)]
pub struct Default;

impl Api for Default {}


pub trait Api {
/// Equivalent to [`sys::ffi::playdate_display::getWidth`]
#[doc(alias = "sys::ffi::playdate_display::getWidth")]
fn get_width(&self) -> unsafe extern "C" fn() -> c_int { *sys::api!(display.getWidth) }
/// Equivalent to [`sys::ffi::playdate_display::getHeight`]
#[doc(alias = "sys::ffi::playdate_display::getHeight")]
fn get_height(&self) -> unsafe extern "C" fn() -> c_int { *sys::api!(display.getHeight) }
/// Equivalent to [`sys::ffi::playdate_display::setRefreshRate`]
#[doc(alias = "sys::ffi::playdate_display::setRefreshRate")]
fn set_refresh_rate(&self) -> unsafe extern "C" fn(rate: c_float) { *sys::api!(display.setRefreshRate) }
/// Equivalent to [`sys::ffi::playdate_display::setInverted`]
#[doc(alias = "sys::ffi::playdate_display::setInverted")]
fn set_inverted(&self) -> unsafe extern "C" fn(flag: c_int) { *sys::api!(display.setInverted) }
/// Equivalent to [`sys::ffi::playdate_display::setScale`]
#[doc(alias = "sys::ffi::playdate_display::setScale")]
fn set_scale(&self) -> unsafe extern "C" fn(s: c_uint) { *sys::api!(display.setScale) }
/// Equivalent to [`sys::ffi::playdate_display::setMosaic`]
#[doc(alias = "sys::ffi::playdate_display::setMosaic")]
fn set_mosaic(&self) -> unsafe extern "C" fn(x: c_uint, y: c_uint) { *sys::api!(display.setMosaic) }
/// Equivalent to [`sys::ffi::playdate_display::setFlipped`]
#[doc(alias = "sys::ffi::playdate_display::setFlipped")]
fn set_flipped(&self) -> unsafe extern "C" fn(x: c_int, y: c_int) { *sys::api!(display.setFlipped) }
/// Equivalent to [`sys::ffi::playdate_display::setOffset`]
#[doc(alias = "sys::ffi::playdate_display::setOffset")]
fn set_offset(&self) -> unsafe extern "C" fn(x: c_int, y: c_int) { *sys::api!(display.setOffset) }
}
}

0 comments on commit fabc504

Please sign in to comment.