From d78f606c9d479526975be9959dc90380fbccdc3c Mon Sep 17 00:00:00 2001 From: Porteries Tristan Date: Sat, 19 Nov 2016 10:51:30 +0000 Subject: [PATCH] UPBGE: Use CListValue::iterator when possible. 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. --- .../Converter/BL_BlenderDataConversion.cpp | 98 ++++++++----------- source/gameengine/Ketsji/KX_KetsjiEngine.cpp | 7 +- source/gameengine/Ketsji/KX_Scene.cpp | 50 +++++----- 3 files changed, 63 insertions(+), 92 deletions(-) diff --git a/source/gameengine/Converter/BL_BlenderDataConversion.cpp b/source/gameengine/Converter/BL_BlenderDataConversion.cpp index 0dfbe13fd1b3..8bc9571df8f5 100644 --- a/source/gameengine/Converter/BL_BlenderDataConversion.cpp +++ b/source/gameengine/Converter/BL_BlenderDataConversion.cpp @@ -1190,10 +1190,8 @@ static void blenderSceneSetBackground(Scene *blenderscene) static KX_GameObject* getGameOb(STR_String busc,CListValue* sumolist) { - - for (int j=0;jGetCount();j++) - { - KX_GameObject* gameobje = (KX_GameObject*) sumolist->GetValue(j); + for (CListValue::iterator it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) { + KX_GameObject *gameobje = *it; if (gameobje->GetName()==busc) return gameobje; } @@ -1561,7 +1559,6 @@ void BL_ConvertBlenderObjects(struct Main* maggie, } // create hierarchy information - int i; std::vector::iterator pcit; for (pcit = vec_parent_child.begin();!(pcit==vec_parent_child.end());++pcit) @@ -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;iGetCount();i++) - { - KX_GameObject* obj = static_cast(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)) @@ -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;iGetCount();++i) - { - KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i); + for (CListValue::iterator it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) { + KX_GameObject* gameobj = *it; if (gameobj->GetSGNode()->GetSGParent() == 0) { parentlist->Add(gameobj->AddRef()); @@ -1665,9 +1660,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie, if (kxscene->GetDbvtCulling()) { bool occlusion = false; - for (i=0; iGetCount();i++) - { - KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i); + for (CListValue::iterator it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) { + KX_GameObject* gameobj = *it; if (gameobj->GetMeshCount() > 0) { bool isactive = objectlist->SearchValue(gameobj); @@ -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;iGetCount();++i) - { - KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i); + for (CListValue::iterator 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;iGetCount();++i) - { - KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i); + for (CListValue::iterator 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; jGetCount();++j) - { - BL_ShapeDeformer *deform = dynamic_cast(((KX_GameObject*)children->GetValue(j))->GetDeformer()); + for (CListValue::iterator it = children->GetBegin(), end = children->GetEnd(); it != end; ++it) { + BL_ShapeDeformer *deform = dynamic_cast((*it)->GetDeformer()); if (deform) deform->LoadShapeDrivers(armobj); } @@ -1714,9 +1705,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie, bool processCompoundChildren = false; // create physics information - for (i=0;iGetCount();i++) - { - KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i); + for (CListValue::iterator 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; @@ -1730,9 +1720,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie, processCompoundChildren = true; // create physics information - for (i=0;iGetCount();i++) - { - KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i); + for (CListValue::iterator 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; @@ -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 it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) { + KX_GameObject *gameobj = *it; Object *blenderobject = gameobj->GetBlenderObject(); Mesh *predifinedBoundMesh = blenderobject->gamePredefinedBound; @@ -1809,10 +1798,9 @@ void BL_ConvertBlenderObjects(struct Main* maggie, } // create physics joints - for (i=0;iGetCount();i++) - { + for (CListValue::iterator 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; @@ -1871,9 +1859,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie, KX_ObstacleSimulation* obssimulation = kxscene->GetObstacleSimulation(); if (obssimulation) { - for ( i=0;iGetCount();i++) - { - KX_GameObject* gameobj = static_cast(objectlist->GetValue(i)); + for (CListValue::iterator it = objectlist->GetBegin(), end = objectlist->GetEnd(); it != end; ++it) { + KX_GameObject* gameobj = *it; struct Object* blenderobject = gameobj->GetBlenderObject(); if (blenderobject->gameflag & OB_HASOBSTACLE) { @@ -1890,9 +1877,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie, PHY_SetActiveEnvironment(currentEnv); //process navigation mesh objects - for ( i=0; iGetCount();i++) - { - KX_GameObject* gameobj = static_cast(objectlist->GetValue(i)); + for (CListValue::iterator 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)) { @@ -1903,9 +1889,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie, obssimulation->AddObstaclesForNavMesh(navmesh); } } - for ( i=0; iGetCount();i++) - { - KX_GameObject* gameobj = static_cast(inactivelist->GetValue(i)); + for (CListValue::iterator 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)) { @@ -1915,25 +1900,22 @@ void BL_ConvertBlenderObjects(struct Main* maggie, } // convert logic bricks, sensors, controllers and actuators - for (i=0;iGetCount();i++) - { - KX_GameObject* gameobj = static_cast(logicbrick_conversionlist->GetValue(i)); + for (CListValue::iterator 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;iGetCount();i++) - { - KX_GameObject* gameobj = static_cast(logicbrick_conversionlist->GetValue(i)); + for (CListValue::iterator 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;iGetCount();i++) - { - KX_GameObject* gameobj = static_cast(logicbrick_conversionlist->GetValue(i)); + for (CListValue::iterator 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; @@ -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;iGetCount();i++) - { - KX_GameObject* gameobj = static_cast(objectlist->GetValue(i)); + for (CListValue::iterator it = objectlist->GetBegin(), end = objectlist->GetEnd(); it != end; ++it) { + KX_GameObject *gameobj = *it; gameobj->ResetState(); } @@ -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;iGetValue(i); if (gameobj->IsDupliGroup()) { @@ -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); diff --git a/source/gameengine/Ketsji/KX_KetsjiEngine.cpp b/source/gameengine/Ketsji/KX_KetsjiEngine.cpp index c987570b4435..2451327918fb 100644 --- a/source/gameengine/Ketsji/KX_KetsjiEngine.cpp +++ b/source/gameengine/Ketsji/KX_KetsjiEngine.cpp @@ -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 it = lightlist->GetBegin(), end = lightlist->GetEnd(); it != end; ++it) { + KX_LightObject *light = *it; RAS_ILightObject *raslight = light->GetLightData(); raslight->Update(); diff --git a/source/gameengine/Ketsji/KX_Scene.cpp b/source/gameengine/Ketsji/KX_Scene.cpp index b708e3471932..f6f8920b3b16 100644 --- a/source/gameengine/Ketsji/KX_Scene.cpp +++ b/source/gameengine/Ketsji/KX_Scene.cpp @@ -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) @@ -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; jGetCount(); ++j) { - child = (KX_GameObject*)children->GetValue(j); + for (CListValue::iterator it = children->GetBegin(), end = children->GetEnd(); it != end; ++it) { + child = *it; if (!child->GetCulled()) { needs_update = true; @@ -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; jGetCount(); ++j) { - child = (KX_GameObject*)children->GetValue(j); + for (CListValue::iterator it = children->GetBegin(), end = children->GetEnd(); it != end; ++it) { + child = *it; if (child->GetDeformer()) { child->GetDeformer()->Update(); @@ -1621,21 +1618,23 @@ void KX_Scene::UpdateAnimations(double curtime) { TaskPool *pool = BLI_task_pool_create(KX_GetActiveEngine()->GetTaskScheduler(), &curtime); - for (int i=0; iGetCount(); ++i) { - BLI_task_pool_push(pool, update_anim_thread_func, m_animatedlist->GetValue(i), false, TASK_PRIORITY_LOW); + for (CListValue::iterator 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 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(); } @@ -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;iGetCount();i++) - { - KX_GameObject* ob = (KX_GameObject*) GetObjectList()->GetValue(i); + + for (CListValue::iterator 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 @@ -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 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 */ @@ -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 it = m_inactivelist->GetBegin(), end = m_inactivelist->GetEnd(); it != end; ++it) { + KX_GameObject* gameobj = *it; MergeScene_GameObject(gameobj, this, other); } @@ -2011,8 +2005,8 @@ bool KX_Scene::MergeScene(KX_Scene *other) // List of all physics objects to merge (needed by ReplicateConstraints). std::vector physicsObjects; - for (unsigned int i = 0; i < otherObjects->GetCount(); ++i) { - KX_GameObject *gameobj = (KX_GameObject *)otherObjects->GetValue(i); + for (CListValue::iterator it = otherObjects->GetBegin(), end = otherObjects->GetEnd(); it != end; ++it) { + KX_GameObject* gameobj = *it; if (gameobj->GetPhysicsController()) { physicsObjects.push_back(gameobj); }