numbers dynamically centered for OpenGL 3.3

This commit is contained in:
Andrii Vynychenko 2019-08-17 03:42:54 +03:00
parent 0fa23a2c47
commit 1671e6fbfc
3 changed files with 39 additions and 23 deletions

View file

@ -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;

View file

@ -175,34 +175,48 @@ 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);
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 Len = str_format(aBuf, 4, "%d", 255);
int DigitsCount = NumbersPower+1;
int FontSize = TextRender()->AdjustFontSize(aBuf, Len, TextureSize)-1;
int CurrentNumber = pow(10, NumbersPower);
if (FontSize < 1)
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);
return TextureID;
}
YOffset += ((TextureSize - FontSize)/2);
int ApproximateTextWidth = TextRender()->CalculateTextWidth(aBuf, DigitsCount, 0, UniversalSuitableFontSize);
int XOffSet = (64-ApproximateTextWidth)/2;
YOffset += ((TextureSize - UniversalSuitableFontSize)/2);
for(int i = 0; i < 256; ++i)
for (; CurrentNumber <= MaxNumber; ++CurrentNumber)
{
int Len = str_format(aBuf, 4, "%d", i);
int XOffSet = (64-((TextureSize/3)*Len))/2;
str_format(aBuf, 4, "%d", CurrentNumber);
float x = (i%16)*64;
float y = (i/16)*64;
float x = (CurrentNumber%16)*64;
float y = (CurrentNumber/16)*64;
TextRender()->UploadEntityLayerText(TextureID, aBuf, Len, x+XOffSet, y+YOffset, FontSize);
TextRender()->UploadEntityLayerText(TextureID, aBuf, DigitsCount, x+XOffSet, y+YOffset, UniversalSuitableFontSize);
}
return TextureID;
}
void CMapImages::InitOverlayTextures()
{
int TextureSize = 64*m_TextureScale/100;

View file

@ -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