Skip to content

Commit

Permalink
Replace CInt usage with Int (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
finnvoor authored Aug 11, 2024
1 parent 778178f commit 419b8a5
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 96 deletions.
12 changes: 6 additions & 6 deletions Sources/PlaydateKit/Core/Display.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions Sources/PlaydateKit/Core/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,47 @@ 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`.
/// Returns the number of bytes read (0 indicating end of 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
Expand Down
92 changes: 48 additions & 44 deletions Sources/PlaydateKit/Core/Graphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,7 +38,7 @@ public enum Graphics {
&frameCount,
&currentFrame
)
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
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -156,11 +156,11 @@ public enum Graphics {
mask: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>?,
data: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>?
) -> (
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
Expand All @@ -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
Expand Down Expand Up @@ -242,31 +242,35 @@ 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) }

// 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`.
Expand Down Expand Up @@ -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?
Expand All @@ -339,7 +343,7 @@ public enum Graphics {
return (
pageGlyph.map { Glyph(pointer: $0) },
bitmap.map { Bitmap(pointer: $0) },
advance
Int(advance)
)
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -574,22 +578,22 @@ 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).
/// If `startAngle` != `endAngle`, this draws an arc between the given angles.
/// 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
Expand All @@ -600,7 +604,7 @@ public enum Graphics {
CInt(rect.y),
CInt(rect.width),
CInt(rect.height),
lineWidth,
CInt(lineWidth),
startAngle,
endAngle,
$0
Expand Down Expand Up @@ -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 {
Expand All @@ -641,7 +645,7 @@ public enum Graphics {
CInt(line.start.y),
CInt(line.end.x),
CInt(line.end.y),
lineWidth,
CInt(lineWidth),
$0
)
}
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions Sources/PlaydateKit/Core/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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.
Expand Down
Loading

0 comments on commit 419b8a5

Please sign in to comment.