How to Debug Draw using the C API?


I was trying to draw debug stuff in the simulator via the C API, but I noticed that in the documentation there's only this function that is somewhat related to debug drawing:


But how to use it?
Does it have any purpose on its own?
I tried to draw on it using pushContext but that seems to cause an error.

Many things seem to still be missing in the C API compared to Lua, is debug drawing one of them?

C API is severely lacking in documentation department.

The only way I managed to get C API to draw anything on the debug bitmap is by manipulating pixels directly. There can be a way to draw stuff using more high-level API functions, but I did not research this angle, since my game does not use any.

To draw pixels directly, you need to get the raw debug bitmap first:

uint8_t* debug_bitmap;
playdate->graphics->getBitmapData(pd->graphics->getDebugBitmap(), NULL, NULL, NULL, NULL, &debug_bitmap);

Directly clearing the debug bitmap messes up the drawing context for Lua. If you do not use Lua, you can just do pd->graphics->clearBitmap(pd->graphics->getDebugBitmap(), kColorBlack); to clear the bitmap. My game does use both C and Lua, so keeping contexts separate is essential. If you game uses this as well, you can do memset(debug_bitmap, 0, 240 * 52); to clear the bitmap.

Finally, you can draw some pixels on the debug bitmap by setting appropriate bits in the bitmap array. From C API documentation:

Rows are 32-bit aligned, so the row stride is 52 bytes, with the extra 2 bytes per row ignored. Bytes are MSB-ordered; i.e., the pixel in column 0 is the 0x80 bit of the first byte of the row.

It took me a good 30 minutes to unpack this phrase and debug the code for it. Basically, you can use this function to set a pixel in the bitmap:

void set_pixel(uint8_t debug_bitmap, int x, int y) {
	uint8_t* bitmap_row = (uint8_t*)&debug_bitmap[y * 52];
        bitmap_row[x / 8] |= 1 << (7 - (x % 8));

I think this sets the pixel to "on" value, but I do not remember. If it does not work the way you want, replace the last line with bitmap_row[x / 8] &= ~(1 << (7 - (x % 8)));.


What is "debug stuff"? How is the debug bitmap different from any other bitmap?
What makes it debuggy ?
Is it painted on top or suchlike?

Debug bitmap is supported only in the simulator and it draws red instead of black/white.

1 Like

Yeah, in Lua you can even choose the color of the overlay.

debugDraw documentation

1 Like