diff --git a/src/engine/textrender.h b/src/engine/textrender.h index b97d44895..5fee79592 100644 --- a/src/engine/textrender.h +++ b/src/engine/textrender.h @@ -100,6 +100,7 @@ public: virtual void UploadEntityLayerText(int TextureID, const char *pText, int Length, float x, float y, int FontHeight) = 0; virtual int AdjustFontSize(const char *pText, int TextLength, int MaxSize = -1) = 0; + virtual int CalculateTextWidth(const char *pText, int TextLength, int FontWidth, int FontHeight) = 0; // old foolish interface virtual void TextColor(float r, float g, float b, float a) = 0; diff --git a/src/game/client/components/mapimages.cpp b/src/game/client/components/mapimages.cpp index 7752ca7a9..3724b3ad7 100644 --- a/src/game/client/components/mapimages.cpp +++ b/src/game/client/components/mapimages.cpp @@ -175,33 +175,47 @@ int CMapImages::UploadEntityLayerText(int TextureSize, int YOffset) int TextureID = Graphics()->LoadTextureRaw(1024, 1024, CImageInfo::FORMAT_ALPHA, pMem, CImageInfo::FORMAT_ALPHA, IGraphics::TEXLOAD_NOMIPMAPS); free(pMem); - char aBuf[4]; - int Len = str_format(aBuf, 4, "%d", 255); - - int FontSize = TextRender()->AdjustFontSize(aBuf, Len, TextureSize)-1; - - if (FontSize < 1) - { - dbg_msg("pFont", "texture with id '%d' will not be loaded. Reason - font is too small", TextureID); - return TextureID; - } - - YOffset += ((TextureSize - FontSize)/2); - - for(int i = 0; i < 256; ++i) - { - int Len = str_format(aBuf, 4, "%d", i); - int XOffSet = (64-((TextureSize/3)*Len))/2; - - float x = (i%16)*64; - float y = (i/16)*64; - - TextRender()->UploadEntityLayerText(TextureID, aBuf, Len, x+XOffSet, y+YOffset, FontSize); - } + UpdateEntityLayerText(TextureID, TextureSize, YOffset, 0); + UpdateEntityLayerText(TextureID, TextureSize, YOffset, 1); + UpdateEntityLayerText(TextureID, TextureSize, YOffset, 2, 255); return TextureID; } +void CMapImages::UpdateEntityLayerText(int TextureID, int TextureSize, int YOffset, int NumbersPower, int MaxNumber) +{ + char aBuf[4]; + 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); + int UniversalSuitableFontSize = CurrentNumberSuitableFontSize*0.9; // should be smoothed enough to fit any digits combination + + if (UniversalSuitableFontSize < 1) + { + dbg_msg("pFont", "texture with id '%d' will not be loaded. Reason - font is too small", TextureID); + } + + int ApproximateTextWidth = TextRender()->CalculateTextWidth(aBuf, DigitsCount, 0, UniversalSuitableFontSize); + int XOffSet = (64-ApproximateTextWidth)/2; + YOffset += ((TextureSize - UniversalSuitableFontSize)/2); + + for (; CurrentNumber <= MaxNumber; ++CurrentNumber) + { + str_format(aBuf, 4, "%d", CurrentNumber); + + float x = (CurrentNumber%16)*64; + float y = (CurrentNumber/16)*64; + + TextRender()->UploadEntityLayerText(TextureID, aBuf, DigitsCount, x+XOffSet, y+YOffset, UniversalSuitableFontSize); + } +} void CMapImages::InitOverlayTextures() { diff --git a/src/game/client/components/mapimages.h b/src/game/client/components/mapimages.h index 4dbde0d0c..446e064d9 100644 --- a/src/game/client/components/mapimages.h +++ b/src/game/client/components/mapimages.h @@ -43,6 +43,7 @@ private: void InitOverlayTextures(); int UploadEntityLayerText(int TextureSize, int YOffset); + void UpdateEntityLayerText(int TextureID, int TextureSize, int YOffset, int NumbersPower, int MaxNumber = -1); }; #endif