Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and enable Sprite draw function #81

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading