Skip to content

Commit 7a9a246

Browse files
CrosRoad95saml1er
authored andcommitted
Feature: function dxDrawCircle (#266)
* test 1 * removed pathfinding from master * . * working version * added ratio * removed dxDrawRectangle example * added check to prevent drawing glitches * added radius check * Renamed resolution to segments * renamed variables for hungarian notation * renamed variables for hungarian notation i missed this file * removed subpixel argument * Update CLuaDrawingDefs.cpp
1 parent f56d598 commit 7a9a246

File tree

5 files changed

+181
-0
lines changed

5 files changed

+181
-0
lines changed

Client/core/Graphics/CGraphics.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,94 @@ void CGraphics::DrawRectQueued(float fX, float fY, float fWidth, float fHeight,
807807
AddQueueItem(Item, bPostGUI);
808808
}
809809

810+
void CGraphics::DrawCircleQueued(float fX, float fY, float fRadius, float fStartAngle, float fStopAngle, unsigned long ulColor, unsigned long ulColorCenter, ushort fSegments, float fRatio, bool bPostGUI)
811+
{
812+
if (g_pCore->IsWindowMinimized())
813+
return;
814+
815+
BeginDrawBatch();
816+
CheckModes(EDrawMode::DX_SPRITE, m_ActiveBlendMode);
817+
818+
// Set up a queue item
819+
sDrawQueueItem Item;
820+
Item.eType = QUEUE_CIRCLE;
821+
Item.blendMode = m_ActiveBlendMode;
822+
Item.Circle.fX = fX;
823+
Item.Circle.fY = fY;
824+
Item.Circle.fRadius = fRadius;
825+
Item.Circle.fStartAngle = fStartAngle;
826+
Item.Circle.fStopAngle = fStopAngle;
827+
Item.Circle.bPostGUI = bPostGUI;
828+
Item.Circle.fSegments = fSegments;
829+
Item.Circle.fRatio = fRatio;
830+
Item.Circle.ulColor = ulColor;
831+
Item.Circle.ulColorCenter = ulColorCenter;
832+
// Add it to the queue
833+
AddQueueItem(Item, bPostGUI);
834+
835+
DrawCircleInternal(fX, fY, fRadius, fStartAngle, fStopAngle, ulColor, ulColorCenter, fSegments, fRatio, bPostGUI);
836+
837+
EndDrawBatch();
838+
}
839+
840+
struct stVertex
841+
{
842+
float x, y, z;
843+
D3DCOLOR color;
844+
};
845+
846+
void CGraphics::DrawCircleInternal(float fX, float fY, float fRadius, float fStartAngle, float fStopAngle, unsigned long ulColor, unsigned long ulColorCenter, ushort fSegments, float fRatio, bool bPostGUI)
847+
{
848+
fStartAngle = D3DXToRadian(fStartAngle);
849+
fStopAngle = D3DXToRadian(fStopAngle);
850+
851+
std::vector<stVertex> vecPoints;
852+
853+
// center
854+
stVertex vertCenter;
855+
vertCenter.x = fX;
856+
vertCenter.y = fY;
857+
vertCenter.z = 0;
858+
vertCenter.color = ulColorCenter;
859+
vecPoints.push_back(vertCenter);
860+
861+
// first
862+
stVertex vertFirst;
863+
vertFirst.x = fX + fRadius * cos(fStartAngle) * fRatio;
864+
vertFirst.y = fY + fRadius * sin(fStartAngle) / fRatio;
865+
vertFirst.z = 0;
866+
vertFirst.color = ulColor;
867+
vecPoints.push_back(vertFirst);
868+
869+
float fSegments2 = (float)fSegments + 1;
870+
float segmentAngle = ((fStopAngle - fStartAngle) / fSegments2);
871+
872+
for (float fAngle = fStartAngle; fAngle <= fStopAngle;)
873+
{
874+
stVertex vertex;
875+
vertex.x = fX + fRadius * cos(fAngle) * fRatio;
876+
vertex.y = fY + fRadius * sin(fAngle) / fRatio;
877+
vertex.z = 0;
878+
vertex.color = ulColor;
879+
vecPoints.push_back(vertex);
880+
fAngle = fAngle + segmentAngle;
881+
}
882+
883+
// last
884+
stVertex vertLast;
885+
vertLast.x = fX + fRadius * cos(fStopAngle) * fRatio;
886+
vertLast.y = fY + fRadius * sin(fStopAngle) / fRatio;
887+
vertLast.z = 0;
888+
vertLast.color = ulColor;
889+
vecPoints.push_back(vertLast);
890+
891+
if (vecPoints.size() >= 3)
892+
{
893+
m_pDevice->SetTexture(0, 0);
894+
m_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, vecPoints.size() - 2, &vecPoints[0], sizeof(stVertex));
895+
}
896+
}
897+
810898
void CGraphics::DrawTextureQueued(float fX, float fY, float fWidth, float fHeight, float fU, float fV, float fSizeU, float fSizeV, bool bRelativeUV,
811899
CMaterialItem* pMaterial, float fRotation, float fRotCenOffX, float fRotCenOffY, unsigned long ulColor, bool bPostGUI)
812900
{
@@ -1445,6 +1533,13 @@ void CGraphics::DrawQueueItem(const sDrawQueueItem& Item)
14451533
DrawRectangleInternal(Item.Rect.fX, Item.Rect.fY, Item.Rect.fWidth, Item.Rect.fHeight, Item.Rect.ulColor, Item.Rect.bSubPixelPositioning);
14461534
break;
14471535
}
1536+
case QUEUE_CIRCLE:
1537+
{
1538+
CheckModes(EDrawMode::DX_SPRITE, Item.blendMode);
1539+
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);
1540+
break;
1541+
}
1542+
14481543
case QUEUE_TEXT:
14491544
{
14501545
RECT rect;

Client/core/Graphics/CGraphics.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class CGraphics : public CGraphicsInterface, public CSingleton<CGraphics>
8080
void DrawLine3D(const CVector& vecBegin, const CVector& vecEnd, unsigned long ulColor, float fWidth = 1.0f);
8181
void DrawRectangle(float fX, float fY, float fWidth, float fHeight, unsigned long ulColor, bool bSubPixelPositioning = false);
8282
void DrawStringOutline(const RECT& rect, unsigned long ulColor, const wchar_t* szText, unsigned long ulFormat, LPD3DXFONT pDXFont);
83+
void DrawCircleInternal(float fX, float fY, float fRadius, float fStartAngle, float fStopAngle, unsigned long ulColor, unsigned long ulColorCenter, ushort fSegments, float fRatio, bool bPostGUI);
8384

8485
void SetBlendMode(EBlendModeType blendMode);
8586
EBlendModeType GetBlendMode(void);
@@ -140,6 +141,8 @@ class CGraphics : public CGraphicsInterface, public CSingleton<CGraphics>
140141
unsigned long ulFormat, ID3DXFont* pDXFont = NULL, bool bPostGUI = false, bool bColorCoded = false, bool bSubPixelPositioning = false,
141142
float fRotation = 0, float fRotationCenterX = 0, float fRotationCenterY = 0);
142143

