kelloaos
kelloaos

Reputation: 41

Draw 8 x 15 Rectangle-Grid in C++ with OpenGL - crash down

i want to create 8 x 15 Matrix of Rectangles in C++ with OpenGl. I first create a Class for just a rectangle. Then i created a class for a row of Rectangles, it contains an Array of 8 Rectangles. Then i created a class for 15 rows (the whole matrix).

I think i have a pointer problem... This is what i did for testing:

created 1..2 rectangles an drew them - ok
created 1..2 rows and drew them -- ok
created 1 field and drew it -- ok
created 2 matrix-- windows just opend and crashed ...it was white with a error msg xD

So this is my display function... it works for example for 3 rows. but if i would put in MatrixRectangles field1(0,0); and MatrixRectangles field2(0,0); and print all out in the display function with void drawAllRows(); it wouldn't work .

void display(void) {
    // glClearColor(1,1,1,0);
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt(0, 0, 0.01, 0, 0, 0, 0, 1, 0);
    glBegin(GL_LINE_LOOP);
    glVertex3f(-1, -1, 0);
    glVertex3f(0, 1, 0);
    glVertex3f(1, -1, 0);
    glEnd();

    row1.drawAllRectangles();
    row2.drawAllRectangles();
    row3.drawAllRectangles();
    glFlush();
}


class RectangleGL {
public:
    RectangleGL();
    RectangleGL(float hoehe, float breite, float startx, float starty);
    virtual ~RectangleGL();
    float getWidth() const;
    float getHight() const;
    void setBreite(float breite);
    void setHight(float hohe);
    void drawRectangle();
    float getStartx() const;
    void setStartx(float startx);
    void printRectangleData();
    void setStarty(float starty);
private:
    float hight;
    float width;
    float startx;
    float starty;
};

#endif /* VIERECKGL_H_ 

/*
 * Reihe_ViereckeGL.cpp
 *
 *  Created on: 19.10.2019
 *      Author: N1
 */

#include "Reihe_ViereckeGL.h"
rowRectangle::rowRectangle() {
    // TODO Auto-generated constructor stub

}
rowRectangle::rowRectangle(float startx,float starty) {
    // TODO Auto-generated constructor stub
    for (int i = 0; i < 8; i++) {
        float m = i * 0.1;
        this->rectangles[i] = new RectangleGL(0.1, 0.1, m+startx, starty); // always a new x position , everytime higher...

    }
}

rowRectangle::~rowRectangle() {
    // TODO Auto-generated destructor stub
}

float rowRectangle::getStarty() const {
    return starty;
}

void rowRectangle::setStarty(float starty) {
    this->starty = starty;
}

void rowRectangle::drawAllRectangles() {

    for (int i = 0; i < 8; i++) {

        this->rectangles[i]->drawRectangle();
    }
}
/*
 * Reihe_ViereckeGL.h
 *
 *  Created on: 19.10.2019
 *      Author: N1
 */

#ifndef REIHE_VIERECKEGL_H_
#define REIHE_VIERECKEGL_H_
#include "ViereckGL.h"
class rowRectangle {
public:
    rowRectangle();
    rowRectangle(float startx,float starty);
    virtual ~rowRectangle();
    float getStarty() const;
    void setStarty(float starty);
    void drawAllRectangles();
    RectangleGL* rectangles[8];
private:
    int anzReihe;
    int rowNr;
    float startx; // offset x..
    float starty; // offset... y
};

#endif /* REIHE_VIERECKEGL_H_ */
/*
 * FeldViereckeGL.h
 *
 *  Created on: 19.10.2019
 *      Author: N1
 */

#ifndef FELDVIERECKEGL_H_
#define FELDVIERECKEGL_H_
#include "Reihe_ViereckeGL.h"
class MatrixRectangles {
public:
    MatrixRectangles(float startx, float starty);
    virtual ~MatrixRectangles();
    rowRectangle *rowRectangles[8];
    void drawAllRows();
};

#endif /* FELDVIERECKEGL_H_ */

    /*
     * FeldViereckeGL.cpp
     *
     *  Created on: 19.10.2019
     *      Author: N1
     */

    #include "FeldViereckeGL.h"

    MatrixRectangles::MatrixRectangles(float startx, float starty) {
        // TODO Auto-generated constructor stub
        for (int i = 0; i < 15; i++) {
            float m = i * 0.1;
            this->rowRectangles[i] = new rowRectangle(startx, m + starty); // everytime a higher position
        }
    }

    MatrixRectangles::~MatrixRectangles() {
        // TODO Auto-generated destructor stub
    }
    void MatrixRectangles::drawAllRows() {
        // zeichne alle reihen
        for (int i = 0; i < 15; i++) {
            rowRectangles[i]->drawAllRectangles(); // draw all rectangles !
        }
    }

i would expect a nice 8 x 15 Rectangle Matrix. But instead the window , which openes crashes down - it gets white - ",,, exe doesnt work anymore"

Upvotes: 1

Views: 391

Answers (1)

tuket
tuket

Reputation: 3931

Your array of rows has only 8 elements:

rowRectangle *rowRectangles[8];

However your are iterating though 15 elements:

for (int i = 0; i < 15; i++) {
    float m = i * 0.1;
    this->rowRectangles[i] = new rowRectangle(startx, m + starty); // everytime a higher position
}

That's why it is crashing.

A good practice is defining a constant variable for the number of rows.

static constexpr int numRows = 15;
rowRectangle *rowRectangles[numRows];

And use numRows everywhere instead of 15. That will prevent bugs in the future. And, when you need to change the number of rows, you only need to do so in one place.

Upvotes: 2

Related Questions