Skip to content

Commit 026e633

Browse files
committed
fix(draw): Replace ParticleSystem pointers with ParticleSystemID for safe particle lookups in W3DTrankDraw, W3DTankTruckDraw, W3DTruckDraw (TheSuperHackers#2235)
1 parent e5922b4 commit 026e633

File tree

6 files changed

+121
-137
lines changed

6 files changed

+121
-137
lines changed

Core/GameEngineDevice/Include/W3DDevice/GameClient/Module/W3DTankDraw.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class W3DTankDraw : public W3DModelDraw
7575
protected:
7676

7777
/// left and right debris emitters for when tank is moving
78-
ParticleSystem* m_treadDebris[2];
78+
ParticleSystemID m_treadDebrisIDs[2];
7979

8080
RenderObjClass *m_prevRenderObj;
8181

@@ -96,7 +96,6 @@ class W3DTankDraw : public W3DModelDraw
9696
void createEmitters( void ); ///< Create particle effects.
9797
void tossEmitters( void ); ///< Create particle effects.
9898

99-
void startMoveDebris( void ); ///< start creating debris from the tank treads
10099
void stopMoveDebris( void ); ///< stop creating debris from the tank treads
101100
void updateTreadObjects(void); ///< update pointers to sub-objects like treads.
102101
void updateTreadPositions(Real uvDelta); ///< update uv coordinates on each tread

Core/GameEngineDevice/Include/W3DDevice/GameClient/Module/W3DTankTruckDraw.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class W3DTankTruckDraw : public W3DModelDraw
9999

100100
/// debris emitters for when tank is moving
101101
enum { DustEffect, DirtEffect, PowerslideEffect };
102-
ParticleSystem* m_truckEffects[3];
102+
ParticleSystemID m_truckEffectIDs[3];
103103

104104
Real m_frontWheelRotation;
105105
Real m_rearWheelRotation;
@@ -122,7 +122,7 @@ class W3DTankTruckDraw : public W3DModelDraw
122122
//Tank Data
123123

124124
/// left and right debris emitters for when tank is moving
125-
ParticleSystem* m_treadDebris[2];
125+
ParticleSystemID m_treadDebrisIDs[2];
126126

127127
enum TreadType { TREAD_LEFT, TREAD_RIGHT, TREAD_MIDDLE }; //types of treads for different vehicles
128128
enum {MAX_TREADS_PER_TANK=4};
@@ -144,7 +144,6 @@ class W3DTankTruckDraw : public W3DModelDraw
144144
void enableEmitters( Bool enable ); ///< stop creating debris from the tank treads
145145
void updateBones( void );
146146

147-
void startMoveDebris( void ); ///< start creating debris from the tank treads
148147
void stopMoveDebris( void ); ///< stop creating debris from the tank treads
149148
void updateTreadObjects(void); ///< update pointers to sub-objects like treads.
150149
void updateTreadPositions(Real uvDelta); ///< update uv coordinates on each tread

Core/GameEngineDevice/Include/W3DDevice/GameClient/Module/W3DTruckDraw.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class W3DTruckDraw : public W3DModelDraw
102102

103103
/// debris emitters for when tank is moving
104104
enum { DustEffect, DirtEffect, PowerslideEffect };
105-
ParticleSystem* m_truckEffects[3];
105+
ParticleSystemID m_truckEffectIDs[3];
106106

107107
Real m_frontWheelRotation;
108108
Real m_rearWheelRotation;

Core/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DTankDraw.cpp

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ W3DTankDraw::W3DTankDraw( Thing *thing, const ModuleData* moduleData )
8989
: W3DModelDraw( thing, moduleData )
9090
, m_prevRenderObj(nullptr)
9191
{
92-
std::fill(m_treadDebris, m_treadDebris + ARRAY_SIZE(m_treadDebris), nullptr);
92+
std::fill(m_treadDebrisIDs, m_treadDebrisIDs + ARRAY_SIZE(m_treadDebrisIDs), INVALID_PARTICLE_SYSTEM_ID);
9393

9494
for (Int i=0; i<MAX_TREADS_PER_TANK; i++)
9595
m_treads[i].m_robj = nullptr;
@@ -107,14 +107,14 @@ W3DTankDraw::W3DTankDraw( Thing *thing, const ModuleData* moduleData )
107107
//-------------------------------------------------------------------------------------------------
108108
void W3DTankDraw::tossEmitters( void )
109109
{
110-
for (size_t i = 0; i < ARRAY_SIZE(m_treadDebris); ++i)
110+
for (size_t i = 0; i < ARRAY_SIZE(m_treadDebrisIDs); ++i)
111111
{
112-
if (m_treadDebris[i] != nullptr)
112+
if (ParticleSystem *particleSys = TheParticleSystemManager->findParticleSystem(m_treadDebrisIDs[i]))
113113
{
114-
m_treadDebris[i]->attachToObject(nullptr);
115-
m_treadDebris[i]->destroy();
116-
m_treadDebris[i] = nullptr;
114+
particleSys->attachToObject(nullptr);
115+
particleSys->destroy();
117116
}
117+
m_treadDebrisIDs[i] = INVALID_PARTICLE_SYSTEM_ID;
118118
}
119119
}
120120

@@ -123,22 +123,23 @@ void W3DTankDraw::tossEmitters( void )
123123
void W3DTankDraw::createEmitters( void )
124124
{
125125
const AsciiString *treadDebrisNames[2];
126-
static_assert(ARRAY_SIZE(treadDebrisNames) == ARRAY_SIZE(m_treadDebris), "Array size must match");
126+
static_assert(ARRAY_SIZE(treadDebrisNames) == ARRAY_SIZE(m_treadDebrisIDs), "Array size must match");
127127
treadDebrisNames[0] = &getW3DTankDrawModuleData()->m_treadDebrisNameLeft;
128128
treadDebrisNames[1] = &getW3DTankDrawModuleData()->m_treadDebrisNameRight;
129129

130-
for (size_t i = 0; i < ARRAY_SIZE(m_treadDebris); ++i)
130+
for (size_t i = 0; i < ARRAY_SIZE(m_treadDebrisIDs); ++i)
131131
{
132-
if (m_treadDebris[i] == nullptr)
132+
if (m_treadDebrisIDs[i] == INVALID_PARTICLE_SYSTEM_ID)
133133
{
134134
if (const ParticleSystemTemplate *sysTemplate = TheParticleSystemManager->findTemplate(*treadDebrisNames[i]))
135135
{
136-
m_treadDebris[i] = TheParticleSystemManager->createParticleSystem( sysTemplate );
137-
m_treadDebris[i]->attachToDrawable(getDrawable());
136+
ParticleSystem *particleSys = TheParticleSystemManager->createParticleSystem( sysTemplate );
137+
particleSys->attachToDrawable(getDrawable());
138138
// important: mark it as do-not-save, since we'll just re-create it when we reload.
139-
m_treadDebris[i]->setSaveable(FALSE);
139+
particleSys->setSaveable(FALSE);
140140
// they come into being stopped.
141-
m_treadDebris[i]->stop();
141+
particleSys->stop();
142+
m_treadDebrisIDs[i] = particleSys->getSystemID();
142143
}
143144
}
144145
}
@@ -156,33 +157,19 @@ W3DTankDraw::~W3DTankDraw()
156157
REF_PTR_RELEASE(m_treads[i].m_robj);
157158
}
158159

159-
//-------------------------------------------------------------------------------------------------
160-
//-------------------------------------------------------------------------------------------------
161-
/**
162-
* Start creating debris from the tank treads
163-
*/
164-
void W3DTankDraw::startMoveDebris( void )
165-
{
166-
if (getDrawable()->isDrawableEffectivelyHidden())
167-
return;
168-
for (size_t i = 0; i < ARRAY_SIZE(m_treadDebris); ++i)
169-
{
170-
if (m_treadDebris[i] != nullptr)
171-
m_treadDebris[i]->start();
172-
}
173-
}
174-
175160
//-------------------------------------------------------------------------------------------------
176161
//-------------------------------------------------------------------------------------------------
177162
/**
178163
* Stop creating debris from the tank treads
179164
*/
180165
void W3DTankDraw::stopMoveDebris( void )
181166
{
182-
for (size_t i = 0; i < ARRAY_SIZE(m_treadDebris); ++i)
167+
for (size_t i = 0; i < ARRAY_SIZE(m_treadDebrisIDs); ++i)
183168
{
184-
if (m_treadDebris[i] != nullptr)
185-
m_treadDebris[i]->stop();
169+
if (ParticleSystem *particleSys = TheParticleSystemManager->findParticleSystem(m_treadDebrisIDs[i]))
170+
{
171+
particleSys->stop();
172+
}
186173
}
187174
}
188175

@@ -330,10 +317,7 @@ void W3DTankDraw::doDrawModule(const Matrix3D* transformMtx)
330317
// if tank is moving, kick up dust and debris
331318
Real velMag = vel->x*vel->x + vel->y*vel->y; // only care about moving on the ground
332319

333-
if (velMag > DEBRIS_THRESHOLD && !getDrawable()->isDrawableEffectivelyHidden() && !getFullyObscuredByShroud())
334-
startMoveDebris();
335-
else
336-
stopMoveDebris();
320+
const Bool doStartMoveDebris = velMag > DEBRIS_THRESHOLD && !getDrawable()->isDrawableEffectivelyHidden() && !getFullyObscuredByShroud();
337321

338322
// kick debris higher the faster we move
339323
Coord3D velMult;
@@ -349,10 +333,18 @@ void W3DTankDraw::doDrawModule(const Matrix3D* transformMtx)
349333
if (velMult.z > 1.0f)
350334
velMult.z = 1.0f;
351335

352-
for (size_t i = 0; i < ARRAY_SIZE(m_treadDebris); ++i)
336+
for (size_t i = 0; i < ARRAY_SIZE(m_treadDebrisIDs); ++i)
353337
{
354-
m_treadDebris[i]->setVelocityMultiplier( &velMult );
355-
m_treadDebris[i]->setBurstCountMultiplier( velMult.z );
338+
if (ParticleSystem *particleSys = TheParticleSystemManager->findParticleSystem(m_treadDebrisIDs[i]))
339+
{
340+
if (doStartMoveDebris)
341+
particleSys->start();
342+
else
343+
particleSys->stop();
344+
345+
particleSys->setVelocityMultiplier( &velMult );
346+
particleSys->setBurstCountMultiplier( velMult.z );
347+
}
356348
}
357349

358350
//Update movement of treads

0 commit comments

Comments
 (0)