From b06544871ae48871fc302706ecc49b78c6bcb3d2 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(-) (limited to 'src/graphics/engine') 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 From 45fd8aad33029746424031e12777f2824bda245e Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 26 Sep 2012 16:31:04 +0200 Subject: Fog color fix; refactoring - fixed fog color setting - removed unused glSecondaryColor and altered struct VertexCol - minor refactoring in CText --- src/graphics/engine/engine.cpp | 16 ++++----- src/graphics/engine/text.cpp | 78 ++++++++++++++++++++++-------------------- src/graphics/engine/text.h | 7 ++-- 3 files changed, 51 insertions(+), 50 deletions(-) (limited to 'src/graphics/engine') diff --git a/src/graphics/engine/engine.cpp b/src/graphics/engine/engine.cpp index 37f9b00..c170922 100644 --- a/src/graphics/engine/engine.cpp +++ b/src/graphics/engine/engine.cpp @@ -3417,10 +3417,10 @@ void CEngine::DrawBackgroundGradient(const Color& up, const Color& down) VertexCol vertex[4] = { - VertexCol(Math::Vector(p1.x, p1.y, 0.0f), color[1], color[2]), - VertexCol(Math::Vector(p1.x, p2.y, 0.0f), color[0], color[2]), - VertexCol(Math::Vector(p2.x, p1.y, 0.0f), color[1], color[2]), - VertexCol(Math::Vector(p2.x, p2.y, 0.0f), color[0], color[2]) + VertexCol(Math::Vector(p1.x, p1.y, 0.0f), color[1]), + VertexCol(Math::Vector(p1.x, p2.y, 0.0f), color[0]), + VertexCol(Math::Vector(p2.x, p1.y, 0.0f), color[1]), + VertexCol(Math::Vector(p2.x, p2.y, 0.0f), color[0]) }; m_device->DrawPrimitive(PRIMITIVE_TRIANGLE_STRIP, vertex, 4); @@ -3567,10 +3567,10 @@ void CEngine::DrawOverColor() VertexCol vertex[4] = { - VertexCol(Math::Vector(p1.x, p1.y, 0.0f), color[1], color[2]), - VertexCol(Math::Vector(p1.x, p2.y, 0.0f), color[0], color[2]), - VertexCol(Math::Vector(p2.x, p1.y, 0.0f), color[1], color[2]), - VertexCol(Math::Vector(p2.x, p2.y, 0.0f), color[0], color[2]) + VertexCol(Math::Vector(p1.x, p1.y, 0.0f), color[1]), + VertexCol(Math::Vector(p1.x, p2.y, 0.0f), color[0]), + VertexCol(Math::Vector(p2.x, p1.y, 0.0f), color[1]), + VertexCol(Math::Vector(p2.x, p2.y, 0.0f), color[0]) }; m_device->DrawPrimitive(PRIMITIVE_TRIANGLE_STRIP, vertex, 4); diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index da9f4ac..00b6617 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -150,8 +150,8 @@ void CText::FlushCache() } void CText::DrawText(const std::string &text, const std::vector &format, - float size, Math::Point pos, float width, TextAlign align, - int eol) + float size, Math::Point pos, float width, TextAlign align, + int eol) { float sw = 0.0f; @@ -172,8 +172,8 @@ void CText::DrawText(const std::string &text, const std::vector &f } void CText::DrawText(const std::string &text, FontType font, - float size, Math::Point pos, float width, TextAlign align, - int eol) + float size, Math::Point pos, float width, TextAlign align, + int eol) { float sw = 0.0f; @@ -194,8 +194,8 @@ void CText::DrawText(const std::string &text, FontType font, } void CText::SizeText(const std::string &text, const std::vector &format, - float size, Math::Point pos, TextAlign align, - Math::Point &start, Math::Point &end) + float size, Math::Point pos, TextAlign align, + Math::Point &start, Math::Point &end) { start = end = pos; @@ -217,8 +217,8 @@ void CText::SizeText(const std::string &text, const std::vector &f } void CText::SizeText(const std::string &text, FontType font, - float size, Math::Point pos, TextAlign align, - Math::Point &start, Math::Point &end) + float size, Math::Point pos, TextAlign align, + Math::Point &start, Math::Point &end) { start = end = pos; @@ -277,7 +277,7 @@ float CText::GetHeight(FontType font, float size) float CText::GetStringWidth(const std::string &text, - const std::vector &format, float size) + const std::vector &format, float size) { assert(StrUtils::Utf8StringLength(text) == format.size()); @@ -344,7 +344,7 @@ float CText::GetCharWidth(UTF8Char ch, FontType font, float size, float offset) int CText::Justify(const std::string &text, const std::vector &format, - float size, float width) + float size, float width) { assert(StrUtils::Utf8StringLength(text) == format.size()); @@ -427,7 +427,7 @@ int CText::Justify(const std::string &text, FontType font, float size, float wid } int CText::Detect(const std::string &text, const std::vector &format, - float size, float offset) + float size, float offset) { assert(StrUtils::Utf8StringLength(text) == format.size()); @@ -500,7 +500,7 @@ int CText::Detect(const std::string &text, FontType font, float size, float offs } void CText::DrawString(const std::string &text, const std::vector &format, - float size, Math::Point pos, float width, int eol) + float size, Math::Point pos, float width, int eol) { assert(StrUtils::Utf8StringLength(text) == format.size()); @@ -511,9 +511,10 @@ void CText::DrawString(const std::string &text, const std::vector unsigned int fmtIndex = 0; - std::vector chars = StringToUTFCharList(text); - for(auto it=chars.begin(); it != chars.end(); ++it){ - + std::vector chars; + StringToUTFCharList(text, chars); + for (auto it = chars.begin(); it != chars.end(); ++it) + { font = static_cast(format[fmtIndex] & FONT_MASK_FONT); // TODO: if (font == FONT_BUTTON) @@ -546,37 +547,38 @@ void CText::DrawString(const std::string &text, const std::vector // 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::StringToUTFCharList(const std::string &text, std::vector &chars) +{ + 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; + + chars.push_back(ch); + } } void CText::DrawString(const std::string &text, FontType font, - float size, Math::Point pos, float width, int eol) + float size, Math::Point pos, float width, int eol) { assert(font != FONT_BUTTON); m_engine->SetState(ENG_RSTATE_TEXT); - std::vector chars = StringToUTFCharList(text); - for(auto it=chars.begin(); it != chars.end(); ++it){ + std::vector chars; + StringToUTFCharList(text, chars); + for (auto it = chars.begin(); it != chars.end(); ++it) + { DrawCharAndAdjustPos(*it, font, size, pos); } } diff --git a/src/graphics/engine/text.h b/src/graphics/engine/text.h index 0ecf7cd..bb9a32d 100644 --- a/src/graphics/engine/text.h +++ b/src/graphics/engine/text.h @@ -293,11 +293,10 @@ protected: float size, Math::Point pos, float width, int eol); void DrawHighlight(FontHighlight hl, Math::Point pos, Math::Point size); void DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::Point &pos); - std::vector - StringToUTFCharList(const std::string &text); + void StringToUTFCharList(const std::string &text, std::vector &chars); protected: - CInstanceManager* m_iMan; + CInstanceManager* m_iMan; CEngine* m_engine; CDevice* m_device; @@ -307,7 +306,7 @@ protected: std::map m_fonts; FontType m_lastFontType; - int m_lastFontSize; + int m_lastFontSize; CachedFont* m_lastCachedFont; }; -- cgit v1.2.3-1-g7c22