Skip to content

Commit 4fe88de

Browse files
authored
[ZH] Use hash maps for Object and Drawable lookups in GameClient, GameLogic like Generals does (#676)
1 parent e5243f1 commit 4fe88de

File tree

4 files changed

+32
-56
lines changed

4 files changed

+32
-56
lines changed

GeneralsMD/Code/GameEngine/Include/GameClient/GameClient.h

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ class SnowManager;
5959

6060
/// Function pointers for use by GameClient callback functions.
6161
typedef void (*GameClientFuncPtr)( Drawable *draw, void *userData );
62-
//typedef std::hash_map<DrawableID, Drawable *, rts::hash<DrawableID>, rts::equal_to<DrawableID> > DrawablePtrHash;
63-
//typedef DrawablePtrHash::iterator DrawablePtrHashIt;
64-
65-
typedef std::vector<Drawable*> DrawablePtrVector;
62+
typedef std::hash_map<DrawableID, Drawable *, rts::hash<DrawableID>, rts::equal_to<DrawableID> > DrawablePtrHash;
63+
typedef DrawablePtrHash::iterator DrawablePtrHashIt;
6664

6765
//-----------------------------------------------------------------------------
6866
/** The Client message dispatcher, this is the last "translator" on the message
@@ -165,8 +163,7 @@ class GameClient : public SubsystemInterface,
165163
UnsignedInt m_frame; ///< Simulation frame number from server
166164

167165
Drawable *m_drawableList; ///< All of the drawables in the world
168-
// DrawablePtrHash m_drawableHash; ///< Used for DrawableID lookups
169-
DrawablePtrVector m_drawableVector;
166+
DrawablePtrHash m_drawableHash; ///< Used for DrawableID lookups
170167

171168
DrawableID m_nextDrawableID; ///< For allocating drawable id's
172169
DrawableID allocDrawableID( void ); ///< Returns a new unique drawable id
@@ -241,18 +238,13 @@ inline Drawable* GameClient::findDrawableByID( const DrawableID id )
241238
if( id == INVALID_DRAWABLE_ID )
242239
return NULL;
243240

244-
// DrawablePtrHashIt it = m_drawableHash.find(id);
245-
// if (it == m_drawableHash.end()) {
246-
// // no such drawable
247-
// return NULL;
248-
// }
249-
//
250-
// return (*it).second;
251-
252-
if( (size_t)id < m_drawableVector.size() )
253-
return m_drawableVector[(size_t)id];
241+
DrawablePtrHashIt it = m_drawableHash.find(id);
242+
if (it == m_drawableHash.end()) {
243+
// no such drawable
244+
return NULL;
245+
}
254246

255-
return NULL;
247+
return (*it).second;
256248
}
257249

258250

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,9 @@ enum
9292

9393
/// Function pointers for use by GameLogic callback functions.
9494
typedef void (*GameLogicFuncPtr)( Object *obj, void *userData );
95-
//typedef std::hash_map<ObjectID, Object *, rts::hash<ObjectID>, rts::equal_to<ObjectID> > ObjectPtrHash;
96-
//typedef ObjectPtrHash::const_iterator ObjectPtrIter;
95+
typedef std::hash_map<ObjectID, Object *, rts::hash<ObjectID>, rts::equal_to<ObjectID> > ObjectPtrHash;
96+
typedef ObjectPtrHash::const_iterator ObjectPtrIter;
9797

98-
typedef std::vector<Object*> ObjectPtrVector;
9998

10099
// ------------------------------------------------------------------------------------------------
101100
/**
@@ -320,8 +319,7 @@ class GameLogic : public SubsystemInterface, public Snapshot
320319
WindowLayout *m_background;
321320

322321
Object* m_objList; ///< All of the objects in the world.
323-
// ObjectPtrHash m_objHash; ///< Used for ObjectID lookups
324-
ObjectPtrVector m_objVector;
322+
ObjectPtrHash m_objHash; ///< Used for ObjectID lookups
325323

326324
// this is a vector, but is maintained as a priority queue.
327325
// never modify it directly; please use the proper access methods.
@@ -412,15 +410,11 @@ inline Object* GameLogic::findObjectByID( ObjectID id )
412410
if( id == INVALID_ID )
413411
return NULL;
414412

415-
// ObjectPtrHash::iterator it = m_objHash.find(id);
416-
// if (it == m_objHash.end())
417-
// return NULL;
418-
//
419-
// return (*it).second;
420-
if( (size_t)id < m_objVector.size() )
421-
return m_objVector[(size_t)id];
413+
ObjectPtrHash::iterator it = m_objHash.find(id);
414+
if (it == m_objHash.end())
415+
return NULL;
422416

423-
return NULL;
417+
return (*it).second;
424418
}
425419

426420

GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,12 @@ void GameClient::init( void )
450450
void GameClient::reset( void )
451451
{
452452
Drawable *draw, *nextDraw;
453-
// m_drawableHash.clear();
454-
// m_drawableHash.resize(DRAWABLE_HASH_SIZE);
455-
456-
m_drawableVector.clear();
457-
m_drawableVector.resize(DRAWABLE_HASH_SIZE, NULL);
453+
m_drawableHash.clear();
454+
#if USING_STLPORT
455+
m_drawableHash.resize(DRAWABLE_HASH_SIZE);
456+
#else
457+
m_drawableHash.reserve(DRAWABLE_HASH_SIZE);
458+
#endif
458459

459460
// need to reset the in game UI to clear drawables before they are destroyed
460461
TheInGameUI->reset();
@@ -862,12 +863,7 @@ void GameClient::addDrawableToLookupTable(Drawable *draw )
862863
return;
863864

864865
// add to lookup
865-
// m_drawableHash[ draw->getID() ] = draw;
866-
DrawableID newID = draw->getID();
867-
while( newID >= m_drawableVector.size() ) // Fail case is hella rare, so faster to double up on size() call
868-
m_drawableVector.resize(m_drawableVector.size() * 2, NULL);
869-
870-
m_drawableVector[ newID ] = draw;
866+
m_drawableHash[ draw->getID() ] = draw;
871867

872868
} // end addDrawableToLookupTable
873869

@@ -882,8 +878,7 @@ void GameClient::removeDrawableFromLookupTable( Drawable *draw )
882878
return;
883879

884880
// remove from table
885-
// m_drawableHash.erase( draw->getID() );
886-
m_drawableVector[ draw->getID() ] = NULL;
881+
m_drawableHash.erase( draw->getID() );
887882

888883
} // end removeDrawableFromLookupTable
889884

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,12 @@ void GameLogic::reset( void )
441441
destroyAllObjectsImmediate();
442442

443443
// set the hash to be rather large. We need to optimize this value later.
444-
// m_objHash.clear();
445-
// m_objHash.resize(OBJ_HASH_SIZE);
446-
m_objVector.clear();
447-
m_objVector.resize(OBJ_HASH_SIZE, NULL);
448-
444+
m_objHash.clear();
445+
#if USING_STLPORT
446+
m_objHash.resize(OBJ_HASH_SIZE);
447+
#else
448+
m_objHash.reserve(OBJ_HASH_SIZE);
449+
#endif
449450
m_gamePaused = FALSE;
450451
m_inputEnabledMemory = TRUE;
451452
m_mouseVisibleMemory = TRUE;
@@ -3857,12 +3858,7 @@ void GameLogic::addObjectToLookupTable( Object *obj )
38573858
return;
38583859

38593860
// add to lookup
3860-
// m_objHash[ obj->getID() ] = obj;
3861-
ObjectID newID = obj->getID();
3862-
while( newID >= m_objVector.size() ) // Fail case is hella rare, so faster to double up on size() call
3863-
m_objVector.resize(m_objVector.size() * 2, NULL);
3864-
3865-
m_objVector[ newID ] = obj;
3861+
m_objHash[ obj->getID() ] = obj;
38663862

38673863
} // end addObjectToLookupTable
38683864

@@ -3877,8 +3873,7 @@ void GameLogic::removeObjectFromLookupTable( Object *obj )
38773873
return;
38783874

38793875
// remove from lookup table
3880-
// m_objHash.erase( obj->getID() );
3881-
m_objVector[ obj->getID() ] = NULL;
3876+
m_objHash.erase( obj->getID() );
38823877

38833878
} // end removeObjectFromLookupTable
38843879

0 commit comments

Comments
 (0)