diff --git a/lib/ivis_opengl/textdraw.cpp b/lib/ivis_opengl/textdraw.cpp index c75727d91..7458de63f 100644 --- a/lib/ivis_opengl/textdraw.cpp +++ b/lib/ivis_opengl/textdraw.cpp @@ -462,8 +462,8 @@ void iV_SetTextColour(PIELIGHT colour) */ int iV_DrawFormattedText(const char* String, UDWORD x, UDWORD y, UDWORD Width, UDWORD Justify) { - char FString[256]; - char FWord[256]; + std::string FString; + std::string FWord; int i; int jx = x; // Default to left justify. int jy = y; @@ -477,7 +477,7 @@ int iV_DrawFormattedText(const char* String, UDWORD x, UDWORD y, UDWORD Width, U bool NewLine = false; // Reset text draw buffer - FString[0] = 0; + FString.clear(); WWidth = 0; @@ -485,10 +485,11 @@ int iV_DrawFormattedText(const char* String, UDWORD x, UDWORD y, UDWORD Width, U while (*curChar != 0 && WWidth < Width && !NewLine) { const char* startOfWord = curChar; - const unsigned int FStringWidth = iV_GetTextWidth(FString); + const unsigned int FStringWidth = iV_GetTextWidth(FString.c_str()); // Get the next word. i = 0; + FWord.clear(); for (; *curChar != 0 && *curChar != ASCII_SPACE && *curChar != ASCII_NEWLINE @@ -497,14 +498,15 @@ int iV_DrawFormattedText(const char* String, UDWORD x, UDWORD y, UDWORD Width, U { if (*curChar == ASCII_COLOURMODE) // If it's a colour mode toggle char then just add it to the word. { - FWord[i] = *curChar; + FWord.push_back(*curChar); // this character won't be drawn so don't deal with its width continue; } // Update this line's pixel width. - WWidth = FStringWidth + iV_GetCountedTextWidth(FWord, i + 1); + //WWidth = FStringWidth + iV_GetCountedTextWidth(FWord.c_str(), i + 1); // This triggers tonnes of valgrind warnings, if the string contains unicode. Adding lots of trailing garbage didn't help... Using iV_GetTextWidth with a null-terminated string, instead. + WWidth = FStringWidth + iV_GetTextWidth(FWord.c_str()); // If this word doesn't fit on the current line then break out if (WWidth > Width) @@ -513,7 +515,7 @@ int iV_DrawFormattedText(const char* String, UDWORD x, UDWORD y, UDWORD Width, U } // If width ok then add this character to the current word. - FWord[i] = *curChar; + FWord.push_back(*curChar); } // Don't forget the space. @@ -524,7 +526,7 @@ int iV_DrawFormattedText(const char* String, UDWORD x, UDWORD y, UDWORD Width, U WWidth += iV_GetCharWidth('-'); if (WWidth <= Width) { - FWord[i] = ' '; + FWord.push_back(' '); ++i; ++curChar; GotSpace = true; @@ -554,33 +556,18 @@ int iV_DrawFormattedText(const char* String, UDWORD x, UDWORD y, UDWORD Width, U break; } - // Terminate the word. - FWord[i] = 0; - // And add it to the output string. - sstrcat(FString, FWord); + FString.append(FWord); } // Remove trailing spaces, useful when doing center alignment. + while (!FString.empty() && FString[FString.size() - 1] == ' ') { - // Find the string length (the "minus one" part - // guarantees that we get the length of the string, not - // the buffer size required to contain it). - size_t len = strnlen1(FString, sizeof(FString)) - 1; - - for (; len != 0; --len) - { - // As soon as we encounter a non-space character, break out - if (FString[len] != ASCII_SPACE) - break; - - // Cut off the current space character from the string - FString[len] = '\0'; - } + FString.erase(FString.size() - 1); // std::string has no pop_back(). } - TWidth = iV_GetTextWidth(FString); + TWidth = iV_GetTextWidth(FString.c_str()); // Do justify. switch (Justify) @@ -599,7 +586,7 @@ int iV_DrawFormattedText(const char* String, UDWORD x, UDWORD y, UDWORD Width, U } // draw the text. - iV_DrawText(FString, jx, jy); + iV_DrawText(FString.c_str(), jx, jy); // and move down a line. jy += iV_GetTextLineSize();