Playdate Nim bindings - C performance, Python like syntax

Now working on a little game to showcase the bindings and better understand what they need down the road!

Playdate Simulator 2023-01-29 at 16.55.45

Will have to wait to have my Playdate to build and test the physics library on the device, not sure how it will behave!

6 Likes

The current version from your GitHub runs quite well on-device!

Are any of your later experiments pushed up? (I don't see any other branches)

Let me know if you'd like me to try anything else.

3 Likes

Right now I'm waiting for feedback by Panic to publish the bindings as a Nim package!

The experiments with physics I'm doing are to tests libraries integration, but I didn't build them for the device yet!

I'll surely update this thread when there's news!

2 Likes

The bindings are now finally available as a Nimble package!

You can check out the repository (link at the top of the thread) for instructions on how to setup the Playdate Nim development environment

3 Likes

Developed a command line tool (spriterot, will be a Nim package too) to generate RotSprite rotations of images.

The idea is to have this tool do the repetitive task of generating rotated bitmap tables automatically at build time and ideally only when the source image has changed.

Starting image:
helix_catcher_arm-rotations-16.png
helix_catcher_arm-rotations-16

Command:

# By default, rotations are generated from 0 to 360 degrees
# Uses count in -rotations-<count> if no override is provided
spriterot ./helix_catcher_arm-rotations-16.png

Result:
helix_catcher_arm-rotations-16-table-58-58.png

Coupled with that, I'm working on adding a new utility object to the Nim bindings:
LCDRotatableBitmap (name could change)

This object will load the automatically generated bitmap table and transparently draw its pre-rendered rotations with the same API of standard LCDBitmaps!

# Auto-resolves the filename
rotatableCatcherArm = try: newRotatableBitmap("/images/helix_catcher_arm", 16) except: nil

...

# Will draw using the rotated pre-rendered frames! Better performance!
rotatableCatcherArm.drawRotated(x, y, angle, 0.5, 0.5, 1, 1)

Here it is in action! (more than 16 frames of course!)
Playdate Simulator 2023-02-05 at 17.55.26

5 Likes

Just released the spriterot command line tool!
Post here!

2 Likes

Thanks to @Nycto (who has been a fantastic contributor) we now have headless testing of the bindings in place!

This, as things develop, will make sure our (and Panic's) future updates don't break anything we worked on without us knowing!
Hurray!

5 Likes

The bindings (v0.8.1) have been updated to properly support OS 2.0! :tada:

5 Likes

Finally added support for Windows in v0.8.5!

Also, the VSCode configuration to build and launch the simulator works on every platform now!

One more thing: while the average user will not have to deal with it, I'm starting to move the build system to a very simple CMake config that will let advanced users easily add libraries and other C code!

3 Likes

The new build system makes it easy to link libraries and do fancy stuff!
We are at v0.9.0 right now.

So, I finally managed to make the bindings work with Chipmunk 2D on device! :tada::tada:
Great performance so far!

IMG_0590 2 8

5 Likes

Well, I went the extra mile and rewrote the CMake build system entirely in Nim.
Version v0.10.0!
Building should be very speedy, it supports incremental builds and per-project customizations.

Now there's no dependencies on Make and CMake, just Nim and a C compiler!

Here's an example from the repo of a config of a projects that links a pre-built static library:

include playdate/build/config

# Add a search path for libraries based on OS.
if defined(device):
    switch("passL", "-L" & getCurrentDir() / "lib" / "device")
elif defined(windows):
    switch("passL", "-L" & getCurrentDir() / "lib" / "windows")
elif defined(macosx):
    switch("passL", "-L" & getCurrentDir() / "lib" / "macos")
elif defined(linux):
    switch("passL", "-L" & getCurrentDir() / "lib" / "linux")
else:
    echo "Platform not supported!"
# Link the chipmunk library.
switch("passL", "-lchipmunk")
9 Likes

Version 0.11.0 just added basic Lua interoperability!
@Nino recently requested the feature, so it had to be added!

Now you can easily do some hybrid Lua/Nim if going full Nim doesn't make you comfortable!

7 Likes

I used it to integrate comic book cutscenes with @cadin 's Panels library. I don't have any drawing talent, but the Panels library gracefully provides example art and full documentation.

Thanks to both!

1 Like

Version 0.12.0 is out!
Thank you @Nycto, @Nino and @paulstraw for your help!

In this release, advanced text drawing functions are now available directly in Nim!
Playdate Simulator 2023-11-29 at 16.18.24-converted-converted

3 Likes

Awesome! Any update to support nim 2.0?

Slowly working on it!

I did a benchmark using @Tengu base template.

Here's my results drawing all (8) the texts in a rect, right aligned!

Lua: 18-19 fps
Nim: 30 fps

Here's the project that includes the Nim version too.
text-in-rect-perf-test.zip (29.1 KB)

3 Likes

30 (default refresh rate limit) implies it could be even faster in Nim

It is 30 fps with unlocked frame rate.
Unfortunately this type of algorithm seems to be bottlenecked by the Playdate struggling a lot doing string manipulations and allocations. I kept them at minimum.

Also, not sure but getTextWidth might be another bottleneck.

1 Like

I hope to share a nim vs Lua benchmark soon, which uses a nim implementation of @matt's benchmark. I'll share it with @samdze first to see if I'm not misrepresenting nim performance due to my Nim noobishness.

Edit: see link below for results

1 Like