From dad42df4c7878e3da67ab66549fc1a3132893160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Mon, 24 Sep 2012 23:55:52 +0200 Subject: made graphics/engine/text cleaner, I hope --- src/graphics/engine/text.cpp | 63 ++++++++++++++++++++++---------------------- src/graphics/engine/text.h | 4 ++- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index 36ad660..da9f4ac 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -509,24 +509,17 @@ void CText::DrawString(const std::string &text, const std::vector FontType font = FONT_COLOBOT; float start = pos.x; - unsigned int index = 0; unsigned int fmtIndex = 0; - while (index < text.length()) - { + + std::vector chars = StringToUTFCharList(text); + for(auto it=chars.begin(); it != chars.end(); ++it){ + font = static_cast(format[fmtIndex] & FONT_MASK_FONT); // TODO: if (font == FONT_BUTTON) if (font == FONT_BUTTON) continue; - UTF8Char ch; - - int len = StrUtils::Utf8CharSizeAt(text, index); - if (len >= 1) - ch.c1 = text[index]; - if (len >= 2) - ch.c2 = text[index+1]; - if (len >= 3) - ch.c3 = text[index+2]; + UTF8Char ch = *it; float offset = pos.x - start; float cw = GetCharWidth(ch, font, size, offset); @@ -545,15 +538,36 @@ void CText::DrawString(const std::string &text, const std::vector DrawHighlight(hl, pos, charSize); } - DrawChar(ch, font, size, pos); + DrawCharAndAdjustPos(ch, font, size, pos); - index += len; fmtIndex++; } // TODO: eol } +std::vector CText::StringToUTFCharList(const std::string &text) { + std::vector v; + unsigned int index = 0; + while (index < text.length()) + { + UTF8Char ch; + + int len = StrUtils::Utf8CharSizeAt(text, index); + if (len >= 1) + ch.c1 = text[index]; + if (len >= 2) + ch.c2 = text[index+1]; + if (len >= 3) + ch.c3 = text[index+2]; + + index += len; + + v.push_back(ch); + } + return v; +} + void CText::DrawString(const std::string &text, FontType font, float size, Math::Point pos, float width, int eol) { @@ -561,22 +575,9 @@ void CText::DrawString(const std::string &text, FontType font, m_engine->SetState(ENG_RSTATE_TEXT); - unsigned int index = 0; - while (index < text.length()) - { - UTF8Char ch; - - int len = StrUtils::Utf8CharSizeAt(text, index); - if (len >= 1) - ch.c1 = text[index]; - if (len >= 2) - ch.c2 = text[index+1]; - if (len >= 3) - ch.c3 = text[index+2]; - - index += len; - - DrawChar(ch, font, size, pos); + std::vector chars = StringToUTFCharList(text); + for(auto it=chars.begin(); it != chars.end(); ++it){ + DrawCharAndAdjustPos(*it, font, size, pos); } } @@ -660,7 +661,7 @@ void CText::DrawHighlight(FontHighlight hl, Math::Point pos, Math::Point size) m_device->SetRenderState(RENDER_STATE_TEXTURING, true); } -void CText::DrawChar(UTF8Char ch, FontType font, float size, Math::Point &pos) +void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::Point &pos) { // TODO: if (font == FONT_BUTTON) if (font == FONT_BUTTON) return; diff --git a/src/graphics/engine/text.h b/src/graphics/engine/text.h index e587609..0ecf7cd 100644 --- a/src/graphics/engine/text.h +++ b/src/graphics/engine/text.h @@ -292,7 +292,9 @@ protected: void DrawString(const std::string &text, FontType font, float size, Math::Point pos, float width, int eol); void DrawHighlight(FontHighlight hl, Math::Point pos, Math::Point size); - void DrawChar(UTF8Char ch, FontType font, float size, Math::Point &pos); + void DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::Point &pos); + std::vector + StringToUTFCharList(const std::string &text); protected: CInstanceManager* m_iMan; -- cgit v1.2.3-1-g7c22