Playdate Nim bindings - C performance, Python like syntax

Final touches before the first alpha, also preparing a micro example:

So, here's what should be available in this first release:

  1. playdate.system (inputs, menu items, log, etc.)
  2. playdate.graphics (draw stuff)
  3. playdate.display (general screen stuff)
  4. playdate.file (read, write and create files and folders)
  5. (partial) playdate.sprite (draw sprites and handle collisions)
  6. (partial) playdate.sound (play sounds and music)

Note that this will be still very WIP and a lot of things still lack.
But what's there should work just fine!

2 Likes

Finally released the first alpha!
Here's the link to the repository:

Also updated the OP with more info!

Please test the bindings, especially on device since I don't have one and I'm not sure it will work properly yet!

1 Like

I’ve only tried the demo so far but that works well. There is one error in the github page though: it says nim simulator for compiling for the device.

1 Like

Thanks! Fixed!

Did you try the hello world PDX or the example in the repository?

I think that’s the same. I ran nim all first but git says only the dylib changed.

1 Like

Oh, nice, you compiled the project yourself! Great!
Thought you used one of the pre-compiled pdxs!

I just made a small change to double check and it works as well. This is really easy to use on a Mac!

1 Like

Taking advantage of Nim APIs, you'll be soon able to decode/encode JSON in this type safe way!

type
    Equip = ref object
        name: string
        damage: Option[int] // optional value
    Entity = ref object
        name: string
        enemy: bool
        health: int
        equip: seq[Equip] // list of equips

try:
    let jsonString = playdate.file.open("/json/data.json", kFileRead).readString()
    let entityObj = parseJson(jsonString).to(Entity) // Yay, type safe decoding

    let encodedEntity = %* entityObj // %* is the operator to encode an object
except:
    // ops, something went wrong, bad json file!
    ...
2 Likes

I'm gradually updating the repository, now playdate.sprite bindings are complete!

I think I'll make a little game to thoroughly test the bindings and also add some Lua-only functions to the API too!

In any case, the bindings are ready to be tested on the device!
Please let me know if you have any problem running them on it!

4 Likes

Love this! I’ve been thinking about using Nim for play date development for a few weeks now, so I’m glad to see someone else has already done the leg work. I’m pumped to give this a try

2 Likes

Also doing a few tests integrating Chipmunk (physics engine), good results so far!

Playdate Simulator 2023-01-18 at 21.26.41

7 Likes

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