From a03a2c2634603c7cd88bd4e54b682ae650f62c50 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Wed, 19 May 2021 03:34:06 +0200 Subject: [PATCH] Render chat panel using common text rendering --- Source/control.cpp | 45 +++++++--------------------- Source/engine/render/text_render.cpp | 14 +++++---- Source/engine/render/text_render.hpp | 2 +- 3 files changed, 21 insertions(+), 40 deletions(-) diff --git a/Source/control.cpp b/Source/control.cpp index 264c76203..ff4e5ed65 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -1764,26 +1764,6 @@ void control_set_gold_curs(int pnum) NewCursor(plr[pnum].HoldItem._iCurs + CURSOR_FIRSTITEM); } -static char *ControlPrintTalkMsg(const CelOutputBuffer &out, char *msg, int *x, int y, text_color color) -{ - *x += 200; - y += 22 + PANEL_Y; - int width = *x; - while (*msg != 0) { - BYTE c = gbFontTransTbl[(BYTE)*msg]; - c = fontframe[GameFontSmall][c]; - width += fontkern[GameFontSmall][c] + 1; - if (width > 450 + PANEL_X) - return msg; - msg++; - if (c != 0) { - PrintChar(out, *x, y, c, color); - } - *x += fontkern[GameFontSmall][c] + 1; - } - return nullptr; -} - void DrawTalkPan(const CelOutputBuffer &out) { if (!talkflag) @@ -1802,23 +1782,21 @@ void DrawTalkPan(const CelOutputBuffer &out) DrawPanelBox(out, 170, sgbPlrTalkTbl + 80, 310, 55, PANEL_X + 170, PANEL_Y + 64); char *msg = sgszTalkMsg; int i = 0; - int x = 0; - for (; i < 39; i += 13) { - x = PANEL_LEFT; - msg = ControlPrintTalkMsg(out, msg, &x, i, COL_WHITE); - if (msg == nullptr) - break; - } - if (msg != nullptr) - *msg = '\0'; - CelDrawTo(out, x, i + 22 + PANEL_Y, *pSPentSpn2Cels, PentSpn2Spin()); + int x = PANEL_LEFT + 200; + int y = PANEL_Y + 22; + + i = DrawString(out, msg, { x, y + i, 250, 39 }, UIS_SILVER, 1, 13, true); + msg[i] = '\0'; + + x += 46; int talkBtn = 0; for (int i = 0; i < 4; i++) { if (i == myplr) continue; - text_color color = COL_RED; + + uint16_t color = UIS_RED; if (whisperList[i]) { - color = COL_GOLD; + color = UIS_GOLD; if (talkButtonsDown[talkBtn]) { int nCel = talkBtn != 0 ? 4 : 3; CelDrawTo(out, 172 + PANEL_X, 84 + 18 * talkBtn + PANEL_Y, *pTalkBtns, nCel); @@ -1830,8 +1808,7 @@ void DrawTalkPan(const CelOutputBuffer &out) CelDrawTo(out, 172 + PANEL_X, 84 + 18 * talkBtn + PANEL_Y, *pTalkBtns, nCel); } if (plr[i].plractive) { - int x = 46 + PANEL_LEFT; - ControlPrintTalkMsg(out, plr[i]._pName, &x, 60 + talkBtn * 18, color); + DrawString(out, plr[i]._pName, { x, y + 60 + talkBtn * 18, 204, 0 }, color); } talkBtn++; diff --git a/Source/engine/render/text_render.cpp b/Source/engine/render/text_render.cpp index 028595a0e..bcb7e80dc 100644 --- a/Source/engine/render/text_render.cpp +++ b/Source/engine/render/text_render.cpp @@ -268,7 +268,7 @@ void WordWrapGameString(char *text, size_t width, size_t size, int spacing) /** * @todo replace SDL_Rect with croped CelOutputBuffer */ -void DrawString(const CelOutputBuffer &out, const char *text, const SDL_Rect &rect, uint16_t flags, int spacing, int lineHeight, bool drawTextCursor) +int DrawString(const CelOutputBuffer &out, const char *text, const SDL_Rect &rect, uint16_t flags, int spacing, int lineHeight, bool drawTextCursor) { GameFontTables size = GameFontSmall; if ((flags & UIS_MED) != 0) @@ -313,10 +313,15 @@ void DrawString(const CelOutputBuffer &out, const char *text, const SDL_Rect &re if (lineHeight == -1) lineHeight = LineHeights[size]; - for (unsigned i = 0; i < textLength; i++) { + unsigned i = 0; + for (; i < textLength; i++) { uint8_t frame = fontframe[size][gbFontTransTbl[static_cast(text[i])]]; int symbolWidth = fontkern[size][frame]; if (text[i] == '\n' || sx + symbolWidth > rightMargin) { + if (sy + lineHeight >= bottomMargin) + break; + sy += lineHeight; + if ((flags & (UIS_CENTER | UIS_RIGHT | UIS_FIT_SPACING)) != 0) lineWidth = GetLineWidth(&text[i + 1], size, spacing, &charactersInLine); @@ -328,9 +333,6 @@ void DrawString(const CelOutputBuffer &out, const char *text, const SDL_Rect &re sx += (w - lineWidth) / 2; else if ((flags & UIS_RIGHT) != 0) sx += w - lineWidth; - sy += lineHeight; - if (sy > bottomMargin) - return; } if (frame != 0) { PrintChar(out, sx, sy, frame, color); @@ -341,6 +343,8 @@ void DrawString(const CelOutputBuffer &out, const char *text, const SDL_Rect &re if (drawTextCursor) { CelDrawTo(out, sx, sy, *pSPentSpn2Cels, PentSpn2Spin()); } + + return i; } int PentSpn2Spin() diff --git a/Source/engine/render/text_render.hpp b/Source/engine/render/text_render.hpp index ad6b5fa35..5126c0918 100644 --- a/Source/engine/render/text_render.hpp +++ b/Source/engine/render/text_render.hpp @@ -61,7 +61,7 @@ void PrintChar(const CelOutputBuffer &out, int sx, int sy, int nCel, text_color */ int GetLineWidth(const char *text, GameFontTables size = GameFontSmall, int spacing = 1, int *charactersInLine = nullptr); void WordWrapGameString(char *text, size_t width, size_t size = GameFontSmall, int spacing = 1); -void DrawString(const CelOutputBuffer &out, const char *text, const SDL_Rect &rect, uint16_t flags = 0, int spacing = 1, int lineHeight = -1, bool drawTextCursor = false); +int DrawString(const CelOutputBuffer &out, const char *text, const SDL_Rect &rect, uint16_t flags = 0, int spacing = 1, int lineHeight = -1, bool drawTextCursor = false); int PentSpn2Spin(); } // namespace devilution