The Playdate SDK is obviously designed around the idea that the framework controls the run loop and on events calls into user code. The user code function is expected to return to the run loop quickly (as discussed here).
Can I reverse this, so that the run loop is in my code, and periodically calls into the framework to let it do whatever event handling it needs to? I am specifically interested in doing it in C, but the answer might apply to Lua too.
I did a quick test by just never returning from
eventHandler() or from the
update() callback and instead calling
pd->graphics->display() regularly. But that didn’t work out too well. While it allowed me to animate stuff on the display, the buttons were unresponsive, and after 10 seconds (on the device, not in the simulator) the
Run loop stalled for more than 10 seconds watchdog kicked in. So apparently
display() really only flushes the display, but doesn’t do the whole usual end-of-frame processing and event handling. Is there anything else I can call to let the framework do its thing, or is this way of operation flat-out unsupported? If it is, could it be supported in the future?
The reason I am asking is that I am planning to try to get MicroPython into a Playdate application in order to run existing games written in Python. These games assume that they control the run loop, they contain an infinite loop that calls out to API functions that update the display, check for button presses, or wait for the next frame time. The MicroPython virtual machine doesn’t help me out either in that regard, running one Python function means one C call that only returns after the Python function returns, there is no way to have it interrupt after a certain number of Python instructions and return to the caller, to resume in a further C call. It does have the ability to call back every so many instructions, though.
I imagine this functionality could also be useful for ports of other existing games or for emulators.
If I am out of luck and there is no way to take over the run loop, I might be able to achieve my goal using this “coroutines for C” library. Lua’s native coroutines were what allowed me to get this to work in a Lua prototype.