How to Debug Draw using the C API?

,

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)));.

4 Likes