mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-19 06:28:19 +00:00
104 lines
3 KiB
C++
104 lines
3 KiB
C++
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
||
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
||
|
|
||
|
#include <engine/graphics.h>
|
||
|
#include <engine/textrender.h>
|
||
|
|
||
|
#include "graph.h"
|
||
|
|
||
|
void CGraph::Init(float Min, float Max)
|
||
|
{
|
||
|
SetMin(Min);
|
||
|
SetMax(Max);
|
||
|
m_Index = 0;
|
||
|
}
|
||
|
|
||
|
void CGraph::SetMin(float Min)
|
||
|
{
|
||
|
m_MinRange = m_Min = Min;
|
||
|
}
|
||
|
|
||
|
void CGraph::SetMax(float Max)
|
||
|
{
|
||
|
m_MaxRange = m_Max = Max;
|
||
|
}
|
||
|
|
||
|
void CGraph::Scale()
|
||
|
{
|
||
|
m_Min = m_MinRange;
|
||
|
m_Max = m_MaxRange;
|
||
|
for(auto Value : m_aValues)
|
||
|
{
|
||
|
if(Value > m_Max)
|
||
|
m_Max = Value;
|
||
|
else if(Value < m_Min)
|
||
|
m_Min = Value;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void CGraph::Add(float v, float r, float g, float b)
|
||
|
{
|
||
|
m_Index = (m_Index + 1) % MAX_VALUES;
|
||
|
InsertAt(m_Index, v, r, g, b);
|
||
|
}
|
||
|
|
||
|
void CGraph::InsertAt(size_t Index, float v, float r, float g, float b)
|
||
|
{
|
||
|
dbg_assert(Index < MAX_VALUES, "Index out of bounds");
|
||
|
m_aValues[Index] = v;
|
||
|
m_aColors[Index][0] = r;
|
||
|
m_aColors[Index][1] = g;
|
||
|
m_aColors[Index][2] = b;
|
||
|
}
|
||
|
|
||
|
void CGraph::Render(IGraphics *pGraphics, ITextRender *pTextRender, float x, float y, float w, float h, const char *pDescription)
|
||
|
{
|
||
|
pGraphics->TextureClear();
|
||
|
|
||
|
pGraphics->QuadsBegin();
|
||
|
pGraphics->SetColor(0.0f, 0.0f, 0.0f, 0.75f);
|
||
|
IGraphics::CQuadItem QuadItem(x, y, w, h);
|
||
|
pGraphics->QuadsDrawTL(&QuadItem, 1);
|
||
|
pGraphics->QuadsEnd();
|
||
|
|
||
|
pGraphics->LinesBegin();
|
||
|
pGraphics->SetColor(0.95f, 0.95f, 0.95f, 1.0f);
|
||
|
IGraphics::CLineItem LineItem(x, y + h / 2, x + w, y + h / 2);
|
||
|
pGraphics->LinesDraw(&LineItem, 1);
|
||
|
pGraphics->SetColor(0.5f, 0.5f, 0.5f, 0.75f);
|
||
|
IGraphics::CLineItem aLineItems[2] = {
|
||
|
IGraphics::CLineItem(x, y + (h * 3) / 4, x + w, y + (h * 3) / 4),
|
||
|
IGraphics::CLineItem(x, y + h / 4, x + w, y + h / 4)};
|
||
|
pGraphics->LinesDraw(aLineItems, std::size(aLineItems));
|
||
|
for(int i = 1; i < MAX_VALUES; i++)
|
||
|
{
|
||
|
float a0 = (i - 1) / (float)MAX_VALUES;
|
||
|
float a1 = i / (float)MAX_VALUES;
|
||
|
int i0 = (m_Index + i - 1) % MAX_VALUES;
|
||
|
int i1 = (m_Index + i) % MAX_VALUES;
|
||
|
|
||
|
float v0 = (m_aValues[i0] - m_Min) / (m_Max - m_Min);
|
||
|
float v1 = (m_aValues[i1] - m_Min) / (m_Max - m_Min);
|
||
|
|
||
|
IGraphics::CColorVertex aColorVertices[2] = {
|
||
|
IGraphics::CColorVertex(0, m_aColors[i0][0], m_aColors[i0][1], m_aColors[i0][2], 0.75f),
|
||
|
IGraphics::CColorVertex(1, m_aColors[i1][0], m_aColors[i1][1], m_aColors[i1][2], 0.75f)};
|
||
|
pGraphics->SetColorVertex(aColorVertices, std::size(aColorVertices));
|
||
|
IGraphics::CLineItem LineItem2(x + a0 * w, y + h - v0 * h, x + a1 * w, y + h - v1 * h);
|
||
|
pGraphics->LinesDraw(&LineItem2, 1);
|
||
|
}
|
||
|
pGraphics->LinesEnd();
|
||
|
|
||
|
const float FontSize = 12.0f;
|
||
|
const float Spacing = 2.0f;
|
||
|
|
||
|
pTextRender->Text(x + Spacing, y + h - FontSize - Spacing, FontSize, pDescription);
|
||
|
|
||
|
char aBuf[32];
|
||
|
str_format(aBuf, sizeof(aBuf), "%.2f", m_Max);
|
||
|
pTextRender->Text(x + w - pTextRender->TextWidth(FontSize, aBuf) - Spacing, y + Spacing, FontSize, aBuf);
|
||
|
|
||
|
str_format(aBuf, sizeof(aBuf), "%.2f", m_Min);
|
||
|
pTextRender->Text(x + w - pTextRender->TextWidth(FontSize, aBuf) - Spacing, y + h - FontSize - Spacing, FontSize, aBuf);
|
||
|
}
|