From bb650b85c48648f75ad8acbcbf7f14d85fa5d002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 31 Mar 2023 22:06:04 +0200 Subject: [PATCH 1/2] Minor refactoring of console rendering variables Remove temporary variables. Mark variables as `const` when possible. Avoid duplicate calculations. Use `bool` instead of `int`. --- src/game/client/components/console.cpp | 36 ++++++++++++-------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/game/client/components/console.cpp b/src/game/client/components/console.cpp index 475ba7601..046903ad5 100644 --- a/src/game/client/components/console.cpp +++ b/src/game/client/components/console.cpp @@ -884,9 +884,7 @@ void CGameConsole::OnRender() CUIRect Screen = *UI()->Screen(); CInstance *pConsole = CurrentConsole(); - float MaxConsoleHeight = Screen.h * 3 / 5.0f; - float ConsoleHeight; - + const float MaxConsoleHeight = Screen.h * 3 / 5.0f; float Progress = (Client()->GlobalTime() - (m_StateChangeEnd - m_StateChangeDuration)) / m_StateChangeDuration; if(Progress >= 1.0f) @@ -916,15 +914,14 @@ void CGameConsole::OnRender() Input()->MouseModeAbsolute(); float ConsoleHeightScale; - if(m_ConsoleState == CONSOLE_OPENING) ConsoleHeightScale = ConsoleScaleFunc(Progress); else if(m_ConsoleState == CONSOLE_CLOSING) ConsoleHeightScale = ConsoleScaleFunc(1.0f - Progress); - else // if (console_state == CONSOLE_OPEN) + else // CONSOLE_OPEN ConsoleHeightScale = ConsoleScaleFunc(1.0f); - ConsoleHeight = ConsoleHeightScale * MaxConsoleHeight; + const float ConsoleHeight = ConsoleHeightScale * MaxConsoleHeight; UI()->MapScreen(); @@ -959,6 +956,7 @@ void CGameConsole::OnRender() Array[1] = IGraphics::CColorVertex(1, 0, 0, 0, 0.0f); Array[2] = IGraphics::CColorVertex(2, 0, 0, 0, 0.25f); Array[3] = IGraphics::CColorVertex(3, 0, 0, 0, 0.25f); + Graphics()->SetColorVertex(Array, 4); QuadItem = IGraphics::CQuadItem(0, ConsoleHeight - 20, Screen.w, 10); Graphics()->QuadsDrawTL(&QuadItem, 1); @@ -973,15 +971,14 @@ void CGameConsole::OnRender() Graphics()->QuadsDrawTL(&QuadItem, 1); Graphics()->QuadsEnd(); - ConsoleHeight -= 22.0f; - { // Get height of 1 line - float LineHeight = TextRender()->TextBoundingBox(FONT_SIZE, " ", -1, -1, LINE_SPACING).m_H; + const float LineHeight = TextRender()->TextBoundingBox(FONT_SIZE, " ", -1, -1.0f, LINE_SPACING).m_H; + + const float RowHeight = FONT_SIZE * 1.5f; - float RowHeight = FONT_SIZE * 1.5f; float x = 3; - float y = ConsoleHeight - RowHeight - 5.0f; + float y = ConsoleHeight - RowHeight - 27.0f; const float InitialX = x; const float InitialY = y; @@ -1153,8 +1150,8 @@ void CGameConsole::OnRender() float CalcOffsetY = 0; while(y - (CalcOffsetY + LineHeight) > RowHeight) CalcOffsetY += LineHeight; - float ClipStartY = y - CalcOffsetY; - Graphics()->ClipEnable(0, ClipStartY * YScale, Screen.w * XScale, y * YScale - ClipStartY * YScale); + const float ClipStartY = (y - CalcOffsetY) * YScale; + Graphics()->ClipEnable(0, ClipStartY, Screen.w * XScale, y * YScale - ClipStartY); while(pEntry) { @@ -1175,25 +1172,24 @@ void CGameConsole::OnRender() OffsetY -= (pConsole->m_BacklogLastActiveLine - SkippedLines) * LineHeight; } - float LocalOffsetY = OffsetY + pEntry->m_YOffset / (float)pEntry->m_LineCount; + const float LocalOffsetY = OffsetY + pEntry->m_YOffset / (float)pEntry->m_LineCount; OffsetY += pEntry->m_YOffset; // Only apply offset if we do not keep scroll position (m_BacklogCurLine == 0) if((pConsole->m_HasSelection || pConsole->m_MouseIsPress) && pConsole->m_NewLineCounter > 0 && pConsole->m_BacklogCurLine == 0) { - float MouseExtraOff = pEntry->m_YOffset; - pConsole->m_MousePress.y -= MouseExtraOff; + pConsole->m_MousePress.y -= pEntry->m_YOffset; if(!pConsole->m_MouseIsPress) - pConsole->m_MouseRelease.y -= MouseExtraOff; + pConsole->m_MouseRelease.y -= pEntry->m_YOffset; } // stop rendering when lines reach the top - bool Outside = y - OffsetY <= RowHeight; - int CanRenderOneLine = y - LocalOffsetY > RowHeight; + const bool Outside = y - OffsetY <= RowHeight; + const bool CanRenderOneLine = y - LocalOffsetY > RowHeight; if(Outside && !CanRenderOneLine) break; - int LinesNotRendered = pEntry->m_LineCount - minimum((int)std::floor((y - LocalOffsetY) / RowHeight), pEntry->m_LineCount); + const int LinesNotRendered = pEntry->m_LineCount - minimum((int)std::floor((y - LocalOffsetY) / RowHeight), pEntry->m_LineCount); pConsole->m_LinesRendered -= LinesNotRendered; TextRender()->SetCursor(&Cursor, 0.0f, y - OffsetY, FONT_SIZE, TEXTFLAG_RENDER); From 0da1743143b3ec6fffb83f0a07a662e6a17e9dc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Tue, 19 Dec 2023 22:09:48 +0100 Subject: [PATCH 2/2] Avoid `while` loop for calculating console Y offset --- src/game/client/components/console.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/game/client/components/console.cpp b/src/game/client/components/console.cpp index 046903ad5..16e3db90b 100644 --- a/src/game/client/components/console.cpp +++ b/src/game/client/components/console.cpp @@ -1147,9 +1147,7 @@ void CGameConsole::OnRender() const float XScale = Graphics()->ScreenWidth() / Screen.w; const float YScale = Graphics()->ScreenHeight() / Screen.h; - float CalcOffsetY = 0; - while(y - (CalcOffsetY + LineHeight) > RowHeight) - CalcOffsetY += LineHeight; + const float CalcOffsetY = LineHeight * std::floor((y - RowHeight) / LineHeight); const float ClipStartY = (y - CalcOffsetY) * YScale; Graphics()->ClipEnable(0, ClipStartY, Screen.w * XScale, y * YScale - ClipStartY);