VGui: use cached string lengths for notify logs

Don't recalculate the string length when displaying it on the VGui console.
This commit is contained in:
Kawe Mazidjatari 2025-02-09 00:30:06 +01:00
parent 26ec02f302
commit 1331c3c67b
3 changed files with 28 additions and 22 deletions

View File

@ -352,7 +352,7 @@ void EngineLoggerSink(LogType_t logType, LogLevel_t logLevel, eDLL_T context,
if (g_bSdkInitialized && logLevel >= LogLevel_t::LEVEL_NOTIFY)
{
// Draw to mini console.
g_TextOverlay.AddLog(overlayContext, message.c_str());
g_TextOverlay.AddLog(overlayContext, message.c_str(), (ssize_t)message.length());
}
#endif // !DEDICATED
}

View File

@ -111,7 +111,7 @@ void CTextOverlay::Update(void)
//-----------------------------------------------------------------------------
// Purpose: add a log to the vector.
//-----------------------------------------------------------------------------
void CTextOverlay::AddLog(const eDLL_T context, const char* pszText)
void CTextOverlay::AddLog(const eDLL_T context, const char* pszText, const ssize_t textLen)
{
Assert(pszText);
@ -121,7 +121,11 @@ void CTextOverlay::AddLog(const eDLL_T context, const char* pszText)
}
AUTO_LOCK(m_Mutex);
m_NotifyLines.AddToTail(CTextNotify{ context, con_notifytime.GetFloat() , pszText });
const int newLine = m_NotifyLines.AddToTail();
TextNotify_s& notify = m_NotifyLines[newLine];
notify.Init(context, con_notifytime.GetFloat(), pszText, textLen);
while (m_NotifyLines.Count() > 0 &&
(m_NotifyLines.Count() > con_notifylines.GetInt()))
@ -145,7 +149,7 @@ void CTextOverlay::DrawNotify(void)
for (int i = 0, j = m_NotifyLines.Count(); i < j; i++)
{
const CTextNotify& notify = m_NotifyLines[i];
const TextNotify_s& notify = m_NotifyLines[i];
Color c = GetLogColorForType(notify.m_Type);
const float flTimeleft = notify.m_flLifeRemaining;
@ -214,10 +218,10 @@ void CTextOverlay::ShouldDraw(const float flFrameTime)
FOR_EACH_VEC_BACK(m_NotifyLines, i)
{
CTextNotify* pNotify = &m_NotifyLines[i];
pNotify->m_flLifeRemaining -= flFrameTime;
TextNotify_s& notify = m_NotifyLines[i];
notify.m_flLifeRemaining -= flFrameTime;
if (pNotify->m_flLifeRemaining <= 0.0f)
if (notify.m_flLifeRemaining <= 0.0f)
{
m_NotifyLines.Remove(i);
continue;

View File

@ -2,21 +2,23 @@
#include "core/stdafx.h"
#include "mathlib/color.h"
struct CTextNotify
class CTextOverlay
{
CTextNotify(const eDLL_T type, const float flTime, const char* pszText)
private:
struct TextNotify_s
{
void Init(const eDLL_T type, const float flTime, const char* pszText, const ssize_t textLen)
{
this->m_Text = pszText;
this->m_flLifeRemaining = flTime;
this->m_Type = type;
this->m_flLifeRemaining = flTime;
this->m_Text.SetDirect(pszText, textLen);
}
eDLL_T m_Type;
float m_flLifeRemaining;
CUtlString m_Text;
};
class CTextOverlay
{
public:
CTextOverlay()
{
@ -26,7 +28,7 @@ public:
}
void Update(void);
void AddLog(const eDLL_T context, const char* pszText);
void AddLog(const eDLL_T context, const char* pszText, const ssize_t textLen);
void DrawNotify(void);
void DrawFormat(const int x, const int y, const Color c, const char* pszFormat, ...) const;
void ShouldDraw(const float flFrameTime);
@ -38,7 +40,7 @@ public:
private:
Color GetLogColorForType(const eDLL_T type) const;
CUtlVector<CTextNotify> m_NotifyLines;
CUtlVector<TextNotify_s> m_NotifyLines;
int m_nFontHeight; // Hardcoded to 16 in this engine.
mutable CThreadFastMutex m_Mutex;