Last active
August 17, 2023 16:05
-
-
Save joyrider3774/67d6783bfc2fefafc40f3f911ec93205 to your computer and use it in GitHub Desktop.
Playdate DrawText using patterns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void drawTextColor(LCDBitmap* BitmapContext, LCDFont *font, const void* text, size_t len, PDStringEncoding encoding, int x, int y, LCDColor color, bool inverted) | |
{ | |
//grab width & height of our to be rendered text | |
pd->graphics->pushContext(BitmapContext); | |
int h = pd->graphics->getFontHeight(font); | |
pd->graphics->setFont(font); | |
int w = pd->graphics->getTextWidth(font, text, len, encoding, 0); | |
//create new bitmap and fillrect with our color / pattern | |
LCDBitmap *bitmap = pd->graphics->newBitmap(w, h, kColorClear); | |
if (inverted) | |
pd->graphics->setDrawMode(kDrawModeInverted); | |
pd->graphics->pushContext(bitmap); | |
pd->graphics->fillRect(0, 0, w, h, color); | |
pd->graphics->popContext(); | |
//create mask with black background and draw text in white on the mask | |
LCDBitmap *bitmapmask = pd->graphics->newBitmap(w, h, kColorBlack); | |
pd->graphics->pushContext(bitmapmask); | |
pd->graphics->setDrawMode(kDrawModeFillWhite); | |
pd->graphics->setFont(font); | |
pd->graphics->drawText(text, len, encoding, 0, 0); | |
pd->graphics->popContext(); | |
//set the mask to the bitmap with our pattern, this will make sure only the text | |
//part (white in mask is drawn from the bitmap) | |
pd->graphics->setBitmapMask(bitmap, bitmapmask); | |
//now draw the bitmap containing our text to the x & y position | |
pd->graphics->drawBitmap(bitmap, x, y, kBitmapUnflipped); | |
pd->graphics->freeBitmap(bitmap); | |
pd->graphics->freeBitmap(bitmapmask); | |
pd->graphics->popContext(); | |
} | |
LCDPattern grey50 = { | |
// Bitmap | |
0b10101010, | |
0b01010101, | |
0b10101010, | |
0b01010101, | |
0b10101010, | |
0b01010101, | |
0b10101010, | |
0b01010101, | |
// Mask | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
}; | |
LCDPattern stripes = { | |
// Bitmap | |
0b11111111, | |
0b00000000, | |
0b11111111, | |
0b00000000, | |
0b11111111, | |
0b00000000, | |
0b11111111, | |
0b00000000, | |
// Mask | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
}; | |
LCDPattern stripes2 = { | |
// Bitmap | |
0b00000000, | |
0b11111111, | |
0b11111111, | |
0b00000000, | |
0b11111111, | |
0b00000000, | |
0b11111111, | |
0b11111111, | |
// Mask | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
0b11111111, | |
}; | |
pd->graphics->clear(kColorWhite); | |
const char* text = "HELLO WORLD"; | |
size_t len = strlen(text); | |
//draw text using a pattern that will look like grey on the playdate (handy for menu's or to show them greyed out instead of white on a black background or so) | |
drawTextColor(NULL, testfont, text, len, kASCIIEncoding, 100, 100, (LCDColor)grey50, false); | |
//draw using a solid color to the screen directly (= NULL first parameter) | |
drawTextColor(NULL, testfont, text, len, kASCIIEncoding, 100, 50, kColorBlack, false); | |
//some other parameters | |
drawTextColor(NULL, testfont, text, len, kASCIIEncoding, 100, 150, (LCDColor)stripes, false); | |
drawTextColor(NULL, testfont, text, len, kASCIIEncoding, 100, 200, (LCDColor)stripes2, false); | |
//white | |
pd->graphics->clear(kColorBlack); | |
drawTextColor(NULL, testfont, text, len, kASCIIEncoding, 100, 50, kColorWhite, false); | |
drawTextColor(NULL, testfont, text, len, kASCIIEncoding, 100, 100, (LCDColor)stripes2, false); | |
drawTextColor(NULL, testfont, text, len, kASCIIEncoding, 100, 200, (LCDColor)stripes2, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment