From 9576d6235e2b4cd2f6306b9ddd1cfb81019ee086 Mon Sep 17 00:00:00 2001
From: Lemur42332543632 <120027513+Lemur42332543632@users.noreply.github.com>
Date: Thu, 7 Nov 2024 22:11:10 +0300
Subject: [PATCH] Add VDP_drawTextClear function
---
inc/vdp_bg.h | 23 +++++++++++++++++++++++
src/vdp_bg.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 68 insertions(+)
diff --git a/inc/vdp_bg.h b/inc/vdp_bg.h
index 8ccb78927..15a8194be 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 bfafed98d..ce392a7aa 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);