From 8923116324e7fa1a41cd12f94b0ec17e0dc5f104 Mon Sep 17 00:00:00 2001 From: Jupeyy Date: Mon, 14 Sep 2020 16:39:05 +0200 Subject: [PATCH 1/2] Fix entity text --- src/engine/client/text.cpp | 8 +++++--- src/engine/textrender.h | 2 +- src/game/client/components/mapimages.cpp | 25 ++++++++++++------------ 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/engine/client/text.cpp b/src/engine/client/text.cpp index 14b2fa106..f2cc4deec 100644 --- a/src/engine/client/text.cpp +++ b/src/engine/client/text.cpp @@ -1768,7 +1768,7 @@ public: Graphics()->MapScreen(ScreenX0, ScreenY0, ScreenX1, ScreenY1); } - virtual void UploadEntityLayerText(void* pTexBuff, int ImageColorChannelCount, int TexWidth, int TexHeight, const char *pText, int Length, float x, float y, int FontSize) + virtual void UploadEntityLayerText(void *pTexBuff, int ImageColorChannelCount, int TexWidth, int TexHeight, int TexSubWidth, int TexSubHeight, const char *pText, int Length, float x, float y, int FontSize) { if (FontSize < 1) return; @@ -1820,8 +1820,10 @@ public: { for(int OffX = 0; OffX < SlotW; ++OffX) { - size_t ImageOffset = (y + OffY) * (TexWidth * ImageColorChannelCount) + ((x + OffX) + WidthLastChars) * ImageColorChannelCount; - size_t GlyphOffset = (OffY) * SlotW + OffX; + int ImgOffX = clamp(x + OffX + WidthLastChars, x, (x + TexSubWidth) - 1); + int ImgOffY = clamp(y + OffY, y, (y + TexSubHeight) - 1); + size_t ImageOffset = ImgOffY * (TexWidth * ImageColorChannelCount) + ImgOffX * ImageColorChannelCount; + size_t GlyphOffset = (OffY)*SlotW + OffX; for(size_t i = 0; i < (size_t)ImageColorChannelCount; ++i) { if(i != (size_t)ImageColorChannelCount - 1) diff --git a/src/engine/textrender.h b/src/engine/textrender.h index e912cc24e..0688dcb19 100644 --- a/src/engine/textrender.h +++ b/src/engine/textrender.h @@ -102,7 +102,7 @@ public: virtual void RenderTextContainer(int TextContainerIndex, STextRenderColor *pTextColor, STextRenderColor *pTextOutlineColor) = 0; virtual void RenderTextContainer(int TextContainerIndex, STextRenderColor *pTextColor, STextRenderColor *pTextOutlineColor, float X, float Y) = 0; - virtual void UploadEntityLayerText(void* pTexBuff, int ImageColorChannelCount, int TexWidth, int TexHeight, const char *pText, int Length, float x, float y, int FontHeight) = 0; + virtual void UploadEntityLayerText(void *pTexBuff, int ImageColorChannelCount, int TexWidth, int TexHeight, int TexSubWidth, int TexSubHeight, const char *pText, int Length, float x, float y, int FontHeight) = 0; virtual int AdjustFontSize(const char *pText, int TextLength, int MaxSize, int MaxWidth) = 0; virtual int CalculateTextWidth(const char *pText, int TextLength, int FontWidth, int FontHeight) = 0; diff --git a/src/game/client/components/mapimages.cpp b/src/game/client/components/mapimages.cpp index 5e21c5676..e2e3116ba 100644 --- a/src/game/client/components/mapimages.cpp +++ b/src/game/client/components/mapimages.cpp @@ -384,18 +384,16 @@ void CMapImages::UpdateEntityLayerText(void* pTexBuffer, int ImageColorChannelCo int DigitsCount = NumbersPower+1; int CurrentNumber = pow(10, NumbersPower); - - if (MaxNumber == -1) - MaxNumber = CurrentNumber*10-1; - - str_format(aBuf, 4, "%d", CurrentNumber); - - int CurrentNumberSuitableFontSize = TextRender()->AdjustFontSize(aBuf, DigitsCount, TextureSize, MaxWidth); - int UniversalSuitableFontSize = CurrentNumberSuitableFontSize*0.95f; // should be smoothed enough to fit any digits combination - int ApproximateTextWidth = TextRender()->CalculateTextWidth(aBuf, DigitsCount, 0, UniversalSuitableFontSize); - int XOffSet = (64-ApproximateTextWidth)/2; - YOffset += ((TextureSize - UniversalSuitableFontSize)/2); + if(MaxNumber == -1) + MaxNumber = CurrentNumber * 10 - 1; + + str_format(aBuf, 4, "%d", CurrentNumber); + + int CurrentNumberSuitableFontSize = TextRender()->AdjustFontSize(aBuf, DigitsCount, TextureSize, MaxWidth); + int UniversalSuitableFontSize = CurrentNumberSuitableFontSize * 0.92f; // should be smoothed enough to fit any digits combination + + YOffset += ((TextureSize - UniversalSuitableFontSize) / 2); for (; CurrentNumber <= MaxNumber; ++CurrentNumber) { @@ -404,7 +402,10 @@ void CMapImages::UpdateEntityLayerText(void* pTexBuffer, int ImageColorChannelCo float x = (CurrentNumber%16)*64; float y = (CurrentNumber/16)*64; - TextRender()->UploadEntityLayerText(pTexBuffer, ImageColorChannelCount, TexWidth, TexHeight, aBuf, DigitsCount, x+XOffSet, y+YOffset, UniversalSuitableFontSize); + int ApproximateTextWidth = TextRender()->CalculateTextWidth(aBuf, DigitsCount, 0, UniversalSuitableFontSize); + int XOffSet = (MaxWidth - clamp(ApproximateTextWidth, 0, MaxWidth)) / 2; + + TextRender()->UploadEntityLayerText(pTexBuffer, ImageColorChannelCount, TexWidth, TexHeight, (TexWidth / 16) - XOffSet, (TexHeight / 16) - YOffset, aBuf, DigitsCount, x + XOffSet, y + YOffset, UniversalSuitableFontSize); } } From fbdd66c092805e99ffcfe5b1165ccfc8fde30402 Mon Sep 17 00:00:00 2001 From: Jupeyy Date: Mon, 14 Sep 2020 16:42:33 +0200 Subject: [PATCH 2/2] Improve grow --- src/engine/client/text.cpp | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/src/engine/client/text.cpp b/src/engine/client/text.cpp index f2cc4deec..800d5a48c 100644 --- a/src/engine/client/text.cpp +++ b/src/engine/client/text.cpp @@ -261,15 +261,15 @@ class CTextRender : public IEngineTextRender m_RenderFlags = Flags; } - void Grow(unsigned char *pIn, unsigned char *pOut, int w, int h) + void Grow(unsigned char *pIn, unsigned char *pOut, int w, int h, int OutlineCount) { for(int y = 0; y < h; y++) for(int x = 0; x < w; x++) { int c = pIn[y*w+x]; - for(int sy = -1; sy <= 1; sy++) - for(int sx = -1; sx <= 1; sx++) + for(int sy = -OutlineCount; sy <= OutlineCount; sy++) + for(int sx = -OutlineCount; sx <= OutlineCount; sx++) { int GetX = x+sx; int GetY = y+sy; @@ -511,28 +511,13 @@ class CTextRender : public IEngineTextRender } UploadGlyph(pFont, 0, X, Y, (int)Width, (int)Height, ms_aGlyphData); - if(OutlineThickness == 1) + Grow(ms_aGlyphData, ms_aGlyphDataOutlined, Width, Height, OutlineThickness); + + while(!GetCharacterSpace(pFont, 1, (int)Width, (int)Height, X, Y)) { - Grow(ms_aGlyphData, ms_aGlyphDataOutlined, Width, Height); - while(!GetCharacterSpace(pFont, 1, (int)Width, (int)Height, X, Y)) - { - IncreaseFontTexture(pFont, 1); - } - UploadGlyph(pFont, 1, X, Y, (int)Width, (int)Height, ms_aGlyphDataOutlined); - } - else - { - for(int i = OutlineThickness; i > 0; i-=2) - { - Grow(ms_aGlyphData, ms_aGlyphDataOutlined, Width, Height); - Grow(ms_aGlyphDataOutlined, ms_aGlyphData, Width, Height); - } - while(!GetCharacterSpace(pFont, 1, (int)Width, (int)Height, X, Y)) - { - IncreaseFontTexture(pFont, 1); - } - UploadGlyph(pFont, 1, X, Y, (int)Width, (int)Height, ms_aGlyphData); + IncreaseFontTexture(pFont, 1); } + UploadGlyph(pFont, 1, X, Y, (int)Width, (int)Height, ms_aGlyphDataOutlined); // set char info {