Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor dxDrawCircle to use primitives queue, fix it's postGui issues #618

Merged
merged 3 commits into from
Sep 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 23 additions & 85 deletions Client/core/Graphics/CGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,25 +824,32 @@ void CGraphics::DrawRectQueued(float fX, float fY, float fWidth, float fHeight,
void CGraphics::DrawCircleQueued(float fX, float fY, float fRadius, float fStartAngle, float fStopAngle, unsigned long ulColor, unsigned long ulColorCenter,
short siSegments, float fRatio, bool bPostGUI)
{
// Check if window is minimized so we don't calculate vertices for no reason.
if (g_pCore->IsWindowMinimized())
return;

// Set up a queue item
sDrawQueueItem Item;
Item.eType = QUEUE_CIRCLE;
Item.blendMode = m_ActiveBlendMode;
Item.Circle.fX = fX;
Item.Circle.fY = fY;
Item.Circle.fRadius = fRadius;
Item.Circle.fStartAngle = fStartAngle;
Item.Circle.fStopAngle = fStopAngle;
Item.Circle.bPostGUI = bPostGUI;
Item.Circle.fSegments = siSegments;
Item.Circle.fRatio = fRatio;
Item.Circle.ulColor = ulColor;
Item.Circle.ulColorCenter = ulColorCenter;
// Add it to the queue
AddQueueItem(Item, bPostGUI);
auto pVecVertices = new std::vector<PrimitiveVertice>();
fStartAngle = D3DXToRadian(fStartAngle);
fStopAngle = D3DXToRadian(fStopAngle);
// Calculate each segment angle
const float kfSegmentAngle = (fStopAngle - fStartAngle) / (siSegments-1);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember removing -1 in (siSegments-1), and it worked without any issues. Any reason for adding it back?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of floating point based loop, i made it to use exactly the amount of segments specified in dxDrawCircle call, that required adding -1 again


// Add center point
pVecVertices->push_back({ fX,fY,0.0f,ulColorCenter });

// And calculate all other vertices
for (short siSeg = 0; siSeg < siSegments; siSeg++)
{
PrimitiveVertice vert;
float curAngle = fStartAngle + siSeg * kfSegmentAngle;
vert.fX = fX + fRadius * cos(curAngle) * fRatio;
vert.fY = fY + fRadius * sin(curAngle) / fRatio;
vert.fZ = 0.0f;
vert.Color = ulColor;
pVecVertices->push_back(vert);
}

DrawPrimitiveQueued(pVecVertices, D3DPT_TRIANGLEFAN, bPostGUI);
}

void CGraphics::DrawPrimitiveQueued(std::vector<PrimitiveVertice>* pVecVertices, D3DPRIMITIVETYPE eType, bool bPostGUI)
Expand Down Expand Up @@ -933,68 +940,6 @@ bool CGraphics::IsValidPrimitiveSize (int iNumVertives, D3DPRIMITIVETYPE eType)
return true;
}

struct stVertex
{
float x, y, z;
D3DCOLOR color;
};

void CGraphics::DrawCircleInternal(float fX, float fY, float fRadius, float fStartAngle, float fStopAngle, unsigned long ulColor, unsigned long ulColorCenter,
short siSegments, float fRatio, bool bPostGUI)
{
fStartAngle = D3DXToRadian(fStartAngle);
fStopAngle = D3DXToRadian(fStopAngle);

std::vector<stVertex> vecPoints;

// center
stVertex vertCenter;
vertCenter.x = fX;
vertCenter.y = fY;
vertCenter.z = 0;
vertCenter.color = ulColorCenter;
vecPoints.push_back(vertCenter);

// first
stVertex vertFirst;
vertFirst.x = fX + fRadius * cos(fStartAngle) * fRatio;
vertFirst.y = fY + fRadius * sin(fStartAngle) / fRatio;
vertFirst.z = 0;
vertFirst.color = ulColor;
vecPoints.push_back(vertFirst);

const float kfSegmentAngle = (fStopAngle - fStartAngle) / siSegments;

// if kfSegmentAngle is 0.0f or less, we'll enter an infinte loop
if (kfSegmentAngle > 0.0f)
{
for (float fAngle = fStartAngle; fAngle <= fStopAngle;)
{
stVertex vertex;
vertex.x = fX + fRadius * cos(fAngle) * fRatio;
vertex.y = fY + fRadius * sin(fAngle) / fRatio;
vertex.z = 0;
vertex.color = ulColor;
vecPoints.push_back(vertex);
fAngle += kfSegmentAngle;
}
}

// last
stVertex vertLast;
vertLast.x = fX + fRadius * cos(fStopAngle) * fRatio;
vertLast.y = fY + fRadius * sin(fStopAngle) / fRatio;
vertLast.z = 0;
vertLast.color = ulColor;
vecPoints.push_back(vertLast);

if (vecPoints.size() >= 3)
{
m_pDevice->SetTexture(0, 0);
m_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, vecPoints.size() - 2, &vecPoints[0], sizeof(stVertex));
}
}

void CGraphics::DrawTextureQueued(float fX, float fY, float fWidth, float fHeight, float fU, float fV, float fSizeU, float fSizeV, bool bRelativeUV,
CMaterialItem* pMaterial, float fRotation, float fRotCenOffX, float fRotCenOffY, unsigned long ulColor, bool bPostGUI)
{
Expand Down Expand Up @@ -1635,13 +1580,6 @@ void CGraphics::DrawQueueItem(const sDrawQueueItem& Item)
DrawRectangleInternal(Item.Rect.fX, Item.Rect.fY, Item.Rect.fWidth, Item.Rect.fHeight, Item.Rect.ulColor, Item.Rect.bSubPixelPositioning);
break;
}
case QUEUE_CIRCLE:
{
CheckModes(EDrawMode::DX_SPRITE, Item.blendMode);
DrawCircleInternal(Item.Circle.fX, Item.Circle.fY, Item.Circle.fRadius, Item.Circle.fStartAngle, Item.Circle.fStopAngle, Item.Circle.ulColor,
Item.Circle.ulColorCenter, Item.Circle.fSegments, Item.Circle.fRatio, Item.Circle.bPostGUI);
break;
}

case QUEUE_TEXT:
{
Expand Down
18 changes: 0 additions & 18 deletions Client/core/Graphics/CGraphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ class CGraphics : public CGraphicsInterface, public CSingleton<CGraphics>
void DrawLine3D(const CVector& vecBegin, const CVector& vecEnd, unsigned long ulColor, float fWidth = 1.0f);
void DrawRectangle(float fX, float fY, float fWidth, float fHeight, unsigned long ulColor, bool bSubPixelPositioning = false);
void DrawStringOutline(const RECT& rect, unsigned long ulColor, const wchar_t* szText, unsigned long ulFormat, LPD3DXFONT pDXFont);
void DrawCircleInternal(float fX, float fY, float fRadius, float fStartAngle, float fStopAngle, unsigned long ulColor, unsigned long ulColorCenter,
short siSegments, float fRatio, bool bPostGUI);

void SetBlendMode(EBlendModeType blendMode);
EBlendModeType GetBlendMode(void);
Expand Down Expand Up @@ -238,7 +236,6 @@ class CGraphics : public CGraphicsInterface, public CSingleton<CGraphics>
QUEUE_RECT,
QUEUE_TEXTURE,
QUEUE_SHADER,
QUEUE_CIRCLE,
QUEUE_PRIMITIVE,
QUEUE_PRIMITIVEMATERIAL,
};
Expand Down Expand Up @@ -279,20 +276,6 @@ class CGraphics : public CGraphicsInterface, public CSingleton<CGraphics>
bool bSubPixelPositioning;
};

struct sDrawQueueCircle
{
float fX;
float fY;
float fRadius;
short fStartAngle;
short fStopAngle;
float fSegments;
float fRatio;
float bPostGUI;
unsigned long ulColor;
unsigned long ulColorCenter;
};

struct sDrawQueueTexture
{
CMaterialItem* pMaterial;
Expand Down Expand Up @@ -336,7 +319,6 @@ class CGraphics : public CGraphicsInterface, public CSingleton<CGraphics>
sDrawQueueText Text;
sDrawQueueRect Rect;
sDrawQueueTexture Texture;
sDrawQueueCircle Circle;
sDrawQueuePrimitive Primitive;
sDrawQueuePrimitiveMaterial PrimitiveMaterial;
};
Expand Down
4 changes: 4 additions & 0 deletions Client/mods/deathmatch/logic/luadefs/CLuaDrawingDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ int CLuaDrawingDefs::DxDrawCircle(lua_State* luaVM)
if (fStopAngle < fStartAngle)
std::swap(fStopAngle, fStartAngle);

// Clamp the angle, so we never draw more than 360 degrees
if (fStartAngle + 360.0f < fStopAngle)
fStopAngle = fStartAngle + 360.0f;

g_pCore->GetGraphics()->DrawCircleQueued(vecPosition.fX, vecPosition.fY, fRadius, fStartAngle, fStopAngle, color, colorCenter, siSegments,
fRatio, bPostGUI);
lua_pushboolean(luaVM, true);
Expand Down