From a4e6ef57d0a25fa74eff94c001f1cab020797ae1 Mon Sep 17 00:00:00 2001
From: Alexander Koz
Date: Thu, 14 Sep 2023 00:49:38 +0400
Subject: [PATCH] add display api (closes #38)
---
.github/workflows/tests.yml | 3 +
Cargo.lock | 7 ++
api/display/Cargo.toml | 40 ++++++++++++
api/display/README.md | 25 ++++++++
api/display/src/lib.rs | 124 ++++++++++++++++++++++++++++++++++++
5 files changed, 199 insertions(+)
create mode 100644 api/display/Cargo.toml
create mode 100644 api/display/README.md
create mode 100644 api/display/src/lib.rs
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index e135d249..d54e966b 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -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
diff --git a/Cargo.lock b/Cargo.lock
index 736bfaf4..e1d6f8f3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2541,6 +2541,13 @@ dependencies = [
"playdate-sys",
]
+[[package]]
+name = "playdate-display"
+version = "0.1.0"
+dependencies = [
+ "playdate-sys",
+]
+
[[package]]
name = "playdate-fs"
version = "0.1.1"
diff --git a/api/display/Cargo.toml b/api/display/Cargo.toml
new file mode 100644
index 00000000..407aaf45
--- /dev/null
+++ b/api/display/Cargo.toml
@@ -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 "]
+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
diff --git a/api/display/README.md b/api/display/README.md
new file mode 100644
index 00000000..bd7b0b70
--- /dev/null
+++ b/api/display/README.md
@@ -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.
diff --git a/api/display/src/lib.rs b/api/display/src/lib.rs
new file mode 100644
index 00000000..5491fa18
--- /dev/null
+++ b/api/display/src/lib.rs
@@ -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);
+
+impl Default for Display {
+ fn default() -> Self { Self(Default::default()) }
+}
+
+impl Display {
+ pub fn new(api: Api) -> Self { Self(api) }
+}
+
+impl Display {
+ pub fn new_with(api: Api) -> Self { Self(api) }
+}
+
+
+impl Display {
+ /// 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) }
+ }
+}