Skip to content

Commit

Permalink
Move update/draw callback to open method
Browse files Browse the repository at this point in the history
  • Loading branch information
finnvoor committed Apr 2, 2024
1 parent a04096c commit 8b72e74
Showing 1 changed file with 97 additions and 61 deletions.
158 changes: 97 additions & 61 deletions Sources/PlaydateKit/Core/Sprite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,50 @@ public import CPlaydate
/// or the enemies that chase after your player. Sprites animate efficiently, and offer collision detection and
/// a host of other built-in functionality.
public enum Sprite {
// MARK: Public

public typealias CollisionResponseType = SpriteCollisionResponseType

public class CollisionInfo {
// MARK: Lifecycle

init(collisions: UnsafeBufferPointer<SpriteCollisionInfo>, actual: Point<Float>) {
self.collisions = collisions
self.actual = actual
}

deinit { collisions.deallocate() }

// MARK: Public

public let collisions: UnsafeBufferPointer<SpriteCollisionInfo>
public let actual: Point<Float>
}
// MARK: Open

public class QueryInfo {
// MARK: Lifecycle

init(info: UnsafeBufferPointer<SpriteQueryInfo>) {
self.info = info
}

deinit {
info.deallocate()
}

// MARK: Public

public let info: UnsafeBufferPointer<SpriteQueryInfo>
}

public class Sprite {
open class Sprite {
// MARK: Lifecycle

/// Allocates and returns a new Sprite.
public init() {
pointer = sprite.newSprite.unsafelyUnwrapped().unsafelyUnwrapped
userdata = unsafeBitCast(self, to: UnsafeMutableRawPointer.self)
setUpdateFunction { sprite in
let userdata = PlaydateKit.Sprite.getUserdata(sprite.unsafelyUnwrapped).unsafelyUnwrapped
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))
}
}

init(pointer: OpaquePointer) {
self.pointer = pointer
userdata = unsafeBitCast(self, to: UnsafeMutableRawPointer.self)
setUpdateFunction { sprite in
let userdata = PlaydateKit.Sprite.getUserdata(sprite.unsafelyUnwrapped).unsafelyUnwrapped
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))
}
}

deinit { sprite.freeSprite.unsafelyUnwrapped(pointer) }

// MARK: Open

open func update() {}

open func draw(bounds _: Rect<Float>, drawRect _: Rect<Float>) {}

// MARK: Public

/// The sprite's stencil bitmap, if set.
Expand Down Expand Up @@ -125,12 +119,6 @@ public enum Sprite {
set { sprite.setVisible.unsafelyUnwrapped(pointer, newValue ? 1 : 0) }
}

/// Gets/sets the sprite’s userdata, an arbitrary pointer used for associating the sprite with other data.
public var userdata: UnsafeMutableRawPointer? {
get { sprite.getUserdata.unsafelyUnwrapped(pointer) }
set { sprite.setUserdata.unsafelyUnwrapped(pointer, newValue) }
}

/// Get/set the `collisionsEnabled` flag of the sprite (along with the `collideRect`, this
/// determines whether the sprite participates in collisions). Set to true by default.
public var collisionsEnabled: Bool {
Expand Down Expand Up @@ -237,25 +225,6 @@ public enum Sprite {
sprite.setIgnoresDrawOffset.unsafelyUnwrapped(pointer, ignoresDrawOffset ? 1 : 0)
}

/// Sets the update function for the sprite.
public func setUpdateFunction(
_ updateFunction: (@convention(c) (_ sprite: OpaquePointer?) -> Void)?
) {
sprite.setUpdateFunction.unsafelyUnwrapped(pointer, updateFunction)
}

/// Sets the draw function for the sprite. Note that the callback is only called when the
/// sprite is on screen and has a size specified via ``setSize(width:height:)`` or ``bounds``.
public func setDrawFunction(
drawFunction: (@convention(c) (
_ sprite: OpaquePointer?,
_ bounds: PDRect,
_ drawRect: PDRect
) -> Void)?
) {
sprite.setDrawFunction.unsafelyUnwrapped(pointer, drawFunction)
}

/// Adds the sprite to the display list, so that it is drawn in the current scene.
public func addToDisplayList() {
sprite.addSprite.unsafelyUnwrapped(pointer)
Expand Down Expand Up @@ -317,6 +286,67 @@ public enum Sprite {
// MARK: Internal

let pointer: OpaquePointer

/// Gets/sets the sprite’s userdata, an arbitrary pointer used for associating the sprite with other data.
var userdata: UnsafeMutableRawPointer? {
get { sprite.getUserdata.unsafelyUnwrapped(pointer) }
set { sprite.setUserdata.unsafelyUnwrapped(pointer, newValue) }
}

/// Sets the draw function for the sprite. Note that the callback is only called when the
/// sprite is on screen and has a size specified via ``setSize(width:height:)`` or ``bounds``.
func setDrawFunction(
drawFunction: (@convention(c) (
_ sprite: OpaquePointer?,
_ bounds: PDRect,
_ drawRect: PDRect
) -> Void)?
) {
sprite.setDrawFunction.unsafelyUnwrapped(pointer, drawFunction)
}

/// Sets the update function for the sprite.
func setUpdateFunction(
_ updateFunction: (@convention(c) (_ sprite: OpaquePointer?) -> Void)?
) {
sprite.setUpdateFunction.unsafelyUnwrapped(pointer, updateFunction)
}
}

// MARK: Public

public typealias CollisionResponseType = SpriteCollisionResponseType

public class CollisionInfo {
// MARK: Lifecycle

init(collisions: UnsafeBufferPointer<SpriteCollisionInfo>, actual: Point<Float>) {
self.collisions = collisions
self.actual = actual
}

deinit { collisions.deallocate() }

// MARK: Public

public let collisions: UnsafeBufferPointer<SpriteCollisionInfo>
public let actual: Point<Float>
}

public class QueryInfo {
// MARK: Lifecycle

init(info: UnsafeBufferPointer<SpriteQueryInfo>) {
self.info = info
}

deinit {
info.deallocate()
}

// MARK: Public

public let info: UnsafeBufferPointer<SpriteQueryInfo>
}

// MARK: - Properties
Expand Down Expand Up @@ -438,6 +468,12 @@ public enum Sprite {
return UnsafeBufferPointer(start: sprites, count: Int(length))
}

// MARK: Internal

static func getUserdata(_ sprite: OpaquePointer) -> UnsafeMutableRawPointer? {
Self.sprite.getUserdata(sprite)
}

// MARK: Private

private static var sprite: playdate_sprite { Playdate.playdateAPI.sprite.pointee }
Expand Down

0 comments on commit 8b72e74

Please sign in to comment.