-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from boozook/api/display
Add display api
- Loading branch information
Showing
5 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
} |