Skip to content

Commit

Permalink
Various improvements (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
finnvoor authored Aug 15, 2024
1 parent 082d8ab commit 5237983
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Sources/CPlaydate/include/CPlaydate.apinotes
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,11 @@ Enumerators:
SwiftName: overlap
- Name: kCollisionTypeBounce
SwiftName: bounce
- Name: kBitmapUnflipped
SwiftName: unflipped
- Name: kBitmapFlippedX
SwiftName: flippedX
- Name: kBitmapFlippedY
SwiftName: flippedY
- Name: kBitmapFlippedXY
SwiftName: flippedXY
14 changes: 11 additions & 3 deletions Sources/PlaydateKit/Core/Graphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public enum Graphics {
}

/// Allocates and returns a new `width` by `height` `Bitmap` filled with `bgcolor`.
public init(width: Int, height: Int, bgColor: Color) {
public init(width: Int, height: Int, bgColor: Color = .clear) {
pointer = bgColor.withLCDColor {
graphics.newBitmap.unsafelyUnwrapped(CInt(width), CInt(height), $0).unsafelyUnwrapped
}
Expand Down Expand Up @@ -136,7 +136,7 @@ public enum Graphics {
}

/// Clears the bitmap, filling with the given `bgcolor`.
public func clear(bgColor: Color) {
public func clear(bgColor: Color = .clear) {
bgColor.withLCDColor {
graphics.clearBitmap.unsafelyUnwrapped(pointer, $0)
}
Expand Down Expand Up @@ -273,6 +273,10 @@ public enum Graphics {
graphics.getTableBitmap.unsafelyUnwrapped(pointer, CInt(index)).map { Bitmap(pointer: $0) }
}

public subscript(_ index: Int) -> Bitmap? {
bitmap(at: index)
}

/// Loads the image table at `path` into the previously allocated `table`.
public func load(from path: String) throws(Playdate.Error) {
var error: UnsafePointer<CChar>?
Expand Down Expand Up @@ -511,7 +515,11 @@ public enum Graphics {
}

/// Draws the `bitmap` with its upper-left corner at location `point`, using the given `flip` orientation.
public static func drawBitmap(_ bitmap: Bitmap, at point: Point, flip: Bitmap.Flip) {
public static func drawBitmap(
_ bitmap: Bitmap,
at point: Point = .zero,
flip: Bitmap.Flip = .unflipped
) {
graphics.drawBitmap.unsafelyUnwrapped(bitmap.pointer, CInt(point.x), CInt(point.y), flip)
}

Expand Down
18 changes: 18 additions & 0 deletions Sources/PlaydateKit/Geometry/Rect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ public struct Rect: Equatable {
public static var zero: Rect { Rect(x: 0, y: 0, width: 0, height: 0) }

public var x, y, width, height: Float

public var origin: Point {
get {
Point(x: x, y: y)
} set {
x = newValue.x
y = newValue.y
}
}

public var center: Point {
get {
Point(x: x + width / 2, y: y + height / 2)
} set {
x = newValue.x - width / 2
y = newValue.y - height / 2
}
}
}

// MARK: AffineTransformable
Expand Down

0 comments on commit 5237983

Please sign in to comment.