Is this the correct way to render text to a bitmap?
Is there a way to get a per-text font height - so "..." has a different height to "OOO" ?
/**
* \brief Render the given text using the font to a bitmap
*
* \details Draw the given text, using the given font into a correctly sized bitmap.
* Populates <rect> with the dimensions, if supplied.
* The height of the text is fixed for the font, so the height of
* "..." is the same as "OOO", but the former will be on the bottom
* of the bitmap.
*
* \returns A pointer to a bitmap that the caller is responsible for freeing.
*/
LCDBitmap *renderTextToBitmap( LCDFont *font, const char *text, LCDColor foreground_colour, LCDColor background_colour, PDRect *rect )
{
#ifdef assert
assert( font && text );
#endif
LCDBitmap *new_bitmap = NULL;
int text_len = strlen( text );
int width = playdate->graphics->getTextWidth( font, text, text_len, kASCIIEncoding, 0 );
int height = playdate->graphics->getFontHeight( font );
new_bitmap = playdate->graphics->newBitmap( width, height, background_colour );
if ( new_bitmap != NULL )
{
playdate->graphics->pushContext( new_bitmap );
playdate->graphics->setDrawMode( foreground_colour );
playdate->graphics->setFont( font );
playdate->graphics->drawText( text, text_len, kASCIIEncoding, 0, 0 );
playdate->graphics->popContext();
playdate->system->logToConsole( "Bitmap Dimension [%dx%d]", width, height );
}
if ( rect != NULL )
{
rect->width = width;
rect->height = height;
}
return new_bitmap;
}
I would call this something like:
// font_big already loaded LCDFont;
PDRect good_job_rect;
LCDBitmap *good_job_bitmap = renderTextToBitmap( font_big, "Good Job!", kColorWhite, kColorClear, &good_job_rect );