0

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();
        // replace the 0 and 1 of the dynamic array with i to get it to repeat:
        glTranslatef(mygame->tankpointer[i].tankx, mygame->tankpointer[i].tanky, -5.0);
        glRotatef(mygame->tankpointer[i].rotation, 0.0, 0.0, 1.0);
        // 6 instead of the previous 3 to make a square:
        glDrawArrays(GL_TRIANGLES, 0, 6);

        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.

3
  • Did you try if (mygame->tankpointer[0]) ? Commented Dec 4, 2015 at 16:16
  • Hello, welcome to StackOverflow! Where is tankpointer defined? Did you meant the if statement to read if (mygame->tankpointer[0])? Commented Dec 4, 2015 at 16:16
  • Don't you just want if (i == 0) { ... first tank ... } else { ... second tank ... }? Commented Dec 4, 2015 at 16:18

1 Answer 1

1

The compiler is complaining because you missed out mygame-> before tankpointer[0]. However, putting this in won't do what you want...

Both pointers should be valid, i.e. they point to tanks. Any non-NULL pointer will evaluate to true inside an if statement, so if (mygame->tankpointer[0]) will always be true (unless tankpointer[0] is actually NULL; then your loop wouldn't work for other reasons).

What you want is to compare the loop index:

if (i == 0) {
  // code to set colour for tank 0
} else {
 // code to set colour for tank 1
}

Better still, for a solution that scales beyond two tanks, define a mapping between tank index and colour. I'll leave it as an exercise to figure out how you might do that.

Note: In the code you provided, you're initialising an array within the scope of the if statement branches. That array won't exist outside each branch, so you'll either need to set the colour property there too, or move the declaration outside, and assign elements inside the conditional branches.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.