QR code as (400x240) MenuImage

Hi,

I am developing a pure-C game and I would like to have a dynamically generated QR code as menu image. The current C API does not have a QR code generation functionality, so I introduced main.lua where I plan to generate the QR code image and set it as a menu image using setMenuImage.

I was able to generate a QR code and display it as a sprite, and I was able to generate and set a dynamic menu image, but I am having trouble combining these two.

I assume that my current issue lies in the fact that QR code is square, while the menuImage has to be exactly 400x240. As far as I can tell, there is no image crop or resize function in the Lua API. I also cannot find any "place image on top of existing image" function that would allow me to overlay the QR code on a 400x240 menuImage.

Is what I am trying to accomplish actually possible with the current SDK? How would one go about generating a 400x240 image with embedded QR code? Many thanks in advance for any hint.

This is untested but you could render the QR Code on the screen and use playdate.graphics.getDisplayImage() (docs) (takes a screenshot of the display and returns the screenshot taken). You could then use that screenshot with setMenuImage().

Hmm, I don’t see any reason why this would not work, however I don’t like the idea of visibly flashing a QR code on the screen in advance, even if it was just for 1 frame… Thinking if it would be possible to tap into the “menu button was pushed” event and only run this right before the menu is displayed? Unfortunately the menu button doesn’t have publicly available event API…

The button itself doesn't have an input API, but you can use the game lifecycle API for this: playdate.gameWillPause() (docs) is triggered when you press the menu button.

Like this, maybe?

qr_image = (generate your QR code)
menu_image = gfx.image.new(400, 240)
gfx.pushContext(menu_image)
qr_image:draw(0, 0)
gfx.popContext()
playdate.setMenuImage(menu_image)

Thank you for this suggestion, I will try it out.

Out of curiosity, would there be a way to render QR code while keeping the update loop in C? Since QR code generator depends on Lua timers, I am having no luck with rendering the QR unless I do my screen updates in Lua. But because my game is primarily C, it would take some time to migrate all the screen update logic to Lua.

I really wish the C SDK had parity with the Lua one. I intended to develop primarily in C but ended up using Lua just because so much more is available out of the box, no need to reinvent the wheel.

1 Like

Could you port the QR code generator to C (it’s under Playdate SDK/CoreLibs/3rdparty/qrencode_panic_mod.lua) or use an existing C library (QR-Code-generator/c at master · nayuki/QR-Code-generator · GitHub)?