Skip to content

Commit

Permalink
Merge pull request #155 from boozook/api/improvements
Browse files Browse the repository at this point in the history
Api various  improvements
  • Loading branch information
boozook authored Oct 3, 2023
2 parents e78ceff + 814a3d7 commit b18b08b
Show file tree
Hide file tree
Showing 20 changed files with 185 additions and 124 deletions.
87 changes: 13 additions & 74 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 api/display/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "playdate-display"
version = "0.3.3"
version = "0.3.4"
readme = "README.md"
description = "High-level Display API built on-top of Playdate API"
keywords = ["playdate", "sdk", "api", "gamedev"]
Expand Down
11 changes: 11 additions & 0 deletions api/display/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ impl Into<c_uint> for DisplayScale {
fn into(self) -> c_uint { (self as u8).into() }
}

impl core::fmt::Display for DisplayScale {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "{}", *self as u8) }
}

impl DisplayScale {
#[inline(always)]
pub const fn as_u8(self) -> u8 { self as u8 }
#[inline(always)]
pub const fn as_int(self) -> c_int { self as u8 as _ }
}


pub mod api {
use core::ffi::c_float;
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.8"
version = "0.3.9"
readme = "README.md"
description = "High-level graphics API built on-top of Playdate API"
keywords = ["playdate", "sdk", "api", "gamedev"]
Expand Down
2 changes: 1 addition & 1 deletion api/gfx/examples/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl State {
.unwrap_or_default();

let char = bitmap_ref.into_bitmap();
let w = char.size().map(|(w, _h)| w).unwrap();
let w = char.size().0;
let x = OFFSET + i as i32 * w;
let y = OFFSET + kern;

Expand Down
14 changes: 11 additions & 3 deletions api/gfx/src/bitmap/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ use sys::ffi::LCDBitmapFlip;
use sys::ffi::LCDRect;


#[derive(Debug, Clone, Copy, core::default::Default)]
pub struct Default;
impl Api for Default {}
/// Default graphics bitmap api end-point, ZST.
///
/// All calls approximately costs ~3 derefs.
pub type Default = crate::api::Default;

/// Cached graphics bitmap api end-point.
///
/// Stores one reference, so size on stack is eq `usize`.
///
/// All calls approximately costs ~1 deref.
pub type Cache = crate::api::Cache;


/// End-point with methods about ops over bitmap.
Expand Down
94 changes: 85 additions & 9 deletions api/gfx/src/bitmap/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl<Api: api::Api, const FOD: bool> Bitmap<Api, FOD> {
///
/// Calls [`sys::ffi::playdate_graphics::getBitmapData`].
#[doc(alias = "sys::ffi::playdate_graphics::getBitmapData")]
pub fn size(&self) -> Result<(c_int, c_int), Error> {
pub fn size(&self) -> (c_int, c_int) {
let mut width: c_int = 0;
let mut height: c_int = 0;
let mut row_bytes: c_int = 0;
Expand All @@ -269,14 +269,14 @@ impl<Api: api::Api, const FOD: bool> Bitmap<Api, FOD> {
)
};

Ok((width, height))
(width, height)
}

/// Returns mutable borrow of bitmap-data by this bitmap.
///
/// Calls [`sys::ffi::playdate_graphics::getBitmapData`].
#[doc(alias = "sys::ffi::playdate_graphics::getBitmapData")]
pub fn bitmap_data<'bitmap>(&'bitmap mut self) -> Result<BitmapData<'bitmap>, Error> {
pub fn bitmap_data<'bitmap>(&'bitmap mut self) -> BitmapData<'bitmap> {
let mut width: c_int = 0;
let mut height: c_int = 0;
let mut row_bytes: c_int = 0;
Expand Down Expand Up @@ -313,12 +313,11 @@ impl<Api: api::Api, const FOD: bool> Bitmap<Api, FOD> {
};
let data = unsafe { core::slice::from_raw_parts_mut::<u8>(*boxed_data, len as usize) };


Ok(BitmapData { width,
height,
row_bytes,
mask,
data })
BitmapData { width,
height,
row_bytes,
mask,
data }
}


Expand Down Expand Up @@ -772,3 +771,80 @@ impl<Api: crate::api::Api> Graphics<Api> {
unsafe { f() };
}
}


impl<Api: crate::api::Api> Graphics<Api> {
/// Draws `self` with its upper-left corner at location `x`, `y`,
/// using the given `flip` orientation.
///
/// Equivalent to [`sys::ffi::playdate_graphics::drawBitmap`].
#[doc(alias = "sys::ffi::playdate_graphics::drawBitmap")]
#[inline(always)]
pub fn draw(&self, bitmap: &impl AnyBitmap, x: c_int, y: c_int, flip: BitmapFlip) {
let f = self.0.draw_bitmap();
unsafe { f(bitmap.as_raw(), x, y, flip) }
}

/// Draws `self` with its upper-left corner at location `x`, `y`
/// __tiled inside a `width` by `height` rectangle__.
///
/// Equivalent to [`sys::ffi::playdate_graphics::tileBitmap`].
#[doc(alias = "sys::ffi::playdate_graphics::tileBitmap")]
#[inline(always)]
pub fn draw_tiled(&self,
bitmap: &impl AnyBitmap,
x: c_int,
y: c_int,
width: c_int,
height: c_int,
flip: BitmapFlip) {
let f = self.0.tile_bitmap();
unsafe { f(bitmap.as_raw(), x, y, width, height, flip) }
}

/// Draws the *bitmap* scaled to `x_scale` and `y_scale`
/// then rotated by `degrees` with its center as given by proportions `center_x` and `center_y` at `x`, `y`;
///
/// that is:
/// * if `center_x` and `center_y` are both 0.5 the center of the image is at (`x`,`y`),
/// * if `center_x` and `center_y` are both 0 the top left corner of the image (before rotation) is at (`x`,`y`), etc.
///
/// Equivalent to [`sys::ffi::playdate_graphics::drawRotatedBitmap`].
#[doc(alias = "sys::ffi::playdate_graphics::drawRotatedBitmap")]
#[inline(always)]
pub fn draw_rotated(&self,
bitmap: &impl AnyBitmap,
x: c_int,
y: c_int,
degrees: c_float,
center_x: c_float,
center_y: c_float,
x_scale: c_float,
y_scale: c_float) {
let f = self.0.draw_rotated_bitmap();
unsafe {
f(
bitmap.as_raw(),
x,
y,
degrees,
center_x,
center_y,
x_scale,
y_scale,
)
}
}

/// Draws this bitmap scaled to `x_scale` and `y_scale` with its upper-left corner at location `x`, `y`.
///
/// Note that flip is not available when drawing scaled bitmaps but negative scale values will achieve the same effect.
///
/// Equivalent to [`sys::ffi::playdate_graphics::drawScaledBitmap`].
#[doc(alias = "sys::ffi::playdate_graphics::drawScaledBitmap")]
#[inline(always)]
pub fn draw_scaled(&self, bitmap: &impl AnyBitmap, x: c_int, y: c_int, x_scale: c_float, y_scale: c_float) {
let f = self.0.draw_scaled_bitmap();
unsafe { f(bitmap.as_raw(), x, y, x_scale, y_scale) }
}
}
Loading

0 comments on commit b18b08b

Please sign in to comment.