From 96284123e7e2927bac766638293640d0adeb911b Mon Sep 17 00:00:00 2001 From: Michael Steenbeek Date: Tue, 15 Nov 2022 00:48:34 +0100 Subject: [PATCH] Reduce usage of gCommonStringFormatBuffer --- src/openrct2-ui/windows/ScenarioSelect.cpp | 7 ++++--- src/openrct2-ui/windows/Tooltip.cpp | 4 ++-- src/openrct2/drawing/Drawing.String.cpp | 9 +++++---- src/openrct2/interface/Chat.cpp | 20 +++++++++++--------- src/openrct2/localisation/Localisation.cpp | 2 +- src/openrct2/localisation/Localisation.h | 4 +++- 6 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/openrct2-ui/windows/ScenarioSelect.cpp b/src/openrct2-ui/windows/ScenarioSelect.cpp index 779c7f25ab..2b91dd51a0 100644 --- a/src/openrct2-ui/windows/ScenarioSelect.cpp +++ b/src/openrct2-ui/windows/ScenarioSelect.cpp @@ -653,9 +653,10 @@ static void DrawCategoryHeading( DrawTextBasic(dpi, { centreX, y }, stringId, {}, { baseColour, TextAlignment::CENTRE }); // Get string dimensions - utf8* buffer = gCommonStringFormatBuffer; - format_string(buffer, 256, stringId, nullptr); - int32_t categoryStringHalfWidth = (gfx_get_string_width(buffer, FontStyle::Medium) / 2) + 4; + utf8 buffer[CommonTextBufferSize]; + auto bufferPtr = buffer; + format_string(bufferPtr, sizeof(buffer), stringId, nullptr); + int32_t categoryStringHalfWidth = (gfx_get_string_width(bufferPtr, FontStyle::Medium) / 2) + 4; int32_t strLeft = centreX - categoryStringHalfWidth; int32_t strRight = centreX + categoryStringHalfWidth; diff --git a/src/openrct2-ui/windows/Tooltip.cpp b/src/openrct2-ui/windows/Tooltip.cpp index 8b77bf41ef..255b4ffe30 100644 --- a/src/openrct2-ui/windows/Tooltip.cpp +++ b/src/openrct2-ui/windows/Tooltip.cpp @@ -31,7 +31,7 @@ static rct_widget window_tooltip_widgets[] = { class TooltipWindow final : public Window { private: - utf8 _tooltipText[sizeof(gCommonStringFormatBuffer)]{}; + utf8 _tooltipText[CommonTextBufferSize]{}; int16_t _tooltipNumLines = 1; public: @@ -106,7 +106,7 @@ private: // Returns the width of the new tooltip text int32_t FormatTextForTooltip(const OpenRCT2String& message) { - utf8 tempBuffer[sizeof(gCommonStringFormatBuffer)]; + utf8 tempBuffer[CommonTextBufferSize]; format_string(tempBuffer, sizeof(tempBuffer), message.str, message.args.Data()); OpenRCT2String formattedMessage{ STR_STRING_TOOLTIP, Formatter() }; diff --git a/src/openrct2/drawing/Drawing.String.cpp b/src/openrct2/drawing/Drawing.String.cpp index cd23bdf4c3..b5c79288b8 100644 --- a/src/openrct2/drawing/Drawing.String.cpp +++ b/src/openrct2/drawing/Drawing.String.cpp @@ -272,10 +272,11 @@ int32_t gfx_wrap_string(utf8* text, int32_t width, FontStyle fontStyle, int32_t* void gfx_draw_string_left_centred( rct_drawpixelinfo* dpi, StringId format, void* args, colour_t colour, const ScreenCoordsXY& coords) { - char* buffer = gCommonStringFormatBuffer; - format_string(buffer, 256, format, args); - int32_t height = string_get_height_raw(buffer, FontStyle::Medium); - gfx_draw_string(dpi, coords - ScreenCoordsXY{ 0, (height / 2) }, buffer, { colour }); + char buffer[CommonTextBufferSize]; + auto bufferPtr = buffer; + format_string(bufferPtr, sizeof(buffer), format, args); + int32_t height = string_get_height_raw(bufferPtr, FontStyle::Medium); + gfx_draw_string(dpi, coords - ScreenCoordsXY{ 0, (height / 2) }, bufferPtr, { colour }); } /** diff --git a/src/openrct2/interface/Chat.cpp b/src/openrct2/interface/Chat.cpp index 27a02d8341..7878a3652f 100644 --- a/src/openrct2/interface/Chat.cpp +++ b/src/openrct2/interface/Chat.cpp @@ -274,11 +274,12 @@ static void chat_clear_input() static int32_t chat_history_draw_string( rct_drawpixelinfo* dpi, const char* text, const ScreenCoordsXY& screenCoords, int32_t width) { - auto buffer = gCommonStringFormatBuffer; - FormatStringToBuffer(gCommonStringFormatBuffer, sizeof(gCommonStringFormatBuffer), "{OUTLINE}{WHITE}{STRING}", text); + char buffer[CommonTextBufferSize]; + auto bufferPtr = buffer; + FormatStringToBuffer(buffer, sizeof(buffer), "{OUTLINE}{WHITE}{STRING}", text); int32_t numLines; - gfx_wrap_string(buffer, width, FontStyle::Medium, &numLines); + gfx_wrap_string(bufferPtr, width, FontStyle::Medium, &numLines); auto lineHeight = font_get_line_height(FontStyle::Medium); int32_t expectedY = screenCoords.y - (numLines * lineHeight); @@ -290,8 +291,8 @@ static int32_t chat_history_draw_string( auto lineY = screenCoords.y; for (int32_t line = 0; line <= numLines; ++line) { - gfx_draw_string(dpi, { screenCoords.x, lineY - (numLines * lineHeight) }, buffer, { TEXT_COLOUR_254 }); - buffer = get_string_end(buffer) + 1; + gfx_draw_string(dpi, { screenCoords.x, lineY - (numLines * lineHeight) }, bufferPtr, { TEXT_COLOUR_254 }); + bufferPtr = get_string_end(bufferPtr) + 1; lineY += lineHeight; } return lineY - screenCoords.y; @@ -301,17 +302,18 @@ static int32_t chat_history_draw_string( // Almost the same as gfx_draw_string_left_wrapped int32_t chat_string_wrapped_get_height(void* args, int32_t width) { - char* buffer = gCommonStringFormatBuffer; - format_string(buffer, 256, STR_STRING, args); + char buffer[CommonTextBufferSize]; + auto bufferPtr = buffer; + format_string(bufferPtr, 256, STR_STRING, args); int32_t numLines; - gfx_wrap_string(buffer, width, FontStyle::Medium, &numLines); + gfx_wrap_string(bufferPtr, width, FontStyle::Medium, &numLines); int32_t lineHeight = font_get_line_height(FontStyle::Medium); int32_t lineY = 0; for (int32_t line = 0; line <= numLines; ++line) { - buffer = get_string_end(buffer) + 1; + bufferPtr = get_string_end(bufferPtr) + 1; lineY += lineHeight; } diff --git a/src/openrct2/localisation/Localisation.cpp b/src/openrct2/localisation/Localisation.cpp index 02e3700926..bade24d985 100644 --- a/src/openrct2/localisation/Localisation.cpp +++ b/src/openrct2/localisation/Localisation.cpp @@ -36,7 +36,7 @@ #include #include -thread_local char gCommonStringFormatBuffer[512]; +thread_local char gCommonStringFormatBuffer[CommonTextBufferSize]; #ifdef DEBUG // Set to true before a string format call to see details of the formatting. diff --git a/src/openrct2/localisation/Localisation.h b/src/openrct2/localisation/Localisation.h index f967baa703..83cb859f98 100644 --- a/src/openrct2/localisation/Localisation.h +++ b/src/openrct2/localisation/Localisation.h @@ -51,11 +51,13 @@ bool is_user_string_id(StringId stringId); #define REAL_NAME_START 0xA000 #define REAL_NAME_END 0xDFFF +constexpr const size_t CommonTextBufferSize = 512; + // Real name data extern const char real_name_initials[16]; extern const char* real_names[1024]; -extern thread_local char gCommonStringFormatBuffer[512]; +extern thread_local char gCommonStringFormatBuffer[CommonTextBufferSize]; extern bool gDebugStringFormatting; extern const StringId SpeedNames[5];