Skip to content

Commit

Permalink
Make frame graph scale accordingly to resolution (multitheftauto#3593)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffsPLASMA authored Aug 21, 2024
1 parent eb66fa2 commit e431474
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
5 changes: 5 additions & 0 deletions Client/core/CChat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,11 @@ void CChat::SetCharacterLimit(int charLimit)
m_iCharacterLimit = charLimit;
}

float CChat::GetChatBottomPosition() const noexcept
{
return m_vecBackgroundSize.fY;
}

CChatLine::CChatLine()
{
m_bActive = false;
Expand Down
2 changes: 2 additions & 0 deletions Client/core/CChat.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ class CChat
constexpr int GetDefaultCharacterLimit() const { return m_iDefaultCharacterLimit; }
constexpr int GetMaxCharacterLimit() const { return m_iMaxCharacterLimit; }

float GetChatBottomPosition() const noexcept;

private:
void LoadCVars();

Expand Down
5 changes: 5 additions & 0 deletions Client/core/CGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ CChat* CLocalGUI::GetChat()
return m_pChat;
}

float CLocalGUI::GetChatBottomPosition() const noexcept
{
return m_pChat->GetChatBottomPosition();
}

CDebugView* CLocalGUI::GetDebugView()
{
return m_pDebugView;
Expand Down
1 change: 1 addition & 0 deletions Client/core/CGUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class CLocalGUI : public CSingleton<CLocalGUI>
bool IsMainMenuVisible();

CChat* GetChat();
float GetChatBottomPosition() const noexcept;
void SetChatBoxVisible(bool bVisible, bool bInputBlocked = true);
bool IsChatBoxVisible();
bool IsChatBoxInputBlocked();
Expand Down
48 changes: 28 additions & 20 deletions Client/core/CGraphStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

namespace
{
#define GRAPHSTAT_HISTORY_SIZE 256

struct SGraphStatLine
{
TIMEUS prevData;
Expand Down Expand Up @@ -113,6 +111,11 @@ void CGraphStats::AddTimingPoint(const char* szName)
if (!IsEnabled())
return;

CGraphicsInterface* pGraphics = g_pCore->GetGraphics();

std::uint32_t viewportWidth = pGraphics->GetViewportWidth();
std::uint32_t sizeX = viewportWidth / 4; // one quarter of screen width

// Start of next frame?
if (szName[0] == 0)
{
Expand All @@ -133,7 +136,7 @@ void CGraphStats::AddTimingPoint(const char* szName)
for (int i = 0; i < Dups; i++)
{
pLine->iDataPos++;
if (pLine->iDataPos > GRAPHSTAT_HISTORY_SIZE - 1)
if (pLine->iDataPos > sizeX - 1)
pLine->iDataPos = 0;
pLine->dataHistory[pLine->iDataPos] = Data;
}
Expand All @@ -153,7 +156,7 @@ void CGraphStats::AddTimingPoint(const char* szName)
// Add new line
MapSet(m_LineList, szName, SGraphStatLine());
pLine = MapFind(m_LineList, szName);
pLine->dataHistory.resize(GRAPHSTAT_HISTORY_SIZE);
pLine->dataHistory.resize(sizeX);
memset(&pLine->dataHistory[0], 0, pLine->dataHistory.size());
pLine->iDataPos = 0;
pLine->prevData = 0;
Expand All @@ -179,7 +182,7 @@ void CGraphStats::AddTimingPoint(const char* szName)

// Inc position
pLine->iDataPos++;
if (pLine->iDataPos > GRAPHSTAT_HISTORY_SIZE - 1)
if (pLine->iDataPos > sizeX - 1)
pLine->iDataPos = 0;

// Insert data point
Expand All @@ -199,44 +202,49 @@ void CGraphStats::Draw()
return;

CGraphicsInterface* pGraphics = g_pCore->GetGraphics();
CLocalGUI* pLocalGUI = g_pCore->GetLocalGUI();

std::uint32_t viewportWidth = pGraphics->GetViewportWidth(); // get width of current resolution
std::uint32_t viewportHeight = pGraphics->GetViewportHeight(); // get height of current resolution
std::uint32_t originX = 10; // offset the graph by 10 pixels from left side of screen
std::uint32_t originY = pLocalGUI->GetChatBottomPosition(); // get chat bottom screen position
std::uint32_t sizeX = viewportWidth / 4; // set the width of graph to 1/4 of current resolution
std::uint32_t sizeY = viewportHeight / 4; // set the height of graph to 1/4 of current resolution
std::uint32_t rangeY = 100; // 100ms

originY = originY + sizeY + 30; // add graph height plus a little gap to the overall Y position

uint uiViewportHeight = pGraphics->GetViewportHeight();
uint uiOriginX = 10;
uint uiOriginY = std::min<int>(500, uiViewportHeight - 10);
uint uiSizeX = GRAPHSTAT_HISTORY_SIZE;
uint uiSizeY = 150;
uint uiRangeY = 100; // 100ms
float fLineScale = 1 / 1000.f / uiRangeY * uiSizeY;
float fLineScale = 1 / 1000.f / rangeY * sizeY;
float fLineHeight = pGraphics->GetDXFontHeight();

// Backgroung box
pGraphics->DrawRectQueued(uiOriginX, uiOriginY - uiSizeY, uiSizeX, uiSizeY, SColorRGBA(0, 0, 0, 128), true);
pGraphics->DrawRectQueued(originX, originY - sizeY, sizeX, sizeY, SColorRGBA(0, 0, 0, 128), true);

// Draw data lines
float fLabelX = uiOriginX + uiSizeX + 22;
float fLabelY = uiOriginY - m_LineList.size() * fLineHeight;
float fLabelX = originX + sizeX + 22;
float fLabelY = originY - m_LineList.size() * fLineHeight;
for (const auto& dataLine : m_LineList)
{
const SGraphStatLine& line = dataLine.second;
int iDataPos = line.iDataPos;
int iDataPosPrev = iDataPos;

for (int i = uiSizeX - 1; i > 0; i--)
for (int i = sizeX - 1; i > 0; i--)
{
float fY0 = line.dataHistory[iDataPos] * fLineScale;
float fY1 = line.dataHistory[iDataPosPrev] * fLineScale;

iDataPosPrev = iDataPos;
iDataPos--;
if (iDataPos == -1)
iDataPos = GRAPHSTAT_HISTORY_SIZE - 1;
iDataPos = sizeX - 1;

pGraphics->DrawLineQueued(uiOriginX + i - 1, uiOriginY - fY0, uiOriginX + i, uiOriginY - fY1, 1, line.color, true);
pGraphics->DrawLineQueued(originX + i - 1, originY - fY0, originX + i, originY - fY1, 1, line.color, true);

if (i == uiSizeX - 1)
if (i == sizeX - 1)
{
// Line from graph to label
pGraphics->DrawLineQueued(uiOriginX + i - 1, uiOriginY - fY0, fLabelX - 2, fLabelY + fLineHeight / 2, 1, line.color, true);
pGraphics->DrawLineQueued(originX + i - 1, originY - fY0, fLabelX - 2, fLabelY + fLineHeight / 2, 1, line.color, true);
}
}

Expand Down

0 comments on commit e431474

Please sign in to comment.