Skip to content

Commit

Permalink
Fix and enable Sprite draw function
Browse files Browse the repository at this point in the history
  • Loading branch information
finnvoor committed Aug 12, 2024
1 parent 6f41a39 commit c517d5a
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions Sources/PlaydateKit/Core/Sprite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public enum Sprite {
let sprite = unsafeBitCast(userdata, to: Sprite.self)
sprite.update()
}
// setDrawFunction { sprite, bounds, drawRect in
// let userdata = PlaydateKit.Sprite.getUserdata(sprite.unsafelyUnwrapped).unsafelyUnwrapped
// let sprite = unsafeBitCast(userdata, to: Sprite.self)
// sprite.draw(bounds: Rect(bounds), drawRect: Rect(drawRect))
// }
setDrawFunction { sprite, bounds, drawRect in
let userdata = PlaydateKit.Sprite.getUserdata(sprite.unsafelyUnwrapped).unsafelyUnwrapped
let sprite = unsafeBitCast(userdata, to: Sprite.self)
sprite.draw(bounds: Rect(bounds), drawRect: Rect(drawRect))
}
setCollisionResponseFunction { sprite, other in
let spriteUserdata = PlaydateKit.Sprite.getUserdata(sprite.unsafelyUnwrapped)
let sprite = unsafeBitCast(spriteUserdata, to: Sprite.self)
Expand All @@ -42,11 +42,11 @@ public enum Sprite {
let sprite = unsafeBitCast(userdata, to: Sprite.self)
sprite.update()
}
// setDrawFunction { sprite, bounds, drawRect in
// let userdata = PlaydateKit.Sprite.getUserdata(sprite.unsafelyUnwrapped).unsafelyUnwrapped
// let sprite = unsafeBitCast(userdata, to: Sprite.self)
// sprite.draw(bounds: Rect(bounds), drawRect: Rect(drawRect))
// }
setDrawFunction { sprite, bounds, drawRect in
let userdata = PlaydateKit.Sprite.getUserdata(sprite.unsafelyUnwrapped).unsafelyUnwrapped
let sprite = unsafeBitCast(userdata, to: Sprite.self)
sprite.draw(bounds: Rect(bounds), drawRect: Rect(drawRect))
}
setCollisionResponseFunction { sprite, other in
let spriteUserdata = PlaydateKit.Sprite.getUserdata(sprite.unsafelyUnwrapped)
let sprite = unsafeBitCast(spriteUserdata, to: Sprite.self)
Expand All @@ -66,8 +66,8 @@ public enum Sprite {

/// If the sprite doesn’t have an image, the sprite’s draw function is called as needed to update the display. Note that this method
/// is only called when the sprite is on screen and has a size specified via ``setSize(width:height:)`` or ``bounds``.
/// > Warning: This currently does not work due to [apple/swift/issues/72626](https://github.com/apple/swift/issues/72626)
@available(*, unavailable) open func draw(bounds _: Rect, drawRect _: Rect) {}
/// > Note: This is only called when ``image`` is `nil`
open func draw(bounds _: Rect, drawRect _: Rect) {}

/// Override to control the type of collision response that should happen when a collision with other occurs.
///
Expand All @@ -84,9 +84,18 @@ public enum Sprite {
public var stencil: Graphics.Bitmap? { _stencil }

/// The bitmap currently assigned to the sprite.
/// > Note: Setting an image will override a Sprite's custom ``draw(bounds:drawRect:)`` function.
public var image: Graphics.Bitmap? {
didSet {
if image != nil { setDrawFunction(drawFunction: nil) }
sprite.setImage.unsafelyUnwrapped(pointer, image?.pointer, imageFlip)
if image == nil {
setDrawFunction { sprite, bounds, drawRect in
let userdata = PlaydateKit.Sprite.getUserdata(sprite.unsafelyUnwrapped).unsafelyUnwrapped
let sprite = unsafeBitCast(userdata, to: Sprite.self)
sprite.draw(bounds: Rect(bounds), drawRect: Rect(drawRect))
}
}
}
}

Expand Down

0 comments on commit c517d5a

Please sign in to comment.