0

A problem has risen in my OpenGL code which looked so simple but it doesn't seem to work like I expected it to be.

The code is as follows:

int x = 0, y = 0, z = 0, TP = 1;
bool initiate_placement = true;
float trans[3][3][2] = {{{-16.0,16.0},{0.0,16.0},{16.0,16.0}},
                        {{-16.0,0.0},{0.0,0.0},{16.0,0.0}},
                        {{-16.0,-16.0},{0,-16.0},{16.0,-16.0}}};

vector<tiles> Place;
tiles VER, HOR, NOREA, NORWE, SOUEA, SOUWE, TNOR, TSOU, TEA, TWE, CRO;

void Tile_Placement()
{
    int A = rand() % 10 + 1;
    if (TP <= 9)
    {
        switch (A)
        {
            case 1:
                VER.vertical(trans[x][y][z],trans[x][y][z + 1]);
                Place.push_back(VER);
                break;
            case 2:
                HOR.horizontal(trans[x][y][z],trans[x][y][z + 1]);
                Place.push_back(HOR);
                break;
            case 3:
                NOREA.north2east(trans[x][y][z],trans[x][y][z + 1]);
                Place.push_back(NOREA);
                break;
            case 4:
                NORWE.north2west(trans[x][y][z],trans[x][y][z + 1]);
                Place.push_back(NORWE);
                break;
            case 5:
                SOUEA.south2east(trans[x][y][z],trans[x][y][z + 1]);
                Place.push_back(SOUEA);
                break;
            case 6:
                SOUWE.south2west(trans[x][y][z],trans[x][y][z + 1]);
                Place.push_back(SOUWE);
                break;
            case 7:
                TNOR.T_north(trans[x][y][z],trans[x][y][z + 1]);
                Place.push_back(TNOR);
                break;
            case 8:
                TSOU.T_south(trans[x][y][z],trans[x][y][z + 1]);
                Place.push_back(TSOU);
                break;
            case 9:
                TEA.T_east(trans[x][y][z],trans[x][y][z + 1]);
                Place.push_back(TEA);
                break;
            case 10:
                TWE.T_west(trans[x][y][z],trans[x][y][z + 1]);
                Place.push_back(TWE);
                break;
            case 11:
                CRO.cross(trans[x][y][z],trans[x][y][z + 1]);
                Place.push_back(CRO);
                break;
            }
        cout << trans[x][y][z] << " , " << trans[x][y][z + 1] << endl;
    }
}

void Tile_LoopZ()
{
    if (z < 1)
    {
        Tile_Placement();
        TP++;
        z++;
        Tile_LoopZ();
    }
    z = 0;
}
void Tile_LoopY()
{
    if (y < 3)
    {
        Tile_LoopZ();
        y++;
        Tile_LoopY();
    }
    y = 0;
}
void Tile_LoopX()
{
    if (initiate_placement == true)
    {
        if (x < 3)
        {
            Tile_LoopY();
            x++;
            Tile_LoopX();
            x = 0;
        }
    }
    initiate_placement = false; //<<< problem is focused here
    x = 0, y = 0, z = 0, TP = 1;
}

void gameScene()
{
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLineWidth(2.0);

    glColor3f(1.0, 1.0, 1.0);
    Tile_LoopX();

    glutSwapBuffers();
}

The code here tells me that in the recursive function Tile_LoopX();, once initiate_placement == true, a code happens that pushes back nine tiles. Once the nine tiles are pushed back, it makes initiate_placement false, that way if gameScene() is called again, Tile_LoopX(); wont be able to run its code.

The things that I know so far is that gameScene() is an infinite loop. Removing initiate_placement = false; makes the tiles randomly spaz out because it's called over and over again. Placing initiate_placement = false; sounds like a good solution but the tiles were never pushed back at all. To test that the code passes through, I added a cout and it actually passes through.

If anything, I might have missed something completely simple and I really need help for it.

UPDATE 4/16/15:

The vectors do get pushed back as I cout'ed the vector's size (which is 9). The problem now lies that the objects within the vector were never visually shown in the program, or even able to use its code within the program.

  • 3
    Stop using global variables. Pass all variables to functions by reference or value. At the very least this will force you to understand the data dependencies of your functions. If your functions are taking 5+ parameters, consider strongly changing them. Your code is a mess of global state, it would be surprising if it worked. – Yakk - Adam Nevraumont Apr 16 '15 at 02:32
  • I have updated my code and followed what you did (quite helpful by the way). After a while, I found the true error to the program. It was at the Disassembly of my Visual Studio program. I used breakpoints to get to the Disassembly and while I was there, that was the sole reason why the tiles were not showing. – VesmirPendel Apr 17 '15 at 07:32

0 Answers0