Skip to content

Commit

Permalink
Sprite visibility (#80)
Browse files Browse the repository at this point in the history
* Expose sprite visibility

* expose context clearing in graphics intf

* Add sprite visibility example to hello_world
  • Loading branch information
tehsmeely committed Jan 12, 2024
1 parent 26afcc3 commit df48ecd
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Crank.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[[target]]
name = "hello_world"

assets = [
"examples/assets/heart.png",
]

[target.metadata]
name = "Hello World"
version = "0.1.0"
Expand Down
Binary file added examples/assets/heart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

extern crate alloc;

use crankstart::log_to_console;
use crankstart::sprite::{Sprite, SpriteManager};
use crankstart_sys::{LCDBitmapFlip, PDButtons};
use {
alloc::boxed::Box,
anyhow::Error,
Expand All @@ -19,21 +22,37 @@ use {
struct State {
location: ScreenPoint,
velocity: ScreenVector,
sprite: Sprite,
}

fn load_sprite() -> Result<Sprite, Error> {
let sprite_manager = SpriteManager::get_mut();
let mut sprite = sprite_manager.new_sprite()?;
let image = Graphics::get().load_bitmap("examples/assets/heart")?;
sprite.set_image(image, LCDBitmapFlip::kBitmapUnflipped)?;
sprite.move_to(200.0, 120.0)?;
sprite.set_z_index(10)?;
sprite.set_opaque(false)?;
sprite_manager.add_sprite(&sprite)?;
Ok(sprite)
}

impl State {
pub fn new(_playdate: &Playdate) -> Result<Box<Self>, Error> {
crankstart::display::Display::get().set_refresh_rate(20.0)?;
let sprite = load_sprite()?;
Ok(Box::new(Self {
location: point2(INITIAL_X, INITIAL_Y),
velocity: vec2(1, 2),
sprite,
}))
}
}

impl Game for State {
fn update(&mut self, _playdate: &mut Playdate) -> Result<(), Error> {
let graphics = Graphics::get();
graphics.clear_context()?;
graphics.clear(LCDColor::Solid(LCDSolidColor::kColorWhite))?;
graphics.draw_text("Hello World Rust", self.location)?;

Expand All @@ -47,10 +66,27 @@ impl Game for State {
self.velocity.y = -self.velocity.y;
}

let (_, pushed, _) = System::get().get_button_state()?;
if (pushed & PDButtons::kButtonA).0 != 0 {
log_to_console!("Button A pushed");
self.sprite
.set_visible(!self.sprite.is_visible().unwrap_or(false))
.unwrap();
}

System::get().draw_fps(0, 0)?;

Ok(())
}

fn update_sprite(
&mut self,
sprite: &mut Sprite,
_playdate: &mut Playdate,
) -> Result<(), Error> {
sprite.mark_dirty()?;
Ok(())
}
}

const INITIAL_X: i32 = (400 - TEXT_WIDTH) / 2;
Expand Down
5 changes: 5 additions & 0 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,11 @@ impl Graphics {
pd_func_caller!((*self.0).pushContext, raw_bitmap)
}

/// Clear the context stack for graphics to make all drawing go to the display framebuffer.
pub fn clear_context(&self) -> Result<(), Error> {
pd_func_caller!((*self.0).pushContext, core::ptr::null_mut())
}

/// Internal function; use `with_context`.
fn pop_context(&self) -> Result<(), Error> {
pd_func_caller!((*self.0).popContext)
Expand Down
39 changes: 39 additions & 0 deletions src/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,27 @@ impl SpriteInner {
pd_func_caller!((*self.playdate_sprite).getTag, self.raw_sprite)
}

pub fn set_visible(&mut self, visible: bool) -> Result<(), Error> {
pd_func_caller!(
(*self.playdate_sprite).setVisible,
self.raw_sprite,
visible as i32
)
}

pub fn is_visible(&self) -> Result<bool, Error> {
let visible = pd_func_caller!((*self.playdate_sprite).isVisible, self.raw_sprite)?;
Ok(visible != 0)
}

pub fn set_opaque(&self, opaque: bool) -> Result<(), Error> {
pd_func_caller!(
(*self.playdate_sprite).setOpaque,
self.raw_sprite,
opaque as i32
)
}

pub fn move_to(&mut self, x: f32, y: f32) -> Result<(), Error> {
pd_func_caller!((*self.playdate_sprite).moveTo, self.raw_sprite, x, y)
}
Expand Down Expand Up @@ -423,6 +444,24 @@ impl Sprite {
.move_to(x, y)
}

pub fn set_visible(&mut self, visible: bool) -> Result<(), Error> {
self.inner
.try_borrow_mut()
.map_err(Error::msg)?
.set_visible(visible)
}

pub fn is_visible(&self) -> Result<bool, Error> {
self.inner.try_borrow().map_err(Error::msg)?.is_visible()
}

pub fn set_opaque(&self, opaque: bool) -> Result<(), Error> {
self.inner
.try_borrow_mut()
.map_err(Error::msg)?
.set_opaque(opaque)
}

pub fn get_position(&self) -> Result<(f32, f32), Error> {
self.inner.try_borrow().map_err(Error::msg)?.get_position()
}
Expand Down

0 comments on commit df48ecd

Please sign in to comment.