Skip to content

Commit 403df24

Browse files
tederisLpsd
andauthored
SVG related fixes (#2832)
* SVG texture lock checks * Lock rect with D3DLOCK_DISCARD * Fix return type inconsistency Co-authored-by: Lpsd <40902730+Lpsd@users.noreply.github.com>
1 parent 3b89b67 commit 403df24

File tree

2 files changed

+28
-37
lines changed

2 files changed

+28
-37
lines changed

Client/mods/deathmatch/logic/CClientVectorGraphicDisplay.cpp

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ void CClientVectorGraphicDisplay::Render()
4242
if (HasUpdated())
4343
{
4444
m_pVectorGraphic->OnUpdate();
45-
}
46-
45+
}
4746
}
4847

4948
void CClientVectorGraphicDisplay::UpdateTexture()
@@ -52,46 +51,41 @@ void CClientVectorGraphicDisplay::UpdateTexture()
5251
return;
5352

5453
Document* svgDocument = m_pVectorGraphic->GetSVGDocument();
55-
56-
if (svgDocument == nullptr)
54+
if (!svgDocument)
5755
return;
5856

5957
CVectorGraphicItem* pVectorGraphicItem = m_pVectorGraphic->GetRenderItem();
60-
6158
if (!pVectorGraphicItem)
6259
return;
6360

6461
IDirect3DSurface9* surface = m_pVectorGraphic->GetRenderItem()->m_pD3DRenderTargetSurface;
65-
6662
if (!surface)
6763
return;
6864

69-
IDirect3DDevice9* device = pVectorGraphicItem->m_pDevice;
70-
71-
uint width = pVectorGraphicItem->m_uiSizeX;
72-
uint height = pVectorGraphicItem->m_uiSizeY;
73-
74-
Bitmap bitmap = svgDocument->renderToBitmap(width, height);
65+
Bitmap bitmap = svgDocument->renderToBitmap(pVectorGraphicItem->m_uiSizeX, pVectorGraphicItem->m_uiSizeY);
66+
if (!bitmap.valid())
67+
return;
7568

7669
// Lock surface
7770
D3DLOCKED_RECT LockedRect;
78-
surface->LockRect(&LockedRect, nullptr, 0);
71+
if (SUCCEEDED(surface->LockRect(&LockedRect, nullptr, D3DLOCK_DISCARD)))
72+
{
73+
auto surfaceData = static_cast<byte*>(LockedRect.pBits);
74+
auto sourceData = static_cast<const byte*>(bitmap.data());
7975

80-
auto surfaceData = static_cast<byte*>(LockedRect.pBits);
81-
auto sourceData = static_cast<const byte*>(bitmap.data());
76+
for (uint32_t y = 0; y < bitmap.height(); ++y)
77+
{
78+
memcpy(surfaceData, sourceData, bitmap.width() * 4); // 4 bytes per pixel
8279

83-
for (uint32_t y = 0; y < bitmap.height(); ++y)
84-
{
85-
memcpy(surfaceData, sourceData, bitmap.width() * 4); // 4 bytes per pixel
80+
// advance row pointers
81+
sourceData += bitmap.stride();
82+
surfaceData += LockedRect.Pitch;
83+
}
8684

87-
// advance row pointers
88-
sourceData += bitmap.stride();
89-
surfaceData += LockedRect.Pitch;
85+
// Unlock surface
86+
surface->UnlockRect();
9087
}
9188

92-
// Unlock surface
93-
surface->UnlockRect();
94-
9589
m_bHasUpdated = false;
9690
}
9791

@@ -101,25 +95,22 @@ void CClientVectorGraphicDisplay::ClearTexture()
10195
return;
10296

10397
CVectorGraphicItem* pVectorGraphicItem = m_pVectorGraphic->GetRenderItem();
104-
10598
if (!pVectorGraphicItem)
10699
return;
107100

108101
IDirect3DSurface9* surface = pVectorGraphicItem->m_pD3DRenderTargetSurface;
109-
110102
if (!surface)
111103
return;
112104

113-
IDirect3DDevice9* device = pVectorGraphicItem->m_pDevice;
114-
115105
// Lock surface
116106
D3DLOCKED_RECT LockedRect;
117-
surface->LockRect(&LockedRect, nullptr, 0);
118-
119-
device->ColorFill(surface, nullptr, D3DCOLOR_ARGB(0, 0, 0, 0));
107+
if (SUCCEEDED(surface->LockRect(&LockedRect, nullptr, D3DLOCK_DISCARD)))
108+
{
109+
std::memset(LockedRect.pBits, 0x0, LockedRect.Pitch * pVectorGraphicItem->m_uiSurfaceSizeY);
120110

121-
// Unlock surface
122-
surface->UnlockRect();
111+
// Unlock surface
112+
surface->UnlockRect();
113+
}
123114

124115
m_bIsCleared = true;
125116
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ CClientVectorGraphic* CLuaVectorGraphicDefs::SVGCreate(lua_State* luaVM, CVector
126126
g_pClientGame->GetManager()->GetRenderElementManager()->CreateVectorGraphic(static_cast<int>(size.fX), static_cast<int>(size.fY));
127127

128128
if (!vectorGraphic)
129-
return false;
129+
return nullptr;
130130

131131
if (pathOrRawData.has_value())
132132
{
@@ -151,7 +151,7 @@ CClientVectorGraphic* CLuaVectorGraphicDefs::SVGCreate(lua_State* luaVM, CVector
151151
if (!didLoad)
152152
{
153153
delete vectorGraphic;
154-
return false;
154+
return nullptr;
155155
}
156156
}
157157
else
@@ -177,15 +177,15 @@ CClientVectorGraphic* CLuaVectorGraphicDefs::SVGCreate(lua_State* luaVM, CVector
177177
if (!didLoad)
178178
{
179179
delete vectorGraphic;
180-
return false;
180+
return nullptr;
181181
}
182182
}
183183
else
184184
{
185185
delete vectorGraphic;
186186

187187
m_pScriptDebugging->LogCustom(luaVM, SString("Unable to load SVG (invalid file path) [%s]", pathOrRawData.value().c_str()));
188-
return false;
188+
return nullptr;
189189
}
190190
}
191191
}

0 commit comments

Comments
 (0)