Skip to content

Commit

Permalink
Implement PaternRef LCDColor
Browse files Browse the repository at this point in the history
The existing Pattern(LCDPattern) has memory issues. See pd-rs#63 for more details.

Had to remove the TextSprite helper, but c'est la vie.
  • Loading branch information
paulstraw committed Mar 28, 2024
1 parent a6e5981 commit bc79f16
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 88 deletions.
10 changes: 4 additions & 6 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,21 @@ pub fn rect_make(x: f32, y: f32, width: f32, height: f32) -> PDRect {
}

#[derive(Clone, Debug)]
pub enum LCDColor {
pub enum LCDColor<'t> {
Solid(LCDSolidColor),
PatternRef(&'t LCDPattern),
Pattern(LCDPattern),
}

impl From<LCDColor> for usize {
impl From<LCDColor<'_>> for usize {
fn from(color: LCDColor) -> Self {
match color {
LCDColor::Solid(solid_color) => solid_color as usize,
LCDColor::Pattern(pattern) => {
let pattern_ptr = &pattern as *const u8;
pattern_ptr as usize
}
LCDColor::PatternRef(pattern) => (pattern as *const u8) as usize,
}
}
}
Expand Down Expand Up @@ -326,10 +328,6 @@ impl Bitmap {
})
}

pub fn into_color(&self, bitmap: Bitmap, top_left: Point2D<i32>) -> Result<LCDColor, Error> {
self.inner.borrow().into_color(bitmap, top_left)
}

pub fn load(&self, path: &str) -> Result<(), Error> {
self.inner.borrow().load(path)
}
Expand Down
82 changes: 0 additions & 82 deletions src/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,88 +611,6 @@ impl SpriteManager {
}
}

/// This is a helper type for drawing text into a sprite. Drawing text into a sprite is the
/// recommended way to display text when using sprites in your game; it removes timing issues and
/// gives you the flexibility of the sprite system rather than draw_text alone.
///
/// After creation with `new`, you can `update_text` as desired, and use `get_sprite` or
/// `get_sprite_mut` to access the `Sprite` for other operations like `move_to` and `get_bounds`
/// (which can tell you the height and width of the generated bitmap).
///
/// Note: it's assumed that you're using the system font and haven't changed its tracking; we have
/// no way to retrieve the current font or tracking with C APIs.
#[derive(Clone, Debug)]
pub struct TextSprite {
sprite: Sprite,
background: LCDColor,
}

impl TextSprite {
/// Creates a `TextSprite`, draws the given text into it over the given background color,
/// and adds the underlying sprite to the `SpriteManager`.
pub fn new<S>(text: S, background: LCDColor) -> Result<Self, Error>
where
S: AsRef<str>,
{
let text = text.as_ref();
let graphics = Graphics::get();
let sprite_manager = SpriteManager::get_mut();

// Currently no getTextTracking C API; assume none has been set.
let tracking = 0;

let width = graphics.get_system_text_width(text, tracking)?;

let text_bitmap =
graphics.new_bitmap(size2(width, SYSTEM_FONT_HEIGHT), background.clone())?;
graphics.with_context(&text_bitmap, || {
graphics.draw_text(text, point2(0, 0))?;
Ok(())
})?;

let mut sprite = sprite_manager.new_sprite()?;
sprite.set_image(text_bitmap, LCDBitmapFlip::kBitmapUnflipped)?;
sprite_manager.add_sprite(&sprite)?;

Ok(Self { sprite, background })
}

pub fn get_sprite(&self) -> &Sprite {
&self.sprite
}

pub fn get_sprite_mut(&mut self) -> &mut Sprite {
&mut self.sprite
}

/// Recreates the underlying bitmap with the given text; use `get_sprite().get_bounds()`
/// to see the new size.
pub fn update_text<S>(&mut self, text: S) -> Result<(), Error>
where
S: AsRef<str>,
{
let text = text.as_ref();
let graphics = Graphics::get();

// Currently no getTextTracking C API; assume none has been set.
let tracking = 0;

let width = graphics.get_system_text_width(text, tracking)?;

let text_bitmap =
graphics.new_bitmap(size2(width, SYSTEM_FONT_HEIGHT), self.background.clone())?;
graphics.with_context(&text_bitmap, || {
graphics.draw_text(text, point2(0, 0))?;
Ok(())
})?;

self.sprite
.set_image(text_bitmap, LCDBitmapFlip::kBitmapUnflipped)?;

Ok(())
}
}

/// This is a helper type for rotating and scaling an image in a sprite.
///
/// After creation with `new`, you can `set_rotation` to update the parameters, and use
Expand Down

0 comments on commit bc79f16

Please sign in to comment.