Skip to content

Commit

Permalink
CHANGE seqBuilder to iterator and other small refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
ninovanhooff committed Oct 25, 2024
1 parent 29a87f1 commit 43b06ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
7 changes: 7 additions & 0 deletions src/playdate/bindings/utils.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import macros

iterator items*[T](rawField: ptr UncheckedArray[T], len: Natural): T =
## iterate through a C array
## To convert to a Nim seq:
## `cArray.items(count).toSeq`
for i in 0..<len:
yield rawField[i]

func toNimSymbol(typeSymbol: string): string =
case typeSymbol:
of "cint":
Expand Down
29 changes: 6 additions & 23 deletions src/playdate/scoreboards.nim
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{.push raises: [].}

import std/importutils
import std/sequtils

import types {.all.}
import bindings/[api, types]
import bindings/[api, types, utils]
import bindings/scoreboards

# Only export public symbols, then import all
export scoreboards
{.hint[DuplicateModuleImport]: off.}
import bindings/scoreboards {.all.}

type
type
PDScore* = object of RootObj
value*, rank*: uint32
player*: string
Expand Down Expand Up @@ -87,17 +88,6 @@ proc scoreBuilder(score: PDScoreRaw): PDScore =
proc scoreBuilder(score: PDScorePtr): PDScore =
scoreBuilder(score[])

proc seqBuilder[T, U](rawField: ptr UncheckedArray[T], length: SomeInteger, itemBuilder: proc (item: T): U {.raises: [].}): seq[U] =
## Coverts a C array to a Nim seq
privateAccess(SDKArray)
let cArray = SDKArray[T](data: rawField, len: length.int)
var newSeq = newSeq[U](length)
for i in 0 ..< length:
let item = cArray[i]
newSeq[i] = itemBuilder(item)
cArray.data = nil # no need for SDKArray to free the data, free function will do it
return newSeq

proc invokePersonalBestCallback(score: PDScorePtr, errorMessage: ConstChar) {.cdecl, raises: [].} =
invokeCallback(
callbackSeqs = privatePersonalBestCallbacks,
Expand Down Expand Up @@ -126,11 +116,7 @@ proc invokeScoresCallback(scoresList: PDScoresListPtr, errorMessage: ConstChar)
freeValue = playdate.scoreboards.freeScoresList,
builder = proc (scoresList: PDScoresListPtr): PDScoresList =
privateAccess(SDKArray)
var scoresSeq = seqBuilder(
rawField = scoresList.scores,
length = scoresList.count,
itemBuilder = scoreBuilder
)
let scoresSeq = scoresList.scores.items(scoresList.count).toSeq.mapIt(scoreBuilder(it))
return newPDScoresList(boardID = $scoresList.boardID, lastUpdated = scoresList.lastUpdated, scores = scoresSeq),
emptyValue = emptyPDScoresList
)
Expand All @@ -142,11 +128,8 @@ proc invokeBoardsListCallback(boardsList: PDBoardsListPtr, errorMessage: ConstCh
errorMessage = errorMessage,
freeValue = playdate.scoreboards.freeBoardsList,
builder = proc (boardsList: PDBoardsListPtr): PDBoardsList =
var boardsSeq = seqBuilder(
rawField = cast[ptr UncheckedArray[PDBoardRaw]](boardsList.boards),
length = boardsList.count,
itemBuilder = proc (board: PDBoardRaw): PDBoard =
newPDBoard(boardID = $board.boardID, name = $board.name)
let boardsSeq: seq[PDBoard] = boardsList.boards.items(boardsList.count).toSeq.mapIt(
newPDBoard(boardID = $it.boardID, name = $it.name)
)
return newPDBoardsList(lastUpdated = boardsList.lastUpdated, boards = boardsSeq),
emptyValue = emptyPDBoardsList
Expand Down

0 comments on commit 43b06ba

Please sign in to comment.