Playdate Nim bindings - C performance, Python like syntax

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

Hi All,
I'm very new to Nim and not sure what I'm doing wrong. I've followed the steps on the playdate-nim git page and have come across a few issues. I'm using vscode on wondows, any help is very much appreciated.

Issue 1.

when I get to the step: nimble setup

I get the below error.


   Tip: 1 messages have been suppressed, use --verbose to show them.

packageparser.nim(395) readPackageInfo

Error:  Could not read package info file in E:\playdate\Games_nim\Test_01\Test_01.nimble;
    ...   Reading as ini file failed with:
    ...     Invalid section: .
    ...   Evaluating as NimScript file failed with:
    ...     E:\playdate\Games_nim\Test_01\Test_01.nimble(14, 1) Error: undeclared identifier: 'r'
    ... printPkgInfo() failed.

Turns out the Test_01.nimble file had strange characters in the last two lines and if I replaced the lines with the below it ran the "nimble setup" command without a problem.


requires "playdate"
include playdate/build/nimble

Not sure if this is correct or not?

I've decided to go with the base temple project on git hub "playdate_example" which is the folder vscode is set to but I get this error when I run nimble simulate:


C:\Users\edit2pc\nimcache\playdate_example_d\simulator@mC@c@sUsers@sedit2pc@s.nimble@spkgs@splaydate-0.12.0@splaydate@sfile.nim.c:8:10: fatal error: pd_api.h: No such file or directory
8 | #include "pd_api.h"
| ^~~~~~~~~~
compilation terminated.
C:\Users\edit2pc\nimcache\playdate_example_d\simulator@mC@c@sUsers@sedit2pc@s.nimble@spkgs@splaydate-0.12.0@splaydate@stypes.nim.c:8:10: fatal error: pd_api.h: No such file or directory
8 | #include "pd_api.h"
| ^~~~~~~~~~
compilation terminated.
C:\Users\edit2pc\nimcache\playdate_example_d\simulator@mC@c@sUsers@sedit2pc@s.nimble@spkgs@splaydate-0.12.0@splaydate@ssprite.nim.c:8:10: fatal error: pd_api.h: No such file or directory
8 | #include "pd_api.h"
| ^~~~~~~~~~
compilat i oErnror : teBurilmd ifanilaed ftore pdac.ka
ge: playdate_example
... Execution failed with exit code 1
... Command: C:\Users\edit2pc.nimble\bin\nim.exe c --colors:on --noNimbleP thC:\Users\edit2pc\nimcache\playdate_example_d\simulator@mC@c@sUsers@sedit2pc@s.nimble@spkgs@splaydate-0.12.0@splaydate@ssound.nim.c:8:10: - :si mul tofatal error: r d:debpd_api.h: No such file or directory
8 | #include ug d:"pd_api.h"Ni bl
| ePk Ve^~rs on
=0.c8.o0 m--ppaithl:C:\Uasetrsi\eodint2 pc\t.neimrblem\pikgns\apltayedadte.-0
.12.0 -o:E:\playdate\Games_nim\playdate-nim-main\playdate_example\playdate_example.exe E:\playdate\Games_nim\playdate-nim-main\playdate_example\src\playdate_example.nim
C:\Users\edit2pc\nimcache\playdate_example_d\simulator@mplaydate_example.nim.c:8:10: fatal error: pd_api.h: No such file or directory
8 | #include "pd_api.h"
| ^
~


Have I missed something? It looks like it can't find the api files but I don't know what I'm meant to do.

Solved!

I found the problem, I feel so stupid! I had the environment path set to the C:\palydatesdk\bin instead of C:\palydatesdk.

Ehy, glad you already solved the problem!
Feel free to ask for support here or on GitHub!

Thanks, Samdze. Iโ€™m very keen to get my head around Nim as it looks great and thank you for putting all this work into nim-playdate.

Iโ€™ve got the example project working and now Iโ€™m trying to do a few things myself but hitting a few walls not sure if some of my code is wrong or not in the Nim bindings yet. I can create a sprite, draw it, move it but not change its centre. I have noticed this is a newish command in C and not sure if itโ€™s in the Nim binding or Iโ€™m just doing it wrong? Here is the code Iโ€™ve used, all work except setCenter().

    # create bitmap
    my_Bitmap = try: playdate.graphics.newBitmap(MY_IMAGE_PATH) except: nil

    # create sprite
    my_sprite = playdate.sprite.newSprite()
    my_sprite.add()
    my_sprite.moveTo(200, 120)
    my_sprite.setImage(my_Bitmap, kBitmapUnflipped)
    my_sprite.setCenter(0.5, 1.0)

This is normal for now, the current version of the Nim bindings doesn't implement setCenter and getCenter.

But there's a pull request that adds them: Add sprite setCenter and getCenter by samdze ยท Pull Request #33 ยท samdze/playdate-nim ยท GitHub

It will be included in the next update!

Thanks for clearing that up. I think I might be having the same issue with BitmapTables so was wondering if there is any way of getting a list of the commands that are connected with the Nim bindings? Unless there is a list some somewhere already.

For now, the only way to reliably have a list of all the functions available is to inspect the source code.

But you can also get some support from VSCode autocomplete with one of the Nim editor plugins, it should show you the methods you can call on each object.

Also keep in mind that the bindings have a more object-oriented approach compared to the C API, so e.g. C BitmapTable functions are often defined as BitmapTable "methods" in Nim.

Thanks that's very helpful, I didn't even think about VScode autocomplete. I'm more of an artist than a coder so still getting my head around how a lot of the coding tools work and features they offer.

1 Like