vendredi 4 décembre 2015

Getting a if statement to work in a for loop (using dynamic arrays and pointers)

Using C++ I want to run a piece of code that draws two tanks (represented as squares). The work asks us to do that using a for loop. I managed to get that to work but now the tanks are the same color. I want to use the if statement to check which tank it is and give it the correct color. I currently have the two tanks in a dynamic array called tankpointer. so my non working code looks like this:

void draw(game *mygame)

{

for (int i = 0; i < 2; i++)
{
    glEnableClientState(GL_VERTEX_ARRAY);

    glEnableClientState(GL_COLOR_ARRAY);

    glMatrixMode(GL_MODELVIEW);

    // draw player 1...

    glVertexPointer(3, GL_FLOAT, 0, tankVertices);

    glColorPointer(4, GL_FLOAT, 0, tank1VertexColors);
    glLoadIdentity();

    glTranslatef(mygame->tankpointer[i].tankx, mygame->tankpointer[i].tanky, -5.0); // replace the 0 and 1 of the dynamic array with i to get it to repeat

    glRotatef(mygame->tankpointer[i].rotation, 0.0, 0.0, 1.0);

    glDrawArrays(GL_TRIANGLES, 0, 6); //6 instead of the previous 3 to make a square. 
    if (tankpointer[0])
    {
        float tank1VertexColors[24] = {

            0.4f, 0.4f, 0.4f, 1.0f,

            0.4f, 0.4f, 0.4f, 1.0f,

            0.4f, 0.4f, 0.4f, 1.0f,

            0.4f, 0.4f, 0.4f, 1.0f,

            0.4f, 0.4f, 0.4f, 1.0f,

            0.4f, 0.4f, 0.4f, 1.0f

        };
    }
    else
    {
        float tank2VertexColors[24] = {

            0.8f, 0.8f, 0.8f, 1.0f,

            0.8f, 0.8f, 0.8f, 1.0f,

            0.8f, 0.8f, 0.8f, 1.0f,

            0.8f, 0.8f, 0.8f, 1.0f,

            0.8f, 0.8f, 0.8f, 1.0f,

            0.8f, 0.8f, 0.8f, 1.0f

        };
}
};

It says if (tankpointer[0]) is undefined. Any help/advice would be useful. Thanks

Aucun commentaire:

Enregistrer un commentaire