Skip to content

Commit

Permalink
UPBGE: Use CListValue::iterator when possible.
Browse files Browse the repository at this point in the history
Using iterator is much faster then index acces excepted for a CListValue.
Also it simplify the write of the casting, but should not be used in every
cases.
  • Loading branch information
panzergame committed Nov 19, 2016
1 parent 668b9fb commit d78f606
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 92 deletions.
98 changes: 39 additions & 59 deletions source/gameengine/Converter/BL_BlenderDataConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1190,10 +1190,8 @@ static void blenderSceneSetBackground(Scene *blenderscene)

static KX_GameObject* getGameOb(STR_String busc,CListValue* sumolist)
{

for (int j=0;j<sumolist->GetCount();j++)
{
KX_GameObject* gameobje = (KX_GameObject*) sumolist->GetValue(j);
for (CListValue::iterator<KX_GameObject> it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) {
KX_GameObject *gameobje = *it;
if (gameobje->GetName()==busc)
return gameobje;
}
Expand Down Expand Up @@ -1561,7 +1559,6 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
}

// create hierarchy information
int i;
std::vector<parentChildLink>::iterator pcit;

for (pcit = vec_parent_child.begin();!(pcit==vec_parent_child.end());++pcit)
Expand All @@ -1588,9 +1585,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
CListValue* childrenlist = childobj->GetChildrenRecursive();
// The returned list by GetChildrenRecursive is not owned by anyone and must not own items, so no AddRef().
childrenlist->Add(childobj);
for ( i=0;i<childrenlist->GetCount();i++)
{
KX_GameObject* obj = static_cast<KX_GameObject*>(childrenlist->GetValue(i));
for (CListValue::baseIterator it = childrenlist->GetBegin(), end = childrenlist->GetEnd(); it != end; ++it) {
CValue *obj = *it;
if (sumolist->RemoveValue(obj))
obj->Release();
if (logicbrick_conversionlist->RemoveValue(obj))
Expand Down Expand Up @@ -1651,9 +1647,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
vec_parent_child.clear();

// find 'root' parents (object that has not parents in SceneGraph)
for (i=0;i<sumolist->GetCount();++i)
{
KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
for (CListValue::iterator<KX_GameObject> it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) {
KX_GameObject* gameobj = *it;
if (gameobj->GetSGNode()->GetSGParent() == 0)
{
parentlist->Add(gameobj->AddRef());
Expand All @@ -1665,9 +1660,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
if (kxscene->GetDbvtCulling())
{
bool occlusion = false;
for (i=0; i<sumolist->GetCount();i++)
{
KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
for (CListValue::iterator<KX_GameObject> it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) {
KX_GameObject* gameobj = *it;
if (gameobj->GetMeshCount() > 0)
{
bool isactive = objectlist->SearchValue(gameobj);
Expand All @@ -1684,26 +1678,23 @@ void BL_ConvertBlenderObjects(struct Main* maggie,

// now that the scenegraph is complete, let's instantiate the deformers.
// We need that to create reusable derived mesh and physic shapes
for (i=0;i<sumolist->GetCount();++i)
{
KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
for (CListValue::iterator<KX_GameObject> it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) {
KX_GameObject* gameobj = *it;
if (gameobj->GetDeformer())
gameobj->GetDeformer()->UpdateBuckets();
}

// Set up armature constraints and shapekey drivers
for (i=0;i<sumolist->GetCount();++i)
{
KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
for (CListValue::iterator<KX_GameObject> it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) {
KX_GameObject* gameobj = *it;
if (gameobj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
{
BL_ArmatureObject *armobj = (BL_ArmatureObject*)gameobj;
armobj->LoadConstraints(converter);

CListValue *children = armobj->GetChildren();
for (int j=0; j<children->GetCount();++j)
{
BL_ShapeDeformer *deform = dynamic_cast<BL_ShapeDeformer*>(((KX_GameObject*)children->GetValue(j))->GetDeformer());
for (CListValue::iterator<KX_GameObject> it = children->GetBegin(), end = children->GetEnd(); it != end; ++it) {
BL_ShapeDeformer *deform = dynamic_cast<BL_ShapeDeformer*>((*it)->GetDeformer());
if (deform)
deform->LoadShapeDrivers(armobj);
}
Expand All @@ -1714,9 +1705,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie,

bool processCompoundChildren = false;
// create physics information
for (i=0;i<sumolist->GetCount();i++)
{
KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
for (CListValue::iterator<KX_GameObject> it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) {
KX_GameObject* gameobj = *it;
struct Object* blenderobject = gameobj->GetBlenderObject();
int nummeshes = gameobj->GetMeshCount();
RAS_MeshObject* meshobj = 0;
Expand All @@ -1730,9 +1720,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie,

processCompoundChildren = true;
// create physics information
for (i=0;i<sumolist->GetCount();i++)
{
KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
for (CListValue::iterator<KX_GameObject> it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) {
KX_GameObject* gameobj = *it;
struct Object* blenderobject = gameobj->GetBlenderObject();
int nummeshes = gameobj->GetMeshCount();
RAS_MeshObject* meshobj = 0;
Expand Down Expand Up @@ -1777,8 +1766,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
}

// Create and set bounding volume.
for (i = 0; i < sumolist->GetCount(); ++i) {
KX_GameObject *gameobj = (KX_GameObject *)sumolist->GetValue(i);
for (CListValue::iterator<KX_GameObject> it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) {
KX_GameObject *gameobj = *it;
Object *blenderobject = gameobj->GetBlenderObject();
Mesh *predifinedBoundMesh = blenderobject->gamePredefinedBound;

Expand Down Expand Up @@ -1809,10 +1798,9 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
}

// create physics joints
for (i=0;i<sumolist->GetCount();i++)
{
for (CListValue::iterator<KX_GameObject> it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) {
PHY_IPhysicsEnvironment *physEnv = kxscene->GetPhysicsEnvironment();
KX_GameObject *gameobj = (KX_GameObject *)sumolist->GetValue(i);
KX_GameObject *gameobj = *it;
struct Object *blenderobject = gameobj->GetBlenderObject();
ListBase *conlist = get_active_constraints2(blenderobject);
bConstraint *curcon;
Expand Down Expand Up @@ -1871,9 +1859,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
KX_ObstacleSimulation* obssimulation = kxscene->GetObstacleSimulation();
if (obssimulation)
{
for ( i=0;i<objectlist->GetCount();i++)
{
KX_GameObject* gameobj = static_cast<KX_GameObject*>(objectlist->GetValue(i));
for (CListValue::iterator<KX_GameObject> it = objectlist->GetBegin(), end = objectlist->GetEnd(); it != end; ++it) {
KX_GameObject* gameobj = *it;
struct Object* blenderobject = gameobj->GetBlenderObject();
if (blenderobject->gameflag & OB_HASOBSTACLE)
{
Expand All @@ -1890,9 +1877,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
PHY_SetActiveEnvironment(currentEnv);

//process navigation mesh objects
for ( i=0; i<objectlist->GetCount();i++)
{
KX_GameObject* gameobj = static_cast<KX_GameObject*>(objectlist->GetValue(i));
for (CListValue::iterator<KX_GameObject> it = objectlist->GetBegin(), end = objectlist->GetEnd(); it != end; ++it) {
KX_GameObject *gameobj = *it;
struct Object* blenderobject = gameobj->GetBlenderObject();
if (blenderobject->type==OB_MESH && (blenderobject->gameflag & OB_NAVMESH))
{
Expand All @@ -1903,9 +1889,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
obssimulation->AddObstaclesForNavMesh(navmesh);
}
}
for ( i=0; i<inactivelist->GetCount();i++)
{
KX_GameObject* gameobj = static_cast<KX_GameObject*>(inactivelist->GetValue(i));
for (CListValue::iterator<KX_GameObject> it = inactivelist->GetBegin(), end = inactivelist->GetEnd(); it != end; ++it) {
KX_GameObject *gameobj = *it;
struct Object* blenderobject = gameobj->GetBlenderObject();
if (blenderobject->type==OB_MESH && (blenderobject->gameflag & OB_NAVMESH))
{
Expand All @@ -1915,25 +1900,22 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
}

// convert logic bricks, sensors, controllers and actuators
for (i=0;i<logicbrick_conversionlist->GetCount();i++)
{
KX_GameObject* gameobj = static_cast<KX_GameObject*>(logicbrick_conversionlist->GetValue(i));
for (CListValue::iterator<KX_GameObject> it = logicbrick_conversionlist->GetBegin(), end = logicbrick_conversionlist->GetEnd(); it != end; ++it) {
KX_GameObject *gameobj = *it;
struct Object* blenderobj = gameobj->GetBlenderObject();
int layerMask = (groupobj.find(blenderobj) == groupobj.end()) ? activeLayerBitInfo : 0;
bool isInActiveLayer = (blenderobj->lay & layerMask)!=0;
BL_ConvertActuators(maggie->name, blenderobj,gameobj,logicmgr,kxscene,ketsjiEngine,layerMask,isInActiveLayer,converter);
}
for ( i=0;i<logicbrick_conversionlist->GetCount();i++)
{
KX_GameObject* gameobj = static_cast<KX_GameObject*>(logicbrick_conversionlist->GetValue(i));
for (CListValue::iterator<KX_GameObject> it = logicbrick_conversionlist->GetBegin(), end = logicbrick_conversionlist->GetEnd(); it != end; ++it) {
KX_GameObject *gameobj = *it;
struct Object* blenderobj = gameobj->GetBlenderObject();
int layerMask = (groupobj.find(blenderobj) == groupobj.end()) ? activeLayerBitInfo : 0;
bool isInActiveLayer = (blenderobj->lay & layerMask)!=0;
BL_ConvertControllers(blenderobj,gameobj,logicmgr, layerMask,isInActiveLayer,converter, libloading);
}
for ( i=0;i<logicbrick_conversionlist->GetCount();i++)
{
KX_GameObject* gameobj = static_cast<KX_GameObject*>(logicbrick_conversionlist->GetValue(i));
for (CListValue::iterator<KX_GameObject> it = logicbrick_conversionlist->GetBegin(), end = logicbrick_conversionlist->GetEnd(); it != end; ++it) {
KX_GameObject *gameobj = *it;
struct Object* blenderobj = gameobj->GetBlenderObject();
int layerMask = (groupobj.find(blenderobj) == groupobj.end()) ? activeLayerBitInfo : 0;
bool isInActiveLayer = (blenderobj->lay & layerMask)!=0;
Expand All @@ -1942,9 +1924,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
gameobj->SetInitState((blenderobj->init_state)?blenderobj->init_state:blenderobj->state);
}
// apply the initial state to controllers, only on the active objects as this registers the sensors
for ( i=0;i<objectlist->GetCount();i++)
{
KX_GameObject* gameobj = static_cast<KX_GameObject*>(objectlist->GetValue(i));
for (CListValue::iterator<KX_GameObject> it = objectlist->GetBegin(), end = objectlist->GetEnd(); it != end; ++it) {
KX_GameObject *gameobj = *it;
gameobj->ResetState();
}

Expand All @@ -1959,8 +1940,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
// has the effect of adding objects at the end of objectlist.
// Only loop through the first part of the list.
int objcount = objectlist->GetCount();
for (i=0;i<objcount;i++)
{
for (unsigned int i = 0; i < objcount; ++i) {
KX_GameObject* gameobj = (KX_GameObject*) objectlist->GetValue(i);
if (gameobj->IsDupliGroup())
{
Expand All @@ -1969,8 +1949,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
}

/* Initialize python components, use a fixed size because some component can add object
* and these objects are only at the end of the list. Never use iterato here because the
* begining iterator can be changed and then pointed to a fake game object.
* and these objects are only at the end of the list. Never use iterator here because the
* beginning iterator can be changed and then pointed to a fake game object.
*/
for (unsigned int i = 0, size = objectlist->GetCount(); i < size; ++i) {
KX_GameObject *gameobj = (KX_GameObject *)objectlist->GetValue(i);
Expand Down
7 changes: 2 additions & 5 deletions source/gameengine/Ketsji/KX_KetsjiEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,14 +820,11 @@ void KX_KetsjiEngine::UpdateAnimations(KX_Scene *scene)
void KX_KetsjiEngine::RenderShadowBuffers(KX_Scene *scene)
{
CListValue *lightlist = scene->GetLightList();
int i;

m_rasterizer->SetAuxilaryClientInfo(scene);

for (i = 0; i < lightlist->GetCount(); i++) {
KX_GameObject *gameobj = (KX_GameObject *)lightlist->GetValue(i);

KX_LightObject *light = (KX_LightObject *)gameobj;
for (CListValue::iterator<KX_LightObject> it = lightlist->GetBegin(), end = lightlist->GetEnd(); it != end; ++it) {
KX_LightObject *light = *it;
RAS_ILightObject *raslight = light->GetLightData();

raslight->Update();
Expand Down
50 changes: 22 additions & 28 deletions source/gameengine/Ketsji/KX_Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1515,11 +1515,8 @@ void KX_Scene::DrawDebug(RAS_IRasterizer *rasty)
void KX_Scene::LogicBeginFrame(double curtime, double framestep)
{
// have a look at temp objects ...
int lastobj = m_tempObjectList->GetCount() - 1;

for (int i = lastobj; i >= 0; i--)
{
CValue* objval = m_tempObjectList->GetValue(i);
for (CListValue::baseIterator it = m_tempObjectList->GetBegin(), end = m_tempObjectList->GetEnd(); it != end; ++it) {
CValue *objval = *it;
CFloatValue* propval = (CFloatValue*) objval->GetProperty("::timebomb");

if (propval)
Expand Down Expand Up @@ -1570,8 +1567,8 @@ static void update_anim_thread_func(TaskPool *pool, void *taskdata, int UNUSED(t
bool has_mesh = false, has_non_mesh = false;

// Check for meshes that haven't been culled
for (int j=0; j<children->GetCount(); ++j) {
child = (KX_GameObject*)children->GetValue(j);
for (CListValue::iterator<KX_GameObject> it = children->GetBegin(), end = children->GetEnd(); it != end; ++it) {
child = *it;

if (!child->GetCulled()) {
needs_update = true;
Expand Down Expand Up @@ -1605,8 +1602,8 @@ static void update_anim_thread_func(TaskPool *pool, void *taskdata, int UNUSED(t
if (gameobj->GetDeformer() && (!parent || parent->GetGameObjectType() != SCA_IObject::OBJ_ARMATURE))
gameobj->GetDeformer()->Update();

for (int j=0; j<children->GetCount(); ++j) {
child = (KX_GameObject*)children->GetValue(j);
for (CListValue::iterator<KX_GameObject> it = children->GetBegin(), end = children->GetEnd(); it != end; ++it) {
child = *it;

if (child->GetDeformer()) {
child->GetDeformer()->Update();
Expand All @@ -1621,21 +1618,23 @@ void KX_Scene::UpdateAnimations(double curtime)
{
TaskPool *pool = BLI_task_pool_create(KX_GetActiveEngine()->GetTaskScheduler(), &curtime);

for (int i=0; i<m_animatedlist->GetCount(); ++i) {
BLI_task_pool_push(pool, update_anim_thread_func, m_animatedlist->GetValue(i), false, TASK_PRIORITY_LOW);
for (CListValue::iterator<KX_GameObject> it = m_animatedlist->GetBegin(), end = m_animatedlist->GetEnd(); it != end; ++it) {
BLI_task_pool_push(pool, update_anim_thread_func, *it, false, TASK_PRIORITY_LOW);
}

BLI_task_pool_work_and_wait(pool);
BLI_task_pool_free(pool);

for (unsigned int i = 0; i < m_animatedlist->GetCount(); ++i) {
((KX_GameObject *)m_animatedlist->GetValue(i))->UpdateActionIPOs();
for (CListValue::iterator<KX_GameObject> it = m_animatedlist->GetBegin(), end = m_animatedlist->GetEnd(); it != end; ++it) {
(*it)->UpdateActionIPOs();
}
}

void KX_Scene::LogicUpdateFrame(double curtime, bool frame)
{
// Update object components
/* Update object components, don't use iterator in loop because component can add objects and then make
* iterators invalid.
*/
for (int i = 0; i < m_objectlist->GetCount(); ++i) {
((KX_GameObject*)m_objectlist->GetValue(i))->UpdateComponents();
}
Expand Down Expand Up @@ -1759,13 +1758,10 @@ void KX_Scene::UpdateObjectActivity(void)
{
if (m_activity_culling) {
/* determine the activity criterium and set objects accordingly */
int i=0;

MT_Vector3 camloc = GetActiveCamera()->NodeGetWorldPosition(); //GetCameraLocation();

for (i=0;i<GetObjectList()->GetCount();i++)
{
KX_GameObject* ob = (KX_GameObject*) GetObjectList()->GetValue(i);

for (CListValue::iterator<KX_GameObject> it = m_objectlist->GetBegin(), end = m_objectlist->GetEnd(); it != end; ++it) {
KX_GameObject* ob = *it;

if (!ob->GetIgnoreActivityCulling()) {
/* Simple test: more than 10 away from the camera, count
Expand Down Expand Up @@ -1988,9 +1984,8 @@ bool KX_Scene::MergeScene(KX_Scene *other)


/* active + inactive == all ??? - lets hope so */
for (int i = 0; i < other->GetObjectList()->GetCount(); i++)
{
KX_GameObject* gameobj = (KX_GameObject*)other->GetObjectList()->GetValue(i);
for (CListValue::iterator<KX_GameObject> it = m_objectlist->GetBegin(), end = m_objectlist->GetEnd(); it != end; ++it) {
KX_GameObject* gameobj = *it;
MergeScene_GameObject(gameobj, this, other);

/* add properties to debug list for LibLoad objects */
Expand All @@ -1999,9 +1994,8 @@ bool KX_Scene::MergeScene(KX_Scene *other)
}
}

for (int i = 0; i < other->GetInactiveList()->GetCount(); i++)
{
KX_GameObject* gameobj = (KX_GameObject*)other->GetInactiveList()->GetValue(i);
for (CListValue::iterator<KX_GameObject> it = m_inactivelist->GetBegin(), end = m_inactivelist->GetEnd(); it != end; ++it) {
KX_GameObject* gameobj = *it;
MergeScene_GameObject(gameobj, this, other);
}

Expand All @@ -2011,8 +2005,8 @@ bool KX_Scene::MergeScene(KX_Scene *other)

// List of all physics objects to merge (needed by ReplicateConstraints).
std::vector<KX_GameObject *> physicsObjects;
for (unsigned int i = 0; i < otherObjects->GetCount(); ++i) {
KX_GameObject *gameobj = (KX_GameObject *)otherObjects->GetValue(i);
for (CListValue::iterator<KX_GameObject> it = otherObjects->GetBegin(), end = otherObjects->GetEnd(); it != end; ++it) {
KX_GameObject* gameobj = *it;
if (gameobj->GetPhysicsController()) {
physicsObjects.push_back(gameobj);
}
Expand Down

0 comments on commit d78f606

Please sign in to comment.