144+
void DrawCircleQueued(float fX, float fY, float fRadius, float fStartAngle, float fStopAngle, unsigned long ulColor, unsigned long ulColorCenter, ushort fSegments, float fRatio, bool bPostGUI);
145+
143146
void OnChangingRenderTarget(uint uiNewViewportSizeX, uint uiNewViewportSizeY);
144147

145148
// Subsystems
@@ -222,6 +225,7 @@ class CGraphics : public CGraphicsInterface, public CSingleton<CGraphics>
222225
QUEUE_RECT,
223226
QUEUE_TEXTURE,
224227
QUEUE_SHADER,
228+
QUEUE_CIRCLE,
225229
};
226230

227231
struct sDrawQueueLine
@@ -260,6 +264,20 @@ class CGraphics : public CGraphicsInterface, public CSingleton<CGraphics>
260264
bool bSubPixelPositioning;
261265
};
262266

267+
struct sDrawQueueCircle
268+
{
269+
float fX;
270+
float fY;
271+
float fRadius;
272+
short fStartAngle;
273+
short fStopAngle;
274+
float fSegments;
275+
float fRatio;
276+
float bPostGUI;
277+
unsigned long ulColor;
278+
unsigned long ulColorCenter;
279+
};
280+
263281
struct sDrawQueueTexture
264282
{
265283
CMaterialItem* pMaterial;
@@ -290,6 +308,7 @@ class CGraphics : public CGraphicsInterface, public CSingleton<CGraphics>
290308
sDrawQueueText Text;
291309
sDrawQueueRect Rect;
292310
sDrawQueueTexture Texture;
311+
sDrawQueueCircle Circle;
293312
};
294313
};
295314

