Merge pull request #8629 from MilkeeyCat/pr_refactor_text_alignment_of_movement_information

Use text containers to render movement information
This commit is contained in:
Robert Müller 2024-07-21 12:05:59 +00:00 committed by GitHub
commit 606212f2d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 55 additions and 39 deletions

View file

@ -28,6 +28,13 @@ CHud::CHud()
m_FrameTimeAvg = 0.0f; m_FrameTimeAvg = 0.0f;
m_FPSTextContainerIndex.Reset(); m_FPSTextContainerIndex.Reset();
m_DDRaceEffectsTextContainerIndex.Reset(); m_DDRaceEffectsTextContainerIndex.Reset();
m_PlayerAngleTextContainerIndex.Reset();
for(int i = 0; i < 2; i++)
{
m_aPlayerSpeedTextContainers[i].Reset();
m_aPlayerPositionContainers[i].Reset();
}
} }
void CHud::ResetHudContainers() void CHud::ResetHudContainers()
@ -44,6 +51,12 @@ void CHud::ResetHudContainers()
TextRender()->DeleteTextContainer(m_FPSTextContainerIndex); TextRender()->DeleteTextContainer(m_FPSTextContainerIndex);
TextRender()->DeleteTextContainer(m_DDRaceEffectsTextContainerIndex); TextRender()->DeleteTextContainer(m_DDRaceEffectsTextContainerIndex);
TextRender()->DeleteTextContainer(m_PlayerAngleTextContainerIndex);
for(int i = 0; i < 2; i++)
{
TextRender()->DeleteTextContainer(m_aPlayerSpeedTextContainers[i]);
TextRender()->DeleteTextContainer(m_aPlayerPositionContainers[i]);
}
} }
void CHud::OnWindowResize() void CHud::OnWindowResize()
@ -1245,6 +1258,28 @@ inline float CHud::GetMovementInformationBoxHeight()
return BoxHeight; return BoxHeight;
} }
void CHud::UpdateMovementInformationTextContainer(STextContainerIndex &TextContainer, float FontSize, float Value, char *pPrevValue, size_t Size)
{
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "%.2f", Value);
if(!TextContainer.Valid() || str_comp(pPrevValue, aBuf) != 0)
{
CTextCursor Cursor;
TextRender()->SetCursor(&Cursor, 0, 0, FontSize, TEXTFLAG_RENDER);
TextRender()->RecreateTextContainer(TextContainer, &Cursor, aBuf);
str_copy(pPrevValue, aBuf, Size);
}
}
void CHud::RenderMovementInformationTextContainer(STextContainerIndex &TextContainer, float X, float Y)
{
if(TextContainer.Valid())
{
TextRender()->RenderTextContainer(TextContainer, TextRender()->DefaultTextColor(), TextRender()->DefaultTextOutlineColor(), X - TextRender()->GetBoundingBoxTextContainer(TextContainer).m_W, Y);
}
}
void CHud::RenderMovementInformation(const int ClientId) void CHud::RenderMovementInformation(const int ClientId)
{ {
// Draw the infomations depending on settings: Position, speed and target angle // Draw the infomations depending on settings: Position, speed and target angle
@ -1303,28 +1338,9 @@ void CHud::RenderMovementInformation(const int ClientId)
} }
float DisplayAngle = Angle * 180.0f / pi; float DisplayAngle = Angle * 180.0f / pi;
char aBuf[128];
float w;
float y = StartY + LineSpacer * 2; float y = StartY + LineSpacer * 2;
float xl = StartX + 2; float xl = StartX + 2;
float xr = m_Width - 2; float xr = m_Width - 2;
int DigitsIndex;
static float s_TextWidth0 = TextRender()->TextWidth(Fontsize, "0.00", -1, -1.0f);
static float s_TextWidth00 = TextRender()->TextWidth(Fontsize, "00.00", -1, -1.0f);
static float s_TextWidth000 = TextRender()->TextWidth(Fontsize, "000.00", -1, -1.0f);
static float s_TextWidth0000 = TextRender()->TextWidth(Fontsize, "0000.00", -1, -1.0f);
static float s_TextWidth00000 = TextRender()->TextWidth(Fontsize, "00000.00", -1, -1.0f);
static float s_TextWidth000000 = TextRender()->TextWidth(Fontsize, "000000.00", -1, -1.0f);
static float s_aTextWidth[6] = {s_TextWidth0, s_TextWidth00, s_TextWidth000, s_TextWidth0000, s_TextWidth00000, s_TextWidth000000};
static float s_TextWidthMinus0 = TextRender()->TextWidth(Fontsize, "-0.00", -1, -1.0f);
static float s_TextWidthMinus00 = TextRender()->TextWidth(Fontsize, "-00.00", -1, -1.0f);
static float s_TextWidthMinus000 = TextRender()->TextWidth(Fontsize, "-000.00", -1, -1.0f);
static float s_TextWidthMinus0000 = TextRender()->TextWidth(Fontsize, "-0000.00", -1, -1.0f);
static float s_TextWidthMinus00000 = TextRender()->TextWidth(Fontsize, "-00000.00", -1, -1.0f);
static float s_TextWidthMinus000000 = TextRender()->TextWidth(Fontsize, "-000000.00", -1, -1.0f);
static float s_aTextWidthMinus[6] = {s_TextWidthMinus0, s_TextWidthMinus00, s_TextWidthMinus000, s_TextWidthMinus0000, s_TextWidthMinus00000, s_TextWidthMinus000000};
if(g_Config.m_ClShowhudPlayerPosition) if(g_Config.m_ClShowhudPlayerPosition)
{ {
@ -1332,17 +1348,13 @@ void CHud::RenderMovementInformation(const int ClientId)
y += MOVEMENT_INFORMATION_LINE_HEIGHT; y += MOVEMENT_INFORMATION_LINE_HEIGHT;
TextRender()->Text(xl, y, Fontsize, "X:", -1.0f); TextRender()->Text(xl, y, Fontsize, "X:", -1.0f);
str_format(aBuf, sizeof(aBuf), "%.2f", Pos.x); UpdateMovementInformationTextContainer(m_aPlayerPositionContainers[0], Fontsize, Pos.x, m_aaPlayerPositionText[0], sizeof(m_aaPlayerPositionText[0]));
DigitsIndex = GetDigitsIndex(Pos.x, 5); RenderMovementInformationTextContainer(m_aPlayerPositionContainers[0], xr, y);
w = (Pos.x < 0) ? s_aTextWidthMinus[DigitsIndex] : s_aTextWidth[DigitsIndex];
TextRender()->Text(xr - w, y, Fontsize, aBuf, -1.0f);
y += MOVEMENT_INFORMATION_LINE_HEIGHT; y += MOVEMENT_INFORMATION_LINE_HEIGHT;
TextRender()->Text(xl, y, Fontsize, "Y:", -1.0f); TextRender()->Text(xl, y, Fontsize, "Y:", -1.0f);
str_format(aBuf, sizeof(aBuf), "%.2f", Pos.y); UpdateMovementInformationTextContainer(m_aPlayerPositionContainers[1], Fontsize, Pos.y, m_aaPlayerPositionText[1], sizeof(m_aaPlayerPositionText[1]));
DigitsIndex = GetDigitsIndex(Pos.y, 5); RenderMovementInformationTextContainer(m_aPlayerPositionContainers[1], xr, y);
w = (Pos.y < 0) ? s_aTextWidthMinus[DigitsIndex] : s_aTextWidth[DigitsIndex];
TextRender()->Text(xr - w, y, Fontsize, aBuf, -1.0f);
y += MOVEMENT_INFORMATION_LINE_HEIGHT; y += MOVEMENT_INFORMATION_LINE_HEIGHT;
} }
@ -1352,17 +1364,13 @@ void CHud::RenderMovementInformation(const int ClientId)
y += MOVEMENT_INFORMATION_LINE_HEIGHT; y += MOVEMENT_INFORMATION_LINE_HEIGHT;
TextRender()->Text(xl, y, Fontsize, "X:", -1.0f); TextRender()->Text(xl, y, Fontsize, "X:", -1.0f);
str_format(aBuf, sizeof(aBuf), "%.2f", DisplaySpeedX); UpdateMovementInformationTextContainer(m_aPlayerSpeedTextContainers[0], Fontsize, DisplaySpeedX, m_aaPlayerSpeedText[0], sizeof(m_aaPlayerSpeedText[0]));
DigitsIndex = GetDigitsIndex(DisplaySpeedX, 5); RenderMovementInformationTextContainer(m_aPlayerSpeedTextContainers[0], xr, y);
w = (DisplaySpeedX < 0) ? s_aTextWidthMinus[DigitsIndex] : s_aTextWidth[DigitsIndex];
TextRender()->Text(xr - w, y, Fontsize, aBuf, -1.0f);
y += MOVEMENT_INFORMATION_LINE_HEIGHT; y += MOVEMENT_INFORMATION_LINE_HEIGHT;
TextRender()->Text(xl, y, Fontsize, "Y:", -1.0f); TextRender()->Text(xl, y, Fontsize, "Y:", -1.0f);
str_format(aBuf, sizeof(aBuf), "%.2f", DisplaySpeedY); UpdateMovementInformationTextContainer(m_aPlayerSpeedTextContainers[1], Fontsize, DisplaySpeedY, m_aaPlayerSpeedText[1], sizeof(m_aaPlayerSpeedText[1]));
DigitsIndex = GetDigitsIndex(DisplaySpeedY, 5); RenderMovementInformationTextContainer(m_aPlayerSpeedTextContainers[1], xr, y);
w = (DisplaySpeedY < 0) ? s_aTextWidthMinus[DigitsIndex] : s_aTextWidth[DigitsIndex];
TextRender()->Text(xr - w, y, Fontsize, aBuf, -1.0f);
y += MOVEMENT_INFORMATION_LINE_HEIGHT; y += MOVEMENT_INFORMATION_LINE_HEIGHT;
} }
@ -1370,10 +1378,9 @@ void CHud::RenderMovementInformation(const int ClientId)
{ {
TextRender()->Text(xl, y, Fontsize, Localize("Angle:"), -1.0f); TextRender()->Text(xl, y, Fontsize, Localize("Angle:"), -1.0f);
y += MOVEMENT_INFORMATION_LINE_HEIGHT; y += MOVEMENT_INFORMATION_LINE_HEIGHT;
str_format(aBuf, sizeof(aBuf), "%.2f", DisplayAngle);
DigitsIndex = GetDigitsIndex(DisplayAngle, 5); UpdateMovementInformationTextContainer(m_PlayerAngleTextContainerIndex, Fontsize, DisplayAngle, m_aPlayerAngleText, sizeof(m_aPlayerAngleText));
w = (DisplayAngle < 0) ? s_aTextWidthMinus[DigitsIndex] : s_aTextWidth[DigitsIndex]; RenderMovementInformationTextContainer(m_PlayerAngleTextContainerIndex, xr, y);
TextRender()->Text(xr - w, y, Fontsize, aBuf, -1.0f);
} }
} }

