Skip to content

Commit

Permalink
Implement pdn (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nycto committed Feb 14, 2025
1 parent 775bf90 commit aae61bd
Show file tree
Hide file tree
Showing 17 changed files with 379 additions and 268 deletions.
8 changes: 7 additions & 1 deletion .github/actions/build-setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ runs:
- run: nimble --accept refresh
shell: bash

- run: nimble install
- run: nimble install --verbose
shell: bash

- run: echo "/github/home/.nimble/bin" >> $GITHUB_PATH
shell: bash

- run: which pdn
shell: bash

- name: Locally publish playdate nimble package
Expand Down
5 changes: 0 additions & 5 deletions .github/actions/project-setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ runs:
using: "composite"
steps:

- name: Nimble example setup
shell: bash
working-directory: ${{ inputs.working-directory }}
run: nimble configure;

- name: Install dependencies
shell: bash
working-directory: ${{ inputs.working-directory }}
Expand Down
13 changes: 5 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ jobs:
- uses: ./.github/actions/project-setup
with:
working-directory: ./playdate_example
- run: nimble simulator
- run: pdn simulator
working-directory: ./playdate_example

example-project:
strategy:
matrix:
Expand All @@ -30,7 +30,7 @@ jobs:
- uses: ./.github/actions/project-setup
with:
working-directory: ./playdate_example
- run: nimble ${{ matrix.target }}
- run: pdn ${{ matrix.target }}
working-directory: ./playdate_example

tests:
Expand Down Expand Up @@ -77,10 +77,7 @@ jobs:
pulseaudio -D --exit-idle-time=-1
pactl load-module module-null-sink sink_name=SpeakerOutput sink_properties=device.description="Dummy_Output"
- run: echo 'switch("d", "${{ matrix.build-flags }}")' >> configs.nims
working-directory: ./tests

- run: nimble simulator
- run: pdn simulator -d:${{ matrix.build-flags }}
working-directory: ./tests

# The first time the simulator runs, it prompts the user with an alert. Obviously, we're running headless,
Expand All @@ -99,4 +96,4 @@ jobs:
working-directory: ./tests
run: |
export HOME="/config"
xvfb-run ../PlaydateSDK-*/bin/PlaydateSimulator tests.pdx
xvfb-run ../PlaydateSDK-*/bin/PlaydateSimulator playdate_tests.pdx
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ htmldocs/
src/tests/
pdex.bin
*.pdx
tests/source/pdxinfo

# macOS general
.DS_Store
Expand Down Expand Up @@ -58,4 +59,9 @@ tests/t_*
tests/playdate_tests

# IntelliJ IDEA
.idea
.idea
nimble.develop
nimble.paths

# Binaries
pdn
64 changes: 29 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,42 +47,31 @@ If you want to start from scratch, here are the steps to follow:

1. If you haven't done it already, start creating a folder (snake_case) and initializing your nimble package inside it running:

```
nimble init
```
Choose `binary` as the package type.
```shell
nimble init
```

Choose `binary` as the package type.

2. Install the `playdate` package:

```
nimble install playdate
```
```shell
nimble install playdate
```

3. Add the `playdate` package as a dependency and configure the build tasks by running the following:
3. Add the `playdate` package as a dependency in your nimble file:

```
echo 'requires "playdate"' >> *.nimble;
echo 'include playdate/build/nimble' >> *.nimble;
echo 'include playdate/build/config' > config.nims;
```

4. Finally, run this command to setup the structure of the project, which prepares your application to be compiled and bundled correctly:

```
nimble configure
```
```shell
echo 'requires "playdate"' >> *.nimble;
```

## Usage

If you haven't done it already, install the package:
```
nimble install playdate
```

`playdate_example` contains a basic example of the bindings utilization.
The example code is in `playdate_example/src/playdate_example.nim`.

Here's also a minimal snippet to make a Nim application:

```nim
import playdate/api
Expand All @@ -96,35 +85,38 @@ proc handler(event: PDSystemEvent, keycode: uint) {.raises: [].} =
if event == kEventInit:
# Errors are handled through exceptions, this is an inline try/except
nimLogoBitmap = try: playdate.graphics.newBitmap("/images/nim_logo") except: nil
# playdate is the global PlaydateAPI instance, available when playdate/api is imported
# playdate is the global PlaydateAPI instance, available when playdate/api is imported
playdate.system.setUpdateCallback(update)
# Used to setup the SDK entrypoint
initSDK()
```

Compile the project (pdx) for the simulator using:
Compile the project (pdx) for the simulator using the `pdn` binary that gets added when
you ran `nimble install playdate`:

```sh
nimble simulator
pdn simulator
```

For the device (pdx):

```sh
nimble device
```
For simulator + device (pdx):
```sh
nimble all
pdn device
```

You can also build for simulator and launch it in one command:

```sh
nimble simulate
pdn simulate
```

The example project `playdate_example` also contains VSCode launch configurations to build, start and debug your Nim application from the editor.

Each project contains a `config.nims` file that can be edited to customize how the project should be built, e.g. adding libraries or other external code.<br>
Each project contains a `config.nims` file that can be edited to customize how the project should be built, e.g. adding libraries or other external code.

Here's an example of a `config.nims` that links a pre-built static library called chipmunk:

```nim
include playdate/build/config
Expand All @@ -151,6 +143,7 @@ There are two ways you can use Nim and Lua in the same project:
2. The main loop is defined in Lua, but you want to call Nim functions.

Either way, you can provide Lua with your Nim functions during Lua initialization:

```nim
proc nimInsideLua(state: LuaStatePtr): cint {.cdecl, raises: [].} = ...
Expand All @@ -164,9 +157,10 @@ proc handler(event: PDSystemEvent, keycode: uint) {.raises: [].} =
```

Calling a Lua function from Nim:

```nim
try:
# Push the argument first
# Push the argument first
playdate.lua.pushInt(5)
playdate.lua.callFunction("funcWithOneArgument", 1)
except:
Expand Down
4 changes: 4 additions & 0 deletions config.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# begin Nimble config (version 2)
when withDir(thisDir(), system.fileExists("nimble.paths")):
include "nimble.paths"
# end Nimble config
4 changes: 2 additions & 2 deletions playdate.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ author = "Samuele Zolfanelli"
description = "Playdate Nim bindings with extra features."
license = "MIT"
srcDir = "src"

installExt = @["nim"]
bin = @["pdn"]

# Dependencies

requires "nim >= 2.0.0"
# requires "nimble < 0.14.0"
3 changes: 3 additions & 0 deletions playdate_example/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*.pdx
source/pdex.*
*.dSYM
playdate_example.pdx.zip
playdate_example.pdx
source/pdxinfo
4 changes: 0 additions & 4 deletions playdate_example/source/pdxinfo

This file was deleted.

66 changes: 66 additions & 0 deletions src/pdn.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import std/[parseopt, strutils, os, macros, options]
import playdate/build/[utils, actions]

type
BuildCommand = enum simulate, simulator, device, clean, bundle
## The various actions that can be executed by this script

CliConfig = object
## Configurations collected from the Cli
command: BuildCommand
noAutoConfig: bool
sdkPath: Option[string]
extraArgs: seq[string]

proc getCliConfig(): CliConfig =
## Parses the build configuration from the input options
for kind, key, val in getopt():
template addExtraArg() =
var command = if kind == cmdLongOption: "--" else: "-"
command &= key
if val != "":
command &= ":" & val
result.extraArgs.add(command)

case kind
of cmdArgument:
result.command = parseEnum[BuildCommand](key)
of cmdLongOption, cmdShortOption:
case key
of "sdk-path": result.sdkPath = some(val)
of "no-auto-config": result.noAutoConfig = true
else: addExtraArg()
of cmdEnd:
discard

let build = getCliConfig()

let dump = getNimbleDump()

let conf = PlaydateConf(
dump: dump,
kind: if build.command == device: DeviceBuild else: SimulatorBuild,
sdkPath: sdkPath(build.sdkPath),
pdxName: dump.name & ".pdx",
nimbleArgs: build.extraArgs,
noAutoConfig: build.noAutoConfig
)

case build.command
of device, simulate, simulator:
conf.configureBuild()
of clean, bundle:
discard

case build.command
of simulator:
conf.simulatorBuild()
of simulate:
conf.simulatorBuild()
conf.runSimulator()
of device:
conf.deviceBuild()
of clean:
conf.runClean()
of bundle:
conf.bundlePDX()
6 changes: 5 additions & 1 deletion src/playdate/bindings/malloc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

{.push stackTrace: off.}

import ../util/initreqs
when (compiles do:
import playdate/util/initreqs):
import playdate/util/initreqs
else:
import ../util/initreqs

when defined(memProfiler):

Expand Down
Loading

0 comments on commit aae61bd

Please sign in to comment.