I hope I can describe my problem generally because I haven't been able to shorten my code enough just yet to fit inside a forum post.
I have a ranged loop that draws an OpenGL scene via iterating a vector of objects and calls the respective draw functions.
Also inside this loop are "static const auto" lamdas that check whether objects need to be deleted in cases which the aforementioned objects go too far out of the scene.
Do you mean just turning the lambda into a named function reference? Or removing the erase/removes as well? Because removing the erase/removes will save lots of time on your loops; it probably would speed up once it stops having to remove things.
Well, I know this may be hard to do without seeing my code but what I am looking for is the best way to be able to remove objects that fall out of scope of the scene without slowing down the drawing of the scene itself too much (I understand there will be some penalty for this, just want it as minimal as possible).
Currently I use a ranged loop for looping through all of the objects for the OpenGL draws.
I was actually surprised that the erase functions slowed down the program so much, I used to have a traditional for loop along these lines and I experienced much less slowdown:
for (std::vector<PhysicsObject*>::iterator it = CurrentTransparentObjects.begin(); it != CurrentTransparentObjects.end(); /* This section of loop intentionally left out. */)
{
(*it)->CalculatePhysicsOrientation();
(*it)->DrawMe();
glm::vec3* CurrentBoxPosition = (*it)->GetCurrentPosition();
if (CurrentBoxPosition->y < LowestYValueAllowed) {
delete (*it);
it = CurrentTransparentObjects.erase(it);
}
else ++it;
}
for (auto & e : CurrentOpaqueObjects) {
e->CalculatePhysicsOrientation();
e->DrawMe();
}
for (auto & e : CurrentTransparentObjects) {
e->CalculatePhysicsOrientation();
e->DrawMe();
}
With just this code, everything runs great.
But now I need to delete objects out of the CurrentOpaqueObjects and CurrentTransparentObjects vectors when their positions go outside of the scene.
Do not use default capture modes. YOu are saving references to all variables context into lambda function. And then you are passing it by copy. Depending on amount of visible locals, you can easily have > 100 bytes copying around.
You need to capture only one variable: