I have an Application object that has a vector of GameChar objects as one of its member variables. This GameChar objects contain a GLbatch that is initialized in the constructor and drawn when you call the draw method of the GameChar object. When the program starts I create an application object and three GameChar objects, and I push the GameChar’s into the Application object’s vector with the push_back() method. Then, inside a game loop, I iterate through the vector and call the draw method of all I’ts objects. The problem is that only the object that was last put in the vector gets drawn. I have assured that the vector is iterated correctly, and that the draw method is called on all the objects, but no matter in what order I call this draw methods (I have tried to iterate the vector from end to start to assure this), only the object that was last inserted to the vector gets drawn.
Here is the code that creates the application and GameChar objects (the constructor gets the arguments in this order: width,height,x,y):
Application app;
GameChar character(0.5f,0.5f,0.0f,0.0f);
GameChar character2(0.5f,0.5f,0.5f,0.5f);
GameChar character3(0.5f,0.5f,-1.0f,-1.0f);
app.characters.push_back(character);
app.characters.push_back(character2);
app.characters.push_back(character3);
And this is the loop:
while (1)
{
vector<GameChar>::size_type sz=characters.size();
unsigned int i;
for (i=0;i<sz;i++)
{
characters[i].update();
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
for (i=0;i<sz;i++)
{
characters[i].draw();
}
glfwSwapBuffers();
}
UPDATE:
Here is the constructor of GameChar:
GameChar::GameChar(GLfloat lWidth,GLfloat lHeight,GLfloat lX,GLfloat lY)
{
xPos=lX;
yPos=lY;
height=lHeight;
width=lWidth;
if (xPos<-1)
{
xPos=-1;
}
if (xPos+width>1)
xPos=1-width;
if (yPos<-1)
yPos=-1;
if (yPos+height>1)
yPos=1-height;
verts[0]=xPos;
verts[1]=yPos;
verts[2]=0.0f;
verts[3]=xPos+width;
verts[4]=yPos;
verts[5]=0.0f;
verts[6]=xPos;
verts[7]=yPos+height;
verts[8]=0.0f;
verts[9]=xPos+width;
verts[10]=yPos+height;
verts[11]=0.0f;
batch.Begin(GL_TRIANGLE_STRIP, 4);
batch.CopyVertexData3f(verts);
batch.End();
}
The draw method:
void GameChar::draw()
{
batch.Draw();
}
And I’ve updated the game loop with the complete code.
Also, If i try to change the code:
for (i=0;i<sz;i++)
{
characters[i].draw();
}
with this code:
for (i=0;i<sz;i++)
{
GLBatch batch;
batch=characters[i].batch;
batch.Draw();
}
No square gets drawn. I really find all this a nonsense
ANOTHER UPDATE:
If I pop_back the third object after pushing it in, like this:
Application app;
GameChar character(0.5f,0.5f,0.0f,0.0f);
GameChar character2(0.5f,0.5f,0.5f,0.5f);
GameChar character3(0.5f,0.5f,-1.0f,-1.0f);
app.characters.push_back(character);
app.characters.push_back(character2);
app.characters.push_back(character3);
app.characters.pop_back();
nothing is drawn, but if don’t push it in, like this:
Application app;
GameChar character(0.5f,0.5f,0.0f,0.0f);
GameChar character2(0.5f,0.5f,0.5f,0.5f);
GameChar character3(0.5f,0.5f,-1.0f,-1.0f);
app.characters.push_back(character);
app.characters.push_back(character2);
The second character is drawn. So I think the problem is that the characters that are already in the vector are invalidated when a new one is pushed, but I don’t know why this happens, or how to fix it
I finally managed to fix it! I still don’t know what’s the cause of the problem, but I’ve changed the vector of GameChar to a vector of pointers to GameChar objects, and now it works!