0

I'm wondering how is the proper way to create objects of classes in the game loop once? For example I've Box, Sphere, Cyllinder classes and I want to create several objects at different time while the program is running and work with them in the future. How is the proper way to preserve this objects? Combine all of classes as vector in one class?

vector<glm::vec3> initVerts = {/*verts position*/};

class Box
{
    vector<glm::vec3> verts;
    Box(): verts(initVerts)       
    void moveBox(glm::vec3 newPos){ /*translate verts*/ }
};

while ( !windowShouldClose())
{
     Box box; 
     box.moveBox(1.0,0.0,0.0); // on the second pass it was another box with initial position
}
javierMarquez
  • 142
  • 2
  • 10
  • Are you asking how to create instances of a class (objects) or how to store them once they're created? Providing some code (or even pseudo code) as to what you're attempting to do would be helpful, I think. – xaxxon Aug 19 '17 at 00:48
  • xaxxon I asking about how to store them once they're created. I'll try to provide code. – javierMarquez Aug 19 '17 at 00:50
  • 1
    Store the objects in a vector. As soon as the vector gets out of scope, the objects will be deleted. If you store pointers instead, then you must delete them on your own. – Ripi2 Aug 19 '17 at 01:07
  • Ripi2 Do I need to create a general class for them in this case? – javierMarquez Aug 19 '17 at 01:11

1 Answers1

1

The simplest way will be to create one vector per class type. To start:

std::vector<Box> boxes;
boxes.reserve(100); // however many you expect to need
Box& box1 = boxes.emplace_back();

while ( !windowShouldClose())
{
    box1.moveBox(1.0,0.0,0.0);
}

Or if you don't need a way to iterate over all your objects, you can just store them individually outside the loop:

Box box1;

while ( !windowShouldClose())
{
    box1.moveBox(1.0,0.0,0.0);
}
John Zwinck
  • 239,568
  • 38
  • 324
  • 436