Client/mods/deathmatch/logic/luadefs/CLuaDrawingDefs.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void CLuaDrawingDefs::LoadFunctions(void)
2121
CLuaCFunctions::AddFunction("dxDrawLine3D", DxDrawLine3D);
2222
CLuaCFunctions::AddFunction("dxDrawText", DxDrawText);
2323
CLuaCFunctions::AddFunction("dxDrawRectangle", DxDrawRectangle);
24+
CLuaCFunctions::AddFunction("dxDrawCircle", DxDrawCircle);
2425
CLuaCFunctions::AddFunction("dxDrawImage", DxDrawImage);
2526
CLuaCFunctions::AddFunction("dxDrawImageSection", DxDrawImageSection);
2627
CLuaCFunctions::AddFunction("dxGetTextWidth", DxGetTextWidth);
@@ -402,6 +403,69 @@ int CLuaDrawingDefs::DxDrawRectangle(lua_State* luaVM)
402403
return 1;
403404
}
404405

406+
int CLuaDrawingDefs::DxDrawCircle(lua_State* luaVM)
407+
{
408+
CVector2D vecPosition;
409+
float fRadius;
410+
float fStartAngle;
411+
float fStopAngle;
412+
SColor color;
413+
SColor colorCenter;
414+
short fSegments;
415+
float fRatio;
416+
bool bPostGUI;
417+
418+
CScriptArgReader argStream(luaVM);
419+
argStream.ReadVector2D(vecPosition);
420+
argStream.ReadNumber(fRadius);
421+
argStream.ReadNumber(fStartAngle, 0);
422+
argStream.ReadNumber(fStopAngle, 360);
423+
argStream.ReadColor(color, 0xFFFFFFFF);
424+
argStream.ReadColor(colorCenter, color);
425+
argStream.ReadNumber(fSegments, 32);
426+
argStream.ReadNumber(fRatio, 1);
427+
argStream.ReadBool(bPostGUI, false);
428+
429+
if (!argStream.HasErrors())
430+
{
431+
const float fMinimumSegments = 3;
432+
const float fMaximumSegments = 1024;
433+
if (fSegments >= fMinimumSegments && fSegments <= fMaximumSegments )
434+
{
435+
const float fMinimumRatio = 0;
436+
const float fMaximumRatio = 100;
437+
if (fRatio > fMinimumRatio && fRatio <= fMaximumRatio)
438+
{
439+
if (fRadius > 0)
440+
{
441+
if (fStopAngle < fStartAngle)
442+
std::swap(fStopAngle, fStartAngle);
443+
444+
g_pCore->GetGraphics()->DrawCircleQueued(vecPosition.fX, vecPosition.fY, fRadius, fStartAngle, fStopAngle, color, colorCenter, fSegments, fRatio, bPostGUI);
445+
lua_pushboolean(luaVM, true);
446+
return 1;
447+
}
448+
}
449+
else
450+
{
451+
lua_pushboolean(luaVM, false);
452+
return 1;
453+
}
454+
}
455+
else
456+
{
457+
lua_pushboolean(luaVM, false);
458+
return 1;
459+
}
460+
}
461+
else
462+
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
463+
464+
// Failed
465+
lua_pushboolean(luaVM, false);
466+
return 1;
467+
}
468+
405469
int CLuaDrawingDefs::DxDrawImage(lua_State* luaVM)
406470
{
407471
// bool dxDrawImage ( float posX, float posY, float width, float height, string filepath [, float rotation = 0, float rotationCenterOffsetX = 0,

Client/mods/deathmatch/logic/luadefs/CLuaDrawingDefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class CLuaDrawingDefs : public CLuaDefs
2424
LUA_DECLARE(DxDrawMaterialSectionLine3D);
2525
LUA_DECLARE(DxDrawText);
2626
LUA_DECLARE(DxDrawRectangle);
27+
LUA_DECLARE(DxDrawCircle);
2728
LUA_DECLARE(DxDrawImage);
2829
LUA_DECLARE(DxDrawImageSection);
2930
LUA_DECLARE_OOP(DxGetTextWidth);

Client/sdk/core/CGraphicsInterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class CGraphicsInterface
127127
float fScaleY, unsigned long ulFormat, ID3DXFont* pDXFont, bool bPostGUI, bool bColorCoded = false,
128128
bool bSubPixelPositioning = false, float fRotation = 0, float fRotationCenterX = 0, float fRotationCenterY = 0) = 0;
129129

130+
virtual void DrawCircleQueued(float fX, float fY, float fRadius, float fStartAngle, float fStopAngle, unsigned long ulColor, unsigned long ulColorCenter, ushort fSegments, float fRatio, bool bPostGUI) = 0;
131+
130132
// Subsystems
131133
virtual CRenderItemManagerInterface* GetRenderItemManager(void) = 0;
132134
virtual CScreenGrabberInterface* GetScreenGrabber(void) = 0;

0 commit comments

Comments
 (0)