diff --git a/inc/vdp_bg.h b/inc/vdp_bg.h index 8ccb7892..15a8194b 100644 --- a/inc/vdp_bg.h +++ b/inc/vdp_bg.h @@ -502,6 +502,29 @@ void VDP_clearTextLineBG(VDPPlane plane, u16 y); * \see VDP_setTextPlane(..) */ void VDP_drawText(const char *str, u16 x, u16 y); + +/** + * \brief + * Clears the field of characters and draws new text.
+ * If the new line is shorter than the one previously drawn in this place, the old characters will be removed.
+ * This function works significantly faster than calling VDP_clearText() and then VDP_drawText(). It is suitable when a lot of updating information needs to be displayed on the screen. + * + * \param str + * String to draw. For correct operation, it must not exceed 20 characters. + * \param maxLength + * Maximum string length. For correct operation, it must not exceed 20 characters. + * \param x + * X position (in tile). + * \param y + * y position (in tile). + * + * \see VDP_drawText(..) + * \see VDP_clearText(..) + * \see VDP_setTextPalette(..) + * \see VDP_setTextPriority(..) + * \see VDP_setTextPlane(..) + */ +void VDP_drawTextClear(const char* str, u8 maxLength, u16 x, u16 y); /** * \brief * Clear a single line portion of text. diff --git a/src/vdp_bg.c b/src/vdp_bg.c index bfafed98..ce392a7a 100644 --- a/src/vdp_bg.c +++ b/src/vdp_bg.c @@ -382,6 +382,51 @@ void VDP_drawText(const char *str, u16 x, u16 y) VDP_drawTextBG(text_plan, str, x, y); } +void VDP_drawTextClear(const char* str, u8 maxLength, u16 x, u16 y) +{ + static const char *spaceStrings[] = { + "", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " " + }; + char result[maxLength + 1]; + char *spaces = spaceStrings[maxLength - strlen(str)]; + char *dst = result; + const char *src; + + src = str; + while (*src) { + *dst++ = *src++; + } + + src = spaces; + while (*src) { + *dst++ = *src++; + } + + *dst = '\0'; + + VDP_drawText(result, x, y); +} + void VDP_clearText(u16 x, u16 y, u16 w) { VDP_clearTextBG(text_plan, x, y, w);