c++for-loopdirectxdirectx-10

How to add a for loop in a DirectX 10 project?


Before I ask for help, I would like to mention that I am very new to DirectX and yes, I do know how to code in C++. But I'm getting errors when I try to complete my homework.

The homework assignment is simple. Draw a circle (using a minimum of 20 triangles) and put a texture on the circle. No problem. But I have to create the vertices using a For loop. This is what I have so far:

double x = 0.1;
double y = 1.0;
double z = 0.5;
double xin = 0.3;
double yin = -0.1;

// Create vertex buffer
SimpleVertex vertices[] =
{

    for (int i = 0; i < 4; i++) {
        XMFLOAT3(0.0f, 0.0f, zf),
        XMFLOAT3(xf, yf, zf),
        XMFLOAT3((x+xin)f, (y+yin)f, zf),
    }


};

I haven't finished adding all the code I want to make the circle. It's more of a test run. But I get an error on my for loop. It doesn't seem to want to read the for, expected an expression. I tried putting the For loop outside of SimpleVertex and it works. How would I add a for loop to make my circle? Thanks for helping the noob. I would wait for office hours, but it's Labor day and I've been at it all weekend.


Solution

  • You have your for loop in a declaration, which you cannot do. You need to declare vertices first, then initialise it with for loop:

    int main()
    {
        // Declare your vertices:
        const auto maxVertices = 20;
        SimpleVertex vertices[maxVertices];
    
        // Initialise the vertices in a for loop
        for (int i = 0; i < maxVertices; ++i)
        {
            vertices[i].Position = /* calculate position */
        }
    }
    

    It's likely you'll use i to calculate one of the positions, something like:

    vertices[i].Position = { i * x, y, z };