Skip to content

Commit 80cc300

Browse files
authored
refactor(tree): Simplify W3DTreeDraw related code (#1614)
1 parent cd86cc4 commit 80cc300

File tree

9 files changed

+69
-103
lines changed

9 files changed

+69
-103
lines changed

Generals/Code/GameEngine/Include/Common/ThingTemplate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ class ThingTemplate : public Overridable
430430
Real getFenceXOffset() const { return m_fenceXOffset; } // return fence offset
431431

432432
Bool isBridge() const { return m_isBridge; } // return fence offset
433+
Bool isBridgeLike() const { return isBridge() || isKindOf(KINDOF_WALK_ON_TOP_OF_WALL); }
433434

434435
// Only Object can ask this. Everyone else should ask the Object. In fact, you really should ask the Object everything.
435436
Real friend_getVisionRange() const { return m_visionRange; } ///< get vision range

Generals/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,14 +1565,7 @@ void GameLogic::startNewGame( Bool saveGame )
15651565
if( thingTemplate == NULL )
15661566
continue;
15671567

1568-
Bool isBridgeLikeObject = false;
1569-
1570-
if (thingTemplate->isBridge())
1571-
isBridgeLikeObject = true;
1572-
if (thingTemplate->isKindOf(KINDOF_WALK_ON_TOP_OF_WALL))
1573-
isBridgeLikeObject = true;
1574-
1575-
if (!isBridgeLikeObject)
1568+
if (!thingTemplate->isBridgeLike())
15761569
continue;
15771570

15781571
Team *team = ThePlayerList->getNeutralPlayer()->getDefaultTeam();
@@ -1679,11 +1672,8 @@ void GameLogic::startNewGame( Bool saveGame )
16791672
if( thingTemplate == NULL )
16801673
continue;
16811674

1682-
Bool isBridgeLikeObject = false;
1683-
if (thingTemplate->isBridge()) isBridgeLikeObject = true;
1684-
if (thingTemplate->isKindOf(KINDOF_WALK_ON_TOP_OF_WALL)) isBridgeLikeObject = true;
1685-
if (isBridgeLikeObject)
1686-
continue; // bridges have to be added earlier.
1675+
if (thingTemplate->isBridgeLike())
1676+
continue; // bridges have to be added earlier.
16871677

16881678
Bool useTrees = TheGlobalData->m_useTrees;
16891679
if (TheRecorder && TheRecorder->isMultiplayer()) {

GeneralsMD/Code/GameEngine/Include/Common/ThingTemplate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ class ThingTemplate : public Overridable
430430
Real getFenceXOffset() const { return m_fenceXOffset; } // return fence offset
431431

432432
Bool isBridge() const { return m_isBridge; } // return fence offset
433+
Bool isBridgeLike() const { return isBridge() || isKindOf(KINDOF_WALK_ON_TOP_OF_WALL); }
433434

434435
// Only Object can ask this. Everyone else should ask the Object. In fact, you really should ask the Object everything.
435436
Real friend_calcVisionRange() const { return m_visionRange; } ///< get vision range

GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ class GameLogic : public SubsystemInterface, public Snapshot
290290
void remakeSleepyUpdate();
291291
void validateSleepyUpdate() const;
292292

293+
static void createOptimizedTree(const ThingTemplate *thingTemplate, Coord3D *pos, Real angle);
294+
293295
private:
294296

295297
/**

GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,14 +1727,7 @@ void GameLogic::startNewGame( Bool loadingSaveGame )
17271727
if( thingTemplate == NULL )
17281728
continue;
17291729

1730-
Bool isBridgeLikeObject = false;
1731-
1732-
if (thingTemplate->isBridge())
1733-
isBridgeLikeObject = true;
1734-
if (thingTemplate->isKindOf(KINDOF_WALK_ON_TOP_OF_WALL))
1735-
isBridgeLikeObject = true;
1736-
1737-
if (!isBridgeLikeObject)
1730+
if (!thingTemplate->isBridgeLike())
17381731
continue;
17391732

17401733
Team *team = ThePlayerList->getNeutralPlayer()->getDefaultTeam();
@@ -1846,15 +1839,9 @@ void GameLogic::startNewGame( Bool loadingSaveGame )
18461839
Coord3D pos = *pMapObj->getLocation();
18471840
pos.z += TheTerrainLogic->getGroundHeight( pos.x, pos.y );
18481841
Real angle = normalizeAngle(pMapObj->getAngle());
1842+
18491843
if (thingTemplate->isKindOf(KINDOF_OPTIMIZED_TREE)) {
1850-
// Opt trees and props just get drawables to tell the client about it, then deleted. jba [6/5/2003]
1851-
// This way there is no logic object to slow down partition manager and core logic stuff.
1852-
Drawable *draw = TheThingFactory->newDrawable(thingTemplate);
1853-
if (draw) {
1854-
draw->setOrientation(angle);
1855-
draw->setPosition( &pos );
1856-
TheGameClient->destroyDrawable(draw);
1857-
}
1844+
createOptimizedTree(thingTemplate, &pos, angle);
18581845
}
18591846
}
18601847
}
@@ -1884,11 +1871,8 @@ void GameLogic::startNewGame( Bool loadingSaveGame )
18841871
if( thingTemplate == NULL )
18851872
continue;
18861873

1887-
Bool isBridgeLikeObject = false;
1888-
if (thingTemplate->isBridge()) isBridgeLikeObject = true;
1889-
if (thingTemplate->isKindOf(KINDOF_WALK_ON_TOP_OF_WALL)) isBridgeLikeObject = true;
1890-
if (isBridgeLikeObject)
1891-
continue; // bridges have to be added earlier.
1874+
if (thingTemplate->isBridgeLike())
1875+
continue; // bridges have to be added earlier.
18921876

18931877
// don't create trees and shrubs if this is one and we have that option off
18941878
if( thingTemplate->isKindOf( KINDOF_SHRUBBERY ) && !useTrees )
@@ -1897,18 +1881,12 @@ void GameLogic::startNewGame( Bool loadingSaveGame )
18971881
Coord3D pos = *pMapObj->getLocation();
18981882
pos.z += TheTerrainLogic->getGroundHeight( pos.x, pos.y );
18991883
Real angle = normalizeAngle(pMapObj->getAngle());
1900-
if (thingTemplate->isKindOf(KINDOF_OPTIMIZED_TREE)) {
1901-
// Opt trees and props just get drawables to tell the client about it, then deleted. jba [6/5/2003]
1902-
// This way there is no logic object to slow down partition manager and core logic stuff.
1903-
Drawable *draw = TheThingFactory->newDrawable(thingTemplate);
1904-
if (draw) {
1905-
draw->setOrientation(angle);
1906-
draw->setPosition( &pos );
19071884

1908-
TheGameClient->destroyDrawable(draw);
1909-
}
1885+
if (thingTemplate->isKindOf(KINDOF_OPTIMIZED_TREE)) {
1886+
createOptimizedTree(thingTemplate, &pos, angle);
19101887
continue;
19111888
}
1889+
19121890
#if 1
19131891
Bool isProp = thingTemplate->isKindOf(KINDOF_PROP);
19141892
Bool isFluff = false;
@@ -2428,6 +2406,20 @@ void GameLogic::startNewGame( Bool loadingSaveGame )
24282406

24292407
}
24302408

2409+
//-----------------------------------------------------------------------------------------
2410+
void GameLogic::createOptimizedTree(const ThingTemplate *thingTemplate, Coord3D *pos, Real angle)
2411+
{
2412+
// Opt trees and props just get drawables to tell the client about it, then deleted. jba [6/5/2003]
2413+
// This way there is no logic object to slow down partition manager and core logic stuff.
2414+
// TheSuperHackers @info Destroying the drawable will register the tree in the tree buffer.
2415+
Drawable *draw = TheThingFactory->newDrawable(thingTemplate);
2416+
if (draw) {
2417+
draw->setOrientation(angle);
2418+
draw->setPosition(pos);
2419+
TheGameClient->destroyDrawable(draw);
2420+
}
2421+
}
2422+
24312423
//-----------------------------------------------------------------------------------------
24322424
static void findAndSelectCommandCenter(Object *obj, void* alreadyFound)
24332425
{

GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/Module/W3DTreeDraw.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,17 @@ class W3DTreeDraw : public DrawModule
8484
W3DTreeDraw( Thing *thing, const ModuleData* moduleData );
8585
// virtual destructor prototype provided by memory pool declaration
8686

87-
virtual void doDrawModule(const Matrix3D* transformMtx);
88-
virtual void setShadowsEnabled(Bool enable) { }
89-
virtual void releaseShadows(void) {}; ///< we don't care about preserving temporary shadows.
90-
virtual void allocateShadows(void) {}; ///< we don't care about preserving temporary shadows.
91-
virtual void setFullyObscuredByShroud(Bool fullyObscured) { }
92-
virtual void reactToTransformChange(const Matrix3D* oldMtx, const Coord3D* oldPos, Real oldAngle);
93-
virtual void reactToGeometryChange() { }
87+
virtual void doDrawModule(const Matrix3D* transformMtx) {}
88+
virtual void setShadowsEnabled(Bool enable) {}
89+
virtual void releaseShadows(void) {} ///< we don't care about preserving temporary shadows.
90+
virtual void allocateShadows(void) {} ///< we don't care about preserving temporary shadows.
91+
virtual void setFullyObscuredByShroud(Bool fullyObscured) {}
92+
virtual void reactToTransformChange(const Matrix3D* oldMtx, const Coord3D* oldPos, Real oldAngle) {}
93+
virtual void reactToGeometryChange() {}
9494

9595
protected:
96-
Bool m_treeAdded;
96+
97+
void addToTreeBuffer();
9798

9899
};
99100

GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DTreeBuffer.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,31 @@ typedef struct {
144144
//
145145
// W3DTreeBuffer: Draw buffer for the trees.
146146
//
147+
// TheSuperHackers @info This class acts as a bootstrap for adding a new tree
148+
// instance to the tree buffer. It does not do anything useful until it is deleted.
147149
//
148150
class W3DTreeBuffer : public Snapshot
149151
{
150152
//friend class BaseHeightMapRenderObjClass;
151153

152-
//-----------------------------------------------------------------------------
153-
// W3DTreeTextureClass
154-
//-----------------------------------------------------------------------------
155-
class W3DTreeTextureClass : public TextureClass
156-
{
157-
W3DMPO_GLUE(W3DTreeTextureClass)
158-
protected:
159-
virtual void Apply(unsigned int stage);
160-
161-
public:
162-
/// Create texture.
163-
W3DTreeTextureClass(unsigned width, unsigned height);
164-
165-
// just use default destructor. ~TerrainTextureClass(void);
166-
public:
167-
int update(W3DTreeBuffer *buffer); ///< Sets the pixels, and returns the actual height of the texture.
168-
void setLOD(Int LOD) const;
169-
};
154+
//-----------------------------------------------------------------------------
155+
// W3DTreeTextureClass
156+
//-----------------------------------------------------------------------------
157+
class W3DTreeTextureClass : public TextureClass
158+
{
159+
W3DMPO_GLUE(W3DTreeTextureClass)
160+
protected:
161+
virtual void Apply(unsigned int stage);
162+
163+
public:
164+
/// Create texture.
165+
W3DTreeTextureClass(unsigned width, unsigned height);
166+
167+
// just use default destructor. ~TerrainTextureClass(void);
168+
public:
169+
int update(W3DTreeBuffer *buffer); ///< Sets the pixels, and returns the actual height of the texture.
170+
void setLOD(Int LOD) const;
171+
};
170172

171173
public:
172174

GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DTreeDraw.cpp

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
// FILE: W3DTreeDraw.cpp ////////////////////////////////////////////////////////////////////////
2626
// Author: Colin Day, December 2001
27-
// Desc: Tracer drawing
27+
// Desc: Tree draw
2828
///////////////////////////////////////////////////////////////////////////////////////////////////
2929

3030
// INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
@@ -109,51 +109,31 @@ void W3DTreeDrawModuleData::buildFieldParse(MultiIniFieldParse& p)
109109

110110
//-------------------------------------------------------------------------------------------------
111111
//-------------------------------------------------------------------------------------------------
112-
W3DTreeDraw::W3DTreeDraw( Thing *thing, const ModuleData* moduleData ) : DrawModule( thing, moduleData ),
113-
m_treeAdded(false)
112+
W3DTreeDraw::W3DTreeDraw( Thing *thing, const ModuleData* moduleData ) : DrawModule( thing, moduleData )
114113
{
115-
116114
}
117115

118-
119116
//-------------------------------------------------------------------------------------------------
120117
//-------------------------------------------------------------------------------------------------
121118
W3DTreeDraw::~W3DTreeDraw( void )
122119
{
120+
addToTreeBuffer();
123121
}
124122

125123
//-------------------------------------------------------------------------------------------------
126-
void W3DTreeDraw::reactToTransformChange( const Matrix3D *oldMtx,
127-
const Coord3D *oldPos,
128-
Real oldAngle )
124+
void W3DTreeDraw::addToTreeBuffer()
129125
{
130-
Drawable *draw = getDrawable();
131-
if (m_treeAdded) {
132-
return;
133-
}
134-
if (draw->getPosition()->x==0.0f && draw->getPosition()->y == 0.0f) {
135-
return;
136-
}
137-
m_treeAdded = true;
138126
const W3DTreeDrawModuleData *moduleData = getW3DTreeDrawModuleData();
139-
if (!moduleData) {
140-
return;
141-
}
127+
const Drawable *draw = getDrawable();
128+
129+
DEBUG_ASSERTCRASH(draw->getPosition()->x == 0.0f && draw->getPosition()->y == 0.0f,
130+
("W3DTreeDraw::addToTreeBuffer - Why place tree at x:0 y:0 ?"));
131+
142132
Real scale = draw->getScale();
143133
Real scaleRandomness = draw->getTemplate()->getInstanceScaleFuzziness();
144134
scaleRandomness = 0.0f; // We use the scale fuzziness inside WB to generate random scales, so they don't change at load time. jba. [4/22/2003]
145135
TheTerrainRenderObject->addTree(draw->getID(), *draw->getPosition(),
146136
scale, draw->getOrientation(), scaleRandomness, moduleData);
147-
148-
}
149-
150-
//-------------------------------------------------------------------------------------------------
151-
//-------------------------------------------------------------------------------------------------
152-
void W3DTreeDraw::doDrawModule(const Matrix3D* transformMtx)
153-
{
154-
155-
return;
156-
157137
}
158138

159139
// ------------------------------------------------------------------------------------------------

GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,7 @@ Int W3DTreeBuffer::getPartitionBucket(const Coord3D &pos) const
344344
}
345345

346346
//=============================================================================
347-
// W3DTreeBuffer::cull
348-
//=============================================================================
349-
/** Culls the trees, marking the visible flag. If a tree becomes visible, it sets
350-
it's sortKey */
347+
// W3DTreeBuffer::updateSway
351348
//=============================================================================
352349
void W3DTreeBuffer::updateSway(const BreezeInfo& info)
353350
{
@@ -1443,7 +1440,7 @@ void W3DTreeBuffer::addTree(DrawableID id, Coord3D location, Real scale, Real an
14431440
m_trees[m_numTrees].bounds.Center *= m_trees[m_numTrees].scale;
14441441
m_trees[m_numTrees].bounds.Radius *= m_trees[m_numTrees].scale;
14451442
m_trees[m_numTrees].bounds.Center += m_trees[m_numTrees].location;
1446-
// Initially set it invisible. cull will update it's visiblity flag.
1443+
// Initially set it invisible. cull will update it's visibility flag.
14471444
m_trees[m_numTrees].visible = false;
14481445
m_trees[m_numTrees].drawableID = id;
14491446

0 commit comments

Comments
 (0)