Developing Games for DIY Handheld Devices
Recently, there's a trend of creating DIY handheld gaming consoles using a single-board microcontroller such as Arduino 1 as the core, and adding a display and buttons to it. The advantage is that you can create a gaming console according to your liking. However, the downside is the proliferation of various devices, which means there isn't a common device that many people own.
Games for these devices are often written in the C language. Therefore, it's necessary to implement the accompanying libraries in C as well.
The processes that need to be written in device-specific code include:
- Device initialization (e.g.,
setup()in Arduino) and calling
- Frame update process (e.g.,
loop()in Arduino) with button state notification by calling
- Drawing and sound-related processes defined in
With this approach, the device-specific implementations required for general devices amount to about 250 lines.
Accommodating this function might be somewhat cumbersome on devices like PyBadge, where only the buzzer volume can be adjusted using
analogWrite(), necessitating manual management of sound timing and generation of square waves.
md_drawCharacter(unsigned char grid[CHARACTER_HEIGHT][CHARACTER_WIDTH], float x, float y, int hash) function, which displays pixel art, also requires some effort. The last hash value is intended for caching the pattern definition of the pixel art rather than parsing and drawing it every time. If you are utilizing the LovyanGFX library 9 for drawing, you should save the LGFX_Sprite and hash value as a pair, then retrieve and execute
Writing code while considering such portability is a fun experience and is something that can't be done without targeting various devices with the library. Creating a unique gaming library, while utilizing common development environments like the Arduino IDE and multi-device compatible libraries like Adafruit Arcada 10 or LovyanGFX, offers a unique experience. Working with a myriad of microcontroller boards presents a different kind of enjoyment compared to developing libraries targeting PCs or smartphones. If interested, I would encourage you to give it a try.
3. M5StickC PLUS ↩
5. Emscripten ↩
10. Adafruit Arcada ↩