Commit aa9652ab authored by salix5's avatar salix5 Committed by GitHub

Use std::wstring in SetStaticText (#2957)

parent 490da977
...@@ -1089,16 +1089,13 @@ void Game::InitStaticText(irr::gui::IGUIStaticText* pControl, irr::u32 cWidth, i ...@@ -1089,16 +1089,13 @@ void Game::InitStaticText(irr::gui::IGUIStaticText* pControl, irr::u32 cWidth, i
scrCardText->setPos(0); scrCardText->setPos(0);
} }
std::wstring Game::SetStaticText(irr::gui::IGUIStaticText* pControl, irr::u32 cWidth, irr::gui::CGUITTFont* font, const wchar_t* text, irr::u32 pos) { std::wstring Game::SetStaticText(irr::gui::IGUIStaticText* pControl, irr::u32 cWidth, irr::gui::CGUITTFont* font, const wchar_t* text, irr::u32 pos) {
size_t pbuffer = 0;
irr::u32 _width = 0, _height = 0; irr::u32 _width = 0, _height = 0;
wchar_t prev = 0; wchar_t prev = 0;
wchar_t strBuffer[4096]{}; std::wstring result;
constexpr size_t buffer_len = sizeof strBuffer / sizeof strBuffer[0] - 1; result.reserve(4096);
const size_t text_len = std::wcslen(text); const size_t text_len = std::wcslen(text);
for(size_t i = 0; i < text_len ; ++i) { for(size_t i = 0; i < text_len ; ++i) {
if (pbuffer >= buffer_len)
break;
wchar_t c = text[i]; wchar_t c = text[i];
irr::u32 w = font->getCharDimension(c).Width + font->getKerningWidth(c, prev); irr::u32 w = font->getCharDimension(c).Width + font->getKerningWidth(c, prev);
prev = c; prev = c;
...@@ -1106,31 +1103,28 @@ std::wstring Game::SetStaticText(irr::gui::IGUIStaticText* pControl, irr::u32 cW ...@@ -1106,31 +1103,28 @@ std::wstring Game::SetStaticText(irr::gui::IGUIStaticText* pControl, irr::u32 cW
continue; continue;
} }
if (c == L'\n') { if (c == L'\n') {
strBuffer[pbuffer++] = L'\n'; result.push_back(L'\n');
_width = 0; _width = 0;
_height++; _height++;
prev = 0; prev = 0;
if (_height == pos) if (_height == pos)
pbuffer = 0; result.clear();
continue; continue;
} }
if (_width > 0 && _width + w > cWidth) { if (_width > 0 && _width + w > cWidth) {
strBuffer[pbuffer++] = L'\n'; result.push_back(L'\n');
_width = 0; _width = 0;
_height++; _height++;
prev = 0; prev = 0;
if (_height == pos) if (_height == pos)
pbuffer = 0; result.clear();
} }
if (pbuffer >= buffer_len)
break;
_width += w; _width += w;
strBuffer[pbuffer++] = c; result.push_back(c);
} }
strBuffer[pbuffer] = 0;
if (pControl) if (pControl)
pControl->setText(strBuffer); pControl->setText(result.c_str());
return std::wstring(strBuffer); return result;
} }
void Game::LoadExpansions() { void Game::LoadExpansions() {
FileSystem::TraversalDir(L"./expansions", [](const wchar_t* name, bool isdir) { FileSystem::TraversalDir(L"./expansions", [](const wchar_t* name, bool isdir) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment