diff --git a/Sources/PlaydateKit/Core/Display.swift b/Sources/PlaydateKit/Core/Display.swift index 67511325..9d52c603 100644 --- a/Sources/PlaydateKit/Core/Display.swift +++ b/Sources/PlaydateKit/Core/Display.swift @@ -6,14 +6,14 @@ public enum Display { /// Returns the height of the display, taking the current scale into account; /// e.g., if the scale is 2, this function returns 120 instead of 240. - public static var height: CInt { - display.getHeight.unsafelyUnwrapped() + public static var height: Int { + Int(display.getHeight.unsafelyUnwrapped()) } /// Returns the width of the display, taking the current scale into account; /// e.g., if the scale is 2, this function returns 200 instead of 400. - public static var width: CInt { - display.getWidth.unsafelyUnwrapped() + public static var width: Int { + Int(display.getWidth.unsafelyUnwrapped()) } /// The nominal refresh rate in frames per second. The default is 30 fps, which is a recommended @@ -77,8 +77,8 @@ public enum Display { /// Offsets the display by the given amount. /// Areas outside of the displayed area are filled with the current background color. - public static func setOffset(dx: CInt, dy: CInt) { - display.setOffset.unsafelyUnwrapped(dx, dy) + public static func setOffset(dx: Int, dy: Int) { + display.setOffset.unsafelyUnwrapped(CInt(dx), CInt(dy)) } // MARK: Private diff --git a/Sources/PlaydateKit/Core/File.swift b/Sources/PlaydateKit/Core/File.swift index ff8a903d..a1723abb 100644 --- a/Sources/PlaydateKit/Core/File.swift +++ b/Sources/PlaydateKit/Core/File.swift @@ -24,10 +24,10 @@ public enum File { } /// Flushes the output buffer of file immediately. Returns the number of bytes written. - public func flush() throws(Playdate.Error) -> CInt { + public func flush() throws(Playdate.Error) -> Int { let writtenCount = file.flush.unsafelyUnwrapped(pointer) guard writtenCount != -1 else { throw lastError } - return writtenCount + return Int(writtenCount) } /// Reads up to `length` bytes from the file handle into the buffer `buffer`. @@ -35,36 +35,36 @@ public enum File { public func read( buffer: UnsafeMutableRawPointer, length: CUnsignedInt - ) throws(Playdate.Error) -> CInt { + ) throws(Playdate.Error) -> Int { let readCount = file.read.unsafelyUnwrapped(pointer, buffer, length) guard readCount != -1 else { throw lastError } - return readCount + return Int(readCount) } /// Sets the read/write offset in the file handle to `position`, relative to the `seek`. public func seek( - to position: CInt, + to position: Int, seek: Seek = .current ) throws(Playdate.Error) { - guard file.seek.unsafelyUnwrapped(pointer, position, seek.rawValue) == 0 else { + guard file.seek.unsafelyUnwrapped(pointer, CInt(position), seek.rawValue) == 0 else { throw lastError } } /// Returns the current read/write offset in the given file handle - public func currentSeekPosition() throws(Playdate.Error) -> CInt { + public func currentSeekPosition() throws(Playdate.Error) -> Int { let offset = file.tell.unsafelyUnwrapped(pointer) guard offset != 0 else { throw lastError } - return offset + return Int(offset) } /// Writes the buffer of bytes `buffer` to the file. Returns the number of bytes written public func write( buffer: UnsafeRawBufferPointer - ) throws(Playdate.Error) -> CInt { + ) throws(Playdate.Error) -> Int { let writtenCount = file.write.unsafelyUnwrapped(pointer, buffer.baseAddress, CUnsignedInt(buffer.count)) guard writtenCount != -1 else { throw lastError } - return writtenCount + return Int(writtenCount) } // MARK: Internal diff --git a/Sources/PlaydateKit/Core/Graphics.swift b/Sources/PlaydateKit/Core/Graphics.swift index 1e3a23f1..d46065fe 100644 --- a/Sources/PlaydateKit/Core/Graphics.swift +++ b/Sources/PlaydateKit/Core/Graphics.swift @@ -23,9 +23,9 @@ public enum Graphics { /// Retrieves information about the video. public var info: ( - width: CInt, height: CInt, + width: Int, height: Int, frameRate: Float, - frameCount: CInt, currentFrame: CInt + frameCount: Int, currentFrame: Int ) { var width: CInt = 0, height: CInt = 0 var frameRate: Float = 0 @@ -38,7 +38,7 @@ public enum Graphics { &frameCount, ¤tFrame ) - return (width, height, frameRate, frameCount, currentFrame) + return (Int(width), Int(height), frameRate, Int(frameCount), Int(currentFrame)) } /// Gets the rendering destination for the video player. If no rendering context has been set, a context bitmap with the same @@ -60,8 +60,8 @@ public enum Graphics { } /// Renders frame number `frameNumber` into the current context. - public func renderFrame(_ frameNumber: CInt) throws(Playdate.Error) { - guard video.renderFrame.unsafelyUnwrapped(pointer, frameNumber) != 0 else { + public func renderFrame(_ frameNumber: Int) throws(Playdate.Error) { + guard video.renderFrame.unsafelyUnwrapped(pointer, CInt(frameNumber)) != 0 else { throw error } } @@ -101,9 +101,9 @@ public enum Graphics { } /// Allocates and returns a new `width` by `height` `Bitmap` filled with `bgcolor`. - public init(width: CInt, height: CInt, bgColor: Color) { + public init(width: Int, height: Int, bgColor: Color) { pointer = bgColor.withLCDColor { - graphics.newBitmap.unsafelyUnwrapped(width, height, $0).unsafelyUnwrapped + graphics.newBitmap.unsafelyUnwrapped(CInt(width), CInt(height), $0).unsafelyUnwrapped } free = true } @@ -156,11 +156,11 @@ public enum Graphics { mask: UnsafeMutablePointer?>?, data: UnsafeMutablePointer?>? ) -> ( - width: CInt, height: CInt, rowBytes: CInt + width: Int, height: Int, rowBytes: Int ) { var width: CInt = 0, height: CInt = 0, rowBytes: CInt = 0 graphics.getBitmapData.unsafelyUnwrapped(pointer, &width, &height, &rowBytes, mask, data) - return (width, height, rowBytes) + return (Int(width), Int(height), Int(rowBytes)) } /// Gets the color of the pixel at `point` in the bitmap. If the coordinate is outside the bounds @@ -173,11 +173,11 @@ public enum Graphics { /// Returns a new, rotated and scaled `Bitmap` based on the given `bitmap`. public func rotated(by rotation: Float, xScale: Float, yScale: Float) -> ( bitmap: Bitmap, - allocatedSize: CInt + allocatedSize: Int ) { var allocatedSize: CInt = 0 let bitmap = graphics.rotatedBitmap.unsafelyUnwrapped(pointer, rotation, xScale, yScale, &allocatedSize).unsafelyUnwrapped - return (Bitmap(pointer: bitmap), allocatedSize) + return (Bitmap(pointer: bitmap), Int(allocatedSize)) } // MARK: Internal @@ -242,8 +242,12 @@ public enum Graphics { } /// Allocates and returns a new `BitmapTable` that can hold `count` `width` by `height` `Bitmaps`. - public init(count: CInt, width: CInt, height: CInt) { - pointer = graphics.newBitmapTable.unsafelyUnwrapped(count, width, height).unsafelyUnwrapped + public init(count: Int, width: Int, height: Int) { + pointer = graphics.newBitmapTable.unsafelyUnwrapped( + CInt(count), + CInt(width), + CInt(height) + ).unsafelyUnwrapped } deinit { graphics.freeBitmapTable(pointer) } @@ -251,22 +255,22 @@ public enum Graphics { // MARK: Public /// The table's image count. - public var imageCount: CInt { + public var imageCount: Int { var count: CInt = 0 graphics.getBitmapTableInfo.unsafelyUnwrapped(pointer, &count, nil) - return count + return Int(count) } /// The number of cells across. - public var cellsWide: CInt { + public var cellsWide: Int { var cellsWide: CInt = 0 graphics.getBitmapTableInfo.unsafelyUnwrapped(pointer, nil, &cellsWide) - return cellsWide + return Int(cellsWide) } /// Returns the `index` bitmap in `table`, If `index` is out of bounds, the function returns nil. - public func bitmap(at index: CInt) -> Bitmap? { - graphics.getTableBitmap.unsafelyUnwrapped(pointer, index).map { Bitmap(pointer: $0) } + public func bitmap(at index: Int) -> Bitmap? { + graphics.getTableBitmap.unsafelyUnwrapped(pointer, CInt(index)).map { Bitmap(pointer: $0) } } /// Loads the image table at `path` into the previously allocated `table`. @@ -326,7 +330,7 @@ public enum Graphics { public func glyph(for character: CUnsignedInt) -> ( pageGlyph: Glyph?, bitmap: Bitmap?, - advance: CInt + advance: Int ) { var advance: CInt = 0 var bitmap: OpaquePointer? @@ -339,7 +343,7 @@ public enum Graphics { return ( pageGlyph.map { Glyph(pointer: $0) }, bitmap.map { Bitmap(pointer: $0) }, - advance + Int(advance) ) } @@ -362,8 +366,8 @@ public enum Graphics { // MARK: Public /// Returns the kerning adjustment between characters `character1` and `character2` as specified by the font. - public func kerning(between character1: CUnsignedInt, and character2: CUnsignedInt) -> CInt { - graphics.getGlyphKerning.unsafelyUnwrapped(pointer, character1, character2) + public func kerning(between character1: CUnsignedInt, and character2: CUnsignedInt) -> Int { + Int(graphics.getGlyphKerning.unsafelyUnwrapped(pointer, character1, character2)) } // MARK: Private @@ -379,15 +383,15 @@ public enum Graphics { /// Returns the width of the given `text` in the font. public func getTextWidth( for text: String, - tracking: CInt - ) -> CInt { - graphics.getTextWidth.unsafelyUnwrapped( + tracking: Int + ) -> Int { + Int(graphics.getTextWidth.unsafelyUnwrapped( pointer, text, text.utf8.count, .kUTF8Encoding, - tracking - ) + CInt(tracking) + )) } /// Returns a `Font.Page` object for the given character code. Each font page contains information @@ -404,9 +408,9 @@ public enum Graphics { } /// The tracking to use when drawing text. - public static var textTracking: CInt { - get { graphics.getTextTracking.unsafelyUnwrapped() } - set { graphics.setTextTracking.unsafelyUnwrapped(newValue) } + public static var textTracking: Int { + get { Int(graphics.getTextTracking.unsafelyUnwrapped()) } + set { graphics.setTextTracking.unsafelyUnwrapped(CInt(newValue)) } } /// The mode used for drawing bitmaps. Note that text drawing uses bitmaps, so this affects how fonts are displayed as well. @@ -477,8 +481,8 @@ public enum Graphics { } /// Sets the leading adjustment (added to the leading specified in the font) to use when drawing text. - public static func setTextLeading(_ leading: CInt) { - graphics.setTextLeading.unsafelyUnwrapped(leading) + public static func setTextLeading(_ leading: Int) { + graphics.setTextLeading.unsafelyUnwrapped(CInt(leading)) } /// Returns true if any of the opaque pixels in `bitmap1` when positioned at `point1` with `flip1` overlap any @@ -574,14 +578,14 @@ public enum Graphics { @discardableResult public static func drawText( _ text: String, at point: Point - ) -> CInt { - graphics.drawText.unsafelyUnwrapped( + ) -> Int { + Int(graphics.drawText.unsafelyUnwrapped( text, text.utf8.count, .kUTF8Encoding, CInt(point.x), CInt(point.y) - ) + )) } /// Draws an ellipse inside the rectangle `rect` of width `lineWidth` (inset from the rectangle bounds). @@ -589,7 +593,7 @@ public enum Graphics { /// Angles are given in degrees, clockwise from due north. public static func drawEllipse( in rect: Rect, - lineWidth: CInt = 1, + lineWidth: Int = 1, startAngle: Float = 0, endAngle: Float = 360, color: Color = .black @@ -600,7 +604,7 @@ public enum Graphics { CInt(rect.y), CInt(rect.width), CInt(rect.height), - lineWidth, + CInt(lineWidth), startAngle, endAngle, $0 @@ -632,7 +636,7 @@ public enum Graphics { /// Draws `line` with a stroke width of `lineWidth` and color `color`. public static func drawLine( _ line: Line, - lineWidth: CInt = 1, + lineWidth: Int = 1, color: Color = .black ) { color.withLCDColor { @@ -641,7 +645,7 @@ public enum Graphics { CInt(line.start.y), CInt(line.end.x), CInt(line.end.y), - lineWidth, + CInt(lineWidth), $0 ) } @@ -762,15 +766,15 @@ public enum Graphics { /// After updating pixels in the buffer returned by `getFrame()`, you must tell the graphics system which rows were updated. /// This function marks a contiguous range of rows as updated (e.g., `markUpdatedRows(0, LCD_ROWS - 1)` tells the system /// to update the entire display). Both `start` and `end` are included in the range. - public static func markUpdatedRows(start: CInt, end: CInt) { - graphics.markUpdatedRows.unsafelyUnwrapped(start, end) + public static func markUpdatedRows(start: Int, end: Int) { + graphics.markUpdatedRows.unsafelyUnwrapped(CInt(start), CInt(end)) } /// Offsets the origin point for all drawing calls to `dx`, `dy` (can be negative). /// /// This is useful, for example, for centering a "camera" on a sprite that is moving around a world larger than the screen. - public static func setDrawOffset(dx: CInt, dy: CInt) { - graphics.setDrawOffset.unsafelyUnwrapped(dx, dy) + public static func setDrawOffset(dx: Int, dy: Int) { + graphics.setDrawOffset.unsafelyUnwrapped(CInt(dx), CInt(dy)) } /// Returns a color using an 8 x 8 pattern using the given `bitmap`. `topLeft` indicates the top left corner of the 8 x 8 pattern. diff --git a/Sources/PlaydateKit/Core/JSON.swift b/Sources/PlaydateKit/Core/JSON.swift index 259018fd..c47096a7 100644 --- a/Sources/PlaydateKit/Core/JSON.swift +++ b/Sources/PlaydateKit/Core/JSON.swift @@ -16,8 +16,8 @@ public enum JSON { using decoder: inout Decoder, reader: Reader, value: inout Value - ) -> CInt { - json.decode.unsafelyUnwrapped(&decoder, reader, &value) + ) -> Int { + Int(json.decode.unsafelyUnwrapped(&decoder, reader, &value)) } /// Decodes a JSON string with the given `decoder`. An instance of `Decoder` must implement `decodeError`. @@ -27,8 +27,8 @@ public enum JSON { using decoder: inout Decoder, jsonString: String, value: inout Value - ) -> CInt { - json.decodeString.unsafelyUnwrapped(&decoder, jsonString, &value) + ) -> Int { + Int(json.decodeString.unsafelyUnwrapped(&decoder, jsonString, &value)) } /// Populates the given `Encoder` `encoder` with the functions necessary to encode arbitrary data into a JSON string. diff --git a/Sources/PlaydateKit/Core/Scoreboards.swift b/Sources/PlaydateKit/Core/Scoreboards.swift index ac08e03b..dd50b10b 100644 --- a/Sources/PlaydateKit/Core/Scoreboards.swift +++ b/Sources/PlaydateKit/Core/Scoreboards.swift @@ -21,8 +21,8 @@ public enum Scoreboards { _ score: UnsafeMutablePointer?, _ errorMessage: UnsafePointer? ) -> Void)? = nil - ) -> CInt { - scoreboards.addScore.unsafelyUnwrapped(boardID, value, callback) + ) -> Int { + Int(scoreboards.addScore.unsafelyUnwrapped(boardID, value, callback)) } /// Gets the player’s personal best score. Invokes the given callback with the score. @@ -35,8 +35,8 @@ public enum Scoreboards { _ score: UnsafeMutablePointer?, _ errorMessage: UnsafePointer? ) -> Void)? = nil - ) -> CInt { - scoreboards.getPersonalBest.unsafelyUnwrapped(boardID, callback) + ) -> Int { + Int(scoreboards.getPersonalBest.unsafelyUnwrapped(boardID, callback)) } /// Free a score struct that was provided to a callback. @@ -51,8 +51,8 @@ public enum Scoreboards { _ boards: UnsafeMutablePointer?, _ errorMessage: UnsafePointer? ) -> Void - ) -> CInt { - scoreboards.getScoreboards.unsafelyUnwrapped(callback) + ) -> Int { + Int(scoreboards.getScoreboards.unsafelyUnwrapped(callback)) } /// Free a list of scoreboards. @@ -66,8 +66,8 @@ public enum Scoreboards { _ scores: UnsafeMutablePointer?, _ errorMessage: UnsafePointer? ) -> Void - ) -> CInt { - scoreboards.getScores.unsafelyUnwrapped(boardID, callback) + ) -> Int { + Int(scoreboards.getScores.unsafelyUnwrapped(boardID, callback)) } /// Free a list of scores. diff --git a/Sources/PlaydateKit/Core/Sprite.swift b/Sources/PlaydateKit/Core/Sprite.swift index 982e4663..8cde18d6 100644 --- a/Sources/PlaydateKit/Core/Sprite.swift +++ b/Sources/PlaydateKit/Core/Sprite.swift @@ -213,9 +213,9 @@ public enum Sprite { /// Specifies a stencil image to be set on the frame buffer before the sprite is drawn. If `tile` is set, the stencil will be tiled. /// Tiled stencils must have width evenly divisible by 32. - public func setStencilImage(_ stencil: Graphics.Bitmap, tile: CInt) { + public func setStencilImage(_ stencil: Graphics.Bitmap, tile: Int) { _stencil = stencil - sprite.setStencilImage.unsafelyUnwrapped(pointer, stencil.pointer, tile) + sprite.setStencilImage.unsafelyUnwrapped(pointer, stencil.pointer, CInt(tile)) } /// Sets the sprite’s stencil to the given pattern. @@ -284,7 +284,7 @@ public enum Sprite { /// Moves the given sprite towards `goal` taking collisions into account and returns an array of `SpriteCollisionInfo`. /// `actualX`, `actualY` are set to the sprite’s position after collisions. If no collisions occurred, this will be the same as /// `goalX`, `goalY`. - public func moveWithCollisions(goal: Point) -> CollisionInfo { + @discardableResult public func moveWithCollisions(goal: Point) -> CollisionInfo { var actualX: Float = 0, actualY: Float = 0 var length: CInt = 0 let collisionInfo = sprite.moveWithCollisions.unsafelyUnwrapped( @@ -387,13 +387,13 @@ public enum Sprite { // MARK: - Properties /// Sets the clipping rectangle for all sprites with a Z index within `startZ` and `endZ` inclusive. - public static func setClipRectsInRange(clipRect: Rect, startZ: CInt, endZ: CInt) { - sprite.setClipRectsInRange.unsafelyUnwrapped(clipRect.lcdRect, startZ, endZ) + public static func setClipRectsInRange(clipRect: Rect, startZ: Int, endZ: Int) { + sprite.setClipRectsInRange.unsafelyUnwrapped(clipRect.lcdRect, CInt(startZ), CInt(endZ)) } /// Clears the clipping rectangle for all sprites with a Z index within `startZ` and `endZ` inclusive. - public static func clearClipRectsInRange(startZ: CInt, endZ: CInt) { - sprite.clearClipRectsInRange.unsafelyUnwrapped(startZ, endZ) + public static func clearClipRectsInRange(startZ: Int, endZ: Int) { + sprite.clearClipRectsInRange.unsafelyUnwrapped(CInt(startZ), CInt(endZ)) } /// When `alwaysRedraw` is set to true, this causes all sprites to draw each frame, whether or not they have been marked dirty. @@ -423,8 +423,8 @@ public enum Sprite { } /// Returns the total number of sprites in the display list. - public static func getDisplayListSpriteCount() -> CInt { - sprite.getSpriteCount.unsafelyUnwrapped() + public static func getDisplayListSpriteCount() -> Int { + Int(sprite.getSpriteCount.unsafelyUnwrapped()) } /// Draws every sprite in the display list. diff --git a/Sources/PlaydateKit/Core/System.swift b/Sources/PlaydateKit/Core/System.swift index 8e5c6437..42f54c17 100644 --- a/Sources/PlaydateKit/Core/System.swift +++ b/Sources/PlaydateKit/Core/System.swift @@ -19,14 +19,14 @@ public enum System { // MARK: Public /// The currently selected option. - public var selectedOption: CInt { + public var selectedOption: Int { get { value } set { value = newValue } } // MARK: Internal - var optionsCallback: ((CInt) -> Void)? + var optionsCallback: ((Int) -> Void)? } /// A menu item that can be checked or unchecked by the player. @@ -79,9 +79,9 @@ public enum System { /// /// For checkmark menu items, 1 means checked, 0 unchecked. /// For option menu items, the value indicates the array index of the currently selected option. - var value: CInt { - get { system.getMenuItemValue.unsafelyUnwrapped(pointer) } - set { system.setMenuItemValue.unsafelyUnwrapped(pointer, newValue) } + var value: Int { + get { Int(system.getMenuItemValue.unsafelyUnwrapped(pointer)) } + set { system.setMenuItemValue.unsafelyUnwrapped(pointer, CInt(newValue)) } } /// Gets/sets the userdata value associated with this menu item. @@ -133,8 +133,8 @@ public enum System { } /// Returns the system timezone offset from GMT, in seconds. - public static var timezoneOffset: CInt { - system.getTimezoneOffset.unsafelyUnwrapped() + public static var timezoneOffset: Int { + Int(system.getTimezoneOffset.unsafelyUnwrapped()) } /// Returns true if the user has set the 24-Hour Time preference in the Settings program. @@ -307,7 +307,7 @@ public enum System { @discardableResult public static func addOptionsMenuItem( title: String, options: UnsafeMutableBufferPointer?>, - callback: ((CInt) -> Void)? = nil + callback: ((Int) -> Void)? = nil ) -> OptionsMenuItem { let pointer = system.addOptionsMenuItem.unsafelyUnwrapped( title, @@ -371,8 +371,8 @@ public enum System { /// to animate to a position offset left by xoffset pixels as the menu is animated in. /// /// This function could be called in response to the kEventPause event in your implementation of eventHandler(). - public static func setMenuImage(_ bitmap: Graphics.Bitmap, xOffset: CInt = 0) { - system.setMenuImage.unsafelyUnwrapped(bitmap.pointer, xOffset) + public static func setMenuImage(_ bitmap: Graphics.Bitmap, xOffset: Int = 0) { + system.setMenuImage.unsafelyUnwrapped(bitmap.pointer, CInt(xOffset)) } /// Provides a callback to receive messages sent to the device over the serial port using the msg command. @@ -419,15 +419,15 @@ public enum System { _ userdata: UnsafeMutableRawPointer? ) -> Bool)?, buttonUserdata: UnsafeMutableRawPointer? = nil, - queueSize: CInt = 5 + queueSize: Int = 5 ) { buttonCallback = callback if callback != nil { system.setButtonCallback.unsafelyUnwrapped({ button, down, when, userdata in (System.buttonCallback?(button, down != 0, when, userdata) ?? false) ? 0 : 1 - }, buttonUserdata, queueSize) + }, buttonUserdata, CInt(queueSize)) } else { - system.setButtonCallback.unsafelyUnwrapped(nil, buttonUserdata, queueSize) + system.setButtonCallback.unsafelyUnwrapped(nil, buttonUserdata, CInt(queueSize)) } } diff --git a/Sources/PlaydateKit/Geometry/Point.swift b/Sources/PlaydateKit/Geometry/Point.swift index b4c5d748..2ca5fd48 100644 --- a/Sources/PlaydateKit/Geometry/Point.swift +++ b/Sources/PlaydateKit/Geometry/Point.swift @@ -9,7 +9,7 @@ public struct Point: Equatable { self.y = y } - @_disfavoredOverload public init(x: CInt, y: CInt) { + @_disfavoredOverload public init(x: Int, y: Int) { self.x = Float(x) self.y = Float(y) } diff --git a/Sources/PlaydateKit/Geometry/Rect.swift b/Sources/PlaydateKit/Geometry/Rect.swift index c066b6d7..2c2efc6d 100644 --- a/Sources/PlaydateKit/Geometry/Rect.swift +++ b/Sources/PlaydateKit/Geometry/Rect.swift @@ -11,7 +11,7 @@ public struct Rect: Equatable { self.height = height } - @_disfavoredOverload public init(x: CInt, y: CInt, width: CInt, height: CInt) { + @_disfavoredOverload public init(x: Int, y: Int, width: Int, height: Int) { self.x = Float(x) self.y = Float(y) self.width = Float(width)