View file

@ -49,6 +49,12 @@ class CHud : public CComponent
SScoreInfo m_aScoreInfo[2]; SScoreInfo m_aScoreInfo[2];
STextContainerIndex m_FPSTextContainerIndex; STextContainerIndex m_FPSTextContainerIndex;
STextContainerIndex m_DDRaceEffectsTextContainerIndex; STextContainerIndex m_DDRaceEffectsTextContainerIndex;
STextContainerIndex m_PlayerAngleTextContainerIndex;
char m_aPlayerAngleText[128];
STextContainerIndex m_aPlayerSpeedTextContainers[2];
char m_aaPlayerSpeedText[2][128];
STextContainerIndex m_aPlayerPositionContainers[2];
char m_aaPlayerPositionText[2][128];
void RenderCursor(); void RenderCursor();
@ -65,6 +71,9 @@ class CHud : public CComponent
void RenderDummyActions(); void RenderDummyActions();
void RenderMovementInformation(const int ClientId); void RenderMovementInformation(const int ClientId);
void UpdateMovementInformationTextContainer(STextContainerIndex &TextContainer, float FontSize, float Value, char *pPrevValue, size_t Size);
void RenderMovementInformationTextContainer(STextContainerIndex &TextContainer, float X, float Y);
void RenderGameTimer(); void RenderGameTimer();
void RenderPauseNotification(); void RenderPauseNotification();
void RenderSuddenDeath(); void RenderSuddenDeath();