[C/C++] Coroutines Library for Playdate

Coroutines for Playdate C/C++

➤➤➤ A C/C++ library for coroutines on the Playdate and in the simulator. ⮜⮜⮜

Lua has a coroutines library, and it's fantastic. It allows deep yielding -- yielding from subroutines called by the coroutines, not just from the top-level coroutine itself. This is better than python's coroutines, for examples, and it avoids the red/blue problem too.

Now we can have coroutines in C/C++ as well. :slight_smile:

7 Likes

This library is proving very useful for me in my quest to bring MicroPython games to the Playdate, as it allows me to have long-running Python code containing its own main loop without stalling the Playdate main loop and running into the 10-second watchdog. Thanks!

I develop on macOS and have made some fixes to the library to allow it to work in the Mac Playdate Simulator (it was previously geared more toward Linux), which @NaOH has merged very quickly. I have only tested it on macOS 13 (Ventura) on an Intel Mac and am curious whether it also works on macOS 14 (Sonoma) and/or Apple Silicon Macs. I am unsure because the OS functions used internally are marked as deprecated in the macOS SDK and might be removed at some point. They have been deprecated for 15 years though (since 10.6), so there is hope that they might stay for another while.

Anyone here who is also interested in this functionality and has macOS 14 (Sonoma) and/or an Apple Silicon Mac handy to give it a quick test?

You don’t even need the Playdate SDK, Xcode command line tools (which are installed along with the Playdate SDK) are sufficient. Just do the following:

cwalther@tartan ~ % git clone https://github.com/nstbayless/playdate-coroutines.git
Cloning into 'playdate-coroutines'...
remote: Enumerating objects: 138, done.
remote: Counting objects: 100% (138/138), done.
remote: Compressing objects: 100% (95/95), done.
remote: Total 138 (delta 85), reused 94 (delta 42), pack-reused 0
Receiving objects: 100% (138/138), 37.94 KiB | 2.23 MiB/s, done.
Resolving deltas: 100% (85/85), done.
cwalther@tartan ~ % cd playdate-coroutines 
cwalther@tartan playdate-coroutines % cc -o test main.c pdco.c
cwalther@tartan playdate-coroutines % ./test
Running coroutine test...
 -> the value of a is 1
 -> the value of a is 2
 -> the value of a is 10
 -> the value of a is 3
Completed. The final value of a is 3
cwalther@tartan playdate-coroutines % 

If the output from ./test is the same, that means it’s working. If it’s not working, it might either crash or give